Sven Tennie pushed to branch wip/supersven/riscv-ncg-fix-mulmayoflo at Glasgow Haskell Compiler / GHC
Commits:
-
22d49a5a
by Sven Tennie at 2026-11-15T12:51:14+01:00
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:
| 1 | +section: compiler
|
|
| 2 | +synopsis: Fix the RISC-V NCG sign-extending MulMayOflo operands in place, corrupting live subword values
|
|
| 3 | +issues: #28131
|
|
| 4 | +mrs: !16999
|
|
| 5 | + |
|
| 6 | +description: {
|
|
| 7 | + ``do_mul_may_oflo`` sign-extended its operands into their own registers. For
|
|
| 8 | + subword widths this violated the zero-extended subword invariant when an
|
|
| 9 | + operand was a local that remained live after the operation.
|
|
| 10 | + |
|
| 11 | + The operands are now sign-extended into fresh registers, leaving the original
|
|
| 12 | + values untouched.
|
|
| 13 | +} |
| ... | ... | @@ -1099,7 +1099,10 @@ getRegister' config plat expr = |
| 1099 | 1099 | do_mul_may_oflo w@W64 x y = do
|
| 1100 | 1100 | (reg_x, format_x, code_x) <- getSomeReg x
|
| 1101 | 1101 | (reg_y, format_y, code_y) <- getSomeReg y
|
| 1102 | - -- TODO: Can't we clobber reg_x and reg_y to save registers?
|
|
| 1102 | + -- Sign-extend into fresh registers: the operands may be locals which
|
|
| 1103 | + -- are still live after this operation.
|
|
| 1104 | + (reg_x', code_x') <- signExtendReg (formatToWidth format_x) W64 reg_x
|
|
| 1105 | + (reg_y', code_y') <- signExtendReg (formatToWidth format_y) W64 reg_y
|
|
| 1103 | 1106 | lo <- getNewRegNat II64
|
| 1104 | 1107 | hi <- getNewRegNat II64
|
| 1105 | 1108 | -- TODO: Overhaul CSET: 3rd operand isn't needed for SNEZ
|
| ... | ... | @@ -1109,12 +1112,12 @@ getRegister' config plat expr = |
| 1109 | 1112 | (intFormat w)
|
| 1110 | 1113 | ( \dst ->
|
| 1111 | 1114 | code_x
|
| 1112 | - `appOL` signExtend (formatToWidth format_x) W64 reg_x reg_x
|
|
| 1115 | + `appOL` code_x'
|
|
| 1113 | 1116 | `appOL` code_y
|
| 1114 | - `appOL` signExtend (formatToWidth format_y) W64 reg_y reg_y
|
|
| 1117 | + `appOL` code_y'
|
|
| 1115 | 1118 | `appOL` toOL
|
| 1116 | - [ annExpr expr (MULH (OpReg w hi) (OpReg w reg_x) (OpReg w reg_y)),
|
|
| 1117 | - MUL (OpReg w lo) (OpReg w reg_x) (OpReg w reg_y),
|
|
| 1119 | + [ annExpr expr (MULH (OpReg w hi) (OpReg w reg_x') (OpReg w reg_y')),
|
|
| 1120 | + MUL (OpReg w lo) (OpReg w reg_x') (OpReg w reg_y'),
|
|
| 1118 | 1121 | SRA (OpReg w lo) (OpReg w lo) (OpImm (ImmInt (widthInBits W64 - 1))),
|
| 1119 | 1122 | ann
|
| 1120 | 1123 | (text "Set flag if result of MULH contains more than sign bits.")
|
| ... | ... | @@ -1144,15 +1147,19 @@ getRegister' config plat expr = |
| 1144 | 1147 | if use32BitMul
|
| 1145 | 1148 | then do
|
| 1146 | 1149 | narrowedReg <- getNewRegNat II64
|
| 1150 | + -- Sign-extend into fresh registers: the operands may be locals
|
|
| 1151 | + -- which are still live after this operation.
|
|
| 1152 | + (reg_x', code_x') <- signExtendReg (formatToWidth format_x) W32 reg_x
|
|
| 1153 | + (reg_y', code_y') <- signExtendReg (formatToWidth format_y) W32 reg_y
|
|
| 1147 | 1154 | pure
|
| 1148 | 1155 | $ Any
|
| 1149 | 1156 | (intFormat w)
|
| 1150 | 1157 | ( \dst ->
|
| 1151 | 1158 | code_x
|
| 1152 | - `appOL` signExtend (formatToWidth format_x) W32 reg_x reg_x
|
|
| 1159 | + `appOL` code_x'
|
|
| 1153 | 1160 | `appOL` code_y
|
| 1154 | - `appOL` signExtend (formatToWidth format_y) W32 reg_y reg_y
|
|
| 1155 | - `snocOL` annExpr expr (MUL (OpReg W32 dst) (OpReg W32 reg_x) (OpReg W32 reg_y))
|
|
| 1161 | + `appOL` code_y'
|
|
| 1162 | + `snocOL` annExpr expr (MUL (OpReg W32 dst) (OpReg W32 reg_x') (OpReg W32 reg_y'))
|
|
| 1156 | 1163 | `appOL` signExtendAdjustPrecission W32 w dst narrowedReg
|
| 1157 | 1164 | `appOL` toOL
|
| 1158 | 1165 | [ ann
|
| 1 | +{-# LANGUAGE GHCForeignImportPrim #-}
|
|
| 2 | +{-# LANGUAGE MagicHash #-}
|
|
| 3 | +{-# LANGUAGE UnliftedFFITypes #-}
|
|
| 4 | + |
|
| 5 | +module Main (main) where
|
|
| 6 | + |
|
| 7 | +import GHC.Exts
|
|
| 8 | +import GHC.Word
|
|
| 9 | + |
|
| 10 | +foreign import prim "runMulMayOfloSubword8zh"
|
|
| 11 | + runMulMayOfloSubword8# :: Word# -> Word# -> Word#
|
|
| 12 | +foreign import prim "runMulMayOfloSubword16zh"
|
|
| 13 | + runMulMayOfloSubword16# :: Word# -> Word# -> Word#
|
|
| 14 | +foreign import prim "runMulMayOfloSubword32zh"
|
|
| 15 | + runMulMayOfloSubword32# :: Word# -> Word# -> Word#
|
|
| 16 | + |
|
| 17 | +main :: IO ()
|
|
| 18 | +main = do
|
|
| 19 | + -- The low 8/16/32 bits are 0xc8/0xff38/0xffffffc8. A live local that has
|
|
| 20 | + -- been sign-extended in place by %mulmayoflo would come back with all upper
|
|
| 21 | + -- bits set instead of zero-extended.
|
|
| 22 | + putStrLn ("W8: " ++ show (W# (runMulMayOfloSubword8# 0xc8## 1##)))
|
|
| 23 | + putStrLn ("W16: " ++ show (W# (runMulMayOfloSubword16# 0xff38## 1##)))
|
|
| 24 | + putStrLn ("W32: " ++ show (W# (runMulMayOfloSubword32# 0xffffffc8## 1##))) |
| 1 | +W8: 200
|
|
| 2 | +W16: 65336
|
|
| 3 | +W32: 4294967240 |
| 1 | +#include "Cmm.h"
|
|
| 2 | + |
|
| 3 | +// Regression test for the in-place sign-extension in do_mul_may_oflo.
|
|
| 4 | +// The local 'a' must remain zero-extended after %mulmayoflo, because it is
|
|
| 5 | +// returned to Haskell as a Word.
|
|
| 6 | +runMulMayOfloSubword8zh (W_ x, W_ y) {
|
|
| 7 | + I8 a, b, needed;
|
|
| 8 | + a = %lobits8(x);
|
|
| 9 | + b = %lobits8(y);
|
|
| 10 | + needed = %mulmayoflo(a, b);
|
|
| 11 | + if (needed != 0::I8) {
|
|
| 12 | + return (0);
|
|
| 13 | + } else {
|
|
| 14 | + return (%zx64(a));
|
|
| 15 | + }
|
|
| 16 | +}
|
|
| 17 | + |
|
| 18 | +runMulMayOfloSubword16zh (W_ x, W_ y) {
|
|
| 19 | + I16 a, b, needed;
|
|
| 20 | + a = %lobits16(x);
|
|
| 21 | + b = %lobits16(y);
|
|
| 22 | + needed = %mulmayoflo(a, b);
|
|
| 23 | + if (needed != 0::I16) {
|
|
| 24 | + return (0);
|
|
| 25 | + } else {
|
|
| 26 | + return (%zx64(a));
|
|
| 27 | + }
|
|
| 28 | +}
|
|
| 29 | + |
|
| 30 | +runMulMayOfloSubword32zh (W_ x, W_ y) {
|
|
| 31 | + I32 a, b, needed;
|
|
| 32 | + a = %lobits32(x);
|
|
| 33 | + b = %lobits32(y);
|
|
| 34 | + needed = %mulmayoflo(a, b);
|
|
| 35 | + if (needed != 0::I32) {
|
|
| 36 | + return (0);
|
|
| 37 | + } else {
|
|
| 38 | + return (%zx64(a));
|
|
| 39 | + }
|
|
| 40 | +} |
| ... | ... | @@ -245,6 +245,19 @@ test('MulMayOflo_full', |
| 245 | 245 | ignore_stdout],
|
| 246 | 246 | multi_compile_and_run,
|
| 247 | 247 | ['MulMayOflo', [('MulMayOflo_full.cmm', '')], ''])
|
| 248 | +# Enable this wherever it can run, so that code-generator bugs are revealed.
|
|
| 249 | +# The only reasonable exclusion is the PPC NCG: its MULLO instruction has no
|
|
| 250 | +# byte/halfword form, so lowering a subword %mulmayoflo there would panic (see
|
|
| 251 | +# PPC/Ppr.hs). An unregisterised PPC compiler lowers %mulmayoflo to
|
|
| 252 | +# hs_mulIntMayOflo instead and can run the test.
|
|
| 253 | +test('MulMayOflo_subword',
|
|
| 254 | + [ req_cmm
|
|
| 255 | + , when(
|
|
| 256 | + (arch('powerpc64') or arch('powerpc64le')) and not unregisterised(),
|
|
| 257 | + skip
|
|
| 258 | + )
|
|
| 259 | + ], multi_compile_and_run,
|
|
| 260 | + ['MulMayOflo_subword', [('MulMayOflo_subword_cmm.cmm', '')], ''])
|
|
| 248 | 261 | test('T24264run', normal, compile_and_run, [''])
|
| 249 | 262 | test('T24295a', normal, compile_and_run, ['-O -floopification'])
|
| 250 | 263 | test('T24295b', normal, compile_and_run, ['-O -floopification -fpedantic-bottoms'])
|