Sven Tennie pushed to branch wip/supersven/fix-aarch64-mul2u-clobber at Glasgow Haskell Compiler / GHC

Commits:

4 changed files:

Changes:

  • changelog.d/fix-aarch64-mul2u-clobber
    1
    +section: codegen
    
    2
    +issues: #28223
    
    3
    +mrs: !17078
    
    4
    +synopsis:
    
    5
    +  Fix AArch64 unsigned MUL2 operand clobbering
    
    6
    +description: {
    
    7
    +  MO_U_Mul2 could clobber an input when computing the low word, producing an
    
    8
    +  incorrect high word. The signed MO_S_Mul2 case was already fixed in #27046;
    
    9
    +  this covers the unsigned operation.
    
    10
    +}

  • compiler/GHC/CmmToAsm/AArch64/CodeGen.hs
    ... ... @@ -2402,8 +2402,8 @@ genCCall target dest_regs arg_regs = do
    2402 2402
               -- Can't handle > 64 bit operands
    
    2403 2403
               | otherwise -> unsupported (MO_S_Mul2 w)
    
    2404 2404
         PrimTarget (MO_U_Mul2  w)
    
    2405
    -          -- The unsigned case is much simpler than the signed, all we need to
    
    2406
    -          -- do is the multiplication straight into the destination registers.
    
    2405
    +          -- The unsigned case is much simpler than the signed: we only need
    
    2406
    +          -- the high and low halves of the product, with no overflow flag.
    
    2407 2407
               | w == W64
    
    2408 2408
               , [src_a, src_b] <- arg_regs
    
    2409 2409
               , [dst_hi, dst_lo] <- dest_regs
    
    ... ... @@ -2413,11 +2413,19 @@ genCCall target dest_regs arg_regs = do
    2413 2413
     
    
    2414 2414
                   let lo = getRegisterReg platform (CmmLocal dst_lo)
    
    2415 2415
                       hi = getRegisterReg platform (CmmLocal dst_hi)
    
    2416
    +
    
    2417
    +              -- Generate a fresh virtual register for the low word computation.
    
    2418
    +              -- This avoids clobbering reg_a or reg_b in the first MUL instruction,
    
    2419
    +              -- which could for example happen if 'lo' and 'reg_a' are the same
    
    2420
    +              -- virtual register.
    
    2421
    +              tmp_lo <- getNewRegNat II64
    
    2422
    +
    
    2416 2423
                   return (
    
    2417 2424
                       code_x `appOL`
    
    2418 2425
                       code_y `snocOL`
    
    2419
    -                  MUL   II64 (OpReg W64 lo) (OpReg W64 reg_a) (OpReg W64 reg_b) `snocOL`
    
    2420
    -                  UMULH (OpReg W64 hi) (OpReg W64 reg_a) (OpReg W64 reg_b)
    
    2426
    +                  MUL   II64 (OpReg W64 tmp_lo) (OpReg W64 reg_a) (OpReg W64 reg_b) `snocOL`
    
    2427
    +                  UMULH (OpReg W64 hi) (OpReg W64 reg_a) (OpReg W64 reg_b) `snocOL`
    
    2428
    +                  MOV   (OpReg W64 lo) (OpReg W64 tmp_lo)
    
    2421 2429
                       )
    
    2422 2430
                 -- For sizes < platform width, we can just perform a multiply and shift
    
    2423 2431
                 -- Need to be careful to truncate the low half, but the upper half should be
    

  • testsuite/tests/codeGen/should_run/T27046.hs
    ... ... @@ -8,18 +8,25 @@ import Control.Monad
    8 8
     import Data.Bits
    
    9 9
       ( shiftL )
    
    10 10
     import GHC.Exts
    
    11
    -  ( Int64# )
    
    11
    +  ( Int64#, Word64# )
    
    12 12
     import GHC.Int
    
    13 13
       ( Int64(..) )
    
    14
    +import GHC.Word
    
    15
    +  ( Word64(..) )
    
    14 16
     
    
    15 17
     foreign import prim "test_mul2_clobber"
    
    16 18
       test_mul2_clobber :: Int64# -> Int64# -> Int64#
    
    17 19
     
    
    20
    +foreign import prim "test_mul2u_clobber"
    
    21
    +  test_mul2u_clobber :: Word64# -> Word64# -> Word64#
    
    22
    +
    
    18 23
     main :: IO ()
    
    19 24
     main = do
    
    20 25
       let
    
    21 26
         I64# x = 1 `shiftL` 32
    
    27
    +    W64# xw = 1 `shiftL` 32
    
    22 28
         hi = I64# $ test_mul2_clobber x x
    
    29
    +    hiw = W64# $ test_mul2u_clobber xw xw
    
    23 30
     
    
    24 31
       unless ( hi == 1 ) $
    
    25 32
         error $ unlines
    
    ... ... @@ -27,3 +34,10 @@ main = do
    27 34
           , "Expected high word: 1"
    
    28 35
           , "  Actual high word: " ++ show hi
    
    29 36
           ]
    
    37
    +
    
    38
    +  unless ( hiw == 1 ) $
    
    39
    +    error $ unlines
    
    40
    +      [ "Incorrect result for unsigned Mul2 operation."
    
    41
    +      , "Expected high word: 1"
    
    42
    +      , "  Actual high word: " ++ show hiw
    
    43
    +      ]

  • testsuite/tests/codeGen/should_run/T27046_cmm.cmm
    ... ... @@ -11,3 +11,14 @@ test_mul2_clobber (bits64 x, bits64 y)
    11 11
     
    
    12 12
         return (hi);
    
    13 13
     }
    
    14
    +
    
    15
    +test_mul2u_clobber (bits64 x, bits64 y)
    
    16
    +{
    
    17
    +    bits64 hi;
    
    18
    +
    
    19
    +    // Deliberately alias the destination 'lo' with the source 'x'
    
    20
    +    // This forces the NCG to use the same virtual register for both.
    
    21
    +    (hi, x) = prim %mul2u_64(x, y);
    
    22
    +
    
    23
    +    return (hi);
    
    24
    +}