[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: ae443d29 by Simon Jakobi at 2026-10-25T14:38:15+01:00 Cmm: exclude the entry block from common block elimination a1d8139054c (!16592) made the entry block win merges, but only in the first round of elimCommonBlocks. Later rounds regroup the blocks by their substituted successors in an order unrelated to the entry, so the entry block could still lose a merge, and with it the proc's info table (#27722). Instead, the entry block is no longer a candidate for elimination. See Note [Retain entry block during common block elimination.] for the reasoning and the downside. Also drop the comment that justified toBlockList over revPostorder with the control-flow optimiser having removed unreachable blocks already. It hasn't necessarily, see Note [unreachable blocks] in GHC.Cmm.Pipeline, and it does not matter here: an unreachable block that wins a merge simply becomes the reachable copy. 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.] -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- 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,22 @@ +#include "Cmm.h" + +// The then-blocks of E1 and E2 are duplicates of the entry block: they +// carry the same number of source notes (block and statement) as the +// entry block (proc and statement), which matters for the block hash +// (#27877). They get merged with each other in the first round of +// common block elimination, E1 and E2 in the second, and only then does +// the entry block have a duplicate. +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/ae443d298f9ecb3e72cbb4d8eb7375ee... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/ae443d298f9ecb3e72cbb4d8eb7375ee... 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)