[Git][ghc/ghc][wip/supersven/riscv-ncg-mul2] RISC-V NCG: Implement 64-bit MO_S_Mul2 and MO_U_Mul2 (#27733)
Sven Tennie pushed to branch wip/supersven/riscv-ncg-mul2 at Glasgow Haskell Compiler / GHC Commits: 671c1a6d by Sven Tennie at 2026-11-28T19:20:27+01:00 RISC-V NCG: Implement 64-bit MO_S_Mul2 and MO_U_Mul2 (#27733) Lower the 64-bit signed and unsigned double-word multiply Cmm operations to native instructions: %mul2_64 (MO_S_Mul2 W64) now emits MUL followed by MULH, and %mul2u_64 (MO_U_Mul2 W64) emits MUL followed by MULHU. Sub-word widths remain unsupported. StgToCmm only emits word-width Mul2 operations, so these can only be requested from hand-written Cmm. Enabling the 64-bit operations also makes timesInt2# and timesWord2# use the native instructions rather than the generic half-word multiply expansion. Tests: T9430 extended; T27046 extended for unsigned aliasing and now passes on RISC-V NCG - - - - - 8 changed files: - + changelog.d/T27733 - compiler/GHC/CmmToAsm/RV64/CodeGen.hs - compiler/GHC/CmmToAsm/RV64/Instr.hs - compiler/GHC/CmmToAsm/RV64/Ppr.hs - compiler/GHC/Driver/Config/StgToCmm.hs - testsuite/tests/codeGen/should_run/T27046.hs - testsuite/tests/codeGen/should_run/T27046_cmm.cmm - testsuite/tests/primops/should_run/T9430.hs Changes: ===================================== changelog.d/T27733 ===================================== @@ -0,0 +1,13 @@ +section: codegen +synopsis: RISC-V NCG: support 64-bit MO_S_Mul2 and MO_U_Mul2 +issues: #27733 +mrs: !17067 + +description: { + The RISC-V NCG now lowers the 64-bit Cmm operations ``MO_S_Mul2 W64`` + (``%mul2_64``) and ``MO_U_Mul2 W64`` (``%mul2u_64``) to ``MUL``/``MULH`` and + ``MUL``/``MULHU``, respectively. Sub-word widths remain unsupported as they + aren't emitted for Haskell code. + + ``timesInt2#`` and ``timesWord2#`` now use these native instructions. +} ===================================== compiler/GHC/CmmToAsm/RV64/CodeGen.hs ===================================== @@ -1781,7 +1781,37 @@ genCCall (PrimTarget mop) dest_regs arg_regs = do MO_UF_Conv w -> mkCCall (word2FloatLabel w) -- Optional MachOps -- These are enabled/disabled by backend flags: GHC.StgToCmm.Config - MO_S_Mul2 _w -> unsupported mop + MO_S_Mul2 w + | w == W64 + , [src_a, src_b] <- arg_regs + , [dst_needed, dst_hi, dst_lo] <- dest_regs + -> do + platform <- getPlatform + (reg_a, _format_x, code_x) <- getSomeReg src_a + (reg_b, _format_y, code_y) <- getSomeReg src_b + + let lo = getRegisterReg platform (CmmLocal dst_lo) + hi = getRegisterReg platform (CmmLocal dst_hi) + nd = getRegisterReg platform (CmmLocal dst_needed) + + -- Use a fresh register for the low half. If dst_lo coincided with + -- one of the sources, computing it first would clobber that source + -- before MULH has read it. + tmp_lo <- getNewRegNat II64 + + pure $ + code_x `appOL` code_y + `snocOL` MUL (OpReg W64 tmp_lo) (OpReg W64 reg_a) (OpReg W64 reg_b) + `snocOL` MULH (OpReg W64 hi) (OpReg W64 reg_a) (OpReg W64 reg_b) + `snocOL` MOV (OpReg W64 lo) (OpReg W64 tmp_lo) + -- The product fits in W64 iff the high half hi is the sign + -- extension of the low half lo, i.e. + -- hi == asr(lo, widthInBits W64 - 1). + -- nd = (hi /= asr(lo, widthInBits W64 - 1)) ? 1 : 0. + `snocOL` SRA (OpReg W64 tmp_lo) (OpReg W64 tmp_lo) + (OpImm (ImmInt (widthInBits W64 - 1))) + `snocOL` CSET (OpReg W64 nd) (OpReg W64 hi) (OpReg W64 tmp_lo) NE + MO_S_Mul2 _ -> unsupportedMul2 MO_S_QuotRem _w -> unsupported mop MO_U_QuotRem _w -> unsupported mop MO_U_QuotRem2 _w -> unsupported mop @@ -1790,7 +1820,29 @@ genCCall (PrimTarget mop) dest_regs arg_regs = do MO_SubWordC _w -> unsupported mop MO_AddIntC _w -> unsupported mop MO_SubIntC _w -> unsupported mop - MO_U_Mul2 _w -> unsupported mop + MO_U_Mul2 w + | w == W64 + , [src_a, src_b] <- arg_regs + , [dst_hi, dst_lo] <- dest_regs + -> do + platform <- getPlatform + (reg_a, _format_x, code_x) <- getSomeReg src_a + (reg_b, _format_y, code_y) <- getSomeReg src_b + + let lo = getRegisterReg platform (CmmLocal dst_lo) + hi = getRegisterReg platform (CmmLocal dst_hi) + + -- Use a fresh register for the low half. If dst_lo coincided with + -- one of the sources, computing it first would clobber that source + -- before MULHU has read it. + tmp_lo <- getNewRegNat II64 + + pure $ + code_x `appOL` code_y + `snocOL` MUL (OpReg W64 tmp_lo) (OpReg W64 reg_a) (OpReg W64 reg_b) + `snocOL` MULHU (OpReg W64 hi) (OpReg W64 reg_a) (OpReg W64 reg_b) + `snocOL` MOV (OpReg W64 lo) (OpReg W64 tmp_lo) + MO_U_Mul2 _ -> unsupportedMul2 MO_VS_Quot {} -> unsupported mop MO_VS_Rem {} -> unsupported mop MO_VU_Quot {} -> unsupported mop @@ -1900,6 +1952,15 @@ genCCall (PrimTarget mop) dest_regs arg_regs = do ++ show mop ++ " not supported here" ) + + unsupportedMul2 :: b + unsupportedMul2 = + panic + ( "outOfLineCmmOp: " ++ show mop + ++ " with " ++ show (length arg_regs) ++ " arguments and " + ++ show (length dest_regs) ++ " results is not supported on RISC-V" + ) + mkCCall :: FastString -> NatM InstrBlock mkCCall name = do config <- getConfig @@ -2119,6 +2180,7 @@ makeFarBranches {- only used when debugging -} _platform statics basic_blocks = ADD {} -> 1 MUL {} -> 1 MULH {} -> 1 + MULHU {} -> 1 NEG {} -> 1 DIV {} -> 1 REM {} -> 1 ===================================== compiler/GHC/CmmToAsm/RV64/Instr.hs ===================================== @@ -81,6 +81,7 @@ regUsageOfInstr platform instr = runUsage platform $ case instr of MUL dst src1 src2 -> readOp src1 <.> readOp src2 <.> writeOp dst NEG dst src -> readOp src <.> writeOp dst MULH dst src1 src2 -> readOp src1 <.> readOp src2 <.> writeOp dst + MULHU dst src1 src2 -> readOp src1 <.> readOp src2 <.> writeOp dst DIV dst src1 src2 -> readOp src1 <.> readOp src2 <.> writeOp dst REM dst src1 src2 -> readOp src1 <.> readOp src2 <.> writeOp dst REMU dst src1 src2 -> readOp src1 <.> readOp src2 <.> writeOp dst @@ -194,6 +195,7 @@ patchRegsOfInstr instr env = case instr of MUL o1 o2 o3 -> MUL (patchOp o1) (patchOp o2) (patchOp o3) NEG o1 o2 -> NEG (patchOp o1) (patchOp o2) MULH o1 o2 o3 -> MULH (patchOp o1) (patchOp o2) (patchOp o3) + MULHU o1 o2 o3 -> MULHU (patchOp o1) (patchOp o2) (patchOp o3) DIV o1 o2 o3 -> DIV (patchOp o1) (patchOp o2) (patchOp o3) REM o1 o2 o3 -> REM (patchOp o1) (patchOp o2) (patchOp o3) REMU o1 o2 o3 -> REMU (patchOp o1) (patchOp o2) (patchOp o3) @@ -587,6 +589,10 @@ data Instr -- -- E.g. for a multiplication with 64bits width: @rd = (rs1 * rs2) >> 64@. MULH Operand Operand Operand + | -- | High part of an unsigned multiplication that doesn't fit into 64bits (integer only) + -- + -- E.g. for a multiplication with 64bits width: @rd = (rs1 * rs2) >> 64@. + MULHU Operand Operand Operand | -- | Unsigned division (integer only) -- -- @rd = |rn ÷ rm|@ @@ -685,6 +691,7 @@ instrCon i = REM {} -> "REM" REMU {} -> "REMU" MULH {} -> "MULH" + MULHU {} -> "MULHU" SUB {} -> "SUB" DIVU {} -> "DIVU" AND {} -> "AND" ===================================== compiler/GHC/CmmToAsm/RV64/Ppr.hs ===================================== @@ -491,6 +491,7 @@ pprInstr platform instr = case instr of | isFloatOp o1 && isFloatOp o2 && isFloatOp o3 -> op3 (text "\tfmul." <> if isSingleOp o1 then text "s" else text "d") o1 o2 o3 | otherwise -> op3 (text "\tmul") o1 o2 o3 MULH o1 o2 o3 -> op3 (text "\tmulh") o1 o2 o3 + MULHU o1 o2 o3 -> op3 (text "\tmulhu") o1 o2 o3 NEG o1 o2 | isFloatOp o1 && isFloatOp o2 && isSingleOp o2 -> op2 (text "\tfneg.s") o1 o2 NEG o1 o2 | isFloatOp o1 && isFloatOp o2 && isDoubleOp o2 -> op2 (text "\tfneg.d") o1 o2 NEG o1 o2 -> op2 (text "\tneg") o1 o2 ===================================== compiler/GHC/Driver/Config/StgToCmm.hs ===================================== @@ -80,8 +80,8 @@ initStgToCmmConfig dflags mod = StgToCmmConfig | otherwise -> const True - , stgToCmmAllowIntMul2Instr = (ncg && (x86ish || aarch64)) || llvm - , stgToCmmAllowWordMul2Instr = (ncg && (x86ish || ppc || aarch64)) || llvm + , stgToCmmAllowIntMul2Instr = (ncg && (x86ish || aarch64 || riscv64)) || llvm + , stgToCmmAllowWordMul2Instr = (ncg && (x86ish || ppc || aarch64 || riscv64)) || llvm , stgToCmmAllowIntWord64X2MinMax = (ncg && (sse4_2 || aarch64)) || llvm -- SIMD flags , stgToCmmVecInstrsErr = vec_err @@ -102,6 +102,9 @@ initStgToCmmConfig dflags mod = StgToCmmConfig aarch64 = case platformArch platform of ArchAArch64 -> True _ -> False + riscv64 = case platformArch platform of + ArchRISCV64 -> True + _ -> False x86ish = case platformArch platform of ArchX86 -> True ArchX86_64 -> True ===================================== 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); +} ===================================== testsuite/tests/primops/should_run/T9430.hs ===================================== @@ -23,6 +23,21 @@ checkI (expX, expY) op (I# a) (I# b) = Just $ "Expected " ++ show expX ++ " and " ++ show expY ++ " but got " ++ show (I# x) ++ " and " ++ show (I# y) +checkI2 + :: (Int, Int, Int) -- ^ expected results + -> (Int# -> Int# -> (# Int#, Int#, Int# #)) -- ^ primop + -> Int -- ^ first argument + -> Int -- ^ second argument + -> Maybe String -- ^ maybe error +checkI2 (expN, expH, expL) op (I# a) (I# b) = + case op a b of + (# n, h, l #) + | I# n == expN && I# h == expH && I# l == expL -> Nothing + | otherwise -> + Just $ + "Expected " ++ show (expN, expH, expL) + ++ " but got " ++ show (I# n, I# h, I# l) + checkW :: (Word, Word) -- ^ expected results -> (Word# -> Word# -> (# Word#, Word# #)) -- ^ primop @@ -59,6 +74,18 @@ check :: String -> Maybe String -> IO () check s (Just err) = error $ "Error for " ++ s ++ ": " ++ err check _ Nothing = return () +expectedTimesInt2 :: Int -> Int -> (Int, Int, Int) +expectedTimesInt2 x y = + let p = toInteger x * toInteger y + radix = (2 :: Integer) ^ wordWidth + hi = p `div` radix + lo = p - hi * radix + needed + | p < toInteger (minBound :: Int) = 1 + | p > toInteger (maxBound :: Int) = 1 + | otherwise = 0 + in (needed, fromInteger hi, fromInteger lo) + main :: IO () main = do -- First something trivial @@ -112,6 +139,33 @@ main = do checkW (2, maxBound - 2) timesWord2# maxBound 3 check "timesWord2# 3 maxBound" $ checkW (2, maxBound - 2) timesWord2# 3 maxBound + check "timesWord2# maxBound maxBound" $ + checkW (maxBound - 1, 1) timesWord2# maxBound maxBound + check "timesWord2# (2^(N-1)) (2^(N-1))" $ + checkW (2 ^ (wordWidth-2), 0) timesWord2# (2 ^ (wordWidth-1)) (2 ^ (wordWidth-1)) + + -- timesInt2# + let checkMul2 x y = + check ("timesInt2# " ++ show x ++ " " ++ show y) $ + checkI2 (expectedTimesInt2 x y) timesInt2# x y + checkMul2 0 0 + checkMul2 0 maxBound + checkMul2 1 (-1) + checkMul2 (-1) 1 + checkMul2 (-1) (-1) + checkMul2 3 5 + checkMul2 (-3) 5 + checkMul2 3 (-5) + checkMul2 (-3) (-5) + checkMul2 minBound 1 + checkMul2 1 minBound + checkMul2 minBound (-1) + checkMul2 (-1) minBound + checkMul2 maxBound 1 + checkMul2 maxBound (-1) + checkMul2 (-1) maxBound + checkMul2 maxBound maxBound + checkMul2 minBound minBound check "quotRemWord2# 0 0 1" $ checkW2 (0, 0) quotRemWord2# 0 0 1 check "quotRemWord2# 0 4 2" $ checkW2 (2, 0) quotRemWord2# 0 4 2 View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/671c1a6df87c3e7122b40747dc9905a1... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/671c1a6df87c3e7122b40747dc9905a1... 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)