Simon Peyton Jones pushed to branch wip/T28187 at Glasgow Haskell Compiler / GHC Commits: cb0d5391 by Simon Peyton Jones at 2026-11-27T23:53:23+00:00 Make -fno-cpr-anal switch off CPR analysis Fixes #28187. Previously `-fcpr-anal` did not control whether CPR analysis ran: the analysis ran whenever worker/wrapper was enabled, and the flag only stopped worker/wrapper from exploiting its results, via the `wo_cpr_anal` field of `WwOpts`. This patch makes `dmd_cpr_ww` in GHC.Core.Opt.Pipeline decide on each pass separately: * Demand analysis runs if -fstrictness is on; it does boxity analysis only if worker/wrapper follows. * CPR analysis runs if -fcpr-anal is on, but only if worker/wrapper follows. See Note [Don't change boxity without worker/wrapper]. * Worker/wrapper runs if -fworker-wrapper is on and at least one of the analyses has run. * The simplifier runs afterwards if at least one of the analyses has run. We can now drop `wo_cpr_anal` altogether: without CPR analysis, no local binder has a constructed CPR signature, so worker/wrapper never does CPR splitting anyway. (The simplifier gives bottoming bindings `botCpr`, but `canUnboxResult` does not unbox for that.) Consequences: * A module compiled with -fno-cpr-anal has no CPR information in its interface file. * -fno-strictness -fworker-wrapper now runs CPR analysis and worker/wrapper, where previously it ran neither. * -flate-dmd-anal runs the same demand/CPR/worker-wrapper round a second time, late in the pipeline, each pass under its own flag; the user guide now says so. * The final demand-analysis run is now guarded by -fstrictness alone; previously -flate-dmd-anal also triggered it, even when no other demand analysis had run. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> - - - - - 6 changed files: - + changelog.d/T28187 - compiler/GHC/Core/Opt/DmdAnal.hs - compiler/GHC/Core/Opt/Pipeline.hs - compiler/GHC/Core/Opt/WorkWrap/Utils.hs - compiler/GHC/Driver/Config/Core/Opt/WorkWrap.hs - docs/users_guide/using-optimisation.rst Changes: ===================================== changelog.d/T28187 ===================================== @@ -0,0 +1,21 @@ +section: compiler +synopsis: + ``-fno-cpr-anal`` now switches off CPR analysis itself, rather than just + stopping worker/wrapper from using its results. +issues: #28187 +mrs: !17063 +description: { + Previously CPR analysis ran whenever worker/wrapper was enabled, regardless + of ``-fcpr-anal``. Now each of the demand analyser, CPR analyser and + worker/wrapper passes is controlled by its own flag: + + - Demand analysis runs if ``-fstrictness`` is on. + - CPR analysis runs if both ``-fcpr-anal`` and ``-fworker-wrapper`` are on. + - Worker/wrapper runs if ``-fworker-wrapper`` is on and at least one of + the two analyses has run. + + One consequence is that a module compiled with ``-fno-cpr-anal`` has no + CPR information in its interface file, so CPR analysis in importing + modules sees less. Also, ``-fno-strictness -fworker-wrapper`` now runs CPR + analysis and worker/wrapper, where previously it ran neither. +} ===================================== compiler/GHC/Core/Opt/DmdAnal.hs ===================================== @@ -195,7 +195,8 @@ to transfer over the previous boxity to the new demand signature, in `setIdDmdAndBoxSig`. Why isn't CprAnal configured with a similar flag? Because if we aren't going to -do worker/wrapper we don't run CPR analysis at all. (see GHC.Core.Opt.Pipeline) +do worker/wrapper we don't run CPR analysis at all. (see `dmd_cpr_ww` in +GHC.Core.Opt.Pipeline) It might be surprising that we only try to preserve *arg* boxity, not boxity on FVs. But FV demands won't make it into interface files anyway, so it's a waste ===================================== compiler/GHC/Core/Opt/Pipeline.hs ===================================== @@ -132,6 +132,7 @@ getCoreToDo dflags hpt_rule_base extra_vars call_arity = gopt Opt_CallArity dflags exitification = gopt Opt_Exitification dflags strictness = gopt Opt_Strictness dflags + cpr_anal = gopt Opt_CprAnal dflags full_laziness = gopt Opt_FullLaziness dflags do_specialise = gopt Opt_Specialise dflags do_float_in = gopt Opt_FloatIn dflags @@ -172,14 +173,25 @@ getCoreToDo dflags hpt_rule_base extra_vars simpl_gently = CoreDoSimplify $ initSimplifyOpts dflags extra_vars max_iter (initGentleSimplMode dflags) hpt_rule_base - dmd_cpr_ww = if ww_on then [CoreDoDemand True,CoreDoCpr,CoreDoWorkerWrapper] - else [CoreDoDemand False] -- NB: No CPR! See Note [Don't change boxity without worker/wrapper] - - - demand_analyser = (CoreDoPasses ( - dmd_cpr_ww ++ - [simplify "post-worker-wrapper"] - )) + -- Demand analysis, CPR analysis, worker/wrapper, and a simplifier run + -- to clean up. Each is controlled by its own flag: + -- * Demand analysis runs if -fstrictness is on; it does boxity + -- analysis only if worker/wrapper follows. + -- * CPR analysis runs if -fcpr-anal is on, but only if worker/wrapper + -- follows. See Note [Don't change boxity without worker/wrapper] + -- in GHC.Core.Opt.DmdAnal + -- * Worker/wrapper runs if -fworker-wrapper is on, but only if at + -- least one of the analyses has run. + -- * The simplifier runs if at least one of the analyses has run. + dmd_cpr_ww simpl_name + = CoreDoPasses + [ runWhen strictness (CoreDoDemand ww_on) + , runWhen do_cpr CoreDoCpr + , runWhen do_ww CoreDoWorkerWrapper + , runWhen (strictness || do_cpr) (simplify simpl_name) ] + where + do_cpr = ww_on && cpr_anal + do_ww = ww_on && (strictness || do_cpr) add_caller_ccs = runWhen (profiling && not (null $ callerCcFilters dflags)) CoreAddCallerCcs @@ -259,7 +271,7 @@ getCoreToDo dflags hpt_rule_base extra_vars ], -- Strictness analysis - runWhen strictness demand_analyser, + dmd_cpr_ww "post-worker-wrapper", runWhen exitification CoreDoExitify, -- See Note [Placement of the exitification pass] @@ -319,16 +331,14 @@ getCoreToDo dflags hpt_rule_base extra_vars --------- End of -O2 passes -------------- - runWhen late_dmd_anal $ CoreDoPasses ( - dmd_cpr_ww ++ [simplify "post-late-ww"] - ), + runWhen late_dmd_anal $ dmd_cpr_ww "post-late-ww", -- Final run of the demand_analyser, ensures that one-shot thunks are -- really really one-shot thunks. Only needed if the demand analyser -- has run at all. See Note [Final Demand Analyser run] in GHC.Core.Opt.DmdAnal -- It is EXTREMELY IMPORTANT to run this pass, otherwise execution -- can become /exponentially/ more expensive. See #11731, #12996. - runWhen (strictness || late_dmd_anal) (CoreDoDemand False), + runWhen strictness (CoreDoDemand False), maybe_rule_check FinalPhase, ===================================== compiler/GHC/Core/Opt/WorkWrap/Utils.hs ===================================== @@ -141,9 +141,6 @@ data WwOpts wo_fam_envs :: !FamInstEnvs , -- | Options for the "Simple optimiser" wo_simple_opts :: !SimpleOpts - , -- | Whether to enable "Constructed Product Result" analysis. - -- (Originally from DOI: 10.1017/S0956796803004751) - wo_cpr_anal :: !Bool , -- | Used for absent argument error message wo_module :: !Module , -- | Generate workers even if the only effect is some args get passed @@ -1603,9 +1600,7 @@ mkWWcpr_entry CoreExpr -> CoreExpr, -- New wrapper. 'nop_fn' if not useful CoreExpr -> CoreExpr) -- New worker. 'nop_fn' if not useful -- ^ Entrypoint to CPR W/W. See Note [Worker/wrapper for CPR] for an overview. -mkWWcpr_entry opts body_ty body_cpr - | not (wo_cpr_anal opts) = return (boringSplit, nop_fn, nop_fn) - | otherwise = do +mkWWcpr_entry opts body_ty body_cpr = do -- Part (1) res_bndr <- mk_res_bndr body_ty let bind_res_bndr body scope = mkDefaultCase body res_bndr scope ===================================== compiler/GHC/Driver/Config/Core/Opt/WorkWrap.hs ===================================== @@ -15,7 +15,6 @@ initWorkWrapOpts :: Module -> DynFlags -> FamInstEnvs -> WwOpts initWorkWrapOpts this_mod dflags fam_envs = MkWwOpts { wo_fam_envs = fam_envs , wo_simple_opts = initSimpleOpts dflags - , wo_cpr_anal = gopt Opt_CprAnal dflags , wo_module = this_mod , wo_unlift_strict = gopt Opt_WorkerWrapperUnlift dflags } ===================================== docs/users_guide/using-optimisation.rst ===================================== @@ -376,6 +376,13 @@ as such you shouldn't need to set any of them explicitly. A flag and then ``sum`` can inline to potentially cancel away the ``I#`` box. + With :ghc-flag:`-fno-cpr-anal`, CPR analysis does not run at all. As a + result the module's interface file records no CPR information (other + than for functions that definitely diverge), so CPR analysis in + importing modules sees less. CPR analysis also does not run with + :ghc-flag:`-fno-worker-wrapper`, because its results are used only by + the worker/wrapper transformation. + Here's an example of the function that *does not* return a constructed product: :: f :: [Int] -> (Int -> Int) -> Int @@ -788,16 +795,19 @@ as such you shouldn't need to set any of them explicitly. A flag it on keeps all auto-generated rules. .. ghc-flag:: -flate-dmd-anal - :shortdesc: Run demand analysis again, at the end of the - simplification pipeline + :shortdesc: Run demand analysis, CPR analysis and worker/wrapper + again, late in the simplification pipeline :type: dynamic :reverse: -fno-late-dmd-anal :category: :default: off - Run demand analysis again, at the end of the simplification - pipeline. We found some opportunities for discovering strictness + Run the demand analysis, CPR analysis and worker/wrapper passes a + second time, late in the simplification pipeline. Each of these + passes runs only if its own flag (:ghc-flag:`-fstrictness`, + :ghc-flag:`-fcpr-anal`, :ghc-flag:`-fworker-wrapper`) is on. + We found some opportunities for discovering strictness that were not visible earlier; and optimisations like :ghc-flag:`-fspec-constr` can create functions with unused arguments which are eliminated by late demand analysis. Improvements are modest, but @@ -1907,6 +1917,7 @@ as such you shouldn't need to set any of them explicitly. A flag :shortdesc: Enable the worker/wrapper transformation. Implied by :ghc-flag:`-O` and by :ghc-flag:`-fstrictness`. :type: dynamic + :reverse: -fno-worker-wrapper :category: :default: off but enabled by :ghc-flag:`-O`. View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/cb0d5391bebf37e717d9ade6f286f1c7... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/cb0d5391bebf37e717d9ade6f286f1c7... 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