Mikolaj Konarski pushed to branch wip/T28016-inline-boring-work-free at Glasgow Haskell Compiler / GHC

Commits:

5 changed files:

Changes:

  • compiler/GHC/Core/Opt/Pipeline.hs
    ... ... @@ -29,6 +29,7 @@ import GHC.Core.Ppr ( pprCoreBindings, pprRules )
    29 29
     import GHC.Core.Utils   ( dumpIdInfoOfProgram, diffBinds, stripTicksE )
    
    30 30
     import GHC.Core.Lint.Interactive ( interactiveInScope )
    
    31 31
     import GHC.Core.Opt.Simplify ( simplifyExpr, simplifyPgm )
    
    32
    +import GHC.Core.Opt.Simplify.Env ( SimplMode(..) )
    
    32 33
     import GHC.Core.Opt.Simplify.Monad
    
    33 34
     import GHC.Core.Opt.Monad
    
    34 35
     import GHC.Core.Opt.Pipeline.Types
    
    ... ... @@ -172,6 +173,24 @@ getCoreToDo dflags hpt_rule_base extra_vars
    172 173
         simpl_gently = CoreDoSimplify $ initSimplifyOpts dflags extra_vars max_iter
    
    173 174
                                         (initGentleSimplMode dflags) hpt_rule_base
    
    174 175
     
    
    176
    +    first_float_switches = FloatOutSwitches
    
    177
    +                { floatOutLambdas     = Just 0
    
    178
    +                , floatOutConstants   = True
    
    179
    +                , floatOutOverSatApps = False
    
    180
    +                , floatToTopLevelOnly = False
    
    181
    +                , floatJoinsToTop     = False  -- Initially, don't float join points at all
    
    182
    +                }
    
    183
    +
    
    184
    +    -- EXPERIMENT (#28016): before each phase, let full laziness float to top
    
    185
    +    -- level the constants that the unfoldings active from that phase expose,
    
    186
    +    -- before case-of-case moves a continuation into them, as the gentle run
    
    187
    +    -- does for the first float-out. See Note [Case-of-case and full laziness].
    
    188
    +    pre_float phase = runWhen full_laziness $ CoreDoPasses
    
    189
    +      [ CoreDoSimplify $ initSimplifyOpts dflags extra_vars 1
    
    190
    +          ((initSimplMode dflags phase "pre-float") { sm_case_case = False })
    
    191
    +          hpt_rule_base
    
    192
    +      , CoreDoFloatOutwards first_float_switches { floatToTopLevelOnly = True } ]
    
    193
    +
    
    175 194
         dmd_cpr_ww = if ww_on then [CoreDoDemand True,CoreDoCpr,CoreDoWorkerWrapper]
    
    176 195
                               else [CoreDoDemand False] -- NB: No CPR! See Note [Don't change boxity without worker/wrapper]
    
    177 196
     
    
    ... ... @@ -206,14 +225,7 @@ getCoreToDo dflags hpt_rule_base extra_vars
    206 225
             -- so that overloaded functions have all their dictionary lambdas manifest
    
    207 226
             runWhen do_specialise CoreDoSpecialising,
    
    208 227
     
    
    209
    -        runWhen full_laziness
    
    210
    -           (CoreDoFloatOutwards $ FloatOutSwitches
    
    211
    -                { floatOutLambdas     = Just 0
    
    212
    -                , floatOutConstants   = True
    
    213
    -                , floatOutOverSatApps = False
    
    214
    -                , floatToTopLevelOnly = False
    
    215
    -                , floatJoinsToTop     = False  -- Initially, don't float join points at all
    
    216
    -                }),
    
    228
    +        runWhen full_laziness (CoreDoFloatOutwards first_float_switches),
    
    217 229
                     -- I have no idea why, but not floating constants to
    
    218 230
                     -- top level is very bad in some cases.
    
    219 231
                     --
    
    ... ... @@ -232,9 +244,11 @@ getCoreToDo dflags hpt_rule_base extra_vars
    232 244
     
    
    233 245
             -- Run the simplifier phases 2,1,0 to allow rewrite rules to fire
    
    234 246
             runWhen do_simpl3
    
    235
    -            (CoreDoPasses $ [ simpl_phase (Phase phase) "main" max_iter
    
    236
    -                            | phase <- [phases, phases-1 .. 1] ] ++
    
    237
    -                            [ simpl_phase (Phase 0) "main" (max max_iter 3) ]),
    
    247
    +            (CoreDoPasses $ concat [ [ pre_float (Phase phase)
    
    248
    +                                     , simpl_phase (Phase phase) "main" max_iter ]
    
    249
    +                                   | phase <- [phases, phases-1 .. 1] ] ++
    
    250
    +                            [ pre_float (Phase 0)
    
    251
    +                            , simpl_phase (Phase 0) "main" (max max_iter 3) ]),
    
    238 252
                     -- Phase 0: allow all Ids to be inlined now
    
    239 253
                     -- This gets foldr inlined before strictness analysis
    
    240 254
     
    

  • testsuite/tests/simplCore/should_run/T15519.hs
    1
    +{-# LANGUAGE MagicHash #-}
    
    2
    +-- The example of Note [Honour INLINE on 0-ary bindings] (#15519): x is
    
    3
    +-- inlined, and g a0 b0 must still be computed once, not at every call of f.
    
    4
    +module Main (main) where
    
    5
    +
    
    6
    +import GHC.Exts
    
    7
    +import Debug.Trace (trace)
    
    8
    +
    
    9
    +data T = T Int#
    
    10
    +
    
    11
    +g :: Int -> Int -> Int
    
    12
    +g a b = trace "g" a * b + 1
    
    13
    +{-# NOINLINE g #-}
    
    14
    +
    
    15
    +a0, b0 :: Int
    
    16
    +a0 = 3
    
    17
    +b0 = 4
    
    18
    +{-# NOINLINE a0 #-}
    
    19
    +{-# NOINLINE b0 #-}
    
    20
    +
    
    21
    +x :: T
    
    22
    +x = case g a0 b0 of I# r -> T r
    
    23
    +{-# INLINE x #-}
    
    24
    +
    
    25
    +h :: T -> Int -> Int
    
    26
    +h (T r) y = I# r + y
    
    27
    +
    
    28
    +f :: Int -> Int
    
    29
    +f y = h x y
    
    30
    +{-# NOINLINE f #-}
    
    31
    +
    
    32
    +main :: IO ()
    
    33
    +main = print (sum (map f [1 .. 3]))

  • testsuite/tests/simplCore/should_run/T15519.stderr
    1
    +g

  • testsuite/tests/simplCore/should_run/T15519.stdout
    1
    +45

  • testsuite/tests/simplCore/should_run/all.T
    ... ... @@ -125,3 +125,4 @@ test('T27071', normal, compile_and_run, ['-O -fworker-wrapper-cbv'])
    125 125
     test('T27005', [], multimod_compile_and_run, ['T27005', '-O'])
    
    126 126
     test('T27705', [extra_hc_opts('+RTS -M500M -RTS')], multimod_compile_and_run,
    
    127 127
          ['T27705', '-O2 -fexpose-all-unfoldings'])
    
    128
    +test('T15519', normal, compile_and_run, ['-O'])