Simon Jakobi pushed to branch wip/sjakobi/cmm-unreachable-blocks-docs at Glasgow Haskell Compiler / GHC

Commits:

4 changed files:

Changes:

  • compiler/GHC/Cmm.hs
    ... ... @@ -154,7 +154,7 @@ toBlockMap (CmmGraph {g_graph=GMany NothingO body NothingO}) = body
    154 154
     -- | Print the blocks reachable from the entry, in reverse postorder.
    
    155 155
     --
    
    156 156
     -- Under @-dppr-debug@ the unreachable blocks stored in the graph are appended
    
    157
    --- too. See Note [unreachable blocks] in "GHC.Cmm.Pipeline".
    
    157
    +-- too. See Note [Unreachable blocks in the Cmm pipeline] in "GHC.Cmm.Pipeline".
    
    158 158
     pprCmmGraph :: Platform -> CmmGraph -> SDoc
    
    159 159
     pprCmmGraph platform g
    
    160 160
        = text "{" <> text "offset"
    

  • compiler/GHC/Cmm/ContFlowOpt.hs
    ... ... @@ -338,11 +338,9 @@ blockConcat splitting_procs g@CmmGraph { g_entry = entry_id }
    338 338
     -- Functions for incrementing and decrementing number of predecessors. If
    
    339 339
     -- decrementing would set the predecessor count to 0, we remove entry from the
    
    340 340
     -- map.
    
    341
    --- Invariant: if a block has no predecessors it should be dropped from the
    
    342
    --- graph because it is unreachable. maybe_concat is constructed to maintain
    
    343
    --- that invariant, but calling replaceLabels may introduce unreachable blocks.
    
    344
    --- We rely on subsequent passes in the Cmm pipeline to remove unreachable
    
    345
    --- blocks.
    
    341
    +-- Shortcutting can leave a block without predecessors. Such a block is
    
    342
    +-- unreachable but stays in the graph.
    
    343
    +-- See Note [Unreachable blocks in the Cmm pipeline] in GHC.Cmm.Pipeline.
    
    346 344
     incPreds, decPreds :: BlockId -> LabelMap Int -> LabelMap Int
    
    347 345
     incPreds bid edges = mapInsertWith (+) bid 1 edges
    
    348 346
     decPreds bid edges = case mapLookup bid edges of
    

  • compiler/GHC/Cmm/Pipeline.hs
    ... ... @@ -79,6 +79,8 @@ cpsTop logger platform cfg dus proc =
    79 79
           -- later passes by removing lots of empty blocks, so we do it
    
    80 80
           -- even when optimisation isn't turned on.
    
    81 81
           --
    
    82
    +      -- NB: It may leave unreachable blocks behind!
    
    83
    +      -- See Note [Unreachable blocks in the Cmm pipeline]
    
    82 84
           CmmProc h l v g <- {-# SCC "cmmCfgOpts(1)" #-}
    
    83 85
                return $ cmmCfgOptsProc splitting_proc_points proc
    
    84 86
           dump Opt_D_dump_cmm_cfg "Post control-flow optimisations (1)" l g
    
    ... ... @@ -173,7 +175,7 @@ cpsTop logger platform cfg dus proc =
    173 175
                         then map (cmmCfgOptsProc splitting_proc_points) g
    
    174 176
                         else g
    
    175 177
           g <- return $ map (removeUnreachableBlocksProc platform) g
    
    176
    -           -- See Note [unreachable blocks]
    
    178
    +           -- See Note [Unreachable blocks in the Cmm pipeline]
    
    177 179
           dumps Opt_D_dump_cmm_cfg "Post control-flow optimisations (2)" g
    
    178 180
     
    
    179 181
           return (dus, Left (cafEnv, g))
    
    ... ... @@ -351,13 +353,19 @@ _GLOBAL_OFFSET_TABLE_, regardless of which entry point we arrived via.
    351 353
     
    
    352 354
     -}
    
    353 355
     
    
    354
    -{- Note [unreachable blocks]
    
    355
    -   ~~~~~~~~~~~~~~~~~~~~~~~~~
    
    356
    +{- Note [Unreachable blocks in the Cmm pipeline]
    
    357
    +   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    356 358
     The control-flow optimiser sometimes leaves unreachable blocks behind
    
    357 359
     containing junk code.  These aren't necessarily a problem, but
    
    358 360
     removing them is good because it might save time in the native code
    
    359 361
     generator later.
    
    360 362
     
    
    363
    +Passes in cpsTop must not assume that every block is reachable.  Stack
    
    364
    +layout and sinking drop unreachable blocks as a side effect of
    
    365
    +rebuilding the graph from revPostorder, but both can be skipped.
    
    366
    +removeUnreachableBlocksProc at the end of cpsTop removes those that
    
    367
    +remain, along with their info tables.
    
    368
    +
    
    361 369
     To make unreachable blocks visible in -ddump-cmm-* output, add -dppr-debug.
    
    362 370
     -}
    
    363 371
     
    

  • compiler/GHC/CmmToAsm/Reg/Linear.hs
    ... ... @@ -311,10 +311,6 @@ process entry_ids block_live =
    311 311
     
    
    312 312
         go [] next_round accum madeProgress
    
    313 313
           | not madeProgress
    
    314
    -          {- BUGS: There are so many unreachable blocks in the code the warnings are overwhelming.
    
    315
    -             pprTrace "RegAlloc.Linear.Main.process: no progress made, bailing out."
    
    316
    -                (  text "Unreachable blocks:"
    
    317
    -                $$ vcat (map ppr next_round)) -}
    
    318 314
           = return $ reverse accum
    
    319 315
     
    
    320 316
           | otherwise
    
    ... ... @@ -861,12 +857,9 @@ allocateRegsAndSpill reading keep spills alloc (r@(VirtualRegWithFormat vr vrFmt
    861 857
                        | otherwise -> doSpill WriteMem
    
    862 858
                     Nothing | reading   ->
    
    863 859
                        pprPanic "allocateRegsAndSpill: Cannot read from uninitialized register" (ppr vr)
    
    864
    -                   -- NOTE: if the input to the NCG contains some
    
    865
    -                   -- unreachable blocks with junk code, this panic
    
    866
    -                   -- might be triggered.  Make sure you only feed
    
    867
    -                   -- sensible code into the NCG.  In GHC.Cmm.Pipeline we
    
    868
    -                   -- call removeUnreachableBlocks at the end for this
    
    869
    -                   -- reason.
    
    860
    +                   -- NOTE: unreachable blocks with junk code could
    
    861
    +                   -- trigger this panic, so sccBlocks in
    
    862
    +                   -- GHC.CmmToAsm.Reg.Liveness discards them (#7574).
    
    870 863
     
    
    871 864
                             | otherwise -> doSpill WriteNew
    
    872 865