Sven Tennie pushed to branch wip/supersven/riscv-ncg-fix-branch-signext at Glasgow Haskell Compiler / GHC Commits: 72ba91c2 by Sven Tennie at 2026-11-15T11:58:06+01:00 RISC-V NCG: Don't sign-extend branch operands in place (#28050) genCondJump's signed subword branch path called signExtend with the same source and destination register. If the operand was a local that is still live after the branch, this corrupted its value, violating the zero-extended subword invariant maintained by the RV64 NCG. Sign-extend into fresh registers and perform the comparison at W64. Fixed tests: T27538, T28050 - - - - - 5 changed files: - + changelog.d/T28050 - compiler/GHC/CmmToAsm/RV64/CodeGen.hs - + testsuite/tests/codeGen/should_run/T28050.hs - + testsuite/tests/codeGen/should_run/T28050.stdout - testsuite/tests/codeGen/should_run/all.T Changes: ===================================== changelog.d/T28050 ===================================== @@ -0,0 +1,14 @@ +section: compiler +synopsis: Fix the RISC-V NCG sign-extending subword branch operands in place, corrupting live values +issues: #28050 +mrs: !16998 + +description: { + The signed subword path of ``genCondJump`` sign-extended its operands in + place. If an operand was a local that remained live after the branch, its + value was corrupted, violating the zero-extended subword invariant that the + RISC-V NCG relies on. + + The operands are now sign-extended into fresh registers and the comparison is + performed at 64-bit width, leaving the source registers untouched. +} ===================================== compiler/GHC/CmmToAsm/RV64/CodeGen.hs ===================================== @@ -1451,21 +1451,23 @@ genCondJump bid expr = do `appOL` code_y `snocOL` annExpr expr (BCOND cmp x' y' (TBlock bid)) - sbcond w cmp = do + sbcond _w cmp = do -- compute both sides. (reg_x, format_x, code_x) <- getSomeReg x (reg_y, format_y, code_y) <- getSomeReg y - let x' = OpReg w reg_x - y' = OpReg w reg_y - return $ case w of - w - | w `elem` [W8, W16, W32] -> - code_x - `appOL` signExtend (formatToWidth format_x) W64 reg_x reg_x - `appOL` code_y - `appOL` signExtend (formatToWidth format_y) W64 reg_y reg_y - `appOL` unitOL (annExpr expr (BCOND cmp x' y' (TBlock bid))) - _ -> code_x `appOL` code_y `appOL` unitOL (annExpr expr (BCOND cmp x' y' (TBlock bid))) + -- Sign-extend the operands into fresh registers rather than in + -- place. The source may be a local which is still live past the + -- branch and in-place extension would violate the subword + -- invariant documented in Note [Signed arithmetic on RISCV64]. + (reg_x', code_x') <- signExtendReg (formatToWidth format_x) W64 reg_x + (reg_y', code_y') <- signExtendReg (formatToWidth format_y) W64 reg_y + let x' = OpReg W64 reg_x' + y' = OpReg W64 reg_y' + return $ code_x + `appOL` code_x' + `appOL` code_y + `appOL` code_y' + `snocOL` annExpr expr (BCOND cmp x' y' (TBlock bid)) fbcond w cmp = do -- ensure we get float regs ===================================== testsuite/tests/codeGen/should_run/T28050.hs ===================================== @@ -0,0 +1,38 @@ +{-# LANGUAGE MagicHash #-} + +-- Regression test for #28050. +-- +-- A subword value must still be zero-extended after it has been tested by a +-- conditional branch. Correct output: +-- (200,65480,4294967240) +-- 200 + +module Main (main) where + +import Data.Int +import Data.Word +import GHC.Exts +import GHC.Word + +f8 :: Int8 -> Word +f8 i = if i < 0 then fromIntegral (fromIntegral i :: Word8) else 0 +{-# NOINLINE f8 #-} + +f16 :: Int16 -> Word +f16 i = if i < 0 then fromIntegral (fromIntegral i :: Word16) else 0 +{-# NOINLINE f16 #-} + +f32 :: Int32 -> Word +f32 i = if i < 0 then fromIntegral (fromIntegral i :: Word32) else 0 +{-# NOINLINE f32 #-} + +g8 :: Word8 -> Word +g8 (W8# w) = case eqWord8# w (wordToWord8# 100##) of + 1# -> 0 + _ -> W# (word8ToWord# w) +{-# NOINLINE g8 #-} + +main :: IO () +main = do + print (f8 (-56), f16 (-56), f32 (-56)) + print (g8 200) ===================================== testsuite/tests/codeGen/should_run/T28050.stdout ===================================== @@ -0,0 +1,2 @@ +(200,65480,4294967240) +200 ===================================== testsuite/tests/codeGen/should_run/all.T ===================================== @@ -305,3 +305,5 @@ test('T27533', [req_cmm, extra_ways(['optasm'])], multi_compile_and_run, test('T27537', normal, compile_and_run, ['-O']) test('T27538', normal, compile_and_run, ['-O']) + +test('T28050', normal, compile_and_run, ['-O']) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/72ba91c276372f731f16714ce804c81c... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/72ba91c276372f731f16714ce804c81c... 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