| Issue |
244584
|
| Summary |
[fuzzer-found][NVPTX] LowerShiftRightParts/LowerShiftLeftParts use a plain ISD shift by (64 - amt): for a constant amt == 0 it folds to undef and the low half becomes -1
|
| Labels |
miscompilation,
backend:NVPTX,
generated by fuzzer
|
| Assignees |
|
| Reporter |
YuanchengJiang
|
The following code:
IR-only reproducer (C/`opt -O2` fold such shift amounts first); run with `llc -O2 -mtriple=nvptx64 -mcpu=sm_90 repro.ll -o -`:
```llvm
; [NVPTX] LowerShiftRightParts / LowerShiftLeftParts use a plain ISD::SHL/SRL by
; (64 - ShAmt), which is a shift by 64 (undefined at DAG level) when ShAmt == 0. When the
; parts' shift amount is the constant 0 (e.g. the low part of an i128 amount whose only
; possible nonzero bit is >= 64), DAG folding turns it into undef -> `or` -> -1, and the low
; half of the "shifted" value becomes -1.
; Here: ushl.sat(b, pdep(b, 1<<96...)): lanes with b even shift by 0 and must return b, but the
; saturation check compares against the bogus -1 and saturates.
; Godbolt: compiler "llc (trunk)" (llctrunk), flags: -O2 -mtriple=nvptx64 -mcpu=sm_90
; buggy: mov.b64 %rd13, -1 ; ... ; selp.b64 %rd12, -1, %rd9, %p1 (check against the constant -1)
; correct: shr.u64/shl.b64 by the real amounts and a real round-trip compare (no constant -1).
define <3 x i127> @f(<3 x i127> %b) {
%v0 = call <3 x i127> @llvm.pdep.v3i127(<3 x i127> %b, <3 x i127> <i127 79228162514264337593543950336, i127 5192296858534827628530496329220096, i127 1125899906842624>)
%v1 = call <3 x i127> @llvm.ushl.sat.v3i127(<3 x i127> %b, <3 x i127> %v0)
ret <3 x i127> %v1
}
; Function Attrs: nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none)
declare <3 x i127> @llvm.pdep.v3i127(<3 x i127>, <3 x i127>) #0
; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <3 x i127> @llvm.ushl.sat.v3i127(<3 x i127>, <3 x i127>) #1
attributes #0 = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
```
Resulted in this output:
```
; lane 0 of @f
shl.b64 %rd9, %rd5, 1;
not.b64 %rd10, %rd9;
or.b64 %rd11, %rd10, %rd8;
setp.ne.b64 %p1, %rd11, 0;
selp.b64 %rd12, -1, %rd9, %p1; ; saturate unless the value is all-ones
mov.b64 %rd13, -1; ; the folded "hi << 64": undef -> or -> -1
```
For an even `b`, `pdep(b, 1<<96)` is 0, so `ushl.sat(b, 0)` must return `b`. The saturation check `(b << s) >> s == b` is compared against the bogus constant -1, so the lane saturates.
| lane input b | expected | actual |
|--------------|----------|--------|
| any even b (shift 0) | b | saturated (all ones) unless b is all-ones |
With the fix, the PTX uses `shr.u64`/`shl.b64` by the real amounts and a real round-trip compare (no constant -1).
Trunk: https://godbolt.org/z/xoEzdsdMd (llc trunk, `-O2 -mtriple=nvptx64 -mcpu=sm_90`; reproduces as of 2026-10-22)
Root cause: `LowerShiftRightParts` in llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp computes `dLo = (aLo >> Amt) | (aHi << (64 - Amt))` with a plain `ISD::SHL`. PTX `shl.b64` clamps the amount, so this works at run time for `Amt == 0`. In the DAG, though, a shift by >= the bit width is undefined. When `Amt` is the constant 0, `getNode` folds `shl hi, 64` to undef, and then `or lo, undef` to -1. `LowerShiftLeftParts` has the same flaw with `srl lo, 64 - Amt`.
Suggested fix: Use `NVPTXISD::SHL_CLAMP` / `NVPTXISD::SRL_CLAMP` (which already exist to model PTX's clamping shifts) for the `64 - Amt` shifts in both parts lowerings, or guard `Amt == 0`. Validated locally (a local tree carries this fix, and the reproducer then computes the real comparison).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs