| Issue |
241100
|
| Summary |
[GlobalISel] shift_of_shifted_logic_chain distributes G_USHLSAT/G_SSHLSAT over AND/XOR, miscompiling saturating fixed-point shifts on RISC-V with -fglobal-isel
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
20000419
|
`CombinerHelper::matchShiftOfShiftedLogic` rewrites `SHIFT (LOGIC (SHIFT X, C0), Y), C1` into `LOGIC (SHIFT X, C0+C1), (SHIFT Y, C1)` and accepts `G_USHLSAT` / `G_SSHLSAT` as the shift. That is valid for plain shifts but not for saturating shifts: whether the inner shift saturates depends on bits that the AND/XOR later clears or flips.
C reproducer (Embedded-C fixed point, `_Sat` left shifts lower to `llvm.ushl.sat`):
```c
#include <stdio.h>
#include <string.h>
typedef _Sat unsigned _Accum usa;
static unsigned bits(usa a){unsigned u;memcpy(&u,&a,4);return u;}
static usa fx(unsigned u){usa a;memcpy(&a,&u,4);return a;}
__attribute__((noinline)) unsigned us_and(unsigned x, unsigned y){
usa a = fx(x) << 20;
usa b = fx(bits(a) & y);
return bits(b << 11);
}
int main(void){ printf("and=%u\n", us_and(8,15)); return 0; }
```
```console
$ clang --target=riscv64-linux-gnu -static -ffixed-point -O2 -fglobal-isel t.c && qemu-riscv64 ./a.out
and=30720
$ clang --target=riscv64-linux-gnu -static -ffixed-point -O2 t.c && qemu-riscv64 ./a.out
and=0
```
Expected `and=0`: `ushl.sat(8, 20) = 0x800000`, `0x800000 & 15 = 0`, `ushl.sat(0, 11) = 0`. The combined form computes `ushl.sat(8, 31) & ushl.sat(15, 11) = 0xffffffff & 30720 = 30720`. `-O1`, `-O2`, `-O3` and `-Os` with `-fglobal-isel` print 30720; `-O0 -fglobal-isel` and SelectionDAG print 0.
Godbolt (trunk, `-O2 -fglobal-isel` vs `-O2`): https://godbolt.org/z/nfGG799qb
The same IR directly (`llc -mtriple=riscv64 -O2 -global-isel`):
```llvm
declare i32 @llvm.ushl.sat.i32(i32, i32)
define i32 @us_and(i32 %x, i32 %y) noinline {
%a = call i32 @llvm.ushl.sat.i32(i32 %x, i32 20)
%b = and i32 %a, %y
%c = call i32 @llvm.ushl.sat.i32(i32 %b, i32 11)
ret i32 %c
}
```
MIR before / after `riscv-prelegalizer-combiner`:
```
%5:_(s32) = G_USHLSAT %0, 20 -> %11:_(s32) = G_USHLSAT %0, 31
%6:_(s32) = G_AND %5, %1 -> %12:_(s32) = G_USHLSAT %1, 11
%8:_(s32) = G_USHLSAT %6, 11 -> %8:_(s32) = G_AND %11, %12
```
Godbolt (llc trunk, IR): https://godbolt.org/z/8jK66YG9b
The same happens for XOR (`ushl.sat(ushl.sat(8, 20) ^ 15, 11)` is `4294967295`; GlobalISel gives `4294936575`) and for `sshl.sat` with AND (`sshl.sat(sshl.sat(1, 20) & 1048575, 11)` is `0`; GlobalISel gives `2147481600`).
Versions: the C test is wrong with clang 22, 23 and trunk (24.0.0, e2b36dd0611a) at `-O1` and above with `-fglobal-isel`. clang 19, 20 and 21 print the correct value: RISC-V GlobalISel could not legalize `G_USHLSAT` before #155642, so those versions fall back to SelectionDAG. AArch64 and x86 GlobalISel still cannot legalize `G_USHLSAT`, so they do not reach a wrong result today.
Where: `llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp`, `matchShiftOfShiftedLogic`; its comment and assert explicitly list `G_USHLSAT/G_SSHLSAT`. RISC-V runs it via `all_combines` in `RISCVPreLegalizerCombiner`. The DAGCombiner equivalent (`combineShiftOfShiftedLogic`) only handles plain shifts.
Possible fix (not tested): do not match `G_USHLSAT` / `G_SSHLSAT` in `matchShiftOfShiftedLogic` (keep `G_SHL` / `G_ASHR` / `G_LSHR`). The sibling `matchShiftImmedChain` (shift of shift, same opcode) is fine for saturating shifts and can stay.
cc @topperc @davemgreen
Disclaimer: This test case was found with LLM-assisted targeted testing, and the report was written with LLM assistance.
Assisted-by: Grok
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs