DmdAnal: Annotate top-level function bindings with demands (#18894)
It's useful to annotate a non-exported top-level function like `g` in ```hs module Lib (h) where g :: Int -> Int -> (Int,Int) g m 1 = (m, 0) g m n = (2 * m, 2 `div` n) {-# NOINLINE g #-} h :: Int -> Int h 1 = 0 h m | odd m = snd (g m 2) | otherwise = uncurry (+) (g 2 m) ``` with its demand `UCU(CS(P(1P(U),SP(U))`, which tells us that whenever `g` was called, the second component of the returned pair was evaluated strictly. Since #18903 we do so for local functions, where we can see all calls. For top-level functions, we can assume that all *exported* functions are demanded according to `topDmd` and thus get sound demands for non-exported top-level functions. The demand on `g` is crucial information for Nested CPR, which may the go on and unbox `g` for the second pair component. That is true even if that pair component may diverge, as is the case for the call site `g 13 0`, which throws a div-by-zero exception. In `T18894b`, you can even see the new demand annotation enabling us to eta-expand a function that we wouldn't be able to eta-expand without Call Arity. We only track bindings of function type in order not to risk huge compile-time regressions, see `isInterestingTopLevelFn`. There was a CoreLint check that rejected strict demand annotations on recursive or top-level bindings, which seems completely unjustified. All the cases I investigated were fine, so I removed it. Fixes #18894.
Showing
- compiler/GHC/Core/FVs.hs 3 additions, 4 deletionscompiler/GHC/Core/FVs.hs
- compiler/GHC/Core/Lint.hs 0 additions, 15 deletionscompiler/GHC/Core/Lint.hs
- compiler/GHC/Core/Opt/DmdAnal.hs 257 additions, 117 deletionscompiler/GHC/Core/Opt/DmdAnal.hs
- compiler/GHC/Core/Opt/Pipeline.hs 13 additions, 5 deletionscompiler/GHC/Core/Opt/Pipeline.hs
- compiler/GHC/Core/Opt/Simplify/Env.hs 2 additions, 3 deletionscompiler/GHC/Core/Opt/Simplify/Env.hs
- compiler/GHC/Core/Opt/WorkWrap.hs 5 additions, 1 deletioncompiler/GHC/Core/Opt/WorkWrap.hs
- compiler/GHC/Core/Opt/WorkWrap/Utils.hs 6 additions, 2 deletionscompiler/GHC/Core/Opt/WorkWrap/Utils.hs
- compiler/GHC/Core/Tidy.hs 2 additions, 2 deletionscompiler/GHC/Core/Tidy.hs
- compiler/GHC/Types/Demand.hs 5 additions, 5 deletionscompiler/GHC/Types/Demand.hs
- compiler/GHC/Types/Id/Info.hs 1 addition, 1 deletioncompiler/GHC/Types/Id/Info.hs
- testsuite/tests/stranal/should_compile/T18894.hs 28 additions, 0 deletionstestsuite/tests/stranal/should_compile/T18894.hs
- testsuite/tests/stranal/should_compile/T18894.stderr 404 additions, 0 deletionstestsuite/tests/stranal/should_compile/T18894.stderr
- testsuite/tests/stranal/should_compile/T18894b.hs 20 additions, 0 deletionstestsuite/tests/stranal/should_compile/T18894b.hs
- testsuite/tests/stranal/should_compile/T18894b.stderr 187 additions, 0 deletionstestsuite/tests/stranal/should_compile/T18894b.stderr
- testsuite/tests/stranal/should_compile/all.T 4 additions, 0 deletionstestsuite/tests/stranal/should_compile/all.T
Loading
Please register or sign in to comment