[Git][ghc/ghc][wip/T27737] Expermimentally disable case-binder unfoldings in Gentle phase
Simon Peyton Jones pushed to branch wip/T27737 at Glasgow Haskell Compiler / GHC Commits: f4e950c7 by Simon Peyton Jones at 2026-11-21T14:49:42+00:00 Expermimentally disable case-binder unfoldings in Gentle phase Provoked by #28164. ---- Problem ---------- f x = join j y = ...(case x of K a b c -> e)..... in case x of K a b c -> ... Here we can float out that (case x of K a b c -> c) But if we float out the inner case x past the join point we get f x = case x of K a b c -> join j y = ...(case x of K a b c -> e)..... in ... Now we simplify the inner case (which is normally good) f x = case x of K a b c -> join j y = ...e..... in ... but now we can't float e. ---- Solution ---------- Don't add local unfoldings inside case alternatives, before FloatOut. A tiny, one-line change. - - - - - 6 changed files: - compiler/GHC/Core/Opt/Simplify/Iteration.hs - + testsuite/tests/simplCore/should_compile/T28164S.hs - testsuite/tests/simplCore/should_compile/all.T - + testsuite/tests/simplCore/should_run/T28164R.hs - + testsuite/tests/simplCore/should_run/T28164R.stdout - testsuite/tests/simplCore/should_run/all.T Changes: ===================================== compiler/GHC/Core/Opt/Simplify/Iteration.hs ===================================== @@ -3785,30 +3785,24 @@ simplAlt env _scrut' imposs_deflt_cons case_bndr' bndr_swap' cont' (Alt DEFAULT ; (floats, rhs') <- simplExprF env' rhs cont' ; return (DEFAULT, [], floats, rhs') } -simplAlt env _scrut' _ case_bndr' bndr_swap' cont' (Alt (LitAlt lit) bndrs rhs) +simplAlt env _scrut' _ case_bndr' bndr_swap' cont' (Alt lit_alt@(LitAlt {}) bndrs rhs) = assert (null bndrs) $ - do { let env' = addAltUnfoldings env case_bndr' bndr_swap' (Lit lit) + do { let env' = addAltUnfoldings env case_bndr' bndr_swap' lit_alt [] ; (floats, rhs') <- simplExprF env' rhs cont' - ; return (LitAlt lit, [], floats, rhs') } + ; return (lit_alt, [], floats, rhs') } -simplAlt env scrut' _ case_bndr' bndr_swap' cont' (Alt (DataAlt con) vs rhs) +simplAlt env scrut' _ case_bndr' bndr_swap' cont' (Alt data_alt@(DataAlt con) vs rhs) = do { -- See Note [Adding evaluatedness info to pattern-bound variables] -- and Note [DataAlt occ info] ; let vs_with_info = adjustFieldsIdInfo scrut' case_bndr' bndr_swap' con vs + ; (env', vs') <- simplBinders env vs_with_info -- Adjust evaluated-ness and occ-info flags before `simplBinders` -- because the latter extends the in-scope set, which propagates this -- adjusted info to use sites. - ; (env', vs') <- simplBinders env vs_with_info - - -- Bind the case-binder to (con args) - ; let inst_tys' = tyConAppArgs (idType case_bndr') - con_app :: OutExpr - con_app = mkConApp2 con inst_tys' vs' - env'' = addAltUnfoldings env' case_bndr' bndr_swap' con_app - + ; let env'' = addAltUnfoldings env' case_bndr' bndr_swap' data_alt vs' ; (floats, rhs') <- simplExprF env'' rhs cont' - ; return (DataAlt con, vs', floats, rhs') } + ; return (data_alt, vs', floats, rhs') } {- Note [Adding evaluatedness info to pattern-bound variables] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -3911,12 +3905,17 @@ addDefaultUnfoldings env case_bndr bndr_swap imposs_deflt_cons | otherwise = env1 -addAltUnfoldings :: SimplEnv -> OutId -> BinderSwapDecision -> OutExpr -> SimplEnv -addAltUnfoldings env case_bndr bndr_swap con_app +addAltUnfoldings :: SimplEnv -> OutId -> BinderSwapDecision + -> AltCon -> [OutVar] -> SimplEnv +addAltUnfoldings env case_bndr bndr_swap con bndrs = env2 where - con_app_unf = mk_simple_unf con_app - env1 = addBinderUnfolding env case_bndr con_app_unf + -- Add unfolding for case_bndr + -- See #28164 + env1 | seCaseCase env || null bndrs + = addBinderUnfolding env case_bndr con_app_unf + | otherwise + = env -- See Note [Add unfolding for scrutinee] -- e.g. case (x |> co) of K a b -> blah @@ -3929,6 +3928,18 @@ addAltUnfoldings env case_bndr bndr_swap con_app | otherwise = env1 + -- Bind the case-binder to (con args) + con_app_unf :: Unfolding + con_app_unf = mk_simple_unf con_app + + con_app :: OutExpr + con_app = case con of + DEFAULT -> pprPanic "addAltUnfoldings" (ppr case_bndr) + LitAlt lit -> Lit lit + DataAlt con -> mkConApp2 con inst_tys bndrs + where + inst_tys = tyConAppArgs (idType case_bndr) + -- Force the opts, so that the whole SimplEnv isn't retained !opts = seUnfoldingOpts env mk_simple_unf = mkSimpleUnfolding opts ===================================== testsuite/tests/simplCore/should_compile/T28164S.hs ===================================== @@ -0,0 +1,17 @@ +{-# LANGUAGE BangPatterns #-} +module T28164S where + +-- step is lazy in k (the x < 0 branch), so its worker takes k boxed. +-- At -O2 liberate-case peels hash's first iteration, which puts a +-- `case k of I# y` in the body of go's join points; go's exit also takes +-- k apart. +{-# NOINLINE step #-} +step :: Int -> Int -> Int +step k x + | x < 0 = 0 + | otherwise = go (hash 0 0) + where + hash !i !acc | i == k = acc + | otherwise = hash (i + 1) (acc * 131 + x + i) + go !j | j < 1000 = j * k + | otherwise = go (j `quot` 7) ===================================== testsuite/tests/simplCore/should_compile/all.T ===================================== @@ -634,3 +634,6 @@ test('T27737a', normal, compile, ['-O']) test('T27910', grep_errmsg(r'KnownBranch'), compile, ['-O -dno-typeable-binds -ddump-simpl-stats -dsuppress-uniques']) test('T27892', grep_errmsg(r'^f = g$'), compile, ['-O -ddump-cse -dsuppress-all -dsuppress-uniques']) test('T28095a', grep_errmsg(r'^f = g$'), compile, ['-O -ddump-cse -dsuppress-all -dsuppress-uniques']) + +# T28164S: the idea is there should be only one 'case k' in 'step' +test('T28164S', grep_errmsg(r'case k'), compile, ['-O2 -ddump-simpl']) ===================================== testsuite/tests/simplCore/should_run/T28164R.hs ===================================== @@ -0,0 +1,35 @@ +module Main (main) where + +import System.Environment (getArgs) + +data T = T ![Int] !Int !Int + +{-# NOINLINE work #-} +work :: [Int] -> [Int] -> Int -> Int -> [Int] +work sh ss o v = [ x * o + v + y | x <- sh, y <- ss ] + +{-# INLINE expensive #-} +expensive :: [Int] -> T -> [Int] +expensive sh (T ss o v) = work sh ss o v + +{-# INLINE z #-} +z :: [Int] -> T -> T -> Int +z sh (T ss _ v) (T _ _ v') + | v == 1, v' == 1 = sum ss +z sh t t' = sum (expensive sh t) + sum (expensive sh t') + +{-# NOINLINE mkT #-} +mkT :: Int -> T +mkT n = T [1 .. n] 3 5 + +c :: T +c = mkT 1000 + +{-# NOINLINE f #-} +f :: T -> Int +f a = z [1 .. 100] a c + +main :: IO () +main = do + [n] <- map read <$> getArgs + print (sum [ f (T [k] 0 2) | k <- [1 .. n] ]) ===================================== testsuite/tests/simplCore/should_run/T28164R.stdout ===================================== @@ -0,0 +1 @@ +13142050000 ===================================== testsuite/tests/simplCore/should_run/all.T ===================================== @@ -126,3 +126,4 @@ test('T27005', [], multimod_compile_and_run, ['T27005', '-O']) test('T27705', [extra_hc_opts('+RTS -M500M -RTS')], multimod_compile_and_run, ['T27705', '-O2 -fexpose-all-unfoldings']) test('T28163', normal, compile_and_run, ['-O']) +test('T28164R', normal, compile_and_run, ['-O']) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/f4e950c72027226fa8fdf6ce37cd4ac7... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/f4e950c72027226fa8fdf6ce37cd4ac7... 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)