Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC Commits: 38d421fb by Sylvain Henry at 2026-11-16T16:02:22-05:00 CoreMap: look up a non-recursive let with the key order of the store xtE stored a `Let (NonRec b r) e` with the body e as the first key and the right-hand side r as the second key. lkE used the opposite order. When a trie level holds one entry, GenMap compares the full keys with (==), and the difference has no effect. When a trie level holds two entries, a lookup of an expression with a non-recursive let fails. A later insert of an equal expression then overwrites the first entry. Now xtE stores the key in the order that lkE uses. Effects of the bug: CSE did not share identical bindings when a module had two different bindings that hold a non-recursive let or join point (#27892). The pattern-match checker gave different representatives to equal guard or view expressions, and thus reported spurious non-exhaustive matches. Fixes #27892. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> - - - - - fa5bc4c0 by Sylvain Henry at 2026-11-16T16:02:22-05:00 CoreMap: ignore the multiplicities of binders BndrMap keyed a binder on its type and on its multiplicity, and eqDeBruijnExpr compared the multiplicities of lambda binders. But the multiplicity of a let binder has no meaning (Note [Multiplicity of let binders] in GHC.Types.Var), and Core-to-core passes do not keep the multiplicities of lambda binders correct (Note [Linting linearity] in GHC.Core.Lint). Thus the trie separated terms that are equal. The key-order bug fixed in the previous commit hid this bug. With that fix, the pattern-match checker gave different representatives to two equal view expressions when one let came from the source and the other let came from the beta-reduction of a linear lambda. Now BndrMap is TypeMapG again, as before the linear types commit, and eqDeBruijnExpr ignores the multiplicities of lambda binders. The haddocks of CoreMap and eqCoreExpr state this contract. Fixes #28095. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> - - - - - 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: ===================================== changelog.d/27892 ===================================== @@ -0,0 +1,12 @@ +section: compiler +synopsis: Fix lookups of non-recursive lets in CoreMap +description: + ``CoreMap`` stored a non-recursive ``let`` under one key path and looked + it up under a different key path. When a map held two different + expressions, each lookup of an expression with a non-recursive ``let`` + or join point failed. Thus CSE did not share identical bindings, and the + pattern-match checker could report spurious non-exhaustive matches for + guards or view patterns with equal expressions. ``CoreMap`` and + ``eqCoreExpr`` also ignore the multiplicities of binders. +mrs: !16936 +issues: #27892 #28095 ===================================== compiler/GHC/Core/Map/Expr.hs ===================================== @@ -28,7 +28,6 @@ import GHC.Core.Map.Type import GHC.Core import GHC.Core.Type import GHC.Types.Tickish -import GHC.Types.Var import GHC.Utils.Misc import GHC.Utils.Outputable @@ -85,6 +84,14 @@ Note [Binders] - the binders in an alternative because they are totally fixed by the context + * We ignore the multiplicity of all binders, in the trie and in + 'eqDeBruijnExpr'. The multiplicity of a let binder has no meaning: see + Note [Multiplicity of let binders] in GHC.Types.Var. Core-to-core + passes do not keep the multiplicities of lambda binders correct: see + Note [Linting linearity] in GHC.Core.Lint. Thus two terms that differ + only in binder multiplicities are equal for CSE and for the + pattern-match checker (#28095). + Note [Empty case alternatives] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * 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. -- | @CoreMap a@ is a map from 'CoreExpr' to @a@. If you are a client, this -- is the type you want. +-- +-- 'CoreMap' is not linear: it ignores the multiplicities of binders. Two +-- expressions that differ only in the multiplicities of their lambda or let +-- binders have the same key. See Note [Binders]. newtype CoreMap a = CoreMap (CoreMapG a) -- TODO(22292): derive @@ -163,7 +174,7 @@ eqDeBruijnExpr (D env1 e1) (D env2 e2) = go e1 e2 where go (Lam b1 e1) (Lam b2 e2) = eqDeBruijnType (D env1 (varType b1)) (D env2 (varType b2)) - && D env1 (varMultMaybe b1) == D env2 (varMultMaybe b2) + -- Ignore multiplicities: see Note [Binders] && eqDeBruijnExpr (D (extendCME env1 b1) e1) (D (extendCME env2 b2) e2) 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 && lext == rext go l r = l == r --- Compares for equality, modulo alpha +-- | Compares for equality, modulo alpha. Like 'CoreMap', it ignores the +-- multiplicities of binders. See Note [Binders]. eqCoreExpr :: CoreExpr -> CoreExpr -> Bool eqCoreExpr e1 e2 = eqDeBruijnExpr (deBruijnize e1) (deBruijnize e2) @@ -374,8 +386,8 @@ xtE (D env (Lam v e)) f m = m { cm_lam = cm_lam m |> xtG (D (extendCME env v) e) |>> xtBndr env v f } xtE (D env (Let (NonRec b r) e)) f m = m { cm_letn = cm_letn m - |> xtG (D (extendCME env b) e) - |>> xtG (D env r) + |> xtG (D env r) + |>> xtG (D (extendCME env b) e) |>> xtBndr env b f } xtE (D env (Let (Rec prs) e)) f m = m { cm_letr = let (bndrs,rhss) = unzip prs ===================================== compiler/GHC/Core/Map/Type.hs ===================================== @@ -534,11 +534,6 @@ instance Eq (DeBruijn a) => Eq (DeBruijn [a]) where D env xs == D env' xs' _ == _ = False -instance Eq (DeBruijn a) => Eq (DeBruijn (Maybe a)) where - D _ Nothing == D _ Nothing = True - D env (Just x) == D env' (Just x') = D env x == D env' x' - _ == _ = False - --------- Variable binders ------------- -- | A 'BndrMap' is a 'TypeMapG' which allows us to distinguish between @@ -548,46 +543,18 @@ instance Eq (DeBruijn a) => Eq (DeBruijn (Maybe a)) where -- we can disambiguate this by matching on the type (or kind, if this -- a binder in a type) of the binder. -- --- We also need to do the same for multiplicity! Which, since multiplicities are --- encoded simply as a 'Type', amounts to have a Trie for a pair of types. Tries --- of pairs are composition. -newtype BndrMap a = BndrMap (TypeMapG (MaybeMap TypeMapG a)) - --- TODO(22292): derive -instance Functor BndrMap where - fmap f = \ (BndrMap tm) -> BndrMap (fmap (fmap f) tm) - {-# INLINE fmap #-} - -instance TrieMap BndrMap where - type Key BndrMap = Var - emptyTM = BndrMap emptyTM - lookupTM = lkBndr emptyCME - alterTM = xtBndr emptyCME - foldTM = fdBndrMap - filterTM = ftBndrMap - mapMaybeTM = mpBndrMap - -fdBndrMap :: (a -> b -> b) -> BndrMap a -> b -> b -fdBndrMap f (BndrMap tm) = foldTM (foldTM f) tm - -mpBndrMap :: (a -> Maybe b) -> BndrMap a -> BndrMap b -mpBndrMap f (BndrMap tm) = BndrMap (fmap (mapMaybeTM f) tm) +-- The map ignores the multiplicity of the binder. +-- See Note [Binders] in GHC.Core.Map.Expr. +type BndrMap = TypeMapG -- We need to use 'BndrMap' for 'Coercion', 'CoreExpr' AND 'Type', since all -- of these data types have binding forms. lkBndr :: CmEnv -> Var -> BndrMap a -> Maybe a -lkBndr env v (BndrMap tymap) = do - multmap <- lkG (D env (varType v)) tymap - lookupTM (D env <$> varMultMaybe v) multmap - - -xtBndr :: forall a . CmEnv -> Var -> XT a -> BndrMap a -> BndrMap a -xtBndr env v xt (BndrMap tymap) = - BndrMap (tymap |> xtG (D env (varType v)) |>> (alterTM (D env <$> varMultMaybe v) xt)) +lkBndr env v m = lkG (D env (varType v)) m -ftBndrMap :: (a -> Bool) -> BndrMap a -> BndrMap a -ftBndrMap f (BndrMap tm) = BndrMap (fmap (filterTM f) tm) +xtBndr :: CmEnv -> Var -> XT a -> BndrMap a -> BndrMap a +xtBndr env v f = xtG (D env (varType v)) f --------- Variable occurrence ------------- data VarMap a = VM { vm_bvar :: BoundVarMap a -- Bound variable ===================================== testsuite/tests/pmcheck/should_compile/T27892a.hs ===================================== @@ -0,0 +1,21 @@ +{-# LANGUAGE ViewPatterns #-} +module T27892a (f) where + +-- The pattern-match checker gives the same representative to the view +-- expressions of clauses 2 and 3. Thus clause 2 excludes Other, and +-- clause 3 covers the rest: f is exhaustive. Clause 1 puts an unrelated +-- key into the CoreMap of the checker. Thus the lookups of clauses 2 and +-- 3 go through the trie and not through the singleton comparison. The +-- lambda prevents the simple optimiser from inlining the let. See #27892. + +data P = MkP Int Int +data W = Wrap (Int -> P) | Other + +expensive :: Int -> Int +expensive n = n * n +{-# NOINLINE expensive #-} + +f :: Int -> Int +f ((\v -> expensive v) -> 0) = 0 +f ((\v -> let x = expensive v in Wrap (\z -> MkP z x)) -> Other) = 1 +f ((\v -> let x = expensive v in Wrap (\z -> MkP z x)) -> Wrap _) = 2 ===================================== testsuite/tests/pmcheck/should_compile/T28095.hs ===================================== @@ -0,0 +1,21 @@ +{-# LANGUAGE LinearTypes, ViewPatterns #-} +module T28095 (f) where + +-- This test is the same as T27892a, but the let of clause 3 comes from +-- the beta-reduction of a linear lambda. Thus its binder has multiplicity +-- One, and the binder of the source let in clause 2 has multiplicity +-- Many. The multiplicity of a let binder has no meaning (see +-- Note [Multiplicity of let binders] in GHC.Types.Var). The checker must +-- give the two expressions the same representative and find f exhaustive. + +data P = MkP Int Int +data W = Wrap (Int %1 -> P) | Other + +expensive :: Int -> Int +expensive n = n * n +{-# NOINLINE expensive #-} + +f :: Int -> Int +f ((\v -> expensive v) -> 0) = 0 +f ((\v -> let x = expensive v in Wrap (\z -> MkP z x)) -> Other) = 1 +f ((\v -> ((\x -> Wrap (\z -> MkP z x)) :: Int %1 -> W) (expensive v)) -> Wrap _) = 2 ===================================== testsuite/tests/pmcheck/should_compile/all.T ===================================== @@ -193,3 +193,5 @@ test('T24867', [], compile_fail, [overlapping_incomplete]) test('T27323', normal, compile, ['-Wredundant-bang-patterns']) test('T27323b', normal, compile, ['-Wredundant-bang-patterns']) test('T27360', normal, compile, [overlapping_incomplete + '-g3']) +test('T27892a', normal, compile, [overlapping_incomplete]) +test('T28095', normal, compile, [overlapping_incomplete]) ===================================== testsuite/tests/simplCore/should_compile/T27892.hs ===================================== @@ -0,0 +1,10 @@ +module T27892 where + +-- CSE must find that f and g have identical right-hand sides. The +-- binding h is between them in the CoreMap, and each body holds a +-- non-recursive let. See #27892. + +f, g, h :: Int -> Int -> Int +f x y = case (if x > y then x * 3 else y * 5) of m -> m * m + x * y +h x y = case (if x > y then x * 7 else y * 9) of m -> m * m + x * y +g x y = case (if x > y then x * 3 else y * 5) of m -> m * m + x * y ===================================== testsuite/tests/simplCore/should_compile/T27892.stderr ===================================== @@ -0,0 +1 @@ +f = g ===================================== testsuite/tests/simplCore/should_compile/T28095a.hs ===================================== @@ -0,0 +1,14 @@ +{-# LANGUAGE LinearTypes #-} +module T28095a where + +-- CSE must find that f and g have identical right-hand sides. The two +-- lambdas differ only in the multiplicity of their binders, and CSE +-- ignores it. See Note [Binders] in GHC.Core.Map.Expr. + +data P = MkP Int Int + +f :: Int %1 -> Int -> P +f x y = MkP x (y * 3) + +g :: Int -> Int -> P +g x y = MkP x (y * 3) ===================================== testsuite/tests/simplCore/should_compile/T28095a.stderr ===================================== @@ -0,0 +1 @@ +f = g ===================================== testsuite/tests/simplCore/should_compile/all.T ===================================== @@ -629,3 +629,5 @@ test('T27749', test('T27629', only_ways(['optasm']), makefile_test, ['T27629']) test('T27936', normal, compile, ['']) 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']) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/13408a2b593d9c1831ea82faf2f5321... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/13408a2b593d9c1831ea82faf2f5321... 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