Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
-
38d421fb
by Sylvain Henry at 2026-11-16T16:02:22-05:00
-
fa5bc4c0
by Sylvain Henry at 2026-11-16T16:02:22-05:00
11 changed files:
- + changelog.d/27892
- compiler/GHC/Core/Map/Expr.hs
- compiler/GHC/Core/Map/Type.hs
- + testsuite/tests/pmcheck/should_compile/T27892a.hs
- + testsuite/tests/pmcheck/should_compile/T28095.hs
- testsuite/tests/pmcheck/should_compile/all.T
- + testsuite/tests/simplCore/should_compile/T27892.hs
- + testsuite/tests/simplCore/should_compile/T27892.stderr
- + testsuite/tests/simplCore/should_compile/T28095a.hs
- + testsuite/tests/simplCore/should_compile/T28095a.stderr
- testsuite/tests/simplCore/should_compile/all.T
Changes:
| 1 | +section: compiler
|
|
| 2 | +synopsis: Fix lookups of non-recursive lets in CoreMap
|
|
| 3 | +description:
|
|
| 4 | + ``CoreMap`` stored a non-recursive ``let`` under one key path and looked
|
|
| 5 | + it up under a different key path. When a map held two different
|
|
| 6 | + expressions, each lookup of an expression with a non-recursive ``let``
|
|
| 7 | + or join point failed. Thus CSE did not share identical bindings, and the
|
|
| 8 | + pattern-match checker could report spurious non-exhaustive matches for
|
|
| 9 | + guards or view patterns with equal expressions. ``CoreMap`` and
|
|
| 10 | + ``eqCoreExpr`` also ignore the multiplicities of binders.
|
|
| 11 | +mrs: !16936
|
|
| 12 | +issues: #27892 #28095 |
| ... | ... | @@ -28,7 +28,6 @@ import GHC.Core.Map.Type |
| 28 | 28 | import GHC.Core
|
| 29 | 29 | import GHC.Core.Type
|
| 30 | 30 | import GHC.Types.Tickish
|
| 31 | -import GHC.Types.Var
|
|
| 32 | 31 | |
| 33 | 32 | import GHC.Utils.Misc
|
| 34 | 33 | import GHC.Utils.Outputable
|
| ... | ... | @@ -85,6 +84,14 @@ Note [Binders] |
| 85 | 84 | - the binders in an alternative
|
| 86 | 85 | because they are totally fixed by the context
|
| 87 | 86 | |
| 87 | + * We ignore the multiplicity of all binders, in the trie and in
|
|
| 88 | + 'eqDeBruijnExpr'. The multiplicity of a let binder has no meaning: see
|
|
| 89 | + Note [Multiplicity of let binders] in GHC.Types.Var. Core-to-core
|
|
| 90 | + passes do not keep the multiplicities of lambda binders correct: see
|
|
| 91 | + Note [Linting linearity] in GHC.Core.Lint. Thus two terms that differ
|
|
| 92 | + only in binder multiplicities are equal for CSE and for the
|
|
| 93 | + pattern-match checker (#28095).
|
|
| 94 | + |
|
| 88 | 95 | Note [Empty case alternatives]
|
| 89 | 96 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 90 | 97 | * For a key (Case e b ty (alt:alts)) we don't need to look the return type
|
| ... | ... | @@ -104,6 +111,10 @@ See also Note [Empty case alternatives] in GHC.Core. |
| 104 | 111 | |
| 105 | 112 | -- | @CoreMap a@ is a map from 'CoreExpr' to @a@. If you are a client, this
|
| 106 | 113 | -- is the type you want.
|
| 114 | +--
|
|
| 115 | +-- 'CoreMap' is not linear: it ignores the multiplicities of binders. Two
|
|
| 116 | +-- expressions that differ only in the multiplicities of their lambda or let
|
|
| 117 | +-- binders have the same key. See Note [Binders].
|
|
| 107 | 118 | newtype CoreMap a = CoreMap (CoreMapG a)
|
| 108 | 119 | |
| 109 | 120 | -- TODO(22292): derive
|
| ... | ... | @@ -163,7 +174,7 @@ eqDeBruijnExpr (D env1 e1) (D env2 e2) = go e1 e2 where |
| 163 | 174 | |
| 164 | 175 | go (Lam b1 e1) (Lam b2 e2)
|
| 165 | 176 | = eqDeBruijnType (D env1 (varType b1)) (D env2 (varType b2))
|
| 166 | - && D env1 (varMultMaybe b1) == D env2 (varMultMaybe b2)
|
|
| 177 | + -- Ignore multiplicities: see Note [Binders]
|
|
| 167 | 178 | && eqDeBruijnExpr (D (extendCME env1 b1) e1) (D (extendCME env2 b2) e2)
|
| 168 | 179 | |
| 169 | 180 | go (Let (NonRec v1 r1) e1) (Let (NonRec v2 r2) e2)
|
| ... | ... | @@ -200,7 +211,8 @@ eqDeBruijnTickish (D env1 t1) (D env2 t2) = go t1 t2 where |
| 200 | 211 | && lext == rext
|
| 201 | 212 | go l r = l == r
|
| 202 | 213 | |
| 203 | --- Compares for equality, modulo alpha
|
|
| 214 | +-- | Compares for equality, modulo alpha. Like 'CoreMap', it ignores the
|
|
| 215 | +-- multiplicities of binders. See Note [Binders].
|
|
| 204 | 216 | eqCoreExpr :: CoreExpr -> CoreExpr -> Bool
|
| 205 | 217 | eqCoreExpr e1 e2 = eqDeBruijnExpr (deBruijnize e1) (deBruijnize e2)
|
| 206 | 218 | |
| ... | ... | @@ -374,8 +386,8 @@ xtE (D env (Lam v e)) f m = m { cm_lam = cm_lam m |
| 374 | 386 | |> xtG (D (extendCME env v) e)
|
| 375 | 387 | |>> xtBndr env v f }
|
| 376 | 388 | xtE (D env (Let (NonRec b r) e)) f m = m { cm_letn = cm_letn m
|
| 377 | - |> xtG (D (extendCME env b) e)
|
|
| 378 | - |>> xtG (D env r)
|
|
| 389 | + |> xtG (D env r)
|
|
| 390 | + |>> xtG (D (extendCME env b) e)
|
|
| 379 | 391 | |>> xtBndr env b f }
|
| 380 | 392 | xtE (D env (Let (Rec prs) e)) f m = m { cm_letr =
|
| 381 | 393 | let (bndrs,rhss) = unzip prs
|
| ... | ... | @@ -534,11 +534,6 @@ instance Eq (DeBruijn a) => Eq (DeBruijn [a]) where |
| 534 | 534 | D env xs == D env' xs'
|
| 535 | 535 | _ == _ = False
|
| 536 | 536 | |
| 537 | -instance Eq (DeBruijn a) => Eq (DeBruijn (Maybe a)) where
|
|
| 538 | - D _ Nothing == D _ Nothing = True
|
|
| 539 | - D env (Just x) == D env' (Just x') = D env x == D env' x'
|
|
| 540 | - _ == _ = False
|
|
| 541 | - |
|
| 542 | 537 | --------- Variable binders -------------
|
| 543 | 538 | |
| 544 | 539 | -- | A 'BndrMap' is a 'TypeMapG' which allows us to distinguish between
|
| ... | ... | @@ -548,46 +543,18 @@ instance Eq (DeBruijn a) => Eq (DeBruijn (Maybe a)) where |
| 548 | 543 | -- we can disambiguate this by matching on the type (or kind, if this
|
| 549 | 544 | -- a binder in a type) of the binder.
|
| 550 | 545 | --
|
| 551 | --- We also need to do the same for multiplicity! Which, since multiplicities are
|
|
| 552 | --- encoded simply as a 'Type', amounts to have a Trie for a pair of types. Tries
|
|
| 553 | --- of pairs are composition.
|
|
| 554 | -newtype BndrMap a = BndrMap (TypeMapG (MaybeMap TypeMapG a))
|
|
| 555 | - |
|
| 556 | --- TODO(22292): derive
|
|
| 557 | -instance Functor BndrMap where
|
|
| 558 | - fmap f = \ (BndrMap tm) -> BndrMap (fmap (fmap f) tm)
|
|
| 559 | - {-# INLINE fmap #-}
|
|
| 560 | - |
|
| 561 | -instance TrieMap BndrMap where
|
|
| 562 | - type Key BndrMap = Var
|
|
| 563 | - emptyTM = BndrMap emptyTM
|
|
| 564 | - lookupTM = lkBndr emptyCME
|
|
| 565 | - alterTM = xtBndr emptyCME
|
|
| 566 | - foldTM = fdBndrMap
|
|
| 567 | - filterTM = ftBndrMap
|
|
| 568 | - mapMaybeTM = mpBndrMap
|
|
| 569 | - |
|
| 570 | -fdBndrMap :: (a -> b -> b) -> BndrMap a -> b -> b
|
|
| 571 | -fdBndrMap f (BndrMap tm) = foldTM (foldTM f) tm
|
|
| 572 | - |
|
| 573 | -mpBndrMap :: (a -> Maybe b) -> BndrMap a -> BndrMap b
|
|
| 574 | -mpBndrMap f (BndrMap tm) = BndrMap (fmap (mapMaybeTM f) tm)
|
|
| 546 | +-- The map ignores the multiplicity of the binder.
|
|
| 547 | +-- See Note [Binders] in GHC.Core.Map.Expr.
|
|
| 548 | +type BndrMap = TypeMapG
|
|
| 575 | 549 | |
| 576 | 550 | -- We need to use 'BndrMap' for 'Coercion', 'CoreExpr' AND 'Type', since all
|
| 577 | 551 | -- of these data types have binding forms.
|
| 578 | 552 | |
| 579 | 553 | lkBndr :: CmEnv -> Var -> BndrMap a -> Maybe a
|
| 580 | -lkBndr env v (BndrMap tymap) = do
|
|
| 581 | - multmap <- lkG (D env (varType v)) tymap
|
|
| 582 | - lookupTM (D env <$> varMultMaybe v) multmap
|
|
| 583 | - |
|
| 584 | - |
|
| 585 | -xtBndr :: forall a . CmEnv -> Var -> XT a -> BndrMap a -> BndrMap a
|
|
| 586 | -xtBndr env v xt (BndrMap tymap) =
|
|
| 587 | - BndrMap (tymap |> xtG (D env (varType v)) |>> (alterTM (D env <$> varMultMaybe v) xt))
|
|
| 554 | +lkBndr env v m = lkG (D env (varType v)) m
|
|
| 588 | 555 | |
| 589 | -ftBndrMap :: (a -> Bool) -> BndrMap a -> BndrMap a
|
|
| 590 | -ftBndrMap f (BndrMap tm) = BndrMap (fmap (filterTM f) tm)
|
|
| 556 | +xtBndr :: CmEnv -> Var -> XT a -> BndrMap a -> BndrMap a
|
|
| 557 | +xtBndr env v f = xtG (D env (varType v)) f
|
|
| 591 | 558 | |
| 592 | 559 | --------- Variable occurrence -------------
|
| 593 | 560 | data VarMap a = VM { vm_bvar :: BoundVarMap a -- Bound variable
|
| 1 | +{-# LANGUAGE ViewPatterns #-}
|
|
| 2 | +module T27892a (f) where
|
|
| 3 | + |
|
| 4 | +-- The pattern-match checker gives the same representative to the view
|
|
| 5 | +-- expressions of clauses 2 and 3. Thus clause 2 excludes Other, and
|
|
| 6 | +-- clause 3 covers the rest: f is exhaustive. Clause 1 puts an unrelated
|
|
| 7 | +-- key into the CoreMap of the checker. Thus the lookups of clauses 2 and
|
|
| 8 | +-- 3 go through the trie and not through the singleton comparison. The
|
|
| 9 | +-- lambda prevents the simple optimiser from inlining the let. See #27892.
|
|
| 10 | + |
|
| 11 | +data P = MkP Int Int
|
|
| 12 | +data W = Wrap (Int -> P) | Other
|
|
| 13 | + |
|
| 14 | +expensive :: Int -> Int
|
|
| 15 | +expensive n = n * n
|
|
| 16 | +{-# NOINLINE expensive #-}
|
|
| 17 | + |
|
| 18 | +f :: Int -> Int
|
|
| 19 | +f ((\v -> expensive v) -> 0) = 0
|
|
| 20 | +f ((\v -> let x = expensive v in Wrap (\z -> MkP z x)) -> Other) = 1
|
|
| 21 | +f ((\v -> let x = expensive v in Wrap (\z -> MkP z x)) -> Wrap _) = 2 |
| 1 | +{-# LANGUAGE LinearTypes, ViewPatterns #-}
|
|
| 2 | +module T28095 (f) where
|
|
| 3 | + |
|
| 4 | +-- This test is the same as T27892a, but the let of clause 3 comes from
|
|
| 5 | +-- the beta-reduction of a linear lambda. Thus its binder has multiplicity
|
|
| 6 | +-- One, and the binder of the source let in clause 2 has multiplicity
|
|
| 7 | +-- Many. The multiplicity of a let binder has no meaning (see
|
|
| 8 | +-- Note [Multiplicity of let binders] in GHC.Types.Var). The checker must
|
|
| 9 | +-- give the two expressions the same representative and find f exhaustive.
|
|
| 10 | + |
|
| 11 | +data P = MkP Int Int
|
|
| 12 | +data W = Wrap (Int %1 -> P) | Other
|
|
| 13 | + |
|
| 14 | +expensive :: Int -> Int
|
|
| 15 | +expensive n = n * n
|
|
| 16 | +{-# NOINLINE expensive #-}
|
|
| 17 | + |
|
| 18 | +f :: Int -> Int
|
|
| 19 | +f ((\v -> expensive v) -> 0) = 0
|
|
| 20 | +f ((\v -> let x = expensive v in Wrap (\z -> MkP z x)) -> Other) = 1
|
|
| 21 | +f ((\v -> ((\x -> Wrap (\z -> MkP z x)) :: Int %1 -> W) (expensive v)) -> Wrap _) = 2 |
| ... | ... | @@ -193,3 +193,5 @@ test('T24867', [], compile_fail, [overlapping_incomplete]) |
| 193 | 193 | test('T27323', normal, compile, ['-Wredundant-bang-patterns'])
|
| 194 | 194 | test('T27323b', normal, compile, ['-Wredundant-bang-patterns'])
|
| 195 | 195 | test('T27360', normal, compile, [overlapping_incomplete + '-g3'])
|
| 196 | +test('T27892a', normal, compile, [overlapping_incomplete])
|
|
| 197 | +test('T28095', normal, compile, [overlapping_incomplete]) |
| 1 | +module T27892 where
|
|
| 2 | + |
|
| 3 | +-- CSE must find that f and g have identical right-hand sides. The
|
|
| 4 | +-- binding h is between them in the CoreMap, and each body holds a
|
|
| 5 | +-- non-recursive let. See #27892.
|
|
| 6 | + |
|
| 7 | +f, g, h :: Int -> Int -> Int
|
|
| 8 | +f x y = case (if x > y then x * 3 else y * 5) of m -> m * m + x * y
|
|
| 9 | +h x y = case (if x > y then x * 7 else y * 9) of m -> m * m + x * y
|
|
| 10 | +g x y = case (if x > y then x * 3 else y * 5) of m -> m * m + x * y |
| 1 | +f = g |
| 1 | +{-# LANGUAGE LinearTypes #-}
|
|
| 2 | +module T28095a where
|
|
| 3 | + |
|
| 4 | +-- CSE must find that f and g have identical right-hand sides. The two
|
|
| 5 | +-- lambdas differ only in the multiplicity of their binders, and CSE
|
|
| 6 | +-- ignores it. See Note [Binders] in GHC.Core.Map.Expr.
|
|
| 7 | + |
|
| 8 | +data P = MkP Int Int
|
|
| 9 | + |
|
| 10 | +f :: Int %1 -> Int -> P
|
|
| 11 | +f x y = MkP x (y * 3)
|
|
| 12 | + |
|
| 13 | +g :: Int -> Int -> P
|
|
| 14 | +g x y = MkP x (y * 3) |
| 1 | +f = g |
| ... | ... | @@ -629,3 +629,5 @@ test('T27749', |
| 629 | 629 | test('T27629', only_ways(['optasm']), makefile_test, ['T27629'])
|
| 630 | 630 | test('T27936', normal, compile, [''])
|
| 631 | 631 | test('T27910', grep_errmsg(r'KnownBranch'), compile, ['-O -dno-typeable-binds -ddump-simpl-stats -dsuppress-uniques'])
|
| 632 | +test('T27892', grep_errmsg(r'^f = g$'), compile, ['-O -ddump-cse -dsuppress-all -dsuppress-uniques'])
|
|
| 633 | +test('T28095a', grep_errmsg(r'^f = g$'), compile, ['-O -ddump-cse -dsuppress-all -dsuppress-uniques']) |