Simon Jakobi pushed to branch wip/sjakobi/cmm-unreachable-blocks-docs at Glasgow Haskell Compiler / GHC Commits: e8cf6c77 by Simon Jakobi at 2026-11-23T04:08:13+01:00 Correct some comments about unreachable Cmm blocks * ContFlowOpt claimed that maybe_concat drops every block that has no predecessors left. But a block that loses its predecessors to shortcutting stays in the graph. * The linear register allocator claimed that the Cmm pipeline keeps unreachable blocks away from it. But the NCG's own Cmm optimisations can make more blocks unreachable after the Cmm pipeline has run. It is sccBlocks that discards unreachable code before register allocation (#7574). Also: * Remove a commented-out trace in the linear allocator that predates #7574. * Say in Note [Unreachable blocks] that the passes between control-flow optimisation and stack layout can encounter unreachable blocks. Capitalise the title of the Note. Context: #27368 Assisted-by: Claude Fable 5.2 - - - - - 4 changed files: - compiler/GHC/Cmm.hs - compiler/GHC/Cmm/ContFlowOpt.hs - compiler/GHC/Cmm/Pipeline.hs - compiler/GHC/CmmToAsm/Reg/Linear.hs Changes: ===================================== compiler/GHC/Cmm.hs ===================================== @@ -154,7 +154,7 @@ toBlockMap (CmmGraph {g_graph=GMany NothingO body NothingO}) = body -- | Print the blocks reachable from the entry, in reverse postorder. -- -- Under @-dppr-debug@ the unreachable blocks stored in the graph are appended --- too. See Note [unreachable blocks] in "GHC.Cmm.Pipeline". +-- too. See Note [Unreachable blocks] in "GHC.Cmm.Pipeline". pprCmmGraph :: Platform -> CmmGraph -> SDoc pprCmmGraph platform g = text "{" <> text "offset" ===================================== compiler/GHC/Cmm/ContFlowOpt.hs ===================================== @@ -338,11 +338,9 @@ blockConcat splitting_procs g@CmmGraph { g_entry = entry_id } -- Functions for incrementing and decrementing number of predecessors. If -- decrementing would set the predecessor count to 0, we remove entry from the -- map. --- Invariant: if a block has no predecessors it should be dropped from the --- graph because it is unreachable. maybe_concat is constructed to maintain --- that invariant, but calling replaceLabels may introduce unreachable blocks. --- We rely on subsequent passes in the Cmm pipeline to remove unreachable --- blocks. +-- A block whose predecessor count drops to 0 is unreachable, but stays in the +-- graph. We rely on subsequent passes in the Cmm pipeline to remove +-- unreachable blocks. incPreds, decPreds :: BlockId -> LabelMap Int -> LabelMap Int incPreds bid edges = mapInsertWith (+) bid 1 edges decPreds bid edges = case mapLookup bid edges of ===================================== compiler/GHC/Cmm/Pipeline.hs ===================================== @@ -173,7 +173,7 @@ cpsTop logger platform cfg dus proc = then map (cmmCfgOptsProc splitting_proc_points) g else g g <- return $ map (removeUnreachableBlocksProc platform) g - -- See Note [unreachable blocks] + -- See Note [Unreachable blocks] dumps Opt_D_dump_cmm_cfg "Post control-flow optimisations (2)" g return (dus, Left (cafEnv, g)) @@ -351,13 +351,19 @@ _GLOBAL_OFFSET_TABLE_, regardless of which entry point we arrived via. -} -{- Note [unreachable blocks] +{- Note [Unreachable blocks] ~~~~~~~~~~~~~~~~~~~~~~~~~ The control-flow optimiser sometimes leaves unreachable blocks behind containing junk code. These aren't necessarily a problem, but removing them is good because it might save time in the native code generator later. +Stack layout drops them as a side effect of rebuilding the graph from +revPostorder, so the passes between control-flow optimisation and stack +layout may encounter them. removeUnreachableBlocksProc at the end of +cpsTop removes the blocks orphaned by the second round of control-flow +optimisation, along with their info tables. + To make unreachable blocks visible in -ddump-cmm-* output, add -dppr-debug. -} ===================================== compiler/GHC/CmmToAsm/Reg/Linear.hs ===================================== @@ -311,10 +311,6 @@ process entry_ids block_live = go [] next_round accum madeProgress | not madeProgress - {- BUGS: There are so many unreachable blocks in the code the warnings are overwhelming. - pprTrace "RegAlloc.Linear.Main.process: no progress made, bailing out." - ( text "Unreachable blocks:" - $$ vcat (map ppr next_round)) -} = return $ reverse accum | otherwise @@ -861,12 +857,9 @@ allocateRegsAndSpill reading keep spills alloc (r@(VirtualRegWithFormat vr vrFmt | otherwise -> doSpill WriteMem Nothing | reading -> pprPanic "allocateRegsAndSpill: Cannot read from uninitialized register" (ppr vr) - -- NOTE: if the input to the NCG contains some - -- unreachable blocks with junk code, this panic - -- might be triggered. Make sure you only feed - -- sensible code into the NCG. In GHC.Cmm.Pipeline we - -- call removeUnreachableBlocks at the end for this - -- reason. + -- NOTE: unreachable blocks with junk code could + -- trigger this panic, so sccBlocks in + -- GHC.CmmToAsm.Reg.Liveness discards them (#7574). | otherwise -> doSpill WriteNew View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/e8cf6c773ed6d1452c54068ad92b4d93... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/e8cf6c773ed6d1452c54068ad92b4d93... 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)