[Git][ghc/ghc][wip/supersven/fix-aarch64-mul2u-clobber] AArch64 NCG: fix unsigned Mul2 operand clobbering (#28223)
Sven Tennie pushed to branch wip/supersven/fix-aarch64-mul2u-clobber at Glasgow Haskell Compiler / GHC Commits: 8debc9ed by Sven Tennie at 2026-11-29T17:00:32+00:00 AArch64 NCG: fix unsigned Mul2 operand clobbering (#28223) MO_U_Mul2 computed the low word into the destination register before UMULH read the sources, producing a wrong high word when the destination aliased an input. Compute the low word in a temporary instead, matching the signed MO_S_Mul2 lowering. Extend T27046 to cover the unsigned case. - - - - - 4 changed files: - + changelog.d/fix-aarch64-mul2u-clobber - compiler/GHC/CmmToAsm/AArch64/CodeGen.hs - testsuite/tests/codeGen/should_run/T27046.hs - testsuite/tests/codeGen/should_run/T27046_cmm.cmm Changes: ===================================== changelog.d/fix-aarch64-mul2u-clobber ===================================== @@ -0,0 +1,10 @@ +section: codegen +issues: #28223 +mrs: !17078 +synopsis: + Fix AArch64 unsigned MUL2 operand clobbering +description: { + MO_U_Mul2 could clobber an input when computing the low word, producing an + incorrect high word. The signed MO_S_Mul2 case was already fixed in #27046; + this covers the unsigned operation. +} ===================================== compiler/GHC/CmmToAsm/AArch64/CodeGen.hs ===================================== @@ -2402,8 +2402,8 @@ genCCall target dest_regs arg_regs = do -- Can't handle > 64 bit operands | otherwise -> unsupported (MO_S_Mul2 w) PrimTarget (MO_U_Mul2 w) - -- The unsigned case is much simpler than the signed, all we need to - -- do is the multiplication straight into the destination registers. + -- The unsigned case is much simpler than the signed: we only need + -- the high and low halves of the product, with no overflow flag. | w == W64 , [src_a, src_b] <- arg_regs , [dst_hi, dst_lo] <- dest_regs @@ -2413,11 +2413,19 @@ genCCall target dest_regs arg_regs = do let lo = getRegisterReg platform (CmmLocal dst_lo) hi = getRegisterReg platform (CmmLocal dst_hi) + + -- Generate a fresh virtual register for the low word computation. + -- This avoids clobbering reg_a or reg_b in the first MUL instruction, + -- which could for example happen if 'lo' and 'reg_a' are the same + -- virtual register. + tmp_lo <- getNewRegNat II64 + return ( code_x `appOL` code_y `snocOL` - MUL II64 (OpReg W64 lo) (OpReg W64 reg_a) (OpReg W64 reg_b) `snocOL` - UMULH (OpReg W64 hi) (OpReg W64 reg_a) (OpReg W64 reg_b) + MUL II64 (OpReg W64 tmp_lo) (OpReg W64 reg_a) (OpReg W64 reg_b) `snocOL` + UMULH (OpReg W64 hi) (OpReg W64 reg_a) (OpReg W64 reg_b) `snocOL` + MOV (OpReg W64 lo) (OpReg W64 tmp_lo) ) -- For sizes < platform width, we can just perform a multiply and shift -- 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 import Data.Bits ( shiftL ) import GHC.Exts - ( Int64# ) + ( Int64#, Word64# ) import GHC.Int ( Int64(..) ) +import GHC.Word + ( Word64(..) ) foreign import prim "test_mul2_clobber" test_mul2_clobber :: Int64# -> Int64# -> Int64# +foreign import prim "test_mul2u_clobber" + test_mul2u_clobber :: Word64# -> Word64# -> Word64# + main :: IO () main = do let I64# x = 1 `shiftL` 32 + W64# xw = 1 `shiftL` 32 hi = I64# $ test_mul2_clobber x x + hiw = W64# $ test_mul2u_clobber xw xw unless ( hi == 1 ) $ error $ unlines @@ -27,3 +34,10 @@ main = do , "Expected high word: 1" , " Actual high word: " ++ show hi ] + + unless ( hiw == 1 ) $ + error $ unlines + [ "Incorrect result for unsigned Mul2 operation." + , "Expected high word: 1" + , " Actual high word: " ++ show hiw + ] ===================================== testsuite/tests/codeGen/should_run/T27046_cmm.cmm ===================================== @@ -11,3 +11,14 @@ test_mul2_clobber (bits64 x, bits64 y) return (hi); } + +test_mul2u_clobber (bits64 x, bits64 y) +{ + bits64 hi; + + // Deliberately alias the destination 'lo' with the source 'x' + // This forces the NCG to use the same virtual register for both. + (hi, x) = prim %mul2u_64(x, y); + + return (hi); +} View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/8debc9edd4096976a714bf18281a253b... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/8debc9edd4096976a714bf18281a253b... 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)
-
Sven Tennie (@supersven)