Note [Analysing with absent demand]
Suppose we analyse an expression with demand A. The "A" means "absent", so this expression will never be needed. What should happen? There are several wrinkles: * We *do* want to analyse the expression regardless. Reason: Note [Always analyse in virgin pass] But we can post-process the results to ignore all the usage demands coming back. This is done by 'multDmdType' with the appropriate (absent) evaluation cardinality A or B. * Nevertheless, which sub-demand should we pick for analysis? Since the demand was absent, any would do. Worker/wrapper will replace absent bindings with an absent filler anyway, so annotations in the RHS of an absent binding don't matter much. Picking 'botSubDmd' would be the most useful, but would also look a bit misleading in the Core output of DmdAnal, because all nested annotations would be bottoming. Better pick 'seqSubDmd', so that we annotate many of those nested bindings with A themselves. * Since we allow unlifted arguments that are not ok-for-speculation, we need to be extra careful in the following situation, because unlifted values are evaluated even if they are not used. Example from #9254: f :: (() -> (# Int#, () #)) -> () Strictness signature is <1C(1,P(A,1L))> I.e. calls k, but discards first component of result f k = case k () of (# _, r #) -> r g :: Int -> () g y = f (\n -> (# case y of I# y2 -> y2, n #)) Here, f's strictness signature says (correctly) that it calls its argument function and ignores the first component of its result. But in function g, we *will* evaluate the 'case y of ...', because it has type Int#. So in the program as written, 'y' will be evaluated. Hence we must record this usage of 'y', else 'g' will say 'y' is absent, and will w/w so that 'y' is bound to an absent filler (see Note [Absent fillers]), leading to a crash when 'y' is evaluated. Now, worker/wrapper could be smarter and replace `case y of I# y2 -> y2` with a suitable absent filler such as `RUBBISH[IntRep] @Int#`. But as long as worker/wrapper isn't equipped to do so, we must be cautious, and follow Note [Anticipating ANF in demand analysis]. That is, in 'dmdAnalStar', we will set the evaluation cardinality to C_11, anticipating the case binding of the complex argument `case y of I# y2 -> y2`. This cardinlities' only effect is in the call to 'multDmdType', where it makes sure that the demand on the arg's free variable 'y' is not absent and strict, so that it is ultimately passed unboxed to 'g'.
References 3
- Always analyse in virgin pass GHC.Core.Opt.DmdAnal
- Anticipating ANF in demand analysis GHC.Core.Opt.DmdAnal
- Absent fillers GHC.Core.Opt.WorkWrap.Utils
Referenced by 2
- GHC.Core.Opt.DmdAnal call site
- Anticipating ANF in demand analysis GHC.Core.Opt.DmdAnal