[Git][ghc/ghc][master] Correct some comments about unreachable Cmm blocks
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC Commits: e8139c28 by Simon Jakobi at 2026-11-26T21:22:56-05: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 any pass in cpsTop can encounter unreachable blocks. Rename it to Note [Unreachable blocks in the Cmm pipeline], so its title no longer differs only in case from an unrelated Note in GHC.Cmm.Dataflow. 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 the Cmm pipeline] 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. +-- Shortcutting can leave a block without predecessors. Such a block is +-- unreachable but stays in the graph. +-- See Note [Unreachable blocks in the Cmm pipeline] in GHC.Cmm.Pipeline. 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 ===================================== @@ -79,6 +79,8 @@ cpsTop logger platform cfg dus proc = -- later passes by removing lots of empty blocks, so we do it -- even when optimisation isn't turned on. -- + -- NB: It may leave unreachable blocks behind! + -- See Note [Unreachable blocks in the Cmm pipeline] CmmProc h l v g <- {-# SCC "cmmCfgOpts(1)" #-} return $ cmmCfgOptsProc splitting_proc_points proc dump Opt_D_dump_cmm_cfg "Post control-flow optimisations (1)" l g @@ -173,7 +175,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 in the Cmm pipeline] dumps Opt_D_dump_cmm_cfg "Post control-flow optimisations (2)" g return (dus, Left (cafEnv, g)) @@ -351,13 +353,19 @@ _GLOBAL_OFFSET_TABLE_, regardless of which entry point we arrived via. -} -{- Note [unreachable blocks] - ~~~~~~~~~~~~~~~~~~~~~~~~~ +{- Note [Unreachable blocks in the Cmm pipeline] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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. +Passes in cpsTop must not assume that every block is reachable. Stack +layout and sinking drop unreachable blocks as a side effect of +rebuilding the graph from revPostorder, but both can be skipped. +removeUnreachableBlocksProc at the end of cpsTop removes those that +remain, 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/e8139c2894cf0fa6968aa7cd67f82cc5... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/e8139c2894cf0fa6968aa7cd67f82cc5... 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)
-
Marge Bot (@marge-bot)