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
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:
| ... | ... | @@ -3916,7 +3916,8 @@ addAltUnfoldings env case_bndr bndr_swap con_app |
| 3916 | 3916 | = env2
|
| 3917 | 3917 | where
|
| 3918 | 3918 | con_app_unf = mk_simple_unf con_app
|
| 3919 | - env1 = addBinderUnfolding env case_bndr con_app_unf
|
|
| 3919 | + env1 | seCaseCase env = addBinderUnfolding env case_bndr con_app_unf
|
|
| 3920 | + | otherwise = env
|
|
| 3920 | 3921 | |
| 3921 | 3922 | -- See Note [Add unfolding for scrutinee]
|
| 3922 | 3923 | -- e.g. case (x |> co) of K a b -> blah
|
| 1 | +{-# LANGUAGE BangPatterns #-}
|
|
| 2 | +module T28164S where
|
|
| 3 | + |
|
| 4 | +-- step is lazy in k (the x < 0 branch), so its worker takes k boxed.
|
|
| 5 | +-- At -O2 liberate-case peels hash's first iteration, which puts a
|
|
| 6 | +-- `case k of I# y` in the body of go's join points; go's exit also takes
|
|
| 7 | +-- k apart.
|
|
| 8 | +{-# NOINLINE step #-}
|
|
| 9 | +step :: Int -> Int -> Int
|
|
| 10 | +step k x
|
|
| 11 | + | x < 0 = 0
|
|
| 12 | + | otherwise = go (hash 0 0)
|
|
| 13 | + where
|
|
| 14 | + hash !i !acc | i == k = acc
|
|
| 15 | + | otherwise = hash (i + 1) (acc * 131 + x + i)
|
|
| 16 | + go !j | j < 1000 = j * k
|
|
| 17 | + | otherwise = go (j `quot` 7) |
| ... | ... | @@ -634,3 +634,6 @@ test('T27737a', normal, compile, ['-O']) |
| 634 | 634 | test('T27910', grep_errmsg(r'KnownBranch'), compile, ['-O -dno-typeable-binds -ddump-simpl-stats -dsuppress-uniques'])
|
| 635 | 635 | test('T27892', grep_errmsg(r'^f = g$'), compile, ['-O -ddump-cse -dsuppress-all -dsuppress-uniques'])
|
| 636 | 636 | test('T28095a', grep_errmsg(r'^f = g$'), compile, ['-O -ddump-cse -dsuppress-all -dsuppress-uniques'])
|
| 637 | + |
|
| 638 | +# T28164S: the idea is there should be only one 'case k' in 'step'
|
|
| 639 | +test('T28164S', grep_errmsg(r'case k'), compile, ['-O2 -ddump-simpl']) |
| 1 | +module Main (main) where
|
|
| 2 | + |
|
| 3 | +import System.Environment (getArgs)
|
|
| 4 | + |
|
| 5 | +data T = T ![Int] !Int !Int
|
|
| 6 | + |
|
| 7 | +{-# NOINLINE work #-}
|
|
| 8 | +work :: [Int] -> [Int] -> Int -> Int -> [Int]
|
|
| 9 | +work sh ss o v = [ x * o + v + y | x <- sh, y <- ss ]
|
|
| 10 | + |
|
| 11 | +{-# INLINE expensive #-}
|
|
| 12 | +expensive :: [Int] -> T -> [Int]
|
|
| 13 | +expensive sh (T ss o v) = work sh ss o v
|
|
| 14 | + |
|
| 15 | +{-# INLINE z #-}
|
|
| 16 | +z :: [Int] -> T -> T -> Int
|
|
| 17 | +z sh (T ss _ v) (T _ _ v')
|
|
| 18 | + | v == 1, v' == 1 = sum ss
|
|
| 19 | +z sh t t' = sum (expensive sh t) + sum (expensive sh t')
|
|
| 20 | + |
|
| 21 | +{-# NOINLINE mkT #-}
|
|
| 22 | +mkT :: Int -> T
|
|
| 23 | +mkT n = T [1 .. n] 3 5
|
|
| 24 | + |
|
| 25 | +c :: T
|
|
| 26 | +c = mkT 1000
|
|
| 27 | + |
|
| 28 | +{-# NOINLINE f #-}
|
|
| 29 | +f :: T -> Int
|
|
| 30 | +f a = z [1 .. 100] a c
|
|
| 31 | + |
|
| 32 | +main :: IO ()
|
|
| 33 | +main = do
|
|
| 34 | + [n] <- map read <$> getArgs
|
|
| 35 | + print (sum [ f (T [k] 0 2) | k <- [1 .. n] ]) |
| 1 | +13142050000 |
| ... | ... | @@ -126,3 +126,4 @@ test('T27005', [], multimod_compile_and_run, ['T27005', '-O']) |
| 126 | 126 | test('T27705', [extra_hc_opts('+RTS -M500M -RTS')], multimod_compile_and_run,
|
| 127 | 127 | ['T27705', '-O2 -fexpose-all-unfoldings'])
|
| 128 | 128 | test('T28163', normal, compile_and_run, ['-O'])
|
| 129 | +test('T28164R', normal, compile_and_run, ['-O']) |