Sven Tennie pushed to branch wip/supersven/riscv-ncg-fix-branch-signext at Glasgow Haskell Compiler / GHC

Commits:

5 changed files:

Changes:

  • changelog.d/T28050
    1
    +section: compiler
    
    2
    +synopsis: Fix the RISC-V NCG sign-extending subword branch operands in place, corrupting live values
    
    3
    +issues: #28050
    
    4
    +
    
    5
    +description: {
    
    6
    +  The signed subword path of ``genCondJump`` sign-extended its operands in
    
    7
    +  place. If an operand was a local that remained live after the branch, its
    
    8
    +  value was corrupted, violating the zero-extended subword invariant that the
    
    9
    +  RISC-V NCG relies on.
    
    10
    +
    
    11
    +  The operands are now sign-extended into fresh registers and the comparison is
    
    12
    +  performed at 64-bit width, leaving the source registers untouched.
    
    13
    +}

  • compiler/GHC/CmmToAsm/RV64/CodeGen.hs
    ... ... @@ -1451,21 +1451,23 @@ genCondJump bid expr = do
    1451 1451
                       `appOL` code_y
    
    1452 1452
                       `snocOL` annExpr expr (BCOND cmp x' y' (TBlock bid))
    
    1453 1453
     
    
    1454
    -          sbcond w cmp = do
    
    1454
    +          sbcond _w cmp = do
    
    1455 1455
                 -- compute both sides.
    
    1456 1456
                 (reg_x, format_x, code_x) <- getSomeReg x
    
    1457 1457
                 (reg_y, format_y, code_y) <- getSomeReg y
    
    1458
    -            let x' = OpReg w reg_x
    
    1459
    -                y' = OpReg w reg_y
    
    1460
    -            return $ case w of
    
    1461
    -              w
    
    1462
    -                | w `elem` [W8, W16, W32] ->
    
    1463
    -                    code_x
    
    1464
    -                      `appOL` signExtend (formatToWidth format_x) W64 reg_x reg_x
    
    1465
    -                      `appOL` code_y
    
    1466
    -                      `appOL` signExtend (formatToWidth format_y) W64 reg_y reg_y
    
    1467
    -                      `appOL` unitOL (annExpr expr (BCOND cmp x' y' (TBlock bid)))
    
    1468
    -              _ -> code_x `appOL` code_y `appOL` unitOL (annExpr expr (BCOND cmp x' y' (TBlock bid)))
    
    1458
    +            -- Sign-extend the operands into fresh registers rather than in
    
    1459
    +            -- place.  The source may be a local which is still live past the
    
    1460
    +            -- branch and in-place extension would violate the subword
    
    1461
    +            -- invariant documented in Note [Signed arithmetic on RISCV64].
    
    1462
    +            (reg_x', code_x') <- signExtendReg (formatToWidth format_x) W64 reg_x
    
    1463
    +            (reg_y', code_y') <- signExtendReg (formatToWidth format_y) W64 reg_y
    
    1464
    +            let x' = OpReg W64 reg_x'
    
    1465
    +                y' = OpReg W64 reg_y'
    
    1466
    +            return $ code_x
    
    1467
    +              `appOL` code_x'
    
    1468
    +              `appOL` code_y
    
    1469
    +              `appOL` code_y'
    
    1470
    +              `snocOL` annExpr expr (BCOND cmp x' y' (TBlock bid))
    
    1469 1471
     
    
    1470 1472
               fbcond w cmp = do
    
    1471 1473
                 -- ensure we get float regs
    

  • testsuite/tests/codeGen/should_run/T28050.hs
    1
    +{-# LANGUAGE MagicHash #-}
    
    2
    +
    
    3
    +-- Regression test for #28050.
    
    4
    +--
    
    5
    +-- A subword value must still be zero-extended after it has been tested by a
    
    6
    +-- conditional branch.  Correct output:
    
    7
    +--   (200,65480,4294967240)
    
    8
    +--   200
    
    9
    +
    
    10
    +module Main (main) where
    
    11
    +
    
    12
    +import Data.Int
    
    13
    +import Data.Word
    
    14
    +import GHC.Exts
    
    15
    +import GHC.Word
    
    16
    +
    
    17
    +f8 :: Int8 -> Word
    
    18
    +f8 i = if i < 0 then fromIntegral (fromIntegral i :: Word8) else 0
    
    19
    +{-# NOINLINE f8 #-}
    
    20
    +
    
    21
    +f16 :: Int16 -> Word
    
    22
    +f16 i = if i < 0 then fromIntegral (fromIntegral i :: Word16) else 0
    
    23
    +{-# NOINLINE f16 #-}
    
    24
    +
    
    25
    +f32 :: Int32 -> Word
    
    26
    +f32 i = if i < 0 then fromIntegral (fromIntegral i :: Word32) else 0
    
    27
    +{-# NOINLINE f32 #-}
    
    28
    +
    
    29
    +g8 :: Word8 -> Word
    
    30
    +g8 (W8# w) = case eqWord8# w (wordToWord8# 100##) of
    
    31
    +  1# -> 0
    
    32
    +  _  -> W# (word8ToWord# w)
    
    33
    +{-# NOINLINE g8 #-}
    
    34
    +
    
    35
    +main :: IO ()
    
    36
    +main = do
    
    37
    +  print (f8 (-56), f16 (-56), f32 (-56))
    
    38
    +  print (g8 200)

  • testsuite/tests/codeGen/should_run/T28050.stdout
    1
    +(200,65480,4294967240)
    
    2
    +200

  • testsuite/tests/codeGen/should_run/all.T
    ... ... @@ -305,3 +305,5 @@ test('T27533', [req_cmm, extra_ways(['optasm'])], multi_compile_and_run,
    305 305
     test('T27537', normal, compile_and_run, ['-O'])
    
    306 306
     
    
    307 307
     test('T27538', normal, compile_and_run, ['-O'])
    
    308
    +
    
    309
    +test('T28050', normal, compile_and_run, ['-O'])