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
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:
| 1 | +section: codegen
|
|
| 2 | +synopsis: RISC-V NCG: support 64-bit MO_S_Mul2 and MO_U_Mul2
|
|
| 3 | +issues: #27733
|
|
| 4 | +mrs: !17067
|
|
| 5 | + |
|
| 6 | +description: {
|
|
| 7 | + The RISC-V NCG now lowers the 64-bit Cmm operations ``MO_S_Mul2 W64``
|
|
| 8 | + (``%mul2_64``) and ``MO_U_Mul2 W64`` (``%mul2u_64``) to ``MUL``/``MULH`` and
|
|
| 9 | + ``MUL``/``MULHU``, respectively. Sub-word widths remain unsupported as they
|
|
| 10 | + aren't emitted for Haskell code.
|
|
| 11 | + |
|
| 12 | + ``timesInt2#`` and ``timesWord2#`` now use these native instructions.
|
|
| 13 | +} |
| ... | ... | @@ -1781,7 +1781,37 @@ genCCall (PrimTarget mop) dest_regs arg_regs = do |
| 1781 | 1781 | MO_UF_Conv w -> mkCCall (word2FloatLabel w)
|
| 1782 | 1782 | -- Optional MachOps
|
| 1783 | 1783 | -- These are enabled/disabled by backend flags: GHC.StgToCmm.Config
|
| 1784 | - MO_S_Mul2 _w -> unsupported mop
|
|
| 1784 | + MO_S_Mul2 w
|
|
| 1785 | + | w == W64
|
|
| 1786 | + , [src_a, src_b] <- arg_regs
|
|
| 1787 | + , [dst_needed, dst_hi, dst_lo] <- dest_regs
|
|
| 1788 | + -> do
|
|
| 1789 | + platform <- getPlatform
|
|
| 1790 | + (reg_a, _format_x, code_x) <- getSomeReg src_a
|
|
| 1791 | + (reg_b, _format_y, code_y) <- getSomeReg src_b
|
|
| 1792 | + |
|
| 1793 | + let lo = getRegisterReg platform (CmmLocal dst_lo)
|
|
| 1794 | + hi = getRegisterReg platform (CmmLocal dst_hi)
|
|
| 1795 | + nd = getRegisterReg platform (CmmLocal dst_needed)
|
|
| 1796 | + |
|
| 1797 | + -- Use a fresh register for the low half. If dst_lo coincided with
|
|
| 1798 | + -- one of the sources, computing it first would clobber that source
|
|
| 1799 | + -- before MULH has read it.
|
|
| 1800 | + tmp_lo <- getNewRegNat II64
|
|
| 1801 | + |
|
| 1802 | + pure $
|
|
| 1803 | + code_x `appOL` code_y
|
|
| 1804 | + `snocOL` MUL (OpReg W64 tmp_lo) (OpReg W64 reg_a) (OpReg W64 reg_b)
|
|
| 1805 | + `snocOL` MULH (OpReg W64 hi) (OpReg W64 reg_a) (OpReg W64 reg_b)
|
|
| 1806 | + `snocOL` MOV (OpReg W64 lo) (OpReg W64 tmp_lo)
|
|
| 1807 | + -- The product fits in W64 iff the high half hi is the sign
|
|
| 1808 | + -- extension of the low half lo, i.e.
|
|
| 1809 | + -- hi == asr(lo, widthInBits W64 - 1).
|
|
| 1810 | + -- nd = (hi /= asr(lo, widthInBits W64 - 1)) ? 1 : 0.
|
|
| 1811 | + `snocOL` SRA (OpReg W64 tmp_lo) (OpReg W64 tmp_lo)
|
|
| 1812 | + (OpImm (ImmInt (widthInBits W64 - 1)))
|
|
| 1813 | + `snocOL` CSET (OpReg W64 nd) (OpReg W64 hi) (OpReg W64 tmp_lo) NE
|
|
| 1814 | + MO_S_Mul2 _ -> unsupportedMul2
|
|
| 1785 | 1815 | MO_S_QuotRem _w -> unsupported mop
|
| 1786 | 1816 | MO_U_QuotRem _w -> unsupported mop
|
| 1787 | 1817 | MO_U_QuotRem2 _w -> unsupported mop
|
| ... | ... | @@ -1790,7 +1820,29 @@ genCCall (PrimTarget mop) dest_regs arg_regs = do |
| 1790 | 1820 | MO_SubWordC _w -> unsupported mop
|
| 1791 | 1821 | MO_AddIntC _w -> unsupported mop
|
| 1792 | 1822 | MO_SubIntC _w -> unsupported mop
|
| 1793 | - MO_U_Mul2 _w -> unsupported mop
|
|
| 1823 | + MO_U_Mul2 w
|
|
| 1824 | + | w == W64
|
|
| 1825 | + , [src_a, src_b] <- arg_regs
|
|
| 1826 | + , [dst_hi, dst_lo] <- dest_regs
|
|
| 1827 | + -> do
|
|
| 1828 | + platform <- getPlatform
|
|
| 1829 | + (reg_a, _format_x, code_x) <- getSomeReg src_a
|
|
| 1830 | + (reg_b, _format_y, code_y) <- getSomeReg src_b
|
|
| 1831 | + |
|
| 1832 | + let lo = getRegisterReg platform (CmmLocal dst_lo)
|
|
| 1833 | + hi = getRegisterReg platform (CmmLocal dst_hi)
|
|
| 1834 | + |
|
| 1835 | + -- Use a fresh register for the low half. If dst_lo coincided with
|
|
| 1836 | + -- one of the sources, computing it first would clobber that source
|
|
| 1837 | + -- before MULHU has read it.
|
|
| 1838 | + tmp_lo <- getNewRegNat II64
|
|
| 1839 | + |
|
| 1840 | + pure $
|
|
| 1841 | + code_x `appOL` code_y
|
|
| 1842 | + `snocOL` MUL (OpReg W64 tmp_lo) (OpReg W64 reg_a) (OpReg W64 reg_b)
|
|
| 1843 | + `snocOL` MULHU (OpReg W64 hi) (OpReg W64 reg_a) (OpReg W64 reg_b)
|
|
| 1844 | + `snocOL` MOV (OpReg W64 lo) (OpReg W64 tmp_lo)
|
|
| 1845 | + MO_U_Mul2 _ -> unsupportedMul2
|
|
| 1794 | 1846 | MO_VS_Quot {} -> unsupported mop
|
| 1795 | 1847 | MO_VS_Rem {} -> unsupported mop
|
| 1796 | 1848 | MO_VU_Quot {} -> unsupported mop
|
| ... | ... | @@ -1900,6 +1952,15 @@ genCCall (PrimTarget mop) dest_regs arg_regs = do |
| 1900 | 1952 | ++ show mop
|
| 1901 | 1953 | ++ " not supported here"
|
| 1902 | 1954 | )
|
| 1955 | + |
|
| 1956 | + unsupportedMul2 :: b
|
|
| 1957 | + unsupportedMul2 =
|
|
| 1958 | + panic
|
|
| 1959 | + ( "outOfLineCmmOp: " ++ show mop
|
|
| 1960 | + ++ " with " ++ show (length arg_regs) ++ " arguments and "
|
|
| 1961 | + ++ show (length dest_regs) ++ " results is not supported on RISC-V"
|
|
| 1962 | + )
|
|
| 1963 | + |
|
| 1903 | 1964 | mkCCall :: FastString -> NatM InstrBlock
|
| 1904 | 1965 | mkCCall name = do
|
| 1905 | 1966 | config <- getConfig
|
| ... | ... | @@ -2119,6 +2180,7 @@ makeFarBranches {- only used when debugging -} _platform statics basic_blocks = |
| 2119 | 2180 | ADD {} -> 1
|
| 2120 | 2181 | MUL {} -> 1
|
| 2121 | 2182 | MULH {} -> 1
|
| 2183 | + MULHU {} -> 1
|
|
| 2122 | 2184 | NEG {} -> 1
|
| 2123 | 2185 | DIV {} -> 1
|
| 2124 | 2186 | REM {} -> 1
|
| ... | ... | @@ -81,6 +81,7 @@ regUsageOfInstr platform instr = runUsage platform $ case instr of |
| 81 | 81 | MUL dst src1 src2 -> readOp src1 <.> readOp src2 <.> writeOp dst
|
| 82 | 82 | NEG dst src -> readOp src <.> writeOp dst
|
| 83 | 83 | MULH dst src1 src2 -> readOp src1 <.> readOp src2 <.> writeOp dst
|
| 84 | + MULHU dst src1 src2 -> readOp src1 <.> readOp src2 <.> writeOp dst
|
|
| 84 | 85 | DIV dst src1 src2 -> readOp src1 <.> readOp src2 <.> writeOp dst
|
| 85 | 86 | REM dst src1 src2 -> readOp src1 <.> readOp src2 <.> writeOp dst
|
| 86 | 87 | REMU dst src1 src2 -> readOp src1 <.> readOp src2 <.> writeOp dst
|
| ... | ... | @@ -194,6 +195,7 @@ patchRegsOfInstr instr env = case instr of |
| 194 | 195 | MUL o1 o2 o3 -> MUL (patchOp o1) (patchOp o2) (patchOp o3)
|
| 195 | 196 | NEG o1 o2 -> NEG (patchOp o1) (patchOp o2)
|
| 196 | 197 | MULH o1 o2 o3 -> MULH (patchOp o1) (patchOp o2) (patchOp o3)
|
| 198 | + MULHU o1 o2 o3 -> MULHU (patchOp o1) (patchOp o2) (patchOp o3)
|
|
| 197 | 199 | DIV o1 o2 o3 -> DIV (patchOp o1) (patchOp o2) (patchOp o3)
|
| 198 | 200 | REM o1 o2 o3 -> REM (patchOp o1) (patchOp o2) (patchOp o3)
|
| 199 | 201 | REMU o1 o2 o3 -> REMU (patchOp o1) (patchOp o2) (patchOp o3)
|
| ... | ... | @@ -587,6 +589,10 @@ data Instr |
| 587 | 589 | --
|
| 588 | 590 | -- E.g. for a multiplication with 64bits width: @rd = (rs1 * rs2) >> 64@.
|
| 589 | 591 | MULH Operand Operand Operand
|
| 592 | + | -- | High part of an unsigned multiplication that doesn't fit into 64bits (integer only)
|
|
| 593 | + --
|
|
| 594 | + -- E.g. for a multiplication with 64bits width: @rd = (rs1 * rs2) >> 64@.
|
|
| 595 | + MULHU Operand Operand Operand
|
|
| 590 | 596 | | -- | Unsigned division (integer only)
|
| 591 | 597 | --
|
| 592 | 598 | -- @rd = |rn ÷ rm|@
|
| ... | ... | @@ -685,6 +691,7 @@ instrCon i = |
| 685 | 691 | REM {} -> "REM"
|
| 686 | 692 | REMU {} -> "REMU"
|
| 687 | 693 | MULH {} -> "MULH"
|
| 694 | + MULHU {} -> "MULHU"
|
|
| 688 | 695 | SUB {} -> "SUB"
|
| 689 | 696 | DIVU {} -> "DIVU"
|
| 690 | 697 | AND {} -> "AND"
|
| ... | ... | @@ -491,6 +491,7 @@ pprInstr platform instr = case instr of |
| 491 | 491 | | isFloatOp o1 && isFloatOp o2 && isFloatOp o3 -> op3 (text "\tfmul." <> if isSingleOp o1 then text "s" else text "d") o1 o2 o3
|
| 492 | 492 | | otherwise -> op3 (text "\tmul") o1 o2 o3
|
| 493 | 493 | MULH o1 o2 o3 -> op3 (text "\tmulh") o1 o2 o3
|
| 494 | + MULHU o1 o2 o3 -> op3 (text "\tmulhu") o1 o2 o3
|
|
| 494 | 495 | NEG o1 o2 | isFloatOp o1 && isFloatOp o2 && isSingleOp o2 -> op2 (text "\tfneg.s") o1 o2
|
| 495 | 496 | NEG o1 o2 | isFloatOp o1 && isFloatOp o2 && isDoubleOp o2 -> op2 (text "\tfneg.d") o1 o2
|
| 496 | 497 | NEG o1 o2 -> op2 (text "\tneg") o1 o2
|
| ... | ... | @@ -80,8 +80,8 @@ initStgToCmmConfig dflags mod = StgToCmmConfig |
| 80 | 80 | | otherwise
|
| 81 | 81 | -> const True
|
| 82 | 82 | |
| 83 | - , stgToCmmAllowIntMul2Instr = (ncg && (x86ish || aarch64)) || llvm
|
|
| 84 | - , stgToCmmAllowWordMul2Instr = (ncg && (x86ish || ppc || aarch64)) || llvm
|
|
| 83 | + , stgToCmmAllowIntMul2Instr = (ncg && (x86ish || aarch64 || riscv64)) || llvm
|
|
| 84 | + , stgToCmmAllowWordMul2Instr = (ncg && (x86ish || ppc || aarch64 || riscv64)) || llvm
|
|
| 85 | 85 | , stgToCmmAllowIntWord64X2MinMax = (ncg && (sse4_2 || aarch64)) || llvm
|
| 86 | 86 | -- SIMD flags
|
| 87 | 87 | , stgToCmmVecInstrsErr = vec_err
|
| ... | ... | @@ -102,6 +102,9 @@ initStgToCmmConfig dflags mod = StgToCmmConfig |
| 102 | 102 | aarch64 = case platformArch platform of
|
| 103 | 103 | ArchAArch64 -> True
|
| 104 | 104 | _ -> False
|
| 105 | + riscv64 = case platformArch platform of
|
|
| 106 | + ArchRISCV64 -> True
|
|
| 107 | + _ -> False
|
|
| 105 | 108 | x86ish = case platformArch platform of
|
| 106 | 109 | ArchX86 -> True
|
| 107 | 110 | ArchX86_64 -> True
|
| ... | ... | @@ -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 | + ] |
| ... | ... | @@ -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 | +} |
| ... | ... | @@ -23,6 +23,21 @@ checkI (expX, expY) op (I# a) (I# b) = |
| 23 | 23 | Just $
|
| 24 | 24 | "Expected " ++ show expX ++ " and " ++ show expY
|
| 25 | 25 | ++ " but got " ++ show (I# x) ++ " and " ++ show (I# y)
|
| 26 | +checkI2
|
|
| 27 | + :: (Int, Int, Int) -- ^ expected results
|
|
| 28 | + -> (Int# -> Int# -> (# Int#, Int#, Int# #)) -- ^ primop
|
|
| 29 | + -> Int -- ^ first argument
|
|
| 30 | + -> Int -- ^ second argument
|
|
| 31 | + -> Maybe String -- ^ maybe error
|
|
| 32 | +checkI2 (expN, expH, expL) op (I# a) (I# b) =
|
|
| 33 | + case op a b of
|
|
| 34 | + (# n, h, l #)
|
|
| 35 | + | I# n == expN && I# h == expH && I# l == expL -> Nothing
|
|
| 36 | + | otherwise ->
|
|
| 37 | + Just $
|
|
| 38 | + "Expected " ++ show (expN, expH, expL)
|
|
| 39 | + ++ " but got " ++ show (I# n, I# h, I# l)
|
|
| 40 | + |
|
| 26 | 41 | checkW
|
| 27 | 42 | :: (Word, Word) -- ^ expected results
|
| 28 | 43 | -> (Word# -> Word# -> (# Word#, Word# #)) -- ^ primop
|
| ... | ... | @@ -59,6 +74,18 @@ check :: String -> Maybe String -> IO () |
| 59 | 74 | check s (Just err) = error $ "Error for " ++ s ++ ": " ++ err
|
| 60 | 75 | check _ Nothing = return ()
|
| 61 | 76 | |
| 77 | +expectedTimesInt2 :: Int -> Int -> (Int, Int, Int)
|
|
| 78 | +expectedTimesInt2 x y =
|
|
| 79 | + let p = toInteger x * toInteger y
|
|
| 80 | + radix = (2 :: Integer) ^ wordWidth
|
|
| 81 | + hi = p `div` radix
|
|
| 82 | + lo = p - hi * radix
|
|
| 83 | + needed
|
|
| 84 | + | p < toInteger (minBound :: Int) = 1
|
|
| 85 | + | p > toInteger (maxBound :: Int) = 1
|
|
| 86 | + | otherwise = 0
|
|
| 87 | + in (needed, fromInteger hi, fromInteger lo)
|
|
| 88 | + |
|
| 62 | 89 | main :: IO ()
|
| 63 | 90 | main = do
|
| 64 | 91 | -- First something trivial
|
| ... | ... | @@ -112,6 +139,33 @@ main = do |
| 112 | 139 | checkW (2, maxBound - 2) timesWord2# maxBound 3
|
| 113 | 140 | check "timesWord2# 3 maxBound" $
|
| 114 | 141 | checkW (2, maxBound - 2) timesWord2# 3 maxBound
|
| 142 | + check "timesWord2# maxBound maxBound" $
|
|
| 143 | + checkW (maxBound - 1, 1) timesWord2# maxBound maxBound
|
|
| 144 | + check "timesWord2# (2^(N-1)) (2^(N-1))" $
|
|
| 145 | + checkW (2 ^ (wordWidth-2), 0) timesWord2# (2 ^ (wordWidth-1)) (2 ^ (wordWidth-1))
|
|
| 146 | + |
|
| 147 | + -- timesInt2#
|
|
| 148 | + let checkMul2 x y =
|
|
| 149 | + check ("timesInt2# " ++ show x ++ " " ++ show y) $
|
|
| 150 | + checkI2 (expectedTimesInt2 x y) timesInt2# x y
|
|
| 151 | + checkMul2 0 0
|
|
| 152 | + checkMul2 0 maxBound
|
|
| 153 | + checkMul2 1 (-1)
|
|
| 154 | + checkMul2 (-1) 1
|
|
| 155 | + checkMul2 (-1) (-1)
|
|
| 156 | + checkMul2 3 5
|
|
| 157 | + checkMul2 (-3) 5
|
|
| 158 | + checkMul2 3 (-5)
|
|
| 159 | + checkMul2 (-3) (-5)
|
|
| 160 | + checkMul2 minBound 1
|
|
| 161 | + checkMul2 1 minBound
|
|
| 162 | + checkMul2 minBound (-1)
|
|
| 163 | + checkMul2 (-1) minBound
|
|
| 164 | + checkMul2 maxBound 1
|
|
| 165 | + checkMul2 maxBound (-1)
|
|
| 166 | + checkMul2 (-1) maxBound
|
|
| 167 | + checkMul2 maxBound maxBound
|
|
| 168 | + checkMul2 minBound minBound
|
|
| 115 | 169 | |
| 116 | 170 | check "quotRemWord2# 0 0 1" $ checkW2 (0, 0) quotRemWord2# 0 0 1
|
| 117 | 171 | check "quotRemWord2# 0 4 2" $ checkW2 (2, 0) quotRemWord2# 0 4 2
|