Ticket #4830 (closed bug: fixed)

Opened 2 years ago

Last modified 2 years ago

Simplifier does case-to-let too eagerly

Reported by: simonpj Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.0.1
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Difficulty:
Test Case: perf/should_run/T4830 Blocked By:
Blocking: Related Tickets:

Description

Roman found an example where the simplifier was turning a case expression to a let binding when it shouldn't

foo :: Int -> Maybe (Double,Double) -> Double 
foo _  Nothing     = 0 
foo 0 (Just (x,y)) = x+y 
foo n (Just (x,y)) = let r = f x y in r `seq` foo (n-1) (Just r)
  where
    f x y | x <= y    = (x,y)
          | otherwise = (y,x)

GHC 7.0.1 generates this

foo =
  \ (ds_dks :: Int) (ds_dkt :: Maybe (Double, Double)) ->
    case ds_dkt of _ {
      Nothing -> lvl_sly;
      Just ipv_skO ->
        case ds_dks of _ { I# ds_dku ->
        case ds_dku of ds_XkC {
          __DEFAULT ->
            case ipv_skO of _ { (x_aax, y_aay) ->
            case x_aax of wild_akY { D# x_al0 ->
            case y_aay of wild1_al2 { D# y_al4 ->
            foo
              (I# (-# ds_XkC 1))
              (Just
                 @ (Double, Double)
                 (case <=## x_al0 y_al4 of _ {
                    False -> (wild1_al2, wild_akY); True -> (wild_akY, wild1_al2)
                  }))
            }
            }
            };
          0 -> case ipv_skO of _ { (x_aau, y_aav) -> plusDouble x_aau y_aav }
        }
        }
    }

That <=## should be outside the call to foo not inside. (The code isn't wrong, it's just less efficient than it should be.)

I know what's wrong. Patch coming.

Change History

Changed 2 years ago by simonpj

  • status changed from new to merge
  • testcase set to perf/should_run/T4830

Fixed by

Wed Dec  8 17:22:51 GMT 2010  simonpj@microsoft.com
  * Make the case-to-let transformation a little less eager
  
  See Note [Case elimination: lifted case].
  Thanks to Roman for identifying this case.

    M ./compiler/simplCore/Simplify.lhs -19 +25

Please merge to stable branch.

I've added a performance test.

Simon

Changed 2 years ago by igloo

  • status changed from merge to closed
  • resolution set to fixed

Merged.

Note: See TracTickets for help on using tickets.