[Git][ghc/ghc][wip/sjakobi/T27722-cbe-exclude-entry] Cmm: exclude the entry block from common block elimination
Simon Jakobi pushed to branch wip/sjakobi/T27722-cbe-exclude-entry at Glasgow Haskell Compiler / GHC Commits: 7536a7c1 by Simon Jakobi at 2026-10-25T21:01:42+01:00 Cmm: exclude the entry block from common block elimination a1d8139054c had fixed #27722 only partially, because it didn't prevent elimination of the entry block in later rounds, where the entry block is no longer guaranteed to come first. To close this loophole, we exclude the entry block from the CBE merge logic. See Note [Retain entry block during common block elimination] for details. This also deletes a misleading comment regarding unreachable blocks: the control-flow optimiser doesn't actually guarantee that no unreachable blocks reach CBE. Fixes #27722. Assisted-by: Claude Fable 5.1 - - - - - 4 changed files: - compiler/GHC/Cmm/CommonBlockElim.hs - + testsuite/tests/cmm/should_run/T27722.hs - + testsuite/tests/cmm/should_run/T27722_cmm.cmm - testsuite/tests/cmm/should_run/all.T Changes: ===================================== compiler/GHC/Cmm/CommonBlockElim.hs ===================================== @@ -66,25 +66,24 @@ elimCommonBlocks 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. - -- 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] + groups = groupByInt hash_block candidates :: [[CmmBlock]] + -- See Note [Retain entry block during common block elimination] + candidates = filter ((/= g_entry g) . entryLabel) (toBlockList g) --- Note [Retain entry block during common block elimination.] --- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +-- 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. +-- block label substitution to the info table *as long as we keep the entry +-- block* (#27722). So we simply keep the entry block out of the list of +-- candidates for elimination. -- --- If we don't we end up with #27722 where the entry block was eliminated in favour --- of another block. +-- The downside is that any duplicates of the entry block stay unmerged. This +-- should be very rare though, and only affects hand-written Cmm: the entry +-- block of any proc generated from STG contains the stack check (see +-- entryHeapCheck in GHC.StgToCmm.Heap) and is therefore distinct from any +-- other block in the same proc. -- Invariant: The blocks in the list are pairwise distinct -- (so avoid comparing them again) ===================================== testsuite/tests/cmm/should_run/T27722.hs ===================================== @@ -0,0 +1,8 @@ +import Control.Monad +import Foreign.Ptr +import System.Exit + +foreign import ccall "&zork_info" zork_info :: Ptr () + +main :: IO () +main = when (zork_info == nullPtr) exitFailure ===================================== testsuite/tests/cmm/should_run/T27722_cmm.cmm ===================================== @@ -0,0 +1,24 @@ +#include "Cmm.h" + +// The then-blocks of E1 and E2 are duplicates of the entry block. They are +// written as braced if-bodies so that, like the entry block, they carry two +// source notes (braces and statement): due to #27877 CBE would otherwise +// never compare them to the entry block. +// +// The then-blocks get merged with each other in the first round of common +// block elimination, E1 and E2 in the second, and only in the third does +// the entry block match the surviving then-block. +INFO_TABLE_RET (zork, RET_SMALL) + return () +{ + bits64 x; + x = 1 :: bits64; + goto E1; +E1: + if (x > 0 :: bits64) { x = 1 :: bits64; goto E2; } else { goto D; } +E2: + if (x > 0 :: bits64) { x = 1 :: bits64; goto E2; } else { goto D; } +D: + if (x > 1 :: bits64) goto E1; + goto E2; +} ===================================== testsuite/tests/cmm/should_run/all.T ===================================== @@ -60,3 +60,6 @@ test('T27447', ], multi_compile_and_run, ['T27447', [('T27447_cmm.cmm', '')], '']) + +test('T27722', [req_cmm], multi_compile_and_run, + ['T27722', [('T27722_cmm.cmm', '')], '-O']) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/7536a7c168b2f83c83c6c518cbd3b289... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/7536a7c168b2f83c83c6c518cbd3b289... 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)
-
Simon Jakobi (@sjakobi)