[Git][ghc/ghc][wip/case-bndr-unf] More wibbles, mainly comments
Simon Peyton Jones pushed to branch wip/case-bndr-unf at Glasgow Haskell Compiler / GHC Commits: 360bcb51 by Simon Peyton Jones at 2026-10-23T17:42:01+01:00 More wibbles, mainly comments - - - - - 10 changed files: - compiler/GHC/Core.hs - compiler/GHC/Core/Lint.hs - compiler/GHC/Core/Opt/Simplify/Env.hs - compiler/GHC/Core/Opt/Simplify/Iteration.hs - compiler/GHC/Core/Opt/Simplify/Utils.hs - compiler/GHC/Core/Opt/SpecConstr.hs - compiler/GHC/Core/Opt/WorkWrap/Utils.hs - compiler/GHC/Core/Utils.hs - compiler/GHC/HsToCore/Utils.hs - compiler/GHC/Types/Id.hs Changes: ===================================== compiler/GHC/Core.hs ===================================== @@ -1393,6 +1393,151 @@ same way as above, otherwise the analysis would not be conservative wrt. the lenient semantics (which includes the strict one). It is also much harder to explain and maintain, as it turned out. +Note [Core binder IdInfo invariants] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +We allow the following IdInfo on different sorts of Core binders. +See the fields in GHC.Types.Id.Info.IdInfo: + +We classify binders into + * let-binders let x=rhs in body + * lambda binders (\x. e) + * case binders case e of x { ... } + * case-alternative binders case e of _ { K x y -> ... } + + Let Lambda Case/case-alt + --------------------------------------------- + Occurrence Yes Yes Yes + + InlinePrag Yes No No + Strictness (DmdSig) Yes No No + CPR info (CprSig) Yes No No + CAF Yes No No + Arity Yes No No + Call-arity Yes No No + Rules (RuleInfo) Yes No No + Unfolding Yes No(CBI1) evaldUnfolding only (CBI2) + Demand info Yes Yes Yes + One-shot No Yes No + ------------------------------------------------ + +The predicate `GHC.Types.Id.lamBndrInfoOK` checks the Lambda and Case columns +of this table (except one-shot info), and Core Lint applies it to every +non-let binder (`lintIdBndr`). + +Generally, lambda binders have only IdInfo that describes their /use/ +(e.g. occ-info, demand info) not their /definition/. See esp (CBI1). + +Case and case-alternative binders are similar because again they lack a definition, +and because they can /become/ a lambda binder when `mkDupableAlt` abstracts over them. + +Places that must take care to maintain these invariants (#27965): + +* The desugarer, when desugaring a strict binding let !x = rhs in body, + generates case x of x { DEFAULT -> body }; the case binder must not inherit + x's IdInfo. See `GHC.HsToCore.Utils.seqVar`. + +* `GHC.Core.Utils.bindNonRec` (used by `mkCoreLet`, and hence by the + Simplifier's `wrapFloats`), which turns let x = rhs in body into + case rhs of x { DEFAULT -> body } when `rhs` is unlifted and not + ok-for-speculation. Here `x` may well have a let-unfolding, e.g. one made + by `makeTrivial`. + +* `GHC.Core.Opt.SpecConstr.mkSeqs`, which builds case arg of arg' { DEFAULT -> rhs } + where `arg` may be an imported Id with an unfolding. + +All three use `GHC.Types.Id.mkCaseBndrFromLetBndr`. + +* The Simplifier's `simplNonRecE` does not use a StrictBind continuation for a + strict let-binding let x = rhs in body if `x` has RULES, a stable unfolding, + or a non-default inline pragma; for if it did, `mkDupableContWithDmds` might + turn `x` into the lambda binder of a join point. See `use_strict_bind`. + +(CBI1) Based on #17530 and #21496 we never attach unfoldings of any kind to lambda + binders, not even `evaldUnfolding`. + + It's just too easy for the call site to change and invalidate the unfolding. + E.g. the caller of the lambda drops a seq (e.g. because the lambda is strict + in its binder) which in turn makes the OtherCon[] unfolding a lie. So + unfoldings on lambda binders can never really be trusted when on lambda + binders if there is the chance of the call site to change. So it's easiest to + just never attach any to lambda binders to begin with, as well as stripping + them off if we e.g. float out an expression while abstracting over some + arguments. + +(CBI2) See Note [Evaluated-ness on case binders] + +(CBI3) Note that /occurrences/ may have IdInfo that is not present on the binder. + Notably, consider case e of r { Just x -> ..r...; Nothing -> ...r... } + The occurrence of `r` in the (Just x) branch will have an unfolding `Just x`, + and similarly in the `Nothing` branch. But the /binder/ of `r` has only + `evaldUnfolding` See Note [Evaluated-ness on case binders] + +Note [Evaluated-ness on case binders] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +The binding site of an Id in case expression (case-binder or case alternative) may +carry an `OtherCon []` (aka `evaldUnfolding`). meaning "this variable is certainly +evaluated". Specifically: + +* Case binders are always evaluated. `GHC.Core.Opt.Simplify.Iteration.simplAlts` and + CorePrep add `evaldUnfolding` to every case binder. + +* Case-alternative binders for strict constructor fields + binders) + data T = MkT !Int Int + case x of MkT y z -> ... + `y` gets an evaldUnfolding. + + This is done by `GHC.Core.Utils.dataConRepFSInstPat` (used by the interface-file loader, + among others), `GHC.Core.SimpleOpt`, the Simplifier (`adjustFieldsIdInfo`), and + worker/wrapper (for the argument and CPR result binders it makes; + see Note [Record evaluated-ness in worker/wrapper] in GHC.Core.Opt.WorkWrap.Utils). + +Why is this information on the /binder/? Within the Simplifier it is not +needed there: in each case alternative the Simplifier adds a more precise +unfolding for the case binder (and evaluated-ness for strict-field binders) to +its in-scope set, and refreshes every occurrence from that. But many other +passes rebuild variable occurrences /from the binder/: substitution-based +passes (SimpleOpt, Specialise, SpecConstr, CSE), SetLevels/FloatOut, the binder +swap in the occurrence analyser, CoreTidy, and CorePrep. After such a pass, +an occurrence knows that the variable is evaluated only if the binder does. + +That knowledge is used, mainly via `exprIsHNF`/`varIsHNF` (not by +`exprOkForSpeculation`; see Note [exprOkForSpeculation and evaluated variables] +in GHC.Core.Utils), by: + +* CorePrep: deciding whether a let-binding is a value (allocate directly) or + a thunk, e.g. `let x = Box y` where `y` is a strict field. + See Note [Pin evaluatedness on floats] in GHC.CoreToStg.Prep. CorePrep runs + just after CoreTidy, which propagates binder IdInfo to occurrences. + +* SetLevels/FloatOut: floating single-alternative cases out of loops, when the + scrutinee is evaluated; see Note [Floating single-alternative cases] in + GHC.Core.Opt.SetLevels. Also the call-by-value marks given to abstracted + variables (`GHC.Types.Id.transferPolyIdInfo`). + +* Worker/wrapper: `move_transit_vars` avoids an unboxed 1-tuple for a single + evaluated transit variable (test T18109); and the CPR result binders of the + wrapper remain evaluated (tests T13077a, #13027). The latter matters because + unboxed-tuple alternatives take a fast path in the Simplifier's + `adjustFieldsIdInfo`, and so get evaluated-ness only from the binder. + +* SimpleOpt: eliminating a case on an evaluated variable, and `mkFieldSeqFloats`. + +* Minor heuristic uses: FloatIn (the late FloatIn runs after FloatOut and CSE + with no Simplifier in between) and occurrences created by CSE or the binder + swap, until the next Simplifier run. + +Core Lint does /not/ depend on this information. Nor does anything in STG or +tag inference. + +Removing the information from the binders would require these passes to track +evaluated-ness in an environment instead (as SpecConstr already does with its +ValueEnv), which they currently don't. So we allow an OtherCon unfolding (and +only that) on case binders and case-alternative binders: +see the table in Note [Core binder IdInfo invariants] + + + ************************************************************************ * * In/Out type synonyms @@ -1742,128 +1887,6 @@ floatBinders (FloatTick {}) = [] The @Unfolding@ type is declared here to avoid numerous loops -Note [Never put `OtherCon` unfoldings on lambda binders] -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Based on #21496 we never attach unfoldings of any kind to lambda binders. -It's just too easy for the call site to change and invalidate the unfolding. -E.g. the caller of the lambda drops a seq (e.g. because the lambda is strict in it's binder) -which in turn makes the OtherCon[] unfolding a lie. -So unfoldings on lambda binders can never really be trusted when on lambda binders if there -is the chance of the call site to change. So it's easiest to just never attach any -to lambda binders to begin with, as well as stripping them off if we e.g. float out -and expression while abstracting over some arguments. - -See also Note [Lambda and case binders have vanilla IdInfo]. - -Note [Lambda and case binders have vanilla IdInfo] -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Invariant: the following binders - * lambda binders (\x. e) - * case binders case e of x { ... } - * case-alternative binders case e of _ { K x y -> ... } -have none of - (a) RULES - (b) a (non-default) inline pragma, such as INLINE or NOINLINE - (c) an unfolding, except that case binders and case-alternative binders may - have an evaluated-ness unfolding (OtherCon); - see Note [Evaluated-ness on case binders] -The predicate `GHC.Types.Id.lamBndrInfoOK` checks (a-c), and Core Lint checks -it (in `lintIdBndr`). Moreover lambda binders have no unfolding at all, not -even an OtherCon one: see Note [Never put `OtherCon` unfoldings on lambda binders]. - -Why? RULES, pragmas, and stable unfoldings describe a /let-bound/ Id, whose -definition is right there in the binding. It is far from clear what they would -mean for a lambda binder, whose value is supplied by the caller; and we can't -easily work out the consequences of allowing them. It is much simpler just to -rule them out. For example, `preInlineBetaUnconditionally` assumes the -invariant, and so does not check the binder's activation or stable unfolding. -This is very much in the spirit of #17530, when we stopped adding unfoldings to -lambda binders. - -Why case binders and case-alternative binders? Because they can become lambda -binders, when `mkDupableAlt` abstracts a join point over them. - -Places that must take care to maintain the invariant (#27965): - -* The desugarer, when desugaring a strict binding let !x = rhs in body, - generates case x of x { DEFAULT -> body }; the case binder must not inherit - x's IdInfo. See `GHC.HsToCore.Utils.seqVar`. - -* `GHC.Core.Utils.bindNonRec` (used by `mkCoreLet`, and hence by the - Simplifier's `wrapFloats`), which turns let x = rhs in body into - case rhs of x { DEFAULT -> body } when `rhs` is unlifted and not - ok-for-speculation. Here `x` may well have a let-unfolding, e.g. one made - by `makeTrivial`. - -Both use `GHC.Types.Id.mkCaseBndrFromLetBndr`. - -* The Simplifier, when duplicating a StrictBind continuation let x = <> in b, - makes a join point whose lambda binder must not inherit x's IdInfo. - See (DSB1) in Note [Duplicating StrictBind] in GHC.Core.Opt.Simplify.Iteration. - -Note [Evaluated-ness on case binders] -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -The binding site of an Id in case expression (case-binder or case alternative) -has only vanillaIdInfo, /except/ that it may carry an `OtherCon []` (aka `evaldUnfolding`). -meaning "this variable is certainly evaluated". Specifically: - -* Case binders are always evaluated. `GHC.Core.Opt.Simplify.Iteration.simplAlts` and - CorePrep add `evaldUnfolding` to every case binder. - -* Case-alternative binders for strict constructor fields - binders) - data T = MkT !Int Int - case x of MkT y z -> ... - `y` gets an evaldUnfolding. - - This is done by `GHC.Core.Utils.dataConRepFSInstPat` (used by the interface-file loader, - among others), `GHC.Core.SimpleOpt`, the Simplifier (`adjustFieldsIdInfo`), and - worker/wrapper (for the argument and CPR result binders it makes; - see Note [Record evaluated-ness in worker/wrapper] in GHC.Core.Opt.WorkWrap.Utils). - -Why is this information on the /binder/? Within the Simplifier it is not -needed there: in each case alternative the Simplifier adds a more precise -unfolding for the case binder (and evaluated-ness for strict-field binders) to -its in-scope set, and refreshes every occurrence from that. But many other -passes rebuild variable occurrences /from the binder/: substitution-based -passes (SimpleOpt, Specialise, SpecConstr, CSE), SetLevels/FloatOut, the binder -swap in the occurrence analyser, CoreTidy, and CorePrep. After such a pass, -an occurrence knows that the variable is evaluated only if the binder does. - -That knowledge is used, mainly via `exprIsHNF`/`varIsHNF` (not by -`exprOkForSpeculation`; see Note [exprOkForSpeculation and evaluated variables] -in GHC.Core.Utils), by: - -* CorePrep: deciding whether a let-binding is a value (allocate directly) or - a thunk, e.g. `let x = Box y` where `y` is a strict field. - See Note [Pin evaluatedness on floats] in GHC.CoreToStg.Prep. CorePrep runs - just after CoreTidy, which propagates binder IdInfo to occurrences. - -* SetLevels/FloatOut: floating single-alternative cases out of loops, when the - scrutinee is evaluated; see Note [Floating single-alternative cases] in - GHC.Core.Opt.SetLevels. Also the call-by-value marks given to abstracted - variables (`GHC.Types.Id.transferPolyIdInfo`). - -* Worker/wrapper: `move_transit_vars` avoids an unboxed 1-tuple for a single - evaluated transit variable (test T18109); and the CPR result binders of the - wrapper remain evaluated (tests T13077a, #13027). The latter matters because - unboxed-tuple alternatives take a fast path in the Simplifier's - `adjustFieldsIdInfo`, and so get evaluated-ness only from the binder. - -* SimpleOpt: eliminating a case on an evaluated variable, and `mkFieldSeqFloats`. - -* Minor heuristic uses: FloatIn (the late FloatIn runs after FloatOut and CSE - with no Simplifier in between) and occurrences created by CSE or the binder - swap, until the next Simplifier run. - -Core Lint does /not/ depend on this information. Nor does anything in STG or -tag inference. - -Removing the information from the binders would require these passes to track -evaluated-ness in an environment instead (as SpecConstr already does with its -ValueEnv), which they currently don't. So we allow an OtherCon unfolding (and -only that) on case binders and case-alternative binders: see (c) in -Note [Lambda and case binders have vanilla IdInfo]. -} -- | Records the /unfolding/ of an identifier, which is approximately the form the ===================================== compiler/GHC/Core/Lint.hs ===================================== @@ -1859,15 +1859,13 @@ lintIdBndr top_lvl bind_site id thing_inside 2 (ppr unf) -- Check that lambda, case, and case-alternative binders have - -- no RULES, no inline pragma, and no unfolding other than OtherCon - -- See Note [Lambda and case binders have vanilla IdInfo] in GHC.Core + -- only the IdInfo permitted by the table in + -- Note [Core binder IdInfo invariants] in GHC.Core ; checkL (is_let_bind || lamBndrInfoOK id) $ hang (text "Non-let binder" <+> quotes (ppr id) - <+> text "has RULES, an inline pragma, or a non-OtherCon unfolding") + <+> text "has IdInfo that only a let-binder may have") 2 (vcat [ text "Binding site:" <+> text (show_site bind_site) - , text "Inline pragma:" <+> ppr (idInlinePragma id) - , text "Rules:" <+> ppr (idCoreRules id) - , text "Unfolding:" <+> ppr (realIdUnfolding id) ]) + , text "IdInfo:" <+> ppr (idInfo id) ]) ; addLoc (IdTy id) (lintValueType id_ty) ===================================== compiler/GHC/Core/Opt/Simplify/Env.hs ===================================== @@ -1372,15 +1372,7 @@ getTCvSubst (SimplEnv { seInScope = in_scope, seTvSubst = tv_env, seCvSubst = cv getFullSubst :: InScopeSet -> SimplEnv -> Subst getFullSubst in_scope (SimplEnv { seIdSubst = id_env, seTvSubst = tv_env, seCvSubst = cv_env }) - = mkSubst in_scope (mapVarEnv to_expr id_env) tv_env cv_env - where - to_expr :: SimplClo -> CoreExpr - -- A tiresome impedence-matcher - to_expr (DoneEx e _) = e - to_expr (DoneId v) = Var v - to_expr (ContEx env e mco) = mkCastMCo e' mco - where - e' = GHC.Core.Subst.substExprSC (getFullSubst in_scope env) e + = mkSubst in_scope (mapVarEnv (simplCloExpr in_scope) id_env) tv_env cv_env simplCloExpr :: InScopeSet -> SimplClo -> OutExpr -- Turn a SimplClo into an OutExpr, by applying the substitution ===================================== compiler/GHC/Core/Opt/Simplify/Iteration.hs ===================================== @@ -1643,7 +1643,7 @@ completeBindX env from_what bndr rhs body cont | FromBeta arg_levity <- from_what , needsCaseBindingL arg_levity rhs -- Enforcing the let-can-float-invariant = do { (env1, bndr1) <- simplNonRecBndr env bndr -- Lambda binders don't have rules - -- See Note [Lambda and case binders have vanilla IdInfo] in GHC.Core + -- See Note [Core binder IdInfo invariants] in GHC.Core ; (floats, expr') <- simplNonRecBody env1 from_what body cont -- Do not float floats past the Case binder below ; let expr'' = wrapFloats floats expr' @@ -1795,7 +1795,7 @@ simplLamBndr :: SimplEnv -> InBndr -> SimplM (SimplEnv, OutBndr) -- could have a stable unfolding; -- see Historical Note [Case binders and join points] -- But now it is much simpler! We now only remove unfoldings. --- See Note [Never put `OtherCon` unfoldings on lambda binders] +-- See (CBI1) in Note [Core binder IdInfo invariants] simplLamBndr env bndr = simplBinder env (zapIdUnfolding bndr) simplLamBndrs :: SimplEnv -> [InBndr] -> SimplM (SimplEnv, [OutBndr]) @@ -1829,7 +1829,7 @@ simplNonRecE :: HasDebugCallStack simplNonRecE env from_what bndr (rhs, rhs_se) body cont | assert (isId bndr && not (isJoinId bndr) ) $ - is_strict_bind + use_strict_bind = -- Evaluate RHS strictly simplExprF (rhs_se `setInScopeFromE` env) rhs (StrictBind { sc_bndr = bndr, sc_body = body, sc_from = from_what @@ -1844,14 +1844,38 @@ simplNonRecE env from_what bndr (rhs, rhs_se) body cont ; return (floats1 `addFloats` floats2, expr') } where - is_strict_bind = case from_what of - FromBeta Unlifted -> True + info = idInfo bndr + + use_strict_bind -- If we are coming from a beta-reduction (FromBeta) we must - -- establish the let-can-float invariant, so go via StrictBind + -- establish the let-can-float invariant, so we must go via StrictBind -- If not, the invariant holds already, and it's optional. - - -- (FromBeta Lifted) or FromLet: look at the demand info - _ -> seCaseCase env && isStrUsedDmd (idDemandInfo bndr) + | FromBeta Unlifted <- from_what + = True + + -- Not if the demand is non-strict + | not (isStrUsedDmd (demandInfo info)) + = False + + -- Not if case-of-case is off + | not (seCaseCase env) + = False + + -- Not for things with RULES or INLINE or NOINLINE + -- because won't survive if we duplicate the join point. e.g. + -- let {-# NOINLINE x #-} + -- x* = case y of { ... } + -- in blah + -- If we make a join point j x = blah, we have nowhere convenient + -- to put that NOINLINE. Ditto INLINE or RULES. It's very unusual, + -- so no harm in simply not using StrictBind. + | FromLet <- from_what + , isStableUnfolding (unfoldingInfo info) + || not (isEmptyRuleInfo (ruleInfo info)) + || not (isAlwaysActive (inlinePragmaActivation (inlinePragInfo info))) + = False + + | otherwise = True ------------------ @@ -3998,15 +4022,10 @@ mkDupableContWithDmds env _ ; let join_body = wrapFloats floats1 join_inner res_ty = contResultType cont - -- If bndr' has IdInfo that a lambda binder must not have, - -- use a fresh lambda binder; see (DSB1) in Note [Duplicating StrictBind] - ; (arg_bndr, join_body') - <- if lamBndrInfoOK bndr' - then return (bndr', join_body) - else do { arg_bndr <- newId (fsLit "x") (idMult bndr') (idType bndr') - ; return (arg_bndr, Let (NonRec bndr' (Var arg_bndr)) join_body) } - - ; mkDupableStrictBind env arg_bndr join_body' res_ty } + -- bndr' is about to become a lambda binder, so it must have suitable + -- IdInfo; simplNonRecE ensures this. See Note [Core binder IdInfo invariants] + ; assertPpr (lamBndrInfoOK bndr') (ppr bndr') $ + mkDupableStrictBind env bndr' join_body res_ty } mkDupableContWithDmds env _ (StrictArg { sc_fun = fun, sc_cont = cont @@ -4200,7 +4219,7 @@ mkDupableAlt _env case_bndr jfloats (Alt con alt_bndrs alt_rhs_in) -- No lambda binder has an unfolding, but (currently) case binders can, -- so we must zap them here. But case binders never have a pragma, -- stable unfolding or RULES, so there is no need to zap those. - -- See Note [Lambda and case binders have vanilla IdInfo] in GHC.Core + -- See Note [Core binder IdInfo invariants] in GHC.Core join_rhs = mkLams (map zapIdUnfolding final_bndrs) rhs_with_seqs ; join_bndr <- newJoinId filtered_binders rhs_ty' @@ -4607,20 +4626,6 @@ So we potentially make a join-point for the body, thus: Just like StrictArg in fact -- and indeed they share code. -(DSB1) Lambda binders must not have a (non-default) inline pragma, a stable - unfolding, or RULES; see Note [Lambda and case binders have vanilla IdInfo] - in GHC.Core, and #27965. But the let-binder `x` may well - have them; e.g. in GHC.Internal.Control.Monad.ST.Lazy.Imp we have - {-# NOINLINE res1 #-} - !res1 = unST fm s - So if `x` has such IdInfo, we use a fresh binder `x'` for the join point, - and keep `x` as a let-binder, retaining all its IdInfo: - let x = <> in b ==> join j x' = let x = x' in b - in j <> - The hole is evaluated before the jump, so `x'` is a value, and `let x = x'` - is just an alias. We do this only when necessary, to avoid leaving an - alias in the common case. - Note [Join point abstraction] Historical note ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ NB: This note is now historical, describing how (in the past) we used ===================================== compiler/GHC/Core/Opt/Simplify/Utils.hs ===================================== @@ -1922,6 +1922,9 @@ preInlineBetaUnconditionally -- Like preInlineLetUnconditionally, but for a beta-redex (\bndr. body) arg -- The argument may or may not be already simplified (DoneEx or ContEx) -- +-- The binder is a lambda binder and so lacks most IdInfo; +-- see Note [Core binder IdInfo invariants] +-- -- Compared to preInlineLetUnconditionally we omit some checks: -- * The binder is never top-level, so no need for the -- Note [Top-level bottoming Ids] check, nor for `early_phase` @@ -1930,12 +1933,16 @@ preInlineBetaUnconditionally -- * A lambda binder never has an inline pragma, a stable unfolding, or RULES -- (see `lamBndrInfoOK`), so no need to check activation, nor for -- Note [Stable unfoldings and preInlineUnconditionally] +-- +-- And we add one extra one +-- * Check the let-can-float invariant, and don't inline if not + preInlineBetaUnconditionally env levity bndr arg | assertPpr (lamBndrInfoOK bndr) (ppr bndr) $ not pre_inline_unconditionally = Nothing | isCoVar bndr = Nothing -- Note [Do not inline CoVars unconditionally] | not (one_occ (idOccInfo bndr)) = Nothing - | needs_case_binding = Nothing + | needs_case_binding = Nothing -- Enforce the let-can-float invariant | otherwise = Just $! extendIdSubst env bndr arg -- If the arg is not yet simplified, it is a ContEx, which captures the -- lexical environment for us to restore in `simplInId`. @@ -1946,24 +1953,24 @@ preInlineBetaUnconditionally env levity bndr arg where pre_inline_unconditionally = sePreInline env + needs_case_binding = needsCaseBindingL levity rhs + -- Ok to test an InExpr or OutExpr in needsCaseBindingL because + -- exprOkForSpeculation is stable under simplification + + rhs :: CoreExpr -- Can be InExpr or OutExpr, but here it does not matter + rhs = case arg of + ContEx _ e _ -> e -- InExpr + DoneEx e _ -> e -- OutExpr + DoneId v -> Var v -- OutExpr + one_occ IAmDead = True -- Happens in ((\x.1) v) one_occ OneOcc{ occ_n_br = 1 , occ_in_lam = NotInsideLam } = True one_occ OneOcc{ occ_n_br = 1 , occ_in_lam = IsInsideLam - , occ_int_cxt = IsInteresting } = case arg of - ContEx _ e _ -> canInlineInLam e - DoneEx e _ -> canInlineInLam e - DoneId v -> canInlineInLam (Var v) + , occ_int_cxt = IsInteresting } = canInlineInLam rhs one_occ _ = False - -- Enforce the let-can-float invariant - -- Ok to test an InExpr in needsCaseBinding because - -- exprOkForSpeculation is stable under simplification - needs_case_binding = case arg of - ContEx _ e _ -> needsCaseBindingL levity e - DoneEx e _ -> needsCaseBindingL levity e - DoneId v -> needsCaseBindingL levity (Var v) canInlineInLam :: CoreExpr -> Bool -- Be very careful before inlining inside a lambda, because (a) we must not ===================================== compiler/GHC/Core/Opt/SpecConstr.hs ===================================== @@ -2072,7 +2072,7 @@ mkSeqs seqees res_ty rhs = -- localiseId: see (SCF1) in Note [SpecConstr and strict fields] -- mkCaseBndrFromLetBndr: arg_id may have IdInfo (e.g. an unfolding, -- if it is a global) that a case binder must not have; see - -- Note [Lambda and case binders have vanilla IdInfo] in GHC.Core + -- Note [Core binder IdInfo invariants] in GHC.Core res_ty ([Alt DEFAULT [] rhs]) ===================================== compiler/GHC/Core/Opt/WorkWrap/Utils.hs ===================================== @@ -1041,7 +1041,7 @@ unbox_one_arg opts arg_var -- Apply str info to new args. Also remove OtherCon unfoldings so they -- don't end up in lambda binders of the worker. - -- See Note [Never put `OtherCon` unfoldings on lambda binders] + -- See (CBI1) in Note [Core binder IdInfo invariants] arg_ids' = map zapIdUnfolding $ zipWithEqual setIdDemandInfo arg_ids ds ===================================== compiler/GHC/Core/Utils.hs ===================================== @@ -758,7 +758,7 @@ bindNonRec bndr rhs body where case_bind = mkDefaultCase rhs (mkCaseBndrFromLetBndr bndr) body -- mkCaseBndrFromLetBndr: bndr may have IdInfo suitable for a - -- let-binder; see Note [Lambda and case binders have vanilla IdInfo] + -- let-binder; see Note [Core binder IdInfo invariants] -- in GHC.Core let_bind = Let (NonRec bndr rhs) body ===================================== compiler/GHC/HsToCore/Utils.hs ===================================== @@ -267,7 +267,7 @@ bindMatchId new mid body -- -- The case binder has the same Name (and hence Unique), type and multiplicity -- as `var`, but not its IdInfo: `var` may have, say, a NOINLINE pragma, but a --- case binder must not; see Note [Lambda and case binders have vanilla IdInfo] +-- case binder must not; see Note [Core binder IdInfo invariants] -- in GHC.Core, and #27965. E.g. `res1` in GHC.Internal.Control.Monad.ST.Lazy.Imp -- has a NOINLINE pragma. seqVar :: Var -> CoreExpr -> CoreExpr ===================================== compiler/GHC/Types/Id.hs ===================================== @@ -893,7 +893,7 @@ setCaseBndrEvald str id -- that a case binder must not have, so we keep its Name, type, multiplicity -- and IdDetails, but reset its IdInfo, except that we record that it is -- evaluated. --- See Note [Lambda and case binders have vanilla IdInfo] in GHC.Core +-- See Note [Core binder IdInfo invariants] in GHC.Core mkCaseBndrFromLetBndr :: Id -> Id mkCaseBndrFromLetBndr id = id `setIdInfo` (vanillaIdInfo `setUnfoldingInfo` evaldUnfolding) @@ -917,20 +917,31 @@ idCoreRules id = ruleInfoRules (idSpecialisation id) idHasRules :: Id -> Bool idHasRules id = not (isEmptyRuleInfo (idSpecialisation id)) --- | True <=> the Id has no (non-default) inline pragma, no RULES, and no --- unfolding except perhaps an evaluated-ness one (OtherCon). Lambda binders, --- case binders, and case-alternative binders must all satisfy this. --- See Note [Lambda and case binders have vanilla IdInfo] in GHC.Core +-- | True <=> the Id has only the IdInfo permitted on a lambda, case, or +-- case-alternative binder. See the table in Note [Core binder IdInfo invariants] +-- in GHC.Core; Core Lint checks this invariant. +-- +-- We allow occurrence info, demand info, and one-shot info (which the table +-- permits only on lambda binders, but a case binder may be about to become one). +-- We also allow an `OtherCon []` unfolding; lambda binders must have no unfolding +-- at all, but Lint checks that separately. lamBndrInfoOK :: Id -> Bool lamBndrInfoOK id - = isDefaultInlinePragma (idInlinePragma id) - && unf_ok (realIdUnfolding id) - && not (idHasRules id) + = isDefaultInlinePragma (inlinePragInfo info) + && isNopSig (dmdSigInfo info) + && isTopCprSig (cprSigInfo info) + && mayHaveCafRefs (cafInfo info) + && arityInfo info == unknownArity + && callArityInfo info == unknownArity + && isEmptyRuleInfo (ruleInfo info) + && unf_ok (realUnfoldingInfo info) where + info = idInfo id + -- See Note [Evaluated-ness on case binders] in GHC.Core - unf_ok NoUnfolding = True - unf_ok (OtherCon _) = True - unf_ok _ = False + unf_ok NoUnfolding = True + unf_ok (OtherCon []) = True + unf_ok _ = False setIdSpecialisation :: Id -> RuleInfo -> Id setIdSpecialisation id spec_info = modifyIdInfo (`setRuleInfo` spec_info) id View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/360bcb51e9ef7eafcae105be6f3ee189... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/360bcb51e9ef7eafcae105be6f3ee189... 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)