Sven Tennie pushed to branch wip/supersven/riscv-ncg-fix-immediate-arith at Glasgow Haskell Compiler / GHC
Commits:
-
c6eeda29
by Sven Tennie at 2026-11-15T12:32:45+00:00
6 changed files:
- + changelog.d/T28133
- compiler/GHC/CmmToAsm/RV64/CodeGen.hs
- + testsuite/tests/codeGen/should_run/SubwordImmArith.hs
- + testsuite/tests/codeGen/should_run/SubwordImmArith.stdout
- + testsuite/tests/codeGen/should_run/SubwordImmArith_cmm.cmm
- testsuite/tests/codeGen/should_run/all.T
Changes:
| 1 | +section: compiler
|
|
| 2 | +synopsis: Fix the RISC-V NCG failing to truncate the result of immediate subword add/sub/or operations
|
|
| 3 | +issues: #28133
|
|
| 4 | +mrs: !17000
|
|
| 5 | + |
|
| 6 | +description: {
|
|
| 7 | + The immediate fast paths for ``MO_Add``, ``MO_Sub`` and ``MO_Or`` emitted the
|
|
| 8 | + operation without truncating the result to the operation width. For subword
|
|
| 9 | + widths this violated the zero-extended subword invariant whenever the first
|
|
| 10 | + operand was a subword local, so a later zero-extending widening conversion
|
|
| 11 | + (a plain ``MOV`` on RV64) exposed the stray high bits.
|
|
| 12 | + |
|
| 13 | + The results are now truncated, as the immediate shift fast paths already did.
|
|
| 14 | + ``MO_And`` needs no change: its operands are zero-extended and ``andi`` always
|
|
| 15 | + leaves a result that is already in range.
|
|
| 16 | +} |
| ... | ... | @@ -851,13 +851,13 @@ getRegister' config plat expr = |
| 851 | 851 | -- 1. Compute Reg +/- n directly.
|
| 852 | 852 | -- For Add/Sub we can directly encode 12bits, or 12bits lsl #12.
|
| 853 | 853 | CmmMachOp (MO_Add w) [CmmReg reg, CmmLit (CmmInt n _)]
|
| 854 | - | fitsIn12bitImm n -> return $ Any (intFormat w) (\d -> unitOL $ annExpr expr (ADD (OpReg w d) (OpReg w' r') (OpImm (ImmInteger n))))
|
|
| 854 | + | fitsIn12bitImm n -> return $ Any (intFormat w) (\d -> unitOL (annExpr expr (ADD (OpReg w d) (OpReg w' r') (OpImm (ImmInteger n)))) `appOL` truncateReg w w d)
|
|
| 855 | 855 | where
|
| 856 | 856 | -- TODO: 12bits lsl #12; e.g. lower 12 bits of n are 0; shift n >> 12, and set lsl to #12.
|
| 857 | 857 | w' = formatToWidth (cmmTypeFormat (cmmRegType reg))
|
| 858 | 858 | r' = getRegisterReg plat reg
|
| 859 | 859 | CmmMachOp (MO_Sub w) [CmmReg reg, CmmLit (CmmInt n _)]
|
| 860 | - | fitsIn12bitImm n -> return $ Any (intFormat w) (\d -> unitOL $ annExpr expr (SUB (OpReg w d) (OpReg w' r') (OpImm (ImmInteger n))))
|
|
| 860 | + | fitsIn12bitImm n -> return $ Any (intFormat w) (\d -> unitOL (annExpr expr (SUB (OpReg w d) (OpReg w' r') (OpImm (ImmInteger n)))) `appOL` truncateReg w w d)
|
|
| 861 | 861 | where
|
| 862 | 862 | -- TODO: 12bits lsl #12; e.g. lower 12 bits of n are 0; shift n >> 12, and set lsl to #12.
|
| 863 | 863 | w' = formatToWidth (cmmTypeFormat (cmmRegType reg))
|
| ... | ... | @@ -911,6 +911,9 @@ getRegister' config plat expr = |
| 911 | 911 | `appOL` truncateReg w w dst
|
| 912 | 912 | )
|
| 913 | 913 | -- 3. Logic &&, ||
|
| 914 | + -- MO_And needs no truncateReg: the first operand is already zero-extended
|
|
| 915 | + -- (subword invariant), and AND can only clear bits, so the result stays in
|
|
| 916 | + -- range however `andi` sign-extends its immediate.
|
|
| 914 | 917 | CmmMachOp (MO_And w) [CmmReg reg, CmmLit (CmmInt n _)]
|
| 915 | 918 | | fitsIn12bitImm n ->
|
| 916 | 919 | return $ Any (intFormat w) (\d -> unitOL $ annExpr expr (AND (OpReg w d) (OpReg w' r') (OpImm (ImmInteger n))))
|
| ... | ... | @@ -919,7 +922,7 @@ getRegister' config plat expr = |
| 919 | 922 | r' = getRegisterReg plat reg
|
| 920 | 923 | CmmMachOp (MO_Or w) [CmmReg reg, CmmLit (CmmInt n _)]
|
| 921 | 924 | | fitsIn12bitImm n ->
|
| 922 | - return $ Any (intFormat w) (\d -> unitOL $ annExpr expr (ORI (OpReg w d) (OpReg w' r') (OpImm (ImmInteger n))))
|
|
| 925 | + return $ Any (intFormat w) (\d -> unitOL (annExpr expr (ORI (OpReg w d) (OpReg w' r') (OpImm (ImmInteger n)))) `appOL` truncateReg w w d)
|
|
| 923 | 926 | where
|
| 924 | 927 | w' = formatToWidth (cmmTypeFormat (cmmRegType reg))
|
| 925 | 928 | r' = getRegisterReg plat reg
|
| 1 | +{-# LANGUAGE GHCForeignImportPrim #-}
|
|
| 2 | +{-# LANGUAGE MagicHash #-}
|
|
| 3 | +{-# LANGUAGE UnliftedFFITypes #-}
|
|
| 4 | + |
|
| 5 | +module Main (main) where
|
|
| 6 | + |
|
| 7 | +import GHC.Exts
|
|
| 8 | + |
|
| 9 | +foreign import prim "runAddImmSubword8zh" runAddImmSubword8# :: Word# -> Word#
|
|
| 10 | +foreign import prim "runAddImmSubword16zh" runAddImmSubword16# :: Word# -> Word#
|
|
| 11 | +foreign import prim "runAddImmSubword32zh" runAddImmSubword32# :: Word# -> Word#
|
|
| 12 | +foreign import prim "runSubImmSubword8zh" runSubImmSubword8# :: Word# -> Word#
|
|
| 13 | +foreign import prim "runSubImmSubword16zh" runSubImmSubword16# :: Word# -> Word#
|
|
| 14 | +foreign import prim "runSubImmSubword32zh" runSubImmSubword32# :: Word# -> Word#
|
|
| 15 | +foreign import prim "runOrImmSubword8zh" runOrImmSubword8# :: Word# -> Word#
|
|
| 16 | +foreign import prim "runOrImmSubword16zh" runOrImmSubword16# :: Word# -> Word#
|
|
| 17 | +foreign import prim "runOrImmSubword32zh" runOrImmSubword32# :: Word# -> Word#
|
|
| 18 | + |
|
| 19 | +main :: IO ()
|
|
| 20 | +main = do
|
|
| 21 | + putStrLn ("add W8 255: " ++ show (W# (runAddImmSubword8# 255##)))
|
|
| 22 | + putStrLn ("add W16 65535: " ++ show (W# (runAddImmSubword16# 65535##)))
|
|
| 23 | + putStrLn ("add W32 4294967295: " ++ show (W# (runAddImmSubword32# 4294967295##)))
|
|
| 24 | + putStrLn ("sub W8 0: " ++ show (W# (runSubImmSubword8# 0##)))
|
|
| 25 | + putStrLn ("sub W16 0: " ++ show (W# (runSubImmSubword16# 0##)))
|
|
| 26 | + putStrLn ("sub W32 0: " ++ show (W# (runSubImmSubword32# 0##)))
|
|
| 27 | + putStrLn ("or W8 0: " ++ show (W# (runOrImmSubword8# 0##)))
|
|
| 28 | + putStrLn ("or W16 0: " ++ show (W# (runOrImmSubword16# 0##)))
|
|
| 29 | + putStrLn ("or W32 0: " ++ show (W# (runOrImmSubword32# 0##))) |
| 1 | +add W8 255: 0
|
|
| 2 | +add W16 65535: 0
|
|
| 3 | +add W32 4294967295: 0
|
|
| 4 | +sub W8 0: 255
|
|
| 5 | +sub W16 0: 65535
|
|
| 6 | +sub W32 0: 4294967295
|
|
| 7 | +or W8 0: 255
|
|
| 8 | +or W16 0: 65535
|
|
| 9 | +or W32 0: 4294967295 |
| 1 | +#include "Cmm.h"
|
|
| 2 | + |
|
| 3 | +/* Regression test for missing truncation of the result of the immediate
|
|
| 4 | + * MO_Add / MO_Sub / MO_Or fast paths in the RV64 NCG. Each function keeps a
|
|
| 5 | + * live sub-word-size local across the operation and returns it zero-extended.
|
|
| 6 | + * The zero-extension lowers to a plain widening MOV on RV64, so any stray high
|
|
| 7 | + * bits left in the register by the operation become visible in the result.
|
|
| 8 | + *
|
|
| 9 | + * See Note [Signed arithmetic on RISCV64] in GHC.CmmToAsm.RV64.CodeGen.
|
|
| 10 | + */
|
|
| 11 | + |
|
| 12 | +runAddImmSubword8zh (W_ x) {
|
|
| 13 | + I8 a;
|
|
| 14 | + a = %lobits8(x);
|
|
| 15 | + a = a + (1::I8);
|
|
| 16 | + return (%zx64(a));
|
|
| 17 | +}
|
|
| 18 | + |
|
| 19 | +runAddImmSubword16zh (W_ x) {
|
|
| 20 | + I16 a;
|
|
| 21 | + a = %lobits16(x);
|
|
| 22 | + a = a + (1::I16);
|
|
| 23 | + return (%zx64(a));
|
|
| 24 | +}
|
|
| 25 | + |
|
| 26 | +runAddImmSubword32zh (W_ x) {
|
|
| 27 | + I32 a;
|
|
| 28 | + a = %lobits32(x);
|
|
| 29 | + a = a + (1::I32);
|
|
| 30 | + return (%zx64(a));
|
|
| 31 | +}
|
|
| 32 | + |
|
| 33 | +runSubImmSubword8zh (W_ x) {
|
|
| 34 | + I8 a;
|
|
| 35 | + a = %lobits8(x);
|
|
| 36 | + a = a - (1::I8);
|
|
| 37 | + return (%zx64(a));
|
|
| 38 | +}
|
|
| 39 | + |
|
| 40 | +runSubImmSubword16zh (W_ x) {
|
|
| 41 | + I16 a;
|
|
| 42 | + a = %lobits16(x);
|
|
| 43 | + a = a - (1::I16);
|
|
| 44 | + return (%zx64(a));
|
|
| 45 | +}
|
|
| 46 | + |
|
| 47 | +runSubImmSubword32zh (W_ x) {
|
|
| 48 | + I32 a;
|
|
| 49 | + a = %lobits32(x);
|
|
| 50 | + a = a - (1::I32);
|
|
| 51 | + return (%zx64(a));
|
|
| 52 | +}
|
|
| 53 | + |
|
| 54 | +runOrImmSubword8zh (W_ x) {
|
|
| 55 | + I8 a;
|
|
| 56 | + a = %lobits8(x);
|
|
| 57 | + a = a | (-1::I8);
|
|
| 58 | + return (%zx64(a));
|
|
| 59 | +}
|
|
| 60 | + |
|
| 61 | +runOrImmSubword16zh (W_ x) {
|
|
| 62 | + I16 a;
|
|
| 63 | + a = %lobits16(x);
|
|
| 64 | + a = a | (-1::I16);
|
|
| 65 | + return (%zx64(a));
|
|
| 66 | +}
|
|
| 67 | + |
|
| 68 | +runOrImmSubword32zh (W_ x) {
|
|
| 69 | + I32 a;
|
|
| 70 | + a = %lobits32(x);
|
|
| 71 | + a = a | (-1::I32);
|
|
| 72 | + return (%zx64(a));
|
|
| 73 | +} |
| ... | ... | @@ -245,6 +245,10 @@ test('MulMayOflo_full', |
| 245 | 245 | ignore_stdout],
|
| 246 | 246 | multi_compile_and_run,
|
| 247 | 247 | ['MulMayOflo', [('MulMayOflo_full.cmm', '')], ''])
|
| 248 | +test('SubwordImmArith',
|
|
| 249 | + [ req_cmm
|
|
| 250 | + ], multi_compile_and_run,
|
|
| 251 | + ['SubwordImmArith', [('SubwordImmArith_cmm.cmm', '')], ''])
|
|
| 248 | 252 | test('T24264run', normal, compile_and_run, [''])
|
| 249 | 253 | test('T24295a', normal, compile_and_run, ['-O -floopification'])
|
| 250 | 254 | test('T24295b', normal, compile_and_run, ['-O -floopification -fpedantic-bottoms'])
|