[Git][ghc/ghc][wip/apk/cmm-cbe] cmm: Always favour entry block during block deduplication.
Andreas Klebinger pushed to branch wip/apk/cmm-cbe at Glasgow Haskell Compiler / GHC Commits: 20b6edc0 by Andreas Klebinger at 2026-08-25T20:03:53+00:00 cmm: Always favour entry block during block deduplication. We now always keep the first block in the CmmGraph. This way we avoid the need to update the entry info table. Failing to do so caused #27722. Fixes #27722. - - - - - 2 changed files: - + changelog.d/T27722-cbe-entry-block.md - compiler/GHC/Cmm/CommonBlockElim.hs Changes: ===================================== changelog.d/T27722-cbe-entry-block.md ===================================== @@ -0,0 +1,5 @@ +section: cmm +issues: #27722 +mrs: !16592 +synopsis: + Fix common block elimination dropping entry block info table in hand written cmm. ===================================== compiler/GHC/Cmm/CommonBlockElim.hs ===================================== @@ -26,6 +26,7 @@ import GHC.Types.Literal.Floating import GHC.Types.Unique.FM import GHC.Types.Unique import GHC.Utils.Word64 (truncateWord64ToWord32) +import GHC.Utils.Panic.Plain (assert) import Control.Arrow (first, second) import Data.List.NonEmpty (NonEmpty (..)) import qualified Data.List.NonEmpty as NE @@ -60,16 +61,31 @@ import qualified Data.List.NonEmpty as NE -- TODO: Use optimization fuel elimCommonBlocks :: CmmGraph -> CmmGraph -elimCommonBlocks g = replaceLabels env $ copyTicks env g +elimCommonBlocks g = + assert (g_entry g == g_entry g') g' where + g' = replaceLabels env $ copyTicks env g env = iterate mapEmpty blocks_with_key -- The order of blocks doesn't matter here. While we could use -- revPostorder which drops unreachable blocks this is done in -- ContFlowOpt already which runs before this pass. So we use -- toBlockList since it is faster. - groups = groupByInt hash_block (toBlockList g) :: [[CmmBlock]] + -- One exception: The entry block most come first or we risk eliminating it + -- in favour of another block. See Note [Retain entry block during common block elimination.] + groups = groupByInt hash_block (toBlockListEntryFirst g) :: [[CmmBlock]] blocks_with_key = [ [ (successors b, [b]) | b <- bs] | bs <- groups] +-- Note [Retain entry block during common block elimination.] +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +-- At the stage we run common block elimination (CBE) we only have one info +-- table for the entry label. Which means we can get away without applying the +-- block label substitution to the info table *as long as we keep the first block*. +-- When combining blocks the first block in the list of blocks is kept, and the later +-- one eliminated, so we can achieve this by simply using toBlockListEntryFirst. +-- +-- If we don't we end up with #27722 where the entry block was eliminated in favour +-- of another block. + -- Invariant: The blocks in the list are pairwise distinct -- (so avoid comparing them again) type DistinctBlocks = [CmmBlock] View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/20b6edc0caca21f2a359a01972da598e... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/20b6edc0caca21f2a359a01972da598e... 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
participants (1)
-
Andreas Klebinger (@AndreasK)