Simon Peyton Jones pushed to branch wip/case-bndr-unf at Glasgow Haskell Compiler / GHC
Commits:
-
bfe578d6
by Simon Peyton Jones at 2026-11-20T23:38:35+00:00
9 changed files:
- compiler/GHC/Core/Opt/CprAnal.hs
- compiler/GHC/Core/Opt/Simplify/Iteration.hs
- compiler/GHC/Core/Opt/WorkWrap.hs
- compiler/GHC/Core/Opt/WorkWrap/Utils.hs
- compiler/GHC/Types/Cpr.hs
- compiler/GHC/Utils/Monad.hs
- + testsuite/tests/simplCore/should_compile/T28158.hs
- + testsuite/tests/simplCore/should_compile/T28158.stderr
- testsuite/tests/simplCore/should_compile/all.T
Changes:
| ... | ... | @@ -343,7 +343,7 @@ cprTransform env id args |
| 343 | 343 | -- Any local binding, except for data structure bindings
|
| 344 | 344 | -- See Note [Efficient Top sigs in SigEnv]
|
| 345 | 345 | | Just sig <- lookupSigEnv env id
|
| 346 | - = applyCprTy (getCprSig sig) (length args)
|
|
| 346 | + = applyCprTy (cprSigType sig) (length args)
|
|
| 347 | 347 | -- See Note [CPR for data structures]
|
| 348 | 348 | | Just rhs <- cprDataStructureUnfolding_maybe id
|
| 349 | 349 | = fst $ cprAnal env rhs
|
| ... | ... | @@ -362,7 +362,7 @@ cprTransform env id args |
| 362 | 362 | = cprTransformDataConWork env con args
|
| 363 | 363 | -- Imported function
|
| 364 | 364 | | otherwise
|
| 365 | - = applyCprTy (getCprSig (idCprSig id)) (length args)
|
|
| 365 | + = applyCprTy (cprSigType (idCprSig id)) (length args)
|
|
| 366 | 366 | |
| 367 | 367 | -- | Precise, hand-written CPR transformers for select Ids
|
| 368 | 368 | cprTransformBespoke :: Id -> [(CprType, CoreArg)] -> Maybe CprType
|
| ... | ... | @@ -870,27 +870,42 @@ Does f have the CPR property? Well, no. |
| 870 | 870 | However, if the strictness analyser has figured out (in a previous
|
| 871 | 871 | iteration) that it's strict, then we DON'T need to forget the CPR info.
|
| 872 | 872 | Instead we can retain the CPR info and do the thunk-splitting transform
|
| 873 | -(see WorkWrap.splitThunk).
|
|
| 873 | +(see Note [Thunk splitting] esp (B) CPR-led thunk splitting).
|
|
| 874 | 874 | |
| 875 | 875 | This made a big difference to PrelBase.modInt, which had something like
|
| 876 | 876 | modInt = \ x -> let r = ... -> I# v in
|
| 877 | 877 | ...body strict in r...
|
| 878 | -r's RHS isn't a value yet; but modInt returns r in various branches, so
|
|
| 879 | -if r doesn't have the CPR property then neither does modInt
|
|
| 878 | +r's RHS isn't a value yet; but `modInt` returns `r` in various branches, so
|
|
| 879 | +if `r` doesn't have the CPR property then neither does `modInt`
|
|
| 880 | + |
|
| 880 | 881 | Another case I found in practice (in Complex.magnitude), looks like this:
|
| 881 | - let k = if ... then I# a else I# b
|
|
| 882 | - in ... body strict in k ....
|
|
| 882 | + let k* = if ... then I# a else I# b
|
|
| 883 | + in ... body strict in k ....
|
|
| 883 | 884 | (For this example, it doesn't matter whether k is returned as part of
|
| 884 | 885 | the overall result; but it does matter that k's RHS has the CPR property.)
|
| 886 | + |
|
| 885 | 887 | Left to itself, the simplifier will make a join point thus:
|
| 886 | - let $j k = ...body strict in k...
|
|
| 887 | - if ... then $j (I# a) else $j (I# b)
|
|
| 888 | + let $j k = ...body strict in k...
|
|
| 889 | + if ... then $j (I# a) else $j (I# b)
|
|
| 890 | + |
|
| 888 | 891 | With thunk-splitting, we get instead
|
| 889 | - let $j x = let k = I#x in ...body strict in k...
|
|
| 890 | - in if ... then $j a else $j b
|
|
| 892 | + let k* = case (if ... then I# a else I# b) of I# z -> I# z
|
|
| 893 | + in ... body strict in k ....
|
|
| 894 | +Now the simplifier should pull out the `case` to give
|
|
| 895 | + case (if ... then I# a else I# b) of I# z ->
|
|
| 896 | + let k = I# z in ... body strict in k ...
|
|
| 897 | +Now case-of-if will give
|
|
| 898 | + let $j z = let k = I# z in ...body strict in k... in
|
|
| 899 | + if ... then $j a else $j b
|
|
| 891 | 900 | This is much better; there's a good chance the I# won't get allocated.
|
| 901 | +TL'DR: During CPR analysis we want to give `k` the CPR property in `body`.
|
|
| 902 | + |
|
| 903 | +What if the type involved has more than one constructor? E.g.
|
|
| 904 | + let k* = if ... then Just a else Just b
|
|
| 905 | + in ... body strict in k ....
|
|
| 906 | +That is good too: see (TS5) in Note [Thunk splitting]
|
|
| 892 | 907 | |
| 893 | -But what about botCpr? Consider
|
|
| 908 | +Finally, what about botCpr? Consider
|
|
| 894 | 909 | lvl = error "boom"
|
| 895 | 910 | fac -1 = lvl
|
| 896 | 911 | fac 0 = 1
|
| ... | ... | @@ -51,7 +51,6 @@ import GHC.Types.Literal ( litIsLifted ) --, mkLitInt ) -- temporarily comment |
| 51 | 51 | import GHC.Types.SourceText
|
| 52 | 52 | import GHC.Types.Id
|
| 53 | 53 | import GHC.Types.Id.Info
|
| 54 | -import GHC.Types.Cpr( isTopCprSig )
|
|
| 55 | 54 | import GHC.Types.InlinePragma
|
| 56 | 55 | import GHC.Types.Name ( mkSystemVarName, isExternalName, getOccFS )
|
| 57 | 56 | import GHC.Types.Demand
|
| ... | ... | @@ -1830,7 +1829,7 @@ simplNonRecE :: HasDebugCallStack |
| 1830 | 1829 | |
| 1831 | 1830 | simplNonRecE env from_what bndr (rhs, rhs_se) body cont
|
| 1832 | 1831 | | assertPpr (isId bndr && not (isJoinId bndr)) (ppr bndr) True
|
| 1833 | - , useStrictBind env from_what bndr
|
|
| 1832 | + , useStrictBind env from_what bndr rhs
|
|
| 1834 | 1833 | = -- Evaluate RHS strictly
|
| 1835 | 1834 | simplExprF (rhs_se `setInScopeFromE` env) rhs
|
| 1836 | 1835 | (StrictBind { sc_bndr = bndr, sc_body = body, sc_from = from_what
|
| ... | ... | @@ -1865,11 +1864,10 @@ simplRecE env pairs body cont |
| 1865 | 1864 | ; return (floats1 `addFloats` floats2, expr') }
|
| 1866 | 1865 | |
| 1867 | 1866 | |
| 1868 | -useStrictBind :: SimplEnv -> FromWhat -> InId -> Bool
|
|
| 1867 | +useStrictBind :: SimplEnv -> FromWhat -> InId -> InExpr -> Bool
|
|
| 1869 | 1868 | -- Can we use a StrictBind continuation for this let-binding?
|
| 1870 | --- If so, return a binder that is suitable to become a lambda binder,
|
|
| 1871 | --- i.e. one satisfying `lamBndrInfoOK`; see (SB3) in Note [StrictBind]
|
|
| 1872 | -useStrictBind env from_what bndr
|
|
| 1869 | +-- See (SB3) in Note [StrictBind]
|
|
| 1870 | +useStrictBind env from_what bndr rhs
|
|
| 1873 | 1871 | -- If we are coming from a beta-reduction (FromBeta) we must
|
| 1874 | 1872 | -- establish the let-can-float invariant, so we must go via StrictBind
|
| 1875 | 1873 | -- If not, the invariant holds already, and it's optional.
|
| ... | ... | @@ -1880,6 +1878,10 @@ useStrictBind env from_what bndr |
| 1880 | 1878 | | not (isStrUsedDmd dmd_info)
|
| 1881 | 1879 | = False
|
| 1882 | 1880 | |
| 1881 | + -- Not if the RHS is ok-for-spec; no benefit
|
|
| 1882 | + | exprOkForSpeculation rhs
|
|
| 1883 | + = False
|
|
| 1884 | + |
|
| 1883 | 1885 | -- Not if case-of-case is off
|
| 1884 | 1886 | -- See (SB1) in Note [StrictBind]
|
| 1885 | 1887 | | not (seCaseCase env)
|
| ... | ... | @@ -1894,8 +1896,6 @@ useStrictBind env from_what bndr |
| 1894 | 1896 | | isStableUnfolding (unfoldingInfo info)
|
| 1895 | 1897 | || not (isEmptyRuleInfo (ruleInfo info))
|
| 1896 | 1898 | || not (isAlwaysActive (inlinePragmaActivation (inlinePragInfo info)))
|
| 1897 | - || not (isNopSig (dmdSigInfo info))
|
|
| 1898 | - || not (isTopCprSig (cprSigInfo info))
|
|
| 1899 | 1899 | = False
|
| 1900 | 1900 | |
| 1901 | 1901 | | otherwise
|
| ... | ... | @@ -4243,9 +4243,8 @@ mkDupableAlt _env case_bndr jfloats (Alt con alt_bndrs alt_rhs_in) |
| 4243 | 4243 | , alt') }
|
| 4244 | 4244 | -- See Note [Duplicated env]
|
| 4245 | 4245 | |
| 4246 | -{-
|
|
| 4247 | -Note [Do not add unfoldings to join points at birth]
|
|
| 4248 | -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 4246 | +{- Note [Do not add unfoldings to join points at birth]
|
|
| 4247 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 4249 | 4248 | Consider this (#15360)
|
| 4250 | 4249 | |
| 4251 | 4250 | case (case (case (case ...))) of
|
| ... | ... | @@ -4316,7 +4315,7 @@ The simplifier will find |
| 4316 | 4315 | (Var v) with continuation
|
| 4317 | 4316 | Select (pn -> rn) (
|
| 4318 | 4317 | Select [I# a -> I# a] (
|
| 4319 | - StrictBind body Stop
|
|
| 4318 | + StrictBind body Stop))
|
|
| 4320 | 4319 | |
| 4321 | 4320 | So we'll call mkDupableCont on
|
| 4322 | 4321 | Select [I# a -> I# a] (StrictBind body Stop)
|
| ... | ... | @@ -4455,7 +4454,7 @@ hack and bit us in multiple small and not-so-small ways |
| 4455 | 4454 | |
| 4456 | 4455 | Consider this
|
| 4457 | 4456 | case (case .. ) of c {
|
| 4458 | - I# c# -> ....c....
|
|
| 4457 | + I# c# -> ....c.... }
|
|
| 4459 | 4458 | |
| 4460 | 4459 | If we make a join point with c but not c# we get
|
| 4461 | 4460 | $j = \c -> ....c....
|
| ... | ... | @@ -4655,30 +4654,43 @@ We simplify `rhs` with a StrictBind continuation that continues with |
| 4655 | 4654 | invariants] in GHC.Core. But a let-binder `x` usually has IdInfo that a
|
| 4656 | 4655 | lambda binder may not have (an unfolding, arity, a demand signature...).
|
| 4657 | 4656 | |
| 4658 | - So `useStrictBind_maybe` zaps that info, keeping only the occurrence info
|
|
| 4659 | - and the demand info. If it happens to stay as a let-binding arity info will
|
|
| 4660 | - be re-derived, so nothing is lost.
|
|
| 4657 | + So when we duplicate StrictBind, in `mkDupableContWithDmds`, we use
|
|
| 4658 | + `mkLamBndrFromLetBndr` to zap that info, keeping only the occurrence
|
|
| 4659 | + info and the demand info.
|
|
| 4661 | 4660 | |
| 4662 | - The exceptions are RULES, a stable unfolding, and a non-default inlining
|
|
| 4663 | - activation: these cannot be re-derived, and we have nowhere convenient to
|
|
| 4664 | - put them if `x` becomes a lambda binder. For example:
|
|
| 4661 | + However, if the let-binder has
|
|
| 4662 | + * RULES
|
|
| 4663 | + * a stable unfolding
|
|
| 4664 | + * a non-default inlining activation
|
|
| 4665 | + we'd permanently lose these things if we lambda-bind it. For example:
|
|
| 4665 | 4666 | let {-# NOINLINE x #-}
|
| 4666 | 4667 | x! = case y of { ... }
|
| 4667 | 4668 | in blah
|
| 4668 | - If we make a join point j x = blah, we have nowhere convenient
|
|
| 4669 | - to put that NOINLINE. Ditto INLINE or RULES. So in these cases we
|
|
| 4670 | - simply don't use StrictBind. It's very unusual, so no harm done.
|
|
| 4671 | - But it does happen; one example is this, in the Applicative (ST s)
|
|
| 4672 | - instance in GHC.Internal.Control.Monad.St.Lazy.Imp
|
|
| 4669 | + If we make a join point j x = blah, we have nowhere convenient to put that
|
|
| 4670 | + NOINLINE. Ditto INLINE or RULES. It's unusual. But it happens; one example
|
|
| 4671 | + is the Applicative (ST s) instance in GHC.Internal.Control.Monad.St.Lazy.Imp
|
|
| 4673 | 4672 | {-# NOINLINE res1 #-}
|
| 4674 | 4673 | !res1 = unST fm s
|
| 4675 | 4674 | |
| 4676 | - TL;DR: the sc_bndr::Id field in StrictBind always satisfies `lamBndrInfoOK`.
|
|
| 4677 | - This means that in the above example { x! = case y of ... } we won't
|
|
| 4678 | - float the `case y` out. And perhaps that's fine because of the NOINLNE.
|
|
| 4679 | - We could imagine instead imposing these conditions only when we /duplicate/
|
|
| 4680 | - the StrictBind, rather than when we /create/ it, but that would require a
|
|
| 4681 | - bigger refactor.
|
|
| 4675 | + So in these cases we simply don't use StrictBind -- see `useStrictBind`. We
|
|
| 4676 | + could imagine instead imposing these conditions only when we /duplicate/ the
|
|
| 4677 | + StrictBind, rather than when we /create/ it, but that would require a bigger
|
|
| 4678 | + refactor.
|
|
| 4679 | + |
|
| 4680 | + What if a thunk has a strictness signature, or CPR information? We
|
|
| 4681 | + could make `useStrictBind` say False in these cases too, but that turns
|
|
| 4682 | + out to be a net loss: keeping a thunk is worse than losing the strictness
|
|
| 4683 | + information. Example, where f1,f2 are both strict:
|
|
| 4684 | + let f* {- Str=<S> -} = case x of { True -> f1; False -> f2 }
|
|
| 4685 | + in body
|
|
| 4686 | + Keeping it as-is would use CBV for calls of `f` in `body`. But
|
|
| 4687 | + allowing StrictBind would give us
|
|
| 4688 | + join j f* = body
|
|
| 4689 | + in case x of { True -> j f1; False -> j f2 }
|
|
| 4690 | + which avoids the thunk. It's a trade-off.
|
|
| 4691 | + |
|
| 4692 | + For CPR info, see Note [Thunk splitting] in GHC.Core.Opt.WorkWrap,
|
|
| 4693 | + especially (B) CPR-led thunk splitting.
|
|
| 4682 | 4694 | |
| 4683 | 4695 | Note [Join point abstraction] Historical note
|
| 4684 | 4696 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| ... | ... | @@ -598,13 +598,19 @@ tryWW ww_opts is_rec fn_id rhs |
| 598 | 598 | = splitFun ww_opts new_fn_id rhs
|
| 599 | 599 | |
| 600 | 600 | -- See Note [Thunk splitting]
|
| 601 | - | isNonRec is_rec, is_thunk
|
|
| 602 | - = splitThunk ww_opts is_rec new_fn_id rhs
|
|
| 601 | + | isNonRec is_rec -- See (TS4)
|
|
| 602 | + , is_thunk -- All join points go via splitFun
|
|
| 603 | + = do { -- Try strictness then CPR; see (TS5)
|
|
| 604 | + rhs' <- splitThunkStr ww_opts new_fn_id rhs `orElseM`
|
|
| 605 | + splitThunkCpr ww_opts new_fn_id rhs `orElseM`
|
|
| 606 | + return rhs
|
|
| 607 | + ; return [ (new_fn_id, rhs') ] }
|
|
| 603 | 608 | |
| 604 | 609 | | otherwise
|
| 605 | 610 | = return [ (new_fn_id, rhs) ]
|
| 606 | 611 | |
| 607 | 612 | where
|
| 613 | + is_join = isJoinId fn_id
|
|
| 608 | 614 | fn_info = idInfo fn_id
|
| 609 | 615 | (wrap_dmds, _) = splitDmdSig (dmdSigInfo fn_info)
|
| 610 | 616 | new_fn_id = zap_join_cpr $ zap_usage fn_id
|
| ... | ... | @@ -613,14 +619,13 @@ tryWW ww_opts is_rec fn_id rhs |
| 613 | 619 | -- See Note [Zapping DmdEnv after Demand Analyzer] and
|
| 614 | 620 | -- See Note [Zapping Used Once info in WorkWrap]
|
| 615 | 621 | |
| 616 | - zap_join_cpr id
|
|
| 617 | - | isJoinId id = id `setIdCprSig` topCprSig
|
|
| 618 | - | otherwise = id
|
|
| 622 | + zap_join_cpr id | is_join = id `setIdCprSig` topCprSig
|
|
| 623 | + | otherwise = id
|
|
| 619 | 624 | -- See Note [Don't w/w join points for CPR]
|
| 620 | 625 | |
| 621 | - is_fun = notNull wrap_dmds || isJoinId fn_id
|
|
| 622 | - is_thunk = not is_fun && not (exprIsHNF rhs) && not (isJoinId fn_id)
|
|
| 623 | - && not (isUnliftedType (idType fn_id))
|
|
| 626 | + is_fun = notNull wrap_dmds || is_join
|
|
| 627 | + is_thunk = not (exprIsHNF rhs)
|
|
| 628 | + && not (isUnliftedType (idType fn_id))
|
|
| 624 | 629 | |
| 625 | 630 | {-
|
| 626 | 631 | Note [Zapping DmdEnv after Demand Analyzer]
|
| ... | ... | @@ -806,7 +811,7 @@ splitFun ww_opts fn_id rhs |
| 806 | 811 | |
| 807 | 812 | (wrap_dmds, div) = splitDmdSig (dmdSigInfo fn_info)
|
| 808 | 813 | |
| 809 | - cpr_ty = getCprSig (cprSigInfo fn_info)
|
|
| 814 | + cpr_ty = cprSigType (cprSigInfo fn_info)
|
|
| 810 | 815 | -- Arity of the CPR sig should match idArity when it's not a join point.
|
| 811 | 816 | -- See Note [Arity trimming for CPR signatures] in GHC.Core.Opt.CprAnal
|
| 812 | 817 | cpr = assertPpr (isJoinId fn_id || cpr_ty == topCprType || ct_arty cpr_ty == arityInfo fn_info)
|
| ... | ... | @@ -957,114 +962,165 @@ the form [Demand=<L,1*(C(1,...(C(1,U))))>] |
| 957 | 962 | |
| 958 | 963 | Note [Thunk splitting]
|
| 959 | 964 | ~~~~~~~~~~~~~~~~~~~~~~
|
| 960 | -Suppose x is used strictly; never mind whether it has the CPR
|
|
| 961 | -property. I'll use a '*' to mean "x* is demanded strictly".
|
|
| 962 | - |
|
| 963 | - let
|
|
| 964 | - x* = x-rhs
|
|
| 965 | +The worker/wrapper split mainly affects let-bound /functions/. But we can also use
|
|
| 966 | +demand and CPR information to improve let-bound /thunks/. Specifically:
|
|
| 967 | + |
|
| 968 | +* (A) Demand-led thunk splitting:
|
|
| 969 | + If a thunk is demanded strictly, perhaps nestedly, we can turn it into a
|
|
| 970 | + case, perhaps nestedly. For example
|
|
| 971 | + let x :: (Int, Int)
|
|
| 972 | + {- Dmd = SP(S,L) -}
|
|
| 973 | + x = rhs
|
|
| 974 | + in body
|
|
| 975 | + We would ultimately like to get
|
|
| 976 | + case rhs of x { (a,b) ->
|
|
| 977 | + case a of { I# a# ->
|
|
| 978 | + body
|
|
| 979 | + |
|
| 980 | + This is much better: no thunk for `rhs`; and known-data-con unfoldings for `x`
|
|
| 981 | + and `a` inside `body`.
|
|
| 982 | + |
|
| 983 | +* (B) CPR-led thunk splitting:
|
|
| 984 | + If a thunk has the (possibly-nested) CPR property /and/ is demanded strictly
|
|
| 985 | + at top level, we can again generate a case
|
|
| 986 | + let x :: (Int, Int)
|
|
| 987 | + {- Dmd = S, Cpr = (_, I# _) -}
|
|
| 988 | + x = rhs
|
|
| 989 | + in body
|
|
| 990 | + The CPR info says that if `x` is evaluated (which it is), it'll return a pair,
|
|
| 991 | + whose second component can very cheaply be evaluated to a (I# b#). We would
|
|
| 992 | + like to end up with
|
|
| 993 | + case rhs of x { (a,b) ->
|
|
| 994 | + case b of { I# b# ->
|
|
| 995 | + body
|
|
| 996 | + which is good for the same reasons as before. See (TS3) for an example.
|
|
| 997 | + |
|
| 998 | +Implementation
|
|
| 999 | +~~~~~~~~~~~~~~
|
|
| 1000 | +The worker/wrapper pass does not generate these nested case expressions directly.
|
|
| 1001 | +Instead it generates a regular let-binding but, since the binder is marked as
|
|
| 1002 | +strict, any `case` expressions in the RHS will float out. Specifically:
|
|
| 1003 | + |
|
| 1004 | +(TS1) For (A), `splitThunkStr` generates the binding
|
|
| 1005 | + let x* = case rhs of { (a,b) ->
|
|
| 1006 | + case a of { I# a# ->
|
|
| 1007 | + (I# a, b)
|
|
| 1008 | + Now when the Simplifier floats the `case` expressions out, we get
|
|
| 1009 | + case rhs of { (a,b) ->
|
|
| 1010 | + case a of { I# a# ->
|
|
| 1011 | + let x = (I# a, b)
|
|
| 965 | 1012 | in body
|
| 1013 | + which is equivalent to the expression in (A) above.
|
|
| 966 | 1014 | |
| 967 | -splitThunk transforms like this:
|
|
| 968 | - let
|
|
| 969 | - x* = let x = x-rhs in
|
|
| 970 | - case x of { I# a -> I# a }
|
|
| 971 | - in body
|
|
| 1015 | + `splitThunkStr` also works on /absent/ thunks: see (TS6).
|
|
| 972 | 1016 | |
| 973 | -This is a little strange: we are re-using the same `x` in the RHS; and
|
|
| 974 | -the RHS takes `x` apart and reboxes it. But because the outer 'let' is
|
|
| 975 | -strict, and the inner let mentions `x` only once, the simplifier
|
|
| 976 | -transform it to
|
|
| 977 | - case x-rhs of
|
|
| 978 | - I# a -> let x* = I# a
|
|
| 979 | - in body
|
|
| 980 | - |
|
| 981 | -That is good: in `body` we know the form of `x`, which
|
|
| 982 | - * gives the CPR property, and
|
|
| 983 | - * allows case-of-case to happen on x
|
|
| 984 | - |
|
| 985 | -Notes
|
|
| 986 | -* I tried transforming like this:
|
|
| 987 | - let
|
|
| 988 | - x* = let x = x-rhs in
|
|
| 989 | - case x of { I# a -> x }
|
|
| 990 | - in body
|
|
| 991 | - where I return `x` itself, rather than reboxing it. But this
|
|
| 992 | - turned out to cause some regressions, which I never fully
|
|
| 993 | - investigated.
|
|
| 994 | - |
|
| 995 | -* Suppose x-rhs is itself a case:
|
|
| 996 | - x-rhs = case e of { T -> I# e1; F -> I# e2 }
|
|
| 997 | - Then we'll get
|
|
| 998 | - join j a = let x* = I# a in body
|
|
| 999 | - in case e of { T -> j e1; F -> j e2 }
|
|
| 1000 | - which is good (no boxing). But in the original, unsplit program
|
|
| 1001 | - we would transform
|
|
| 1002 | - let x* = case e of ... in body
|
|
| 1003 | - ==> join j2 x = body
|
|
| 1004 | - in case e of { T -> j2 (I# e1); F -> j (I# e2) }
|
|
| 1017 | + Rather cleverly, `splitThunkStr` uses `mkWWstr_one`, the /existing/ function
|
|
| 1018 | + argument w/w splitting function, to do the unpacking. This automatically
|
|
| 1019 | + deals with nested strictness.
|
|
| 1020 | + |
|
| 1021 | +(TS2) For (B), `splitThunkCpr` behaves exactly analogously, again
|
|
| 1022 | + using `mkWWcpr_one` to do the unpacking. It works only for /demanded/ thunks.
|
|
| 1023 | + |
|
| 1024 | +Wrinkles:
|
|
| 1025 | + |
|
| 1026 | +(TS3) Here's an example of (B), thunk-splitting based on CPR information, in action.
|
|
| 1027 | + Suppose we have
|
|
| 1028 | + let x* = case e of { T -> Just e1; F -> Just e2 }
|
|
| 1029 | + in body
|
|
| 1030 | + If the Simplifier floats the `case` out, we get
|
|
| 1031 | + join j2 x = body
|
|
| 1032 | + in case e of { T -> j2 (Just e1); F -> j2 (Just e2) }
|
|
| 1005 | 1033 | which is not good (boxing).
|
| 1006 | 1034 | |
| 1007 | -* In fact, splitThunk uses the function argument w/w splitting
|
|
| 1008 | - function, mkWWstr_one, so that if x's demand is deeper (say U(U(L,L),L))
|
|
| 1009 | - then the splitting will go deeper too.
|
|
| 1035 | + If we first do the thunk-splitting transformation, we get
|
|
| 1036 | + let x* = case (case e of { T -> Just e1; F -> Just e2 }) of
|
|
| 1037 | + Just a -> Just a
|
|
| 1038 | + in body
|
|
| 1010 | 1039 | |
| 1011 | -* For recursive thunks, the Simplifier is unable to float `x-rhs` out of
|
|
| 1012 | - `x*`'s RHS, because `x*` occurs freely in `x-rhs`, and will just change it
|
|
| 1013 | - back to the original definition, so we just split non-recursive thunks.
|
|
| 1040 | + Now when the Simplifier floats we will get:
|
|
| 1041 | + join j a = let x* = Just a in body
|
|
| 1042 | + in case e of { T -> j e1; F -> j e2 }
|
|
| 1043 | + which is much better (no boxing).
|
|
| 1014 | 1044 | |
| 1015 | -Note [Thunk splitting for top-level binders]
|
|
| 1016 | -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 1017 | -Top-level bindings are never strict. Yet they can be absent, as T14270 shows:
|
|
| 1018 | - |
|
| 1019 | - module T14270 (mkTrApp) where
|
|
| 1020 | - mkTrApp x y
|
|
| 1021 | - | Just ... <- ... typeRepKind x ...
|
|
| 1022 | - = undefined
|
|
| 1023 | - | otherwise
|
|
| 1024 | - = undefined
|
|
| 1025 | - typeRepKind = Tick scc undefined
|
|
| 1026 | - |
|
| 1027 | -(T19180 is a profiling-free test case for this)
|
|
| 1028 | -Note that `typeRepKind` is not exported and its only use site in
|
|
| 1029 | -`mkTrApp` guards a bottoming expression. Thus, demand analysis
|
|
| 1030 | -figures out that `typeRepKind` is absent and splits the thunk to
|
|
| 1031 | - |
|
| 1032 | - typeRepKind =
|
|
| 1033 | - let typeRepKind = Tick scc undefined in
|
|
| 1034 | - let typeRepKind = absentError in
|
|
| 1035 | - typeRepKind
|
|
| 1036 | - |
|
| 1037 | -But now we have a local binding with an External Name
|
|
| 1038 | -(See Note [About the NameSorts]). That will trigger a CoreLint error, which we
|
|
| 1039 | -get around by localising the Id for the auxiliary bindings in 'splitThunk'.
|
|
| 1045 | +(TS4) We only do thunk-splitting for thunks that are non-recursive
|
|
| 1046 | + For recursive thunks, the Simplifier is unable to float `x-rhs` out of
|
|
| 1047 | + `x*`'s RHS, because `x*` occurs freely in `x-rhs`, and will just change it
|
|
| 1048 | + back to the original definition.
|
|
| 1049 | + |
|
| 1050 | +(TS5) Notice that (B) CPR-led thunk splitting can be useful:
|
|
| 1051 | + * when multi-constructor data types are involved, so that the argument-unboxing
|
|
| 1052 | + mechanism (A) is impotent. See the example in (TS3), which uses `Maybe`.
|
|
| 1053 | + * when the binder is only demanded head-strictly, but CPR has nested information.
|
|
| 1054 | + |
|
| 1055 | + So (A) and (B) have complementary benefits. We might do even better by
|
|
| 1056 | + /combining/ (A) and (B), but at the moment we try (A) first, and then try (B)
|
|
| 1057 | + if (A) is impotent. Combining them would be more work.
|
|
| 1058 | + |
|
| 1059 | +(TS6) We can also exploit /absence/. Consider
|
|
| 1060 | + let x {-# Dmd = A #-} = rhs
|
|
| 1061 | + in body
|
|
| 1062 | + Generally speaking `x` will be absent becuase it is passed to functions that
|
|
| 1063 | + don't use it, in which case worker/wrapper on those calls will make `x` unused.
|
|
| 1064 | + But suppose `x` is absent inside a /lazy/ argument:
|
|
| 1065 | + let f p = (fst p, 99) in
|
|
| 1066 | + let x = rhs in
|
|
| 1067 | + f (True, x)
|
|
| 1068 | + Here f's argument is lazy, but `x` is definitely absent, and we could replace
|
|
| 1069 | + `x` with an absent filler. Using (TS1) would give us
|
|
| 1070 | + let x = absentError "urk" in ...
|
|
| 1071 | + which is great.
|
|
| 1072 | + |
|
| 1073 | + So `splitThunkStr` works /regardless/ of the demand on `x`; whether it fires is
|
|
| 1074 | + determined only by the `useful` flag returned by `mkWWstr_one`.
|
|
| 1075 | + |
|
| 1076 | +(TS7) Adopting (TS6) leads to one other complication: a top-level binder with
|
|
| 1077 | + an External Name (see Note [About the NameSorts]) can be Absent. If so,
|
|
| 1078 | + it must not be used as a local binder in the `Let` created by `splitThunkStr`.
|
|
| 1079 | + T19180 is a test case for this rather obscure situation.
|
|
| 1080 | + |
|
| 1081 | + Solution: use `localiseId` to localise the binder for the `Let`.
|
|
| 1040 | 1082 | -}
|
| 1041 | 1083 | |
| 1042 | --- | See Note [Thunk splitting].
|
|
| 1043 | ---
|
|
| 1044 | --- splitThunk converts the *non-recursive* binding
|
|
| 1084 | +-- | splitThunkStr converts the *non-recursive* binding
|
|
| 1045 | 1085 | -- x = e
|
| 1046 | 1086 | -- into
|
| 1047 | --- x = let x' = e in
|
|
| 1048 | --- case x' of I# y -> let x' = I# y in x'
|
|
| 1049 | --- See comments above. Is it not beautifully short?
|
|
| 1050 | --- Moreover, it works just as well when there are
|
|
| 1051 | --- several binders, and if the binders are lifted
|
|
| 1052 | --- E.g. x = e
|
|
| 1053 | --- --> x = let x' = e in
|
|
| 1054 | --- case x' of (a,b) -> let x' = (a,b) in x'
|
|
| 1055 | --- Here, x' is a localised version of x, in case x is a
|
|
| 1056 | --- top-level Id with an External Name, because Lint rejects local binders with
|
|
| 1057 | --- External Names; see Note [About the NameSorts] in GHC.Types.Name.
|
|
| 1058 | ---
|
|
| 1059 | --- How can we do thunk-splitting on a top-level binder? See
|
|
| 1060 | --- Note [Thunk splitting for top-level binders].
|
|
| 1061 | -splitThunk :: WwOpts -> RecFlag -> Var -> Expr Var -> UniqSM [(Var, Expr Var)]
|
|
| 1062 | -splitThunk ww_opts is_rec x rhs
|
|
| 1063 | - = assert (not (isJoinId x)) $
|
|
| 1064 | - do { let x' = localiseId x -- See comment above
|
|
| 1065 | - ; (useful,_args, wrap_fn, fn_arg)
|
|
| 1066 | - <- mkWWstr_one ww_opts x' NotMarkedStrict
|
|
| 1067 | - ; let res = [ (x, Let (NonRec x' rhs) (wrap_fn fn_arg)) ]
|
|
| 1068 | - ; if useful then assertPpr (isNonRec is_rec) (ppr x) -- The thunk must be non-recursive
|
|
| 1069 | - return res
|
|
| 1070 | - else return [(x, rhs)] } |
|
| 1087 | +-- x = let x = e in
|
|
| 1088 | +-- case x of I# y -> I# y
|
|
| 1089 | +-- See Note [Thunk splitting].
|
|
| 1090 | +splitThunkStr :: WwOpts -> Var -> CoreExpr -> UniqSM (Maybe CoreExpr)
|
|
| 1091 | +splitThunkStr ww_opts x rhs
|
|
| 1092 | + = do { let x' = localiseId x -- See (TS7)
|
|
| 1093 | + ; (useful, _transit_vars, wrap_fn, fn_arg) <- mkWWstr_one ww_opts x' NotMarkedStrict
|
|
| 1094 | + -- Use mkWWstr_one: see (TS1) in Note [Thunk splitting]
|
|
| 1095 | + |
|
| 1096 | + ; if useful
|
|
| 1097 | + then return (Just (Let (NonRec x' rhs) $
|
|
| 1098 | + wrap_fn fn_arg))
|
|
| 1099 | + else return Nothing }
|
|
| 1100 | + |
|
| 1101 | +splitThunkCpr :: WwOpts -> Var -> CoreExpr -> UniqSM (Maybe CoreExpr)
|
|
| 1102 | +-- See Note [Thunk splitting]
|
|
| 1103 | +-- Given x* = rhs, where x* is used outermost-strictly, and x* has a (nested) CPR
|
|
| 1104 | +-- property, say Just (Just _), return this new RHS:
|
|
| 1105 | +-- let x' = rhs in
|
|
| 1106 | +-- case x' of { Just p -> case p of { Just q -> -- Unpacking code
|
|
| 1107 | +-- Just (Just q) }} -- Repacking code
|
|
| 1108 | +-- Works for multi-constructor data types, where splitThunkStr will fail
|
|
| 1109 | +splitThunkCpr ww_opts x rhs
|
|
| 1110 | + | isStrUsedDmd (idDemandInfo x) -- See (TS2)
|
|
| 1111 | + = assertPpr (cprTypeArity cpr_type == 0) (ppr x) $ -- It's a thunk
|
|
| 1112 | + do { (useful, _transit_vars, rebuild_result, unpacker) <- mkWWcpr_one ww_opts x cpr
|
|
| 1113 | + -- Use mkWWcpr_one: see (TS2) in Note [Thunk splitting]
|
|
| 1114 | + ; if useful
|
|
| 1115 | + then return (Just (Let (NonRec x rhs) $
|
|
| 1116 | + unpacker rebuild_result))
|
|
| 1117 | + else return Nothing }
|
|
| 1118 | + |
|
| 1119 | + | otherwise
|
|
| 1120 | + = return Nothing
|
|
| 1121 | + where
|
|
| 1122 | + cpr_type :: CprType
|
|
| 1123 | + cpr_type = cprSigType (idCprSig x)
|
|
| 1124 | + cpr :: Cpr
|
|
| 1125 | + cpr = cprTypeCpr cpr_type
|
|
| 1126 | + |
| ... | ... | @@ -8,7 +8,7 @@ A library for the ``worker\/wrapper'' back-end to the strictness analyser |
| 8 | 8 | {-# LANGUAGE ViewPatterns #-}
|
| 9 | 9 | |
| 10 | 10 | module GHC.Core.Opt.WorkWrap.Utils
|
| 11 | - ( WwOpts(..), mkWwBodies, mkWWstr, mkWWstr_one
|
|
| 11 | + ( WwOpts(..), mkWwBodies, mkWWstr, mkWWstr_one, mkWWcpr_one
|
|
| 12 | 12 | , needsVoidWorkerArg
|
| 13 | 13 | , DataConPatContext(..)
|
| 14 | 14 | , UnboxingDecision(..), canUnboxArg
|
| ... | ... | @@ -230,8 +230,7 @@ mkWwBodies opts fun_id ww_arity arg_vars res_ty demands res_cpr |
| 230 | 230 | ; let (work_args, work_marks) = unzip work_args_str
|
| 231 | 231 | |
| 232 | 232 | -- Do CPR w/w. See Note [Always do CPR w/w]
|
| 233 | - ; (useful2, wrap_fn_cpr, work_fn_cpr)
|
|
| 234 | - <- mkWWcpr_entry opts res_ty' res_cpr
|
|
| 233 | + ; (useful2, wrap_fn_cpr, work_fn_cpr) <- mkWWcpr_entry opts res_ty' res_cpr
|
|
| 235 | 234 | |
| 236 | 235 | ; let (work_lam_args, work_call_args, work_call_str)
|
| 237 | 236 | | needsVoidWorkerArg fun_id arg_vars work_args
|
| ... | ... | @@ -985,6 +984,12 @@ mkWWstr opts args str_marks |
| 985 | 984 | -- * wrap_arg assumes work_args are in scope, and builds a ConApp that
|
| 986 | 985 | -- reconstructs the RHS of wrap_var that we pass to the original RHS
|
| 987 | 986 | -- See Note [Worker/wrapper for Strictness and Absence]
|
| 987 | +--
|
|
| 988 | +-- E.g. mkWWstr_one (x :: (Int,Int) with strictess saying to unbox the pair and second Int
|
|
| 989 | +-- = ( True -- useful
|
|
| 990 | +-- , [p,q#], -- work_args
|
|
| 991 | +-- , \e -> case x of (p,q) -> case q of I# q# -> e -- wrap_fn
|
|
| 992 | +-- , (p, I# q) ) -- wrap_arg
|
|
| 988 | 993 | mkWWstr_one :: WwOpts
|
| 989 | 994 | -> Var
|
| 990 | 995 | -> StrictnessMark
|
| ... | ... | @@ -1637,7 +1642,9 @@ mk_res_bndr body_ty = do |
| 1637 | 1642 | -- 1. A 'WwUse' capturing whether the split does anything useful.
|
| 1638 | 1643 | -- 2. The list of transit variables (see the Note).
|
| 1639 | 1644 | -- 3. The result builder expression for the wrapper. The original case binder if not useful.
|
| 1640 | --- 4. The result unpacking expression for the worker. 'nop_fn' if not useful.
|
|
| 1645 | +-- Assumes that the transit vars are in scope
|
|
| 1646 | +-- 4. The result unpacking expression for the worker. 'nop_fn' if not useful
|
|
| 1647 | +-- Brings the transit vars into scope
|
|
| 1641 | 1648 | type CprWwResultOne = (WwUse, OrdList Var, CoreExpr , CoreExpr -> CoreExpr)
|
| 1642 | 1649 | type CprWwResultMany = (WwUse, OrdList Var, [CoreExpr], CoreExpr -> CoreExpr)
|
| 1643 | 1650 | |
| ... | ... | @@ -1768,7 +1775,7 @@ To achieve said transformation, 'mkWWcpr_entry' |
| 1768 | 1775 | what belongs in the case scrutinee of the unpacker through the communicated
|
| 1769 | 1776 | Id `r`: The unpacking expression will be free in that variable.
|
| 1770 | 1777 | (This is a similar contract as that of 'mkWWstr_one' for strict args.)
|
| 1771 | - 3. 'mkWWstr_one' produces a bunch of *transit vars*: Those result variables
|
|
| 1778 | + 3. 'mkWWcpr_one' produces a bunch of *transit vars*: Those result variables
|
|
| 1772 | 1779 | that have to be transferred from the worker to the wrapper, where the
|
| 1773 | 1780 | constructed result can be rebuilt, `a` and `b` above. Part (3) is
|
| 1774 | 1781 | responsible for tupling them up in the worker and taking the tuple apart
|
| ... | ... | @@ -8,6 +8,7 @@ |
| 8 | 8 | module GHC.Types.Cpr (
|
| 9 | 9 | Cpr (ConCpr), topCpr, botCpr, flatConCpr, asConCpr,
|
| 10 | 10 | CprType (..), topCprType, botCprType, flatConCprType,
|
| 11 | + cprTypeArity, cprTypeCpr,
|
|
| 11 | 12 | lubCprType, applyCprTy, abstractCprTy, trimCprTy,
|
| 12 | 13 | UnpackConFieldsResult (..), unpackConFieldsCpr,
|
| 13 | 14 | CprSig (..), topCprSig, isTopCprSig, mkCprSigForArity, mkCprSig,
|
| ... | ... | @@ -106,6 +107,12 @@ data CprType |
| 106 | 107 | -- 'ct_arty' arguments
|
| 107 | 108 | }
|
| 108 | 109 | |
| 110 | +cprTypeArity :: CprType -> Arity
|
|
| 111 | +cprTypeArity = ct_arty
|
|
| 112 | + |
|
| 113 | +cprTypeCpr :: CprType -> Cpr
|
|
| 114 | +cprTypeCpr = ct_cpr
|
|
| 115 | + |
|
| 109 | 116 | instance Eq CprType where
|
| 110 | 117 | a == b = ct_cpr a == ct_cpr b
|
| 111 | 118 | && (ct_arty a == ct_arty b || ct_cpr a == topCpr)
|
| ... | ... | @@ -167,7 +174,7 @@ seqCprTy (CprType _ cpr) = seqCpr cpr |
| 167 | 174 | |
| 168 | 175 | -- | The arity of the wrapped 'CprType' is the arity at which it is safe
|
| 169 | 176 | -- to unleash. See Note [Understanding DmdType and DmdSig] in "GHC.Types.Demand"
|
| 170 | -newtype CprSig = CprSig { getCprSig :: CprType }
|
|
| 177 | +newtype CprSig = CprSig { cprSigType :: CprType }
|
|
| 171 | 178 | deriving (Eq, Binary)
|
| 172 | 179 | |
| 173 | 180 | -- | Turns a 'CprType' computed for the particular 'Arity' into a 'CprSig'
|
| ... | ... | @@ -16,7 +16,7 @@ module GHC.Utils.Monad |
| 16 | 16 | , mapAccumLM
|
| 17 | 17 | , mapSndM
|
| 18 | 18 | , concatMapM
|
| 19 | - , mapMaybeM
|
|
| 19 | + , mapMaybeM, orElseM
|
|
| 20 | 20 | , anyM, allM, orM
|
| 21 | 21 | , foldlM, foldlM_, foldrM, foldMapM
|
| 22 | 22 | , whenM, unlessM
|
| ... | ... | @@ -250,6 +250,14 @@ unlessM :: Monad m => m Bool -> m () -> m () |
| 250 | 250 | unlessM condM acc = do { cond <- condM
|
| 251 | 251 | ; unless cond acc }
|
| 252 | 252 | |
| 253 | +-- | Monadic version of @orElse@
|
|
| 254 | +infixr 4 `orElseM`
|
|
| 255 | +orElseM :: Monad m => m (Maybe a) -> m a -> m a
|
|
| 256 | +orElseM ma def = do { mb_r <- ma
|
|
| 257 | + ; case mb_r of
|
|
| 258 | + Just x -> return x
|
|
| 259 | + Nothing -> def }
|
|
| 260 | + |
|
| 253 | 261 | -- | Like 'filterM', only it reverses the sense of the test.
|
| 254 | 262 | filterOutM :: (Applicative m) => (a -> m Bool) -> [a] -> m [a]
|
| 255 | 263 | filterOutM p =
|
| 1 | +module T28158 where
|
|
| 2 | + |
|
| 3 | +-- Here we miss CPR on y
|
|
| 4 | +-- It's a bit of an obscure example, but still; see #28158
|
|
| 5 | +f :: a -> a -> a -> [Int] -> [Int]
|
|
| 6 | +f a b c xs = let y = case xs of
|
|
| 7 | + [0] -> Just a
|
|
| 8 | + [1] -> Just b
|
|
| 9 | + _ -> Just c
|
|
| 10 | + in h y (case y of
|
|
| 11 | + Just z -> reverse (reverse (reverse (
|
|
| 12 | + reverse (reverse (reverse (
|
|
| 13 | + reverse (reverse xs)))))))
|
|
| 14 | + Nothing -> xs++xs)
|
|
| 15 | + |
|
| 16 | +h :: Maybe a -> b -> b
|
|
| 17 | +h (Just y) e = h Nothing e
|
|
| 18 | +h Nothing e = e |
| 1 | + |
|
| 2 | +==================== Tidy Core ====================
|
|
| 3 | +Result size of Tidy Core
|
|
| 4 | + = {terms: 72, types: 76, coercions: 0, joins: 1/1}
|
|
| 5 | + |
|
| 6 | +Rec {
|
|
| 7 | +h = \ @a @b ds e ->
|
|
| 8 | + case ds of {
|
|
| 9 | + Nothing -> e;
|
|
| 10 | + Just y -> h Nothing e
|
|
| 11 | + }
|
|
| 12 | +end Rec }
|
|
| 13 | + |
|
| 14 | +f = \ @a a1 b c xs ->
|
|
| 15 | + join {
|
|
| 16 | + $j ww
|
|
| 17 | + = h (Just ww)
|
|
| 18 | + (reverse1
|
|
| 19 | + (reverse1
|
|
| 20 | + (reverse1
|
|
| 21 | + (reverse1
|
|
| 22 | + (reverse1 (reverse1 (reverse1 (reverse xs) []) []) []) [])
|
|
| 23 | + [])
|
|
| 24 | + [])
|
|
| 25 | + []) } in
|
|
| 26 | + case xs of {
|
|
| 27 | + [] -> jump $j c;
|
|
| 28 | + : ds ds1 ->
|
|
| 29 | + case ds of { I# ds2 ->
|
|
| 30 | + case ds2 of {
|
|
| 31 | + __DEFAULT -> jump $j c;
|
|
| 32 | + 0# ->
|
|
| 33 | + case ds1 of {
|
|
| 34 | + [] -> jump $j a1;
|
|
| 35 | + : ipv ipv1 -> jump $j c
|
|
| 36 | + };
|
|
| 37 | + 1# ->
|
|
| 38 | + case ds1 of {
|
|
| 39 | + [] -> jump $j b;
|
|
| 40 | + : ipv ipv1 -> jump $j c
|
|
| 41 | + }
|
|
| 42 | + }
|
|
| 43 | + }
|
|
| 44 | + }
|
|
| 45 | + |
|
| 46 | + |
|
| 47 | + |
| ... | ... | @@ -636,3 +636,4 @@ test('T27962a', [extra_files(['T27962.hs'])], multimod_compile, ['T27962', '-v0 |
| 636 | 636 | test('T27963', [grep_errmsg(r'f/42')], compile, ['-O -ddump-rule-firings'])
|
| 637 | 637 | test('T27965', normal, compile, ['-O'])
|
| 638 | 638 | test('T27965a', normal, compile, ['-O'])
|
| 639 | +test('T28158', normal, compile, ['-O -dno-typeable-binds -dsuppress-all -dsuppress-uniques -ddump-simpl']) |