| Issue |
235271
|
| Summary |
[clang][NVPTX][DAGCombiner] `__builtin_clzll(x | 1) >> 5` is folded to 0: visitSRL's `srl (ctlz x), log2(BW)` fold uses the i32 result width of NVPTX's ctlz of an i64 value
|
| Labels |
miscompilation,
backend:NVPTX,
generated by fuzzer
|
| Assignees |
|
| Reporter |
YuanchengJiang
|
The following code:
```cpp
/* [NVPTX][DAGCombiner] __builtin_clzll(x | 1) >> 5 is folded to 0.
* NVPTX lowers a 64-bit ctlz to an i32-typed ISD::CTLZ of the i64 value
* (lowerCTLZCTPOP: PTX clz.b64 returns a 32-bit count). DAGCombiner's fold
* (srl (ctlz x), log2(bitwidth)) -> 0 if x has a known-one bit
* takes the bit width from the i32 result, so it treats the count as the
* count of a 32-bit value (at most 31 when x != 0) and folds the shift by 5 to
* 0. The i64 count of x | 1 is 32..63 whenever x < 2^32, so the shift is 1.
*
* Godbolt: compiler "x86-64 clang (trunk)" with flags:
* --target=nvptx64-nvidia-cuda -march=sm_80 -O2
* Godbolt cannot execute PTX: view the PTX of `f`:
* buggy: st.param.b32 [func_retval0], 0; (constant 0)
* correct: clz.b64 %r1, %rd2; shr.u32 %r2, %r1, 5; (1 for x < 2^32)
* Run: on any NVIDIA GPU (e.g. as a CUDA device function); every sm_* is affected.
*
* EXPECTED (correct compiler): f(0) = 1, f(1000) = 1, f(1<<40) = 0
* BUGGY (vanilla trunk): f(0) = 0, f(1000) = 0, f(1<<40) = 0
* MISMATCH (exit 1)
*/
#include <stdio.h>
__attribute__((noinline)) int f(unsigned long long x) {
return __builtin_clzll(x | 1) >> 5; /* 1 iff the count is >= 32, i.e. x < 2^32 */
}
volatile unsigned long long g0 = 0, g1 = 1000, g2 = 1ull << 40;
int main(void) {
int a = f(g0), b = f(g1), c = f(g2);
printf("f(0) = %d (expected 1)\n", a);
printf("f(1000) = %d (expected 1)\n", b);
printf("f(1<<40) = %d (expected 0)\n", c);
int ok = a == 1 && b == 1 && c == 0;
if (!ok)
printf("MISMATCH\n");
return !ok;
}
```
Resulted in this output:
```
; PTX of f (clang --target=nvptx64-nvidia-cuda -march=sm_80 -O2)
.visible .func (.param .b32 func_retval0) f(
.param .b64 f_param_0
)
{
st.param.b32 [func_retval0], 0; // constant 0 for every input
ret;
}
```
Correct PTX is `or.b64 %rd2, %rd1, 1; clz.b64 %r1, %rd2; shr.u32 %r2, %r1, 5;`.
| input x | expected f(x) | actual f(x) |
|---------|---------------|-------------|
| 0 | 1 | 0 |
| 1000 | 1 | 0 |
| 1<<40 | 0 | 0 |
(Expected values come from running the program with gcc -O2 on x86-64. No GPU was available, so the actual values are read from the PTX.)
Trunk: https://godbolt.org/z/7f4aqfo7r (x86-64 clang trunk, `--target=nvptx64-nvidia-cuda -march=sm_80 -O2`, PTX view; reproduces as of 2026-10-22)
Root cause: `lowerCTLZCTPOP` in llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp lowers a 64-bit ctlz to an `i32`-typed `ISD::CTLZ` whose operand is the `i64` value, because PTX `clz.b64` returns a 32-bit count. `DAGCombiner::visitSRL` (llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp) folds `srl (ctlz x), log2(BW)` to 0 when `x` has a known-one bit. It takes `BW` from the 32-bit result, so it reasons "ctlz of a nonzero value is < 32". For the 64-bit operand the count can be up to 63, and `>> 5` is 1 for every nonzero `x < 2^32`. The sibling cases of this fold, `SimplifySetCC`'s `(srl (ctlz x), 5) == 0`, and `SimplifySelectCC`'s `select_cc x == 0, BW, ctlz(x)` fold make the same width assumption (`x ? __builtin_clzll(x) : 32` gives 64 for x = 0 on NVPTX).
Suggested fix: In `visitSRL`, also require `N0.getOperand(0).getScalarValueSizeInBits() == OpSizeInBits` (and guard the sibling folds the same way), or have NVPTX stop building mixed-width CTLZ/CTPOP nodes. The visitSRL guard is validated locally (a local tree carries it, and `f` then compiles to `clz.b64; shr.u32 5`).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs