Simon Peyton Jones pushed to branch wip/case-bndr-unf at Glasgow Haskell Compiler / GHC Commits: 2adf59e3 by Simon Peyton Jones at 2026-10-19T18:03:39+01:00 SpecConstr: give the case binder in mkSeqs suitable IdInfo mkSeqs (see Note [SpecConstr and strict fields]) builds case arg of arg' { DEFAULT -> rhs } where arg' is arg, localised. But arg may have IdInfo that a case binder must not have; e.g. in test T24944 it was an imported global with a top-level unfolding, which tripped the new Core Lint check. Use mkCaseBndrFromLetBndr. See Note [Lambda and case binders have vanilla IdInfo] in GHC.Core, and #27965. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> - - - - - f48ae985 by Simon Peyton Jones at 2026-10-22T17:45:00+01:00 Wibbles - - - - - 4 changed files: - 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 Changes: ===================================== compiler/GHC/Core/Opt/Simplify/Env.hs ===================================== @@ -479,6 +479,10 @@ type SimplIdSubst = IdEnv SimplClo -- IdId |--> OutExpr -- Roughly: either an (InExpr, StaticEnv) pair for an -- as-yet-unsimplified expression -- or an OutExpr, for an already-simplified one +-- It's a bit like thunk: we may "evaluate" the SimplClo in a ApplyToVal +-- and thereby transform it from `ContEx` to `DoneEx` +-- This happens in the `ApplyToVal` case of `mkDupableContWithDmds` +-- See Note [The argument of ApplyToVal] in GHC.Core.Opt.Simplify.Utils data SimplClo = DoneEx OutExpr JoinPointHood -- If x :-> DoneEx e ja is in the SimplIdSubst @@ -486,7 +490,6 @@ data SimplClo -- and ja = Just a <=> x is a join-point of arity a -- See Note [Join arity in SimplIdSubst] - | DoneId OutId -- If x :-> DoneId v is in the SimplIdSubst -- then replace occurrences of x by v ===================================== compiler/GHC/Core/Opt/Simplify/Iteration.hs ===================================== @@ -1226,17 +1226,8 @@ simplExprF1 env (Tick t expr) cont = {-#SCC "simplTick" #-} simplTick env t exp simplExprF1 env (Coercion co) cont = {-#SCC "simplCoercionF" #-} simplCoercionF env co cont simplExprF1 env (Cast body co) cont - = do { co1 <- {-#SCC "simplCast-simplCoercion" #-} - simplCoercion env co - - ; let cont1 = {-#SCC "simplCast-addCoerce" #-} - if isReflCo co1 - then cont -- See Note [Optimising reflexivity] - else pushCast env co1 cont - -- True <=> co1 is optimised - - ; {-#SCC "simplCast-simplExprF" #-} - simplExprF env body cont1 } + = do { co1 <- simplCoercion env co + ; simplExprF env body (pushCast env co1 cont) } simplExprF1 env (App fun arg) cont = {-#SCC "simplExprF1-App" #-} case arg of @@ -1652,8 +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 [Lambda and case binders have vanilla IdInfo] 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' @@ -1687,69 +1677,12 @@ completeBindX env from_what bndr rhs body cont ; let all_floats = rhs_floats `addFloats` bind_float `addFloats` body_floats ; return ( all_floats, body' ) } -{- -************************************************************************ + +{- ********************************************************************* * * -\subsection{Lambdas} + Arguments * * -************************************************************************ --} - -{- Note [Optimising reflexivity] -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -It's important (for compiler performance) to get rid of reflexivity as soon -as it appears. See #11735, #14737, and #15019. - -In particular, we want to behave well on - - * e |> co1 |> co2 - where the two happen to cancel out entirely. That is quite common; - e.g. a newtype wrapping and unwrapping cancel. - - - * (f |> co) @t1 @t2 ... @tn x1 .. xm - Here we will use pushCoTyArg and pushCoValArg successively, which - build up SelCo stacks. Silly to do that if co is reflexive. - -However, we don't want to call isReflexiveCo too much, because it uses -type equality which is expensive on big types (#14737 comment:7). - -A good compromise (determined experimentally) seems to be to call -isReflexiveCo - * when composing casts, and - * at the end - -In investigating this I saw missed opportunities for on-the-fly -coercion shrinkage. See #15090. - -Note [Avoid re-simplifying coercions] -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -In some benchmarks (with deeply nested cases) we successively push -casts onto the SimplCont. We don't want to call the coercion optimiser -on each successive composition -- that's at least quadratic. So: - -* The CastIt constructor in SimplCont has a `sc_opt :: Bool` flag to - record whether the coercion optimiser has been applied to the coercion. - -* In `simplCast`, when we see (Cast e co), we simplify `co` to get - an OutCoercion, and built a CastIt with sc_opt=True. - - Actually not quite: if we are simplifying the result of inlining an - unfolding (seInlineDepth > 0), then instead of /optimising/ it again, - just /substitute/ which is cheaper. See `simplCoercion`. - -* In `pushCast` if we combine this new coercion with an existing one, - we build a CastIt for (co1 ; co2) with sc_opt=False. - -* When unpacking a CastIt, in `rebuild`, we optimise the (presumably - composed) coercion if sc_opt=False; this is done by `optOutCoercion`. - -* When duplicating a continuation in `mkDupableContWithDmds`, before - duplicating a CastIt, optimise the coercion. Otherwise we'll end up - optimising it separately in the duplicate copies. - -* See also Note [The MOutCoercion field of ContEx] in GHC.Core.Opt.Simplify.Utils. --} +********************************************************************* -} simplArg :: SimplEnvIS -- ^ Used only for its InScopeSet -> Maybe ArgInfo -- ^ Just <=> This arg `ai` occurs in an app @@ -1771,13 +1704,12 @@ simplArg env mb_arg_info fun_ty (ContEx arg_se arg mco) cont = addCastMCo mco stop ; simplExprC arg_env' arg cont } -{- -************************************************************************ + +{- ********************************************************************* * * -\subsection{Lambdas} + Lambdas * * -************************************************************************ --} +********************************************************************* -} simplNonRecBody :: SimplEnv -> FromWhat -> InExpr -> SimplCont @@ -2295,8 +2227,8 @@ simplOutId env fun cont env' = arg_env `addNewInScopeIds` [s'] hole_ty = mkVisFunTy m realWorldStatePrimTy new_runrw_res_ty cont' = pushCastMCo env' arg_mco $ - ApplyToVal { sc_arg = DoneEx (Var s') NotJoinPoint - , sc_dup = dup + ApplyToVal { sc_arg = DoneId s' + , sc_dup = dup , sc_cont = inner_cont , sc_hole_ty = hole_ty } -- cont' applies to s', then K ===================================== compiler/GHC/Core/Opt/Simplify/Utils.hs ===================================== @@ -175,7 +175,6 @@ data SimplCont -- Invariant: never an identity coercion , sc_opt :: Bool -- True <=> sc_co has had optCoercion applied to it -- See Note [Avoid re-simplifying coercions] - -- in GHC.Core.Opt.Simplify.Iteration , sc_cont :: SimplCont } | ApplyToVal -- (ApplyToVal arg K)[e] = K[ e arg ] @@ -886,10 +885,65 @@ But we /also/ sometimes want to substitute that argument for a lambda-binder, in `preInlineBetaUnconditionally`; and since the substitution maps an Id to a SimplClo, the pending cast comes along for free. Then we deal with it appropriately when /applying/ the substitution in `simplInId`. + +Note [Optimising reflexivity] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +It's important (for compiler performance) to get rid of reflexivity as soon +as it appears. See #11735, #14737, and #15019. + +In particular, we want to behave well on + + * e |> co1 |> co2 + where the two happen to cancel out entirely. That is quite common; + e.g. a newtype wrapping and unwrapping cancel. + + + * (f |> co) @t1 @t2 ... @tn x1 .. xm + Here we will use pushCoTyArg and pushCoValArg successively, which + build up SelCo stacks. Silly to do that if co is reflexive. + +However, we don't want to call isReflexiveCo too much, because it uses +type equality which is expensive on big types (#14737 comment:7). + +A good compromise (determined experimentally) seems to be to call +isReflexiveCo + * when composing casts, and + * at the end + +In investigating this I saw missed opportunities for on-the-fly +coercion shrinkage. See #15090. + +Note [Avoid re-simplifying coercions] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +In some benchmarks (with deeply nested cases) we successively push +casts onto the SimplCont. We don't want to call the coercion optimiser +on each successive composition -- that's at least quadratic. So: + +* The CastIt constructor in SimplCont has a `sc_opt :: Bool` flag to + record whether the coercion optimiser has been applied to the coercion. + +* In `simplCast`, when we see (Cast e co), we simplify `co` to get + an OutCoercion, and built a CastIt with sc_opt=True. + + Actually not quite: if we are simplifying the result of inlining an + unfolding (seInlineDepth > 0), then instead of /optimising/ it again, + just /substitute/ which is cheaper. See `simplCoercion`. + +* In `pushCast` if we combine this new coercion with an existing one, + we build a CastIt for (co1 ; co2) with sc_opt=False. + +* When unpacking a CastIt, in `rebuild`, we optimise the (presumably + composed) coercion if sc_opt=False; this is done by `optOutCoercion`. + +* When duplicating a continuation in `mkDupableContWithDmds`, before + duplicating a CastIt, optimise the coercion. Otherwise we'll end up + optimising it separately in the duplicate copies. + +* See also Note [The MOutCoercion field of ContEx] -} optOutCoercion :: SimplEnvIS -> OutCoercion -> Bool -> OutCoercion --- See Note [Avoid re-simplifying coercions] in GHC.Core.Opt.Simplify.Iteration +-- See Note [Avoid re-simplifying coercions] optOutCoercion env co already_optimised | already_optimised = co -- Ditto | otherwise = optCoercion opts empty_subst co @@ -909,7 +963,8 @@ pushCastMCo env (MCo co) cont = pushCast env co cont pushCast :: SimplEnv -> OutCoercion -> SimplCont -> SimplCont pushCast env co cont - = go co True cont + | isReflCo co = cont -- See Note [Optimising reflexivity] + | otherwise = go co True cont where -- ToDo: pushCast Refl (ApplylToVal arg1 (ApplyToVal arg2 ...)) @@ -918,7 +973,7 @@ pushCast env co cont go co1 _ (CastIt { sc_co = co2, sc_cont = cont }) -- See Note [Optimising reflexivity] in GHC.Core.Opt.Simplify.Iteration = go (mkTransCo co1 co2) False cont -- False: (mkTransCo co1 co2) is not fully optimised - -- See Note [Avoid re-simplifying coercions] in GHC.Core.Opt.Simplify.Iteration + -- See Note [Avoid re-simplifying coercions] go co co_is_opt (ApplyToTy { sc_arg_ty = arg_ty, sc_cont = tail }) | Just (tyL, arg_ty', m_co') <- pushCoTyArg co arg_ty ===================================== compiler/GHC/Core/Opt/SpecConstr.hs ===================================== @@ -2068,7 +2068,11 @@ mkSeqs seqees res_ty rhs = -- Argument representing strict field and it's worth passing via cbv | wantCbvForId arg_id = Case (Var arg_id) - (localiseId arg_id) -- See (SCF1) in Note [SpecConstr and strict fields] + (mkCaseBndrFromLetBndr (localiseId arg_id)) + -- 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 res_ty ([Alt DEFAULT [] rhs]) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/7cf966272fca8a0665a5bcb8850ef20... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/7cf966272fca8a0665a5bcb8850ef20... 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