Sven Tennie pushed to branch wip/supersven/riscv-ncg-fix-mulmayoflo at Glasgow Haskell Compiler / GHC Commits: eba79831 by Sven Tennie at 2026-11-15T12:50:10+01:00 RISC-V NCG: Don't sign-extend MulMayOflo operands in place (#28131) do_mul_may_oflo sign-extended its operands into their own registers. For subword widths this violates the zero-extended subword invariant when an operand is a local that is still live after the operation. Sign-extend into fresh registers instead, as genCondJump now does. This is a follow-up to the in-place sign-extension described in #28050. Fixed tests: MulMayOflo_subword - - - - - 6 changed files: - + changelog.d/T28131 - compiler/GHC/CmmToAsm/RV64/CodeGen.hs - + testsuite/tests/codeGen/should_run/MulMayOflo_subword.hs - + testsuite/tests/codeGen/should_run/MulMayOflo_subword.stdout - + testsuite/tests/codeGen/should_run/MulMayOflo_subword_cmm.cmm - testsuite/tests/codeGen/should_run/all.T Changes: ===================================== changelog.d/T28131 ===================================== @@ -0,0 +1,12 @@ +section: compiler +synopsis: Fix the RISC-V NCG sign-extending MulMayOflo operands in place, corrupting live subword values +issues: #28131 + +description: { + ``do_mul_may_oflo`` sign-extended its operands into their own registers. For + subword widths this violated the zero-extended subword invariant when an + operand was a local that remained live after the operation. + + The operands are now sign-extended into fresh registers, leaving the original + values untouched. +} ===================================== compiler/GHC/CmmToAsm/RV64/CodeGen.hs ===================================== @@ -1099,7 +1099,10 @@ getRegister' config plat expr = do_mul_may_oflo w@W64 x y = do (reg_x, format_x, code_x) <- getSomeReg x (reg_y, format_y, code_y) <- getSomeReg y - -- TODO: Can't we clobber reg_x and reg_y to save registers? + -- Sign-extend into fresh registers: the operands may be locals which + -- are still live after this operation. + (reg_x', code_x') <- signExtendReg (formatToWidth format_x) W64 reg_x + (reg_y', code_y') <- signExtendReg (formatToWidth format_y) W64 reg_y lo <- getNewRegNat II64 hi <- getNewRegNat II64 -- TODO: Overhaul CSET: 3rd operand isn't needed for SNEZ @@ -1109,12 +1112,12 @@ getRegister' config plat expr = (intFormat w) ( \dst -> code_x - `appOL` signExtend (formatToWidth format_x) W64 reg_x reg_x + `appOL` code_x' `appOL` code_y - `appOL` signExtend (formatToWidth format_y) W64 reg_y reg_y + `appOL` code_y' `appOL` toOL - [ annExpr expr (MULH (OpReg w hi) (OpReg w reg_x) (OpReg w reg_y)), - MUL (OpReg w lo) (OpReg w reg_x) (OpReg w reg_y), + [ annExpr expr (MULH (OpReg w hi) (OpReg w reg_x') (OpReg w reg_y')), + MUL (OpReg w lo) (OpReg w reg_x') (OpReg w reg_y'), SRA (OpReg w lo) (OpReg w lo) (OpImm (ImmInt (widthInBits W64 - 1))), ann (text "Set flag if result of MULH contains more than sign bits.") @@ -1144,15 +1147,19 @@ getRegister' config plat expr = if use32BitMul then do narrowedReg <- getNewRegNat II64 + -- Sign-extend into fresh registers: the operands may be locals + -- which are still live after this operation. + (reg_x', code_x') <- signExtendReg (formatToWidth format_x) W32 reg_x + (reg_y', code_y') <- signExtendReg (formatToWidth format_y) W32 reg_y pure $ Any (intFormat w) ( \dst -> code_x - `appOL` signExtend (formatToWidth format_x) W32 reg_x reg_x + `appOL` code_x' `appOL` code_y - `appOL` signExtend (formatToWidth format_y) W32 reg_y reg_y - `snocOL` annExpr expr (MUL (OpReg W32 dst) (OpReg W32 reg_x) (OpReg W32 reg_y)) + `appOL` code_y' + `snocOL` annExpr expr (MUL (OpReg W32 dst) (OpReg W32 reg_x') (OpReg W32 reg_y')) `appOL` signExtendAdjustPrecission W32 w dst narrowedReg `appOL` toOL [ ann ===================================== testsuite/tests/codeGen/should_run/MulMayOflo_subword.hs ===================================== @@ -0,0 +1,24 @@ +{-# LANGUAGE GHCForeignImportPrim #-} +{-# LANGUAGE MagicHash #-} +{-# LANGUAGE UnliftedFFITypes #-} + +module Main (main) where + +import GHC.Exts +import GHC.Word + +foreign import prim "runMulMayOfloSubword8zh" + runMulMayOfloSubword8# :: Word# -> Word# -> Word# +foreign import prim "runMulMayOfloSubword16zh" + runMulMayOfloSubword16# :: Word# -> Word# -> Word# +foreign import prim "runMulMayOfloSubword32zh" + runMulMayOfloSubword32# :: Word# -> Word# -> Word# + +main :: IO () +main = do + -- The low 8/16/32 bits are 0xc8/0xff38/0xffffffc8. A live local that has + -- been sign-extended in place by %mulmayoflo would come back with all upper + -- bits set instead of zero-extended. + putStrLn ("W8: " ++ show (W# (runMulMayOfloSubword8# 0xc8## 1##))) + putStrLn ("W16: " ++ show (W# (runMulMayOfloSubword16# 0xff38## 1##))) + putStrLn ("W32: " ++ show (W# (runMulMayOfloSubword32# 0xffffffc8## 1##))) ===================================== testsuite/tests/codeGen/should_run/MulMayOflo_subword.stdout ===================================== @@ -0,0 +1,3 @@ +W8: 200 +W16: 65336 +W32: 4294967240 ===================================== testsuite/tests/codeGen/should_run/MulMayOflo_subword_cmm.cmm ===================================== @@ -0,0 +1,40 @@ +#include "Cmm.h" + +// Regression test for the in-place sign-extension in do_mul_may_oflo. +// The local 'a' must remain zero-extended after %mulmayoflo, because it is +// returned to Haskell as a Word. +runMulMayOfloSubword8zh (W_ x, W_ y) { + I8 a, b, needed; + a = %lobits8(x); + b = %lobits8(y); + needed = %mulmayoflo(a, b); + if (needed != 0::I8) { + return (0); + } else { + return (%zx64(a)); + } +} + +runMulMayOfloSubword16zh (W_ x, W_ y) { + I16 a, b, needed; + a = %lobits16(x); + b = %lobits16(y); + needed = %mulmayoflo(a, b); + if (needed != 0::I16) { + return (0); + } else { + return (%zx64(a)); + } +} + +runMulMayOfloSubword32zh (W_ x, W_ y) { + I32 a, b, needed; + a = %lobits32(x); + b = %lobits32(y); + needed = %mulmayoflo(a, b); + if (needed != 0::I32) { + return (0); + } else { + return (%zx64(a)); + } +} ===================================== testsuite/tests/codeGen/should_run/all.T ===================================== @@ -245,6 +245,19 @@ test('MulMayOflo_full', ignore_stdout], multi_compile_and_run, ['MulMayOflo', [('MulMayOflo_full.cmm', '')], '']) +# Enable this wherever it can run, so that code-generator bugs are revealed. +# The only reasonable exclusion is the PPC NCG: its MULLO instruction has no +# byte/halfword form, so lowering a subword %mulmayoflo there would panic (see +# PPC/Ppr.hs). An unregisterised PPC compiler lowers %mulmayoflo to +# hs_mulIntMayOflo instead and can run the test. +test('MulMayOflo_subword', + [ req_cmm + , when( + (arch('powerpc64') or arch('powerpc64le')) and not unregisterised(), + skip + ) + ], multi_compile_and_run, + ['MulMayOflo_subword', [('MulMayOflo_subword_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/eba79831299a1e46b4becb50299cf120... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/eba79831299a1e46b4becb50299cf120... 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