[Git][ghc/ghc][wip/case-bndr-unf] Wibble [skip ci]
Simon Peyton Jones pushed to branch wip/case-bndr-unf at Glasgow Haskell Compiler / GHC Commits: 38b6e0e7 by Simon Peyton Jones at 2026-11-19T23:48:40+00:00 Wibble [skip ci] - - - - - 1 changed file: - compiler/GHC/Core/Opt/WorkWrap.hs Changes: ===================================== compiler/GHC/Core/Opt/WorkWrap.hs ===================================== @@ -968,64 +968,96 @@ the form [Demand=<L,1*(C(1,...(C(1,U))))>] Note [Thunk splitting] ~~~~~~~~~~~~~~~~~~~~~~ -Consider a strict non-recursive let-binding: - let x* = x-rhs - in body - -(I'll use a '*' to mean "x* is demanded strictly".) -`splitThunk` transforms like this: - let x* = case x-rhs of { I# a -> I# a } - in body - -This may seem a little strange: the RHS takes `x` apart and reboxes it. -But because the outer 'let' is strict, the simplifier uses `StrictBind` -to float the `case` out, giving - case x-rhs of I# a -> - let x* = I# a in body - -That is good: in `body` we know the form of `x`, which - * gives the CPR property, and - * allows case-of-known-constructor to happen on x - * can avoid reboxing: see (TS1) +The worker/wrapper split mainly affects let-bound /functions/. But we can also use +demand and CPR information to improve let-bound /thunks/. Specifically: + +* (A) Demand-led thunk splitting: + If a thunk is demanded strictly, perhaps nestedly, we can turn it into a + case, perhaps nestedly. For example + let x :: (Int, Int) + {- Dmd = SP(S,L) -} + x = rhs + in body + We would ultimately like to get + case rhs of x { (a,b) -> + case a of { I# a# -> + body + + This much better: no thunk for `rhs`; and known-data-con unfoldings for `x` + and `a` inside `body`. + +* (B) CPR-led thunk splitting: + If a thunk has the (possibly-nested) CPR property /and/ is demanded strictly + at top level, we can again generate a case + let x :: (Int, Int) + {- Dmd = S, Cpr = (_, I# _) -} + x = rhs + in body + The CPR info says that if `x` is evaluated (which is is), it'll return a pair, + whose second component can very cheaply be evaluated to a (I# b#). We would + like to end up with + case rhs of x { (a,b) -> + case b of { I# b# -> + body + which is good for the same reasons as before. See (TS1) for an example. + +Notice that (B) CPR-led thunk splitting can be useful: + * If multi-constructor data types are involved, so that the argument-unboxing + mechanism (A) is impotent. + * If the binder is only demanded at top level, but CPR has nested information. + +We might do even better by combinding (A) and (B) but at the moment we try (A) +first, and then try (B) if (A) is impotent. + +Implementation +~~~~~~~~~~~~~~ +The worker/wrapper pass does not generate these nested case expressions directly. +Instead it generates a regular let-binding but, since the binder is marked as +strict, any `case` expessions in the RHS will float out. Specifically; + +* For (A), `splitThunkStr` generates the binding + let x* = case rhs of { (a,b) -> + case a of { I# a# -> + (I# a, b) + Now when the Simplifier floats the `case` expressions out, we get + case rhs of { (a.b) -> + case a of { I# a# -> + let x = (I# a b) + in body + which is equivalent to the expression in (A) above. + + Rather cleverly, `splitThunkStr` uses `mkWWstr_one`, the /existing/ function + argument w/w splitting function, to do the unpacking. This automatically + deals with nested strictness. + +* For (B), `splitThunkCpr` behaves exactly analogously, again using `mkWWcpr_one` + to do the unpacking. Wrinkles: -(T1) Suppose x-rhs is itself a case, so we started with - let x* = case e of { T -> I# e1; F -> I# e2 } +(TS1) Here's an example of (B), thunk-splitting based on CPR information, in action. + Suppose we have + let x* = case e of { T -> Just e1; F -> Just e2 } in body - If the Simplifire floats the `case` out, we get + If the Simplifier floats the `case` out, we get join j2 x = body - in case e of { T -> j2 (I# e1); F -> j (I# e2) } + in case e of { T -> j2 (Just e1); F -> j (Just e2) } which is not good (boxing). If we first do the thunk-splitting transformation, we get - let x* = case (case e of { T -> I# e1; F -> I# e2 }) of - I# a -> I# a + let x* = case (case e of { T -> Just e1; F -> Just e2 }) of + Just a -> Just a in body Now when the Simplifier floats we will get: - join j a = let x* = I# a in body + join j a = let x* = Just a in body in case e of { T -> j e1; F -> j e2 } - which is good (no boxing). + which is much better (no boxing). -(TS3) In fact, splitThunk uses the function argument w/w splitting - function, mkWWstr_one, so in our example we /actually/ get - let - x* = let x = x-rhs in - case x of { I# a -> I# a } - in body - Here `x` is used exactly once in the RHS so it'll be inlined. - - A big advantage of using `mkWWstr_one` is that if x's demand is deeper - (say U(U(L,L),L)) then the splitting will go deeper too. - -(TS4) For recursive thunks, the Simplifier is unable to float `x-rhs` out of +(TS2) For recursive thunks, the Simplifier is unable to float `x-rhs` out of `x*`'s RHS, because `x*` occurs freely in `x-rhs`, and will just change it back to the original definition, so we just split non-recursive thunks. -(TS5) We can do thunk-splitting for CPR too, to get stuff that strictness - cannot. Consider - Note [Thunk splitting for top-level binders] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Top-level bindings are never strict. Yet they can be absent, as T14270 shows: @@ -1076,7 +1108,7 @@ splitThunkStr :: WwOpts -> RecFlag -> Var -> CoreExpr -> UniqSM (Maybe CoreExpr) splitThunkStr ww_opts is_rec x rhs = assert (not (isJoinId x)) $ assertPpr (isNonRec is_rec) (ppr x) $ -- The thunk must be non-recursive - -- See (TS4) in Note [Thunk splitting] + -- See (TS2) in Note [Thunk splitting] do { let x' = localiseId x -- See comment above ; (useful, _transit_vars, wrap_fn, fn_arg) <- mkWWstr_one ww_opts x' NotMarkedStrict -- Use mkWWstr_one: see (TS3) in Note [Thunk splitting] @@ -1087,6 +1119,7 @@ splitThunkStr ww_opts is_rec x rhs else return Nothing } splitThunkCpr :: WwOpts -> RecFlag -> Var -> CoreExpr -> UniqSM (Maybe CoreExpr) +-- See Note [Thunk splitting] -- Given x* = rhs, where x* is used outermost-strictly, and x* has a (nested) CPR -- property, say Just (Just _), return this new RHS: -- let x' = rhs in @@ -1097,7 +1130,7 @@ splitThunkCpr ww_opts is_rec x rhs | isStrUsedDmd (idDemandInfo x) = assertPpr (cprTypeArity cpr_type == 0) (ppr x) $ -- It's a thunk assertPpr (isNonRec is_rec) (ppr x) $ -- The thunk must be non-recursive - -- See (TS4) in Note [Thunk splitting] + -- See (TS2) in Note [Thunk splitting] do { (useful, _transit_vars, rebuild_result, unpacker) <- mkWWcpr_one ww_opts x' cpr ; if useful then return (Just (Let (NonRec x' rhs) $ View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/38b6e0e797a61b027219ef98309b75de... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/38b6e0e797a61b027219ef98309b75de... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
participants (1)
-
Simon Peyton Jones (@simonpj)