[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: 8cafe4c3 by Simon Peyton Jones at 2026-11-21T14:08:20+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 ===================================== @@ -3916,7 +3916,8 @@ addAltUnfoldings env case_bndr bndr_swap con_app = env2 where con_app_unf = mk_simple_unf con_app - env1 = addBinderUnfolding env case_bndr con_app_unf + env1 | seCaseCase env = 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 ===================================== 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/8cafe4c3f3841620a4439a3e08f05447... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/8cafe4c3f3841620a4439a3e08f05447... 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)