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 RISC-V NCG: Truncate results of immediate MO_Add/MO_Sub/MO_Or fast paths (#28133) The immediate fast paths for MO_Add, MO_Sub and MO_Or emitted the operation without truncating the result to the operation width. For subword widths this violates the zero-extended subword invariant (see Note [Signed arithmetic on RISCV64]) whenever the first operand is a subword local, so a later zero-extending widening conversion (a plain MOV on RV64) exposed the stray high bits set by the operation. The immediate MO_Shl/MO_S_Shr/MO_U_Shr fast paths already truncate their results; do the same for add/sub/or. MO_And needs no change: its operands are zero-extended and `andi` always leaves a result that is already in range. Fixed (new) test: SubwordImmArith - - - - - 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: ===================================== changelog.d/T28133 ===================================== @@ -0,0 +1,16 @@ +section: compiler +synopsis: Fix the RISC-V NCG failing to truncate the result of immediate subword add/sub/or operations +issues: #28133 +mrs: !17000 + +description: { + The immediate fast paths for ``MO_Add``, ``MO_Sub`` and ``MO_Or`` emitted the + operation without truncating the result to the operation width. For subword + widths this violated the zero-extended subword invariant whenever the first + operand was a subword local, so a later zero-extending widening conversion + (a plain ``MOV`` on RV64) exposed the stray high bits. + + The results are now truncated, as the immediate shift fast paths already did. + ``MO_And`` needs no change: its operands are zero-extended and ``andi`` always + leaves a result that is already in range. +} ===================================== compiler/GHC/CmmToAsm/RV64/CodeGen.hs ===================================== @@ -851,13 +851,13 @@ getRegister' config plat expr = -- 1. Compute Reg +/- n directly. -- For Add/Sub we can directly encode 12bits, or 12bits lsl #12. CmmMachOp (MO_Add w) [CmmReg reg, CmmLit (CmmInt n _)] - | fitsIn12bitImm n -> return $ Any (intFormat w) (\d -> unitOL $ annExpr expr (ADD (OpReg w d) (OpReg w' r') (OpImm (ImmInteger n)))) + | 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) where -- TODO: 12bits lsl #12; e.g. lower 12 bits of n are 0; shift n >> 12, and set lsl to #12. w' = formatToWidth (cmmTypeFormat (cmmRegType reg)) r' = getRegisterReg plat reg CmmMachOp (MO_Sub w) [CmmReg reg, CmmLit (CmmInt n _)] - | fitsIn12bitImm n -> return $ Any (intFormat w) (\d -> unitOL $ annExpr expr (SUB (OpReg w d) (OpReg w' r') (OpImm (ImmInteger n)))) + | 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) where -- TODO: 12bits lsl #12; e.g. lower 12 bits of n are 0; shift n >> 12, and set lsl to #12. w' = formatToWidth (cmmTypeFormat (cmmRegType reg)) @@ -911,6 +911,9 @@ getRegister' config plat expr = `appOL` truncateReg w w dst ) -- 3. Logic &&, || + -- MO_And needs no truncateReg: the first operand is already zero-extended + -- (subword invariant), and AND can only clear bits, so the result stays in + -- range however `andi` sign-extends its immediate. CmmMachOp (MO_And w) [CmmReg reg, CmmLit (CmmInt n _)] | fitsIn12bitImm n -> 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 = r' = getRegisterReg plat reg CmmMachOp (MO_Or w) [CmmReg reg, CmmLit (CmmInt n _)] | fitsIn12bitImm n -> - return $ Any (intFormat w) (\d -> unitOL $ annExpr expr (ORI (OpReg w d) (OpReg w' r') (OpImm (ImmInteger n)))) + return $ Any (intFormat w) (\d -> unitOL (annExpr expr (ORI (OpReg w d) (OpReg w' r') (OpImm (ImmInteger n)))) `appOL` truncateReg w w d) where w' = formatToWidth (cmmTypeFormat (cmmRegType reg)) r' = getRegisterReg plat reg ===================================== testsuite/tests/codeGen/should_run/SubwordImmArith.hs ===================================== @@ -0,0 +1,29 @@ +{-# LANGUAGE GHCForeignImportPrim #-} +{-# LANGUAGE MagicHash #-} +{-# LANGUAGE UnliftedFFITypes #-} + +module Main (main) where + +import GHC.Exts + +foreign import prim "runAddImmSubword8zh" runAddImmSubword8# :: Word# -> Word# +foreign import prim "runAddImmSubword16zh" runAddImmSubword16# :: Word# -> Word# +foreign import prim "runAddImmSubword32zh" runAddImmSubword32# :: Word# -> Word# +foreign import prim "runSubImmSubword8zh" runSubImmSubword8# :: Word# -> Word# +foreign import prim "runSubImmSubword16zh" runSubImmSubword16# :: Word# -> Word# +foreign import prim "runSubImmSubword32zh" runSubImmSubword32# :: Word# -> Word# +foreign import prim "runOrImmSubword8zh" runOrImmSubword8# :: Word# -> Word# +foreign import prim "runOrImmSubword16zh" runOrImmSubword16# :: Word# -> Word# +foreign import prim "runOrImmSubword32zh" runOrImmSubword32# :: Word# -> Word# + +main :: IO () +main = do + putStrLn ("add W8 255: " ++ show (W# (runAddImmSubword8# 255##))) + putStrLn ("add W16 65535: " ++ show (W# (runAddImmSubword16# 65535##))) + putStrLn ("add W32 4294967295: " ++ show (W# (runAddImmSubword32# 4294967295##))) + putStrLn ("sub W8 0: " ++ show (W# (runSubImmSubword8# 0##))) + putStrLn ("sub W16 0: " ++ show (W# (runSubImmSubword16# 0##))) + putStrLn ("sub W32 0: " ++ show (W# (runSubImmSubword32# 0##))) + putStrLn ("or W8 0: " ++ show (W# (runOrImmSubword8# 0##))) + putStrLn ("or W16 0: " ++ show (W# (runOrImmSubword16# 0##))) + putStrLn ("or W32 0: " ++ show (W# (runOrImmSubword32# 0##))) ===================================== testsuite/tests/codeGen/should_run/SubwordImmArith.stdout ===================================== @@ -0,0 +1,9 @@ +add W8 255: 0 +add W16 65535: 0 +add W32 4294967295: 0 +sub W8 0: 255 +sub W16 0: 65535 +sub W32 0: 4294967295 +or W8 0: 255 +or W16 0: 65535 +or W32 0: 4294967295 ===================================== testsuite/tests/codeGen/should_run/SubwordImmArith_cmm.cmm ===================================== @@ -0,0 +1,73 @@ +#include "Cmm.h" + +/* Regression test for missing truncation of the result of the immediate + * MO_Add / MO_Sub / MO_Or fast paths in the RV64 NCG. Each function keeps a + * live sub-word-size local across the operation and returns it zero-extended. + * The zero-extension lowers to a plain widening MOV on RV64, so any stray high + * bits left in the register by the operation become visible in the result. + * + * See Note [Signed arithmetic on RISCV64] in GHC.CmmToAsm.RV64.CodeGen. + */ + +runAddImmSubword8zh (W_ x) { + I8 a; + a = %lobits8(x); + a = a + (1::I8); + return (%zx64(a)); +} + +runAddImmSubword16zh (W_ x) { + I16 a; + a = %lobits16(x); + a = a + (1::I16); + return (%zx64(a)); +} + +runAddImmSubword32zh (W_ x) { + I32 a; + a = %lobits32(x); + a = a + (1::I32); + return (%zx64(a)); +} + +runSubImmSubword8zh (W_ x) { + I8 a; + a = %lobits8(x); + a = a - (1::I8); + return (%zx64(a)); +} + +runSubImmSubword16zh (W_ x) { + I16 a; + a = %lobits16(x); + a = a - (1::I16); + return (%zx64(a)); +} + +runSubImmSubword32zh (W_ x) { + I32 a; + a = %lobits32(x); + a = a - (1::I32); + return (%zx64(a)); +} + +runOrImmSubword8zh (W_ x) { + I8 a; + a = %lobits8(x); + a = a | (-1::I8); + return (%zx64(a)); +} + +runOrImmSubword16zh (W_ x) { + I16 a; + a = %lobits16(x); + a = a | (-1::I16); + return (%zx64(a)); +} + +runOrImmSubword32zh (W_ x) { + I32 a; + a = %lobits32(x); + a = a | (-1::I32); + return (%zx64(a)); +} ===================================== testsuite/tests/codeGen/should_run/all.T ===================================== @@ -245,6 +245,10 @@ test('MulMayOflo_full', ignore_stdout], multi_compile_and_run, ['MulMayOflo', [('MulMayOflo_full.cmm', '')], '']) +test('SubwordImmArith', + [ req_cmm + ], multi_compile_and_run, + ['SubwordImmArith', [('SubwordImmArith_cmm.cmm', '')], '']) test('T24264run', normal, compile_and_run, ['']) test('T24295a', normal, compile_and_run, ['-O -floopification']) test('T24295b', normal, compile_and_run, ['-O -floopification -fpedantic-bottoms']) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/c6eeda29d749ae69e99c2e60f5f795a3... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/c6eeda29d749ae69e99c2e60f5f795a3... 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