summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Graf <sebastian.graf@kit.edu>2023-03-06 18:58:10 +0100
committerSebastian Graf <sebastian.graf@kit.edu>2023-03-08 15:04:20 +0100
commite607d32d44bd82901f3f12012f5838469221b649 (patch)
tree104d93a32def76354836827e4b74a3ddbc3311ab
parent232cfc241c14ba6a49d9552a90a94857255e455d (diff)
downloadhaskell-wip/T22997.tar.gz
DmdAnal: Fix a panic on OPAQUE and trivial/PAP RHS (#22997)wip/T22997
We should not panic in `add_demands` (now `set_lam_dmds`), because that code path is legimitely taken for OPAQUE PAP bindings, as in T22997. Fixes #22997.
-rw-r--r--compiler/GHC/Core/Opt/DmdAnal.hs22
-rw-r--r--testsuite/tests/stranal/should_compile/T22997.hs9
-rw-r--r--testsuite/tests/stranal/should_compile/all.T2
3 files changed, 23 insertions, 10 deletions
diff --git a/compiler/GHC/Core/Opt/DmdAnal.hs b/compiler/GHC/Core/Opt/DmdAnal.hs
index 0bcabf55d3..ece13d894b 100644
--- a/compiler/GHC/Core/Opt/DmdAnal.hs
+++ b/compiler/GHC/Core/Opt/DmdAnal.hs
@@ -1916,10 +1916,11 @@ finaliseArgBoxities env fn threshold_arity rhs_dmds div rhs
-- Check for an OPAQUE function: see Note [OPAQUE pragma]
-- In that case, trim off all boxity info from argument demands
+ -- and demand info on lambda binders
-- See Note [The OPAQUE pragma and avoiding the reboxing of arguments]
| isOpaquePragma (idInlinePragma fn)
, let trimmed_rhs_dmds = map trimBoxity rhs_dmds
- = (trimmed_rhs_dmds, add_demands trimmed_rhs_dmds rhs)
+ = (trimmed_rhs_dmds, set_lam_dmds trimmed_rhs_dmds rhs)
-- Check that we have enough visible binders to match the
-- threshold arity; if not, we won't do worker/wrapper
@@ -1939,8 +1940,8 @@ finaliseArgBoxities env fn threshold_arity rhs_dmds div rhs
-- vcat [text "function:" <+> ppr fn
-- , text "dmds before:" <+> ppr (map idDemandInfo (filter isId bndrs))
-- , text "dmds after: " <+> ppr arg_dmds' ]) $
- (arg_dmds', add_demands arg_dmds' rhs)
- -- add_demands: we must attach the final boxities to the lambda-binders
+ (arg_dmds', set_lam_dmds arg_dmds' rhs)
+ -- set_lam_dmds: we must attach the final boxities to the lambda-binders
-- of the function, both because that's kosher, and because CPR analysis
-- uses the info on the binders directly.
where
@@ -2032,17 +2033,18 @@ finaliseArgBoxities env fn threshold_arity rhs_dmds div rhs
| positiveTopBudget bg_inner' = (bg_inner', dmd')
| otherwise = (bg_inner, trimBoxity dmd)
- add_demands :: [Demand] -> CoreExpr -> CoreExpr
+ set_lam_dmds :: [Demand] -> CoreExpr -> CoreExpr
-- Attach the demands to the outer lambdas of this expression
- add_demands [] e = e
- add_demands (dmd:dmds) (Lam v e)
- | isTyVar v = Lam v (add_demands (dmd:dmds) e)
- | otherwise = Lam (v `setIdDemandInfo` dmd) (add_demands dmds e)
- add_demands dmds (Cast e co) = Cast (add_demands dmds e) co
+ set_lam_dmds (dmd:dmds) (Lam v e)
+ | isTyVar v = Lam v (set_lam_dmds (dmd:dmds) e)
+ | otherwise = Lam (v `setIdDemandInfo` dmd) (set_lam_dmds dmds e)
+ set_lam_dmds dmds (Cast e co) = Cast (set_lam_dmds dmds e) co
-- This case happens for an OPAQUE function, which may look like
-- f = (\x y. blah) |> co
-- We give it strictness but no boxity (#22502)
- add_demands dmds e = pprPanic "add_demands" (ppr dmds $$ ppr e)
+ set_lam_dmds _ e = e
+ -- In the OPAQUE case, the list of demands at this point might be
+ -- non-empty, e.g., when looking at a PAP. Hence don't panic (#22997).
finaliseLetBoxity
:: AnalEnv
diff --git a/testsuite/tests/stranal/should_compile/T22997.hs b/testsuite/tests/stranal/should_compile/T22997.hs
new file mode 100644
index 0000000000..4c77f70a6e
--- /dev/null
+++ b/testsuite/tests/stranal/should_compile/T22997.hs
@@ -0,0 +1,9 @@
+module T22997 where
+
+{-# OPAQUE trivial #-}
+trivial :: Int -> Int
+trivial = succ
+
+{-# OPAQUE pap #-}
+pap :: Integer -> Integer
+pap = (42 +)
diff --git a/testsuite/tests/stranal/should_compile/all.T b/testsuite/tests/stranal/should_compile/all.T
index c5f142567f..0355def88e 100644
--- a/testsuite/tests/stranal/should_compile/all.T
+++ b/testsuite/tests/stranal/should_compile/all.T
@@ -88,3 +88,5 @@ test('EtaExpansion', normal, compile, [''])
test('T22039', normal, compile, [''])
# T22388: Should see $winteresting but not $wboring
test('T22388', [ grep_errmsg(r'^\S+\$w\S+') ], compile, ['-dsuppress-uniques -ddump-simpl'])
+# T22997: Just a panic that should not happen
+test('T22997', normal, compile, [''])