| Issue |
244582
|
| Summary |
[fuzzer-found][GlobalISel] reduce_shl_of_extend turns `shl (sext x), 0` into `zext x` (sign extension lost; also hits signed mul-high by 1)
|
| Labels |
backend:RISC-V,
miscompilation,
generated by fuzzer
|
| Assignees |
|
| Reporter |
YuanchengJiang
|
The following code:
IR-only reproducer: InstCombine removes `shl 0` / `mul 1` before codegen, and no C path was found. GlobalISel's own `right_identity_zero` does not remove the shift for types wider than 64 bits. Run with `llc -O2 -mtriple=riscv64 -global-isel repro.ll`; `main` prints two lines, and both should be `-1`.
```llvm
@.s = private constant [4 x i8] c"%d\0A\00"
declare i32 @printf(ptr, ...)
; (sext i32 x to i128) << 0 : GlobalISel reduce_shl_of_extend rewrites it to zext(shl x, 0)
define i128 @f(i32 %x) noinline {
%e = sext i32 %x to i128
%r = shl i128 %e, 0
ret i128 %r
}
; signed 64x64->128 multiply-high by 1 (mul_to_shl makes the shl by 0)
define i64 @g(i64 %x) noinline {
%a = sext i64 %x to i128
%m = mul i128 %a, 1
%s = ashr i128 %m, 64
%r = trunc i128 %s to i64
ret i64 %r
}
define i32 @main() {
%v = call i128 @f(i32 -1)
%h = lshr i128 %v, 96
%t = trunc i128 %h to i32
call i32 (ptr, ...) @printf(ptr @.s, i32 %t) ; expected -1
%w = call i64 @g(i64 -5)
%wt = trunc i64 %w to i32
call i32 (ptr, ...) @printf(ptr @.s, i32 %wt) ; expected -1
ret i32 0
}
```
Resulted in this output:
```
f: ; expected: sext.w a0,a0 ; srai a1,a0,63
slli a0, a0, 32
srli a0, a0, 32
li a1, 0
ret
g: ; expected: srai a0,a0,63
li a0, 0
ret
```
| function | input | expected | GlobalISel -O2 (riscv64 / riscv32) |
|---|---|---|---|
| `f` (bits 96..127) | `x = -1` | `-1` | `0` |
| `g` (signed mul-high by 1) | `x = -5` | `-1` | `0` |
SelectionDAG (no `-global-isel`) is correct. riscv32 `-global-isel` and `-O1` are wrong the same way.
Trunk: https://godbolt.org/z/dEbT9jr5a
Root cause: `CombinerHelper::matchCombineShlOfExtend` rewrites `shl ([sza]ext x), C` to `zext (shl x, C)` when `x` has at least `C` known leading zeros. For `G_SEXT` this is only valid if the sign bit of `x` is known zero. A shift amount C ≥ 1 guarantees that, but C = 0 does not, so `shl (sext x), 0` becomes `zext x`. The shift by 0 survives for s65+ types because `right_identity_zero`'s `0` immediate does not match G_CONSTANTs wider than 64 bits (`getIConstantVRegSExtVal` returns nullopt). `mul_to_shl` creates the same shift from `mul (sext x), 1`.
Suggested fix: in `matchCombineShlOfExtend`, reject `G_SEXT` sources when `MinLeadingZeros == 0` (equivalently, require ShiftAmt > 0 for sext). Separately, let the 0/1/-1 immediates of MIR patterns match constants wider than 64 bits. Validated locally: with the first change, both functions compile to the SelectionDAG code, the AGT check no longer fires, and the 22 in-tree GlobalISel combiner RUN lines that contain G_SHL and G_SEXT give identical output.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs