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
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:
| 1 | +section: compiler
|
|
| 2 | +synopsis:
|
|
| 3 | + ``-fno-cpr-anal`` now switches off CPR analysis itself, rather than just
|
|
| 4 | + stopping worker/wrapper from using its results.
|
|
| 5 | +issues: #28187
|
|
| 6 | +mrs: !17063
|
|
| 7 | +description: {
|
|
| 8 | + Previously CPR analysis ran whenever worker/wrapper was enabled, regardless
|
|
| 9 | + of ``-fcpr-anal``. Now each of the demand analyser, CPR analyser and
|
|
| 10 | + worker/wrapper passes is controlled by its own flag:
|
|
| 11 | + |
|
| 12 | + - Demand analysis runs if ``-fstrictness`` is on.
|
|
| 13 | + - CPR analysis runs if both ``-fcpr-anal`` and ``-fworker-wrapper`` are on.
|
|
| 14 | + - Worker/wrapper runs if ``-fworker-wrapper`` is on and at least one of
|
|
| 15 | + the two analyses has run.
|
|
| 16 | + |
|
| 17 | + One consequence is that a module compiled with ``-fno-cpr-anal`` has no
|
|
| 18 | + CPR information in its interface file, so CPR analysis in importing
|
|
| 19 | + modules sees less. Also, ``-fno-strictness -fworker-wrapper`` now runs CPR
|
|
| 20 | + analysis and worker/wrapper, where previously it ran neither.
|
|
| 21 | +} |
| ... | ... | @@ -195,7 +195,8 @@ to transfer over the previous boxity to the new demand signature, in |
| 195 | 195 | `setIdDmdAndBoxSig`.
|
| 196 | 196 | |
| 197 | 197 | Why isn't CprAnal configured with a similar flag? Because if we aren't going to
|
| 198 | -do worker/wrapper we don't run CPR analysis at all. (see GHC.Core.Opt.Pipeline)
|
|
| 198 | +do worker/wrapper we don't run CPR analysis at all. (see `dmd_cpr_ww` in
|
|
| 199 | +GHC.Core.Opt.Pipeline)
|
|
| 199 | 200 | |
| 200 | 201 | It might be surprising that we only try to preserve *arg* boxity, not boxity on
|
| 201 | 202 | FVs. But FV demands won't make it into interface files anyway, so it's a waste
|
| ... | ... | @@ -132,6 +132,7 @@ getCoreToDo dflags hpt_rule_base extra_vars |
| 132 | 132 | call_arity = gopt Opt_CallArity dflags
|
| 133 | 133 | exitification = gopt Opt_Exitification dflags
|
| 134 | 134 | strictness = gopt Opt_Strictness dflags
|
| 135 | + cpr_anal = gopt Opt_CprAnal dflags
|
|
| 135 | 136 | full_laziness = gopt Opt_FullLaziness dflags
|
| 136 | 137 | do_specialise = gopt Opt_Specialise dflags
|
| 137 | 138 | do_float_in = gopt Opt_FloatIn dflags
|
| ... | ... | @@ -172,14 +173,25 @@ getCoreToDo dflags hpt_rule_base extra_vars |
| 172 | 173 | simpl_gently = CoreDoSimplify $ initSimplifyOpts dflags extra_vars max_iter
|
| 173 | 174 | (initGentleSimplMode dflags) hpt_rule_base
|
| 174 | 175 | |
| 175 | - dmd_cpr_ww = if ww_on then [CoreDoDemand True,CoreDoCpr,CoreDoWorkerWrapper]
|
|
| 176 | - else [CoreDoDemand False] -- NB: No CPR! See Note [Don't change boxity without worker/wrapper]
|
|
| 177 | - |
|
| 178 | - |
|
| 179 | - demand_analyser = (CoreDoPasses (
|
|
| 180 | - dmd_cpr_ww ++
|
|
| 181 | - [simplify "post-worker-wrapper"]
|
|
| 182 | - ))
|
|
| 176 | + -- Demand analysis, CPR analysis, worker/wrapper, and a simplifier run
|
|
| 177 | + -- to clean up. Each is controlled by its own flag:
|
|
| 178 | + -- * Demand analysis runs if -fstrictness is on; it does boxity
|
|
| 179 | + -- analysis only if worker/wrapper follows.
|
|
| 180 | + -- * CPR analysis runs if -fcpr-anal is on, but only if worker/wrapper
|
|
| 181 | + -- follows. See Note [Don't change boxity without worker/wrapper]
|
|
| 182 | + -- in GHC.Core.Opt.DmdAnal
|
|
| 183 | + -- * Worker/wrapper runs if -fworker-wrapper is on, but only if at
|
|
| 184 | + -- least one of the analyses has run.
|
|
| 185 | + -- * The simplifier runs if at least one of the analyses has run.
|
|
| 186 | + dmd_cpr_ww simpl_name
|
|
| 187 | + = CoreDoPasses
|
|
| 188 | + [ runWhen strictness (CoreDoDemand ww_on)
|
|
| 189 | + , runWhen do_cpr CoreDoCpr
|
|
| 190 | + , runWhen do_ww CoreDoWorkerWrapper
|
|
| 191 | + , runWhen (strictness || do_cpr) (simplify simpl_name) ]
|
|
| 192 | + where
|
|
| 193 | + do_cpr = ww_on && cpr_anal
|
|
| 194 | + do_ww = ww_on && (strictness || do_cpr)
|
|
| 183 | 195 | |
| 184 | 196 | add_caller_ccs =
|
| 185 | 197 | runWhen (profiling && not (null $ callerCcFilters dflags)) CoreAddCallerCcs
|
| ... | ... | @@ -259,7 +271,7 @@ getCoreToDo dflags hpt_rule_base extra_vars |
| 259 | 271 | ],
|
| 260 | 272 | |
| 261 | 273 | -- Strictness analysis
|
| 262 | - runWhen strictness demand_analyser,
|
|
| 274 | + dmd_cpr_ww "post-worker-wrapper",
|
|
| 263 | 275 | |
| 264 | 276 | runWhen exitification CoreDoExitify,
|
| 265 | 277 | -- See Note [Placement of the exitification pass]
|
| ... | ... | @@ -319,16 +331,14 @@ getCoreToDo dflags hpt_rule_base extra_vars |
| 319 | 331 | |
| 320 | 332 | --------- End of -O2 passes --------------
|
| 321 | 333 | |
| 322 | - runWhen late_dmd_anal $ CoreDoPasses (
|
|
| 323 | - dmd_cpr_ww ++ [simplify "post-late-ww"]
|
|
| 324 | - ),
|
|
| 334 | + runWhen late_dmd_anal $ dmd_cpr_ww "post-late-ww",
|
|
| 325 | 335 | |
| 326 | 336 | -- Final run of the demand_analyser, ensures that one-shot thunks are
|
| 327 | 337 | -- really really one-shot thunks. Only needed if the demand analyser
|
| 328 | 338 | -- has run at all. See Note [Final Demand Analyser run] in GHC.Core.Opt.DmdAnal
|
| 329 | 339 | -- It is EXTREMELY IMPORTANT to run this pass, otherwise execution
|
| 330 | 340 | -- can become /exponentially/ more expensive. See #11731, #12996.
|
| 331 | - runWhen (strictness || late_dmd_anal) (CoreDoDemand False),
|
|
| 341 | + runWhen strictness (CoreDoDemand False),
|
|
| 332 | 342 | |
| 333 | 343 | maybe_rule_check FinalPhase,
|
| 334 | 344 |
| ... | ... | @@ -141,9 +141,6 @@ data WwOpts |
| 141 | 141 | wo_fam_envs :: !FamInstEnvs
|
| 142 | 142 | , -- | Options for the "Simple optimiser"
|
| 143 | 143 | wo_simple_opts :: !SimpleOpts
|
| 144 | - , -- | Whether to enable "Constructed Product Result" analysis.
|
|
| 145 | - -- (Originally from DOI: 10.1017/S0956796803004751)
|
|
| 146 | - wo_cpr_anal :: !Bool
|
|
| 147 | 144 | , -- | Used for absent argument error message
|
| 148 | 145 | wo_module :: !Module
|
| 149 | 146 | , -- | Generate workers even if the only effect is some args get passed
|
| ... | ... | @@ -1603,9 +1600,7 @@ mkWWcpr_entry |
| 1603 | 1600 | CoreExpr -> CoreExpr, -- New wrapper. 'nop_fn' if not useful
|
| 1604 | 1601 | CoreExpr -> CoreExpr) -- New worker. 'nop_fn' if not useful
|
| 1605 | 1602 | -- ^ Entrypoint to CPR W/W. See Note [Worker/wrapper for CPR] for an overview.
|
| 1606 | -mkWWcpr_entry opts body_ty body_cpr
|
|
| 1607 | - | not (wo_cpr_anal opts) = return (boringSplit, nop_fn, nop_fn)
|
|
| 1608 | - | otherwise = do
|
|
| 1603 | +mkWWcpr_entry opts body_ty body_cpr = do
|
|
| 1609 | 1604 | -- Part (1)
|
| 1610 | 1605 | res_bndr <- mk_res_bndr body_ty
|
| 1611 | 1606 | let bind_res_bndr body scope = mkDefaultCase body res_bndr scope
|
| ... | ... | @@ -15,7 +15,6 @@ initWorkWrapOpts :: Module -> DynFlags -> FamInstEnvs -> WwOpts |
| 15 | 15 | initWorkWrapOpts this_mod dflags fam_envs = MkWwOpts
|
| 16 | 16 | { wo_fam_envs = fam_envs
|
| 17 | 17 | , wo_simple_opts = initSimpleOpts dflags
|
| 18 | - , wo_cpr_anal = gopt Opt_CprAnal dflags
|
|
| 19 | 18 | , wo_module = this_mod
|
| 20 | 19 | , wo_unlift_strict = gopt Opt_WorkerWrapperUnlift dflags
|
| 21 | 20 | } |
| ... | ... | @@ -376,6 +376,13 @@ as such you shouldn't need to set any of them explicitly. A flag |
| 376 | 376 | |
| 377 | 377 | and then ``sum`` can inline to potentially cancel away the ``I#`` box.
|
| 378 | 378 | |
| 379 | + With :ghc-flag:`-fno-cpr-anal`, CPR analysis does not run at all. As a
|
|
| 380 | + result the module's interface file records no CPR information (other
|
|
| 381 | + than for functions that definitely diverge), so CPR analysis in
|
|
| 382 | + importing modules sees less. CPR analysis also does not run with
|
|
| 383 | + :ghc-flag:`-fno-worker-wrapper`, because its results are used only by
|
|
| 384 | + the worker/wrapper transformation.
|
|
| 385 | + |
|
| 379 | 386 | Here's an example of the function that *does not* return a constructed product: ::
|
| 380 | 387 | |
| 381 | 388 | f :: [Int] -> (Int -> Int) -> Int
|
| ... | ... | @@ -788,16 +795,19 @@ as such you shouldn't need to set any of them explicitly. A flag |
| 788 | 795 | it on keeps all auto-generated rules.
|
| 789 | 796 | |
| 790 | 797 | .. ghc-flag:: -flate-dmd-anal
|
| 791 | - :shortdesc: Run demand analysis again, at the end of the
|
|
| 792 | - simplification pipeline
|
|
| 798 | + :shortdesc: Run demand analysis, CPR analysis and worker/wrapper
|
|
| 799 | + again, late in the simplification pipeline
|
|
| 793 | 800 | :type: dynamic
|
| 794 | 801 | :reverse: -fno-late-dmd-anal
|
| 795 | 802 | :category:
|
| 796 | 803 | |
| 797 | 804 | :default: off
|
| 798 | 805 | |
| 799 | - Run demand analysis again, at the end of the simplification
|
|
| 800 | - pipeline. We found some opportunities for discovering strictness
|
|
| 806 | + Run the demand analysis, CPR analysis and worker/wrapper passes a
|
|
| 807 | + second time, late in the simplification pipeline. Each of these
|
|
| 808 | + passes runs only if its own flag (:ghc-flag:`-fstrictness`,
|
|
| 809 | + :ghc-flag:`-fcpr-anal`, :ghc-flag:`-fworker-wrapper`) is on.
|
|
| 810 | + We found some opportunities for discovering strictness
|
|
| 801 | 811 | that were not visible earlier; and optimisations like
|
| 802 | 812 | :ghc-flag:`-fspec-constr` can create functions with unused arguments which
|
| 803 | 813 | 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 |
| 1907 | 1917 | :shortdesc: Enable the worker/wrapper transformation. Implied by :ghc-flag:`-O`
|
| 1908 | 1918 | and by :ghc-flag:`-fstrictness`.
|
| 1909 | 1919 | :type: dynamic
|
| 1920 | + :reverse: -fno-worker-wrapper
|
|
| 1910 | 1921 | :category:
|
| 1911 | 1922 | |
| 1912 | 1923 | :default: off but enabled by :ghc-flag:`-O`.
|