Simon Jakobi pushed to branch wip/sjakobi/T27722-cbe-exclude-entry at Glasgow Haskell Compiler / GHC

Commits:

4 changed files:

Changes:

  • compiler/GHC/Cmm/CommonBlockElim.hs
    ... ... @@ -66,25 +66,24 @@ elimCommonBlocks g =
    66 66
       where
    
    67 67
          g' = replaceLabels env $ copyTicks env g
    
    68 68
          env = iterate mapEmpty blocks_with_key
    
    69
    -     -- The order of blocks doesn't matter here. While we could use
    
    70
    -     -- revPostorder which drops unreachable blocks this is done in
    
    71
    -     -- ContFlowOpt already which runs before this pass. So we use
    
    72
    -     -- toBlockList since it is faster.
    
    73
    -     -- One exception: The entry block most come first or we risk eliminating it
    
    74
    -     -- in favour of another block. See Note [Retain entry block during common block elimination.]
    
    75
    -     groups = groupByInt hash_block (toBlockListEntryFirst g) :: [[CmmBlock]]
    
    76 69
          blocks_with_key = [ [ (successors b, [b]) | b <- bs] | bs <- groups]
    
    70
    +     groups = groupByInt hash_block candidates :: [[CmmBlock]]
    
    71
    +     -- See Note [Retain entry block during common block elimination]
    
    72
    +     candidates = filter ((/= g_entry g) . entryLabel) (toBlockList g)
    
    77 73
     
    
    78
    --- Note [Retain entry block during common block elimination.]
    
    79
    --- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    74
    +-- Note [Retain entry block during common block elimination]
    
    75
    +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    80 76
     -- At the stage we run common block elimination (CBE) we only have one info
    
    81 77
     -- table for the entry label. Which means we can get away without applying the
    
    82
    --- block label substitution to the info table *as long as we keep the first block*.
    
    83
    --- When combining blocks the first block in the list of blocks is kept, and the later
    
    84
    --- one eliminated, so we can achieve this by simply using toBlockListEntryFirst.
    
    78
    +-- block label substitution to the info table *as long as we keep the entry
    
    79
    +-- block* (#27722). So we simply keep the entry block out of the list of
    
    80
    +-- candidates for elimination.
    
    85 81
     --
    
    86
    --- If we don't we end up with #27722 where the entry block was eliminated in favour
    
    87
    --- of another block.
    
    82
    +-- The downside is that any duplicates of the entry block stay unmerged. This
    
    83
    +-- should be very rare though, and only affects hand-written Cmm: the entry
    
    84
    +-- block of any proc generated from STG contains the stack check (see
    
    85
    +-- entryHeapCheck in GHC.StgToCmm.Heap) and is therefore distinct from any
    
    86
    +-- other block in the same proc.
    
    88 87
     
    
    89 88
     -- Invariant: The blocks in the list are pairwise distinct
    
    90 89
     -- (so avoid comparing them again)
    

  • testsuite/tests/cmm/should_run/T27722.hs
    1
    +import Control.Monad
    
    2
    +import Foreign.Ptr
    
    3
    +import System.Exit
    
    4
    +
    
    5
    +foreign import ccall "&zork_info" zork_info :: Ptr ()
    
    6
    +
    
    7
    +main :: IO ()
    
    8
    +main = when (zork_info == nullPtr) exitFailure

  • testsuite/tests/cmm/should_run/T27722_cmm.cmm
    1
    +#include "Cmm.h"
    
    2
    +
    
    3
    +// The then-blocks of E1 and E2 are duplicates of the entry block. They are
    
    4
    +// written as braced if-bodies so that, like the entry block, they carry two
    
    5
    +// source notes (braces and statement): due to #27877 CBE would otherwise
    
    6
    +// never compare them to the entry block.
    
    7
    +//
    
    8
    +// The then-blocks get merged with each other in the first round of common
    
    9
    +// block elimination, E1 and E2 in the second, and only in the third does
    
    10
    +// the entry block match the surviving then-block.
    
    11
    +INFO_TABLE_RET (zork, RET_SMALL)
    
    12
    +    return ()
    
    13
    +{
    
    14
    +  bits64 x;
    
    15
    +  x = 1 :: bits64;
    
    16
    +  goto E1;
    
    17
    +E1:
    
    18
    +  if (x > 0 :: bits64) { x = 1 :: bits64; goto E2; } else { goto D; }
    
    19
    +E2:
    
    20
    +  if (x > 0 :: bits64) { x = 1 :: bits64; goto E2; } else { goto D; }
    
    21
    +D:
    
    22
    +  if (x > 1 :: bits64) goto E1;
    
    23
    +  goto E2;
    
    24
    +}

  • testsuite/tests/cmm/should_run/all.T
    ... ... @@ -60,3 +60,6 @@ test('T27447',
    60 60
          ],
    
    61 61
          multi_compile_and_run,
    
    62 62
          ['T27447', [('T27447_cmm.cmm', '')], ''])
    
    63
    +
    
    64
    +test('T27722', [req_cmm], multi_compile_and_run,
    
    65
    +     ['T27722', [('T27722_cmm.cmm', '')], '-O'])