Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC Commits: 14656fd4 by Simon Jakobi at 2026-10-23T09:12:27-04:00 Cmm: keep the CBE substitution idempotent When blocks are merged over multiple rounds during common block elimination, a block that won a merge in an earlier round can still lose a merge in a later round. The substitution map (Subst) used to record this as a chain: the earlier loser mapped to a block that was itself eliminated. In PLT terminology, the substitution became *non-idempotent*. Applying such a substitution with a single lookup left edges to unreachable blocks in the Cmm graph, eventually leading to a setInfoTableStackMap panic (#27368). With this patch, each round's fresh substitutions are applied to the old ones as they are added (extendSubst), so every eliminated block maps directly to its final replacement and the substitution stays idempotent. Fixes #27368. Assisted-by: Claude Fable 5.1 - - - - - 4 changed files: - + changelog.d/27368 - compiler/GHC/Cmm/CommonBlockElim.hs - + testsuite/tests/codeGen/should_compile/T27368.hs - testsuite/tests/codeGen/should_compile/all.T Changes: ===================================== changelog.d/27368 ===================================== @@ -0,0 +1,5 @@ +section: compiler +synopsis: Fix a ``setInfoTableStackMap`` panic caused by common block elimination + leaving references to eliminated blocks. +issues: #27368 +mrs: !16765 ===================================== compiler/GHC/Cmm/CommonBlockElim.hs ===================================== @@ -90,6 +90,10 @@ elimCommonBlocks g = -- (so avoid comparing them again) type DistinctBlocks = [CmmBlock] type Key = [Label] + +-- | A substitution maps each eliminated block to its replacement. +-- +-- Invariant: the substitution is idempotent, i.e. no value is a key. type Subst = LabelMap BlockId -- The outer list groups by hash. We retain this grouping throughout. @@ -108,9 +112,24 @@ iterate subst blocks where (new_subst2, db) = mergeBlockList subst dbs - subst' = subst `mapUnion` new_substs + subst' = extendSubst subst new_substs updated_blocks = map (map (first (map (lookupBid subst')))) merged_blocks +-- | Extend the substitution with the merges of one more round. +-- +-- A block that won a merge in an earlier round may have lost one in this +-- round, so the existing mapping is updated to keep the result idempotent: +-- +-- > old: A -> B +-- > new: B -> C +-- > result: A -> C, B -> C +-- +extendSubst :: Subst -> Subst -> Subst +extendSubst old new = assert (idempotent result) result + where + result = mapMap (lookupBid new) old `mapUnion` new + idempotent s = not (any (`mapMember` s) s) + -- Combine two lists of blocks. -- While they are internally distinct they can still share common blocks. mergeBlocks :: Subst -> DistinctBlocks -> DistinctBlocks -> (Subst, DistinctBlocks) @@ -218,9 +237,7 @@ dont_care _other = False eqBid :: LabelMap BlockId -> BlockId -> BlockId -> Bool eqBid subst bid bid' = lookupBid subst bid == lookupBid subst bid' lookupBid :: LabelMap BlockId -> BlockId -> BlockId -lookupBid subst bid = case mapLookup bid subst of - Just bid -> lookupBid subst bid - Nothing -> bid +lookupBid subst bid = mapFindWithDefault bid bid subst -- Middle nodes and expressions can contain BlockIds, in particular in -- CmmStackSlot and CmmBlock, so we have to use a special equality for ===================================== testsuite/tests/codeGen/should_compile/T27368.hs ===================================== @@ -0,0 +1,17 @@ +-- Common block elimination merges the duplicated blocks over several +-- rounds. Compiling this at -O used to panic in setInfoTableStackMap (#27368). + +module T27368 (f) where + +{-# NOINLINE put #-} +put :: Int -> Int -> IO () +put h x = if h + x == 12345 then errorWithoutStackTrace "boom" else pure () + +data T = N | J Int | K + +f :: Int -> Bool -> T -> IO () +f h a t = do + if a + then do put h 1; case t of { N -> pure (); J _ -> put h 3; K -> put h 4 }; put h 0; put h 0; put h 0; put h 0 + else do put h 2; case t of { N -> pure (); J _ -> put h 3; K -> put h 4 }; put h 0; put h 0; put h 0; put h 0 + put h 0 ===================================== testsuite/tests/codeGen/should_compile/all.T ===================================== @@ -150,3 +150,5 @@ test('T16351', normal, compile, ['-O2 -ddump-simpl -dno-typeable-binds -dsuppres test('T20298a', normal, compile, ['-O2 -ddump-simpl -dno-typeable-binds -dsuppress-all -dsuppress-uniques']) test('T20298b', normal, compile, ['-O2 -dno-bignum-rules -ddump-simpl -dno-typeable-binds -dsuppress-all -dsuppress-uniques']) test('T20298c', normal, compile, ['-O2 -dno-builtin-rules -ddump-simpl -dno-typeable-binds -dsuppress-all -dsuppress-uniques']) + +test('T27368', normal, compile, ['-O']) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/14656fd45e4d1cef388587dd73ee4edb... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/14656fd45e4d1cef388587dd73ee4edb... 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