| Issue |
235270
|
| Summary |
[clang][RISCV] canonicalizeMaskForVLPredicate accepts a wrapping (mod 2^EltBits) step sequence, so lanes of `(splat(b) | <..., 4,5,6,7>) < n` are computed wrong
|
| Labels |
backend:RISC-V,
miscompilation,
generated by fuzzer
|
| Assignees |
|
| Reporter |
YuanchengJiang
|
The following code:
```cpp
/* [RISCV] canonicalizeMaskForVLPredicate (RISCVISelLowering.cpp, called from performSETCCCombine) rewrites
* setcc ult (add nuw / or disjoint splat(base), <a, a+1, a+2, ...>), splat(n)
* -> setcc ult <0, 1, 2, ...>, splat(n - umin(n, base + a))
* but BuildVectorSDNode::isArithmeticSequence matches sequences modulo 2^EltBits and infers `a` through poison lanes:
* here lanes 60..63 hold 4,5,6,7, so a = 4 - 60 = 200 (mod 256), and lane i is compared as base + 200 + i (>= 256,
* i.e. lane index i >= n - (base + 200)) instead of as base + (i - 56).
* f(0, 250): every lane 60..63 is (0|4..7) < 250 = true, so f must return -4; the compiled code returns 0.
*
* Godbolt: x86-64 clang (trunk), flags: --target=riscv64-linux-gnu -march=rv64gcv -O2
* (also -march=rv64gc (no V: the combine runs before type legalization), -O1, -O3; -O0 is correct)
* buggy f: ... andi a0,a0,248 ; addi a0,a0,-56 ; ... sub ; ... vid.v ; vmsltu.vx (lane-index compare)
* EXPECTED: -4 0 BUGGY: 0 1
*/
int printf(const char *, ...);
typedef unsigned char v64u8 __attribute__((vector_size(64)));
typedef unsigned char v4u8 __attribute__((vector_size(4)));
typedef signed char v64i8 __attribute__((vector_size(64)));
/* c: lanes 0..59 undefined (shufflevector index -1), lanes 60..63 = 4,5,6,7. b | c is `or disjoint`
* (x & 0xF8 has no bits in common with 4..7). Returns the sum of the mask lanes 60..63 (-1 per true lane). */
__attribute__((noinline)) int f(unsigned char x, unsigned char n) {
v64u8 c = __builtin_shufflevector((v4u8){4, 5, 6, 7}, (v4u8){4, 5, 6, 7}, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,1,2,3);
v64u8 b = (v64u8){} + (unsigned char)(x & 0xF8);
v64i8 m = (v64i8)((b | c) < n);
return m[60] + m[61] + m[62] + m[63];
}
volatile unsigned char X = 0, NN = 250;
int main(void) {
int r = f(X, NN);
int e = 0;
for (int i = 60; i < 64; i++) e -= (((X & 0xF8) | (i - 56)) < NN);
printf("%d\n", r); /* expected -4 */
printf("%d\n", r != e); /* 1 = MISMATCH */
return r != e;
}
```
Resulted in this output:
```
0
1
```
| build (`--target=riscv64-linux-gnu`, qemu-riscv64 8.2) | `f(0, 250)` |
|---|---|
| expected | `-4` |
| `-march=rv64gcv -O0` | `-4` |
| `-march=rv64gcv -O1 / -O2 / -O3` | `0` |
| `-march=rv64gc -O2` (no V) | `0` |
| `riscv32 -march=rv32gcv / rv32gc -O2` | `0` |
Trunk: https://godbolt.org/z/fW8fa6TnK
Root cause: `canonicalizeMaskForVLPredicate` (RISCVISelLowering.cpp, called from `performSETCCCombine`) rewrites `setcc ult (add nuw/or disjoint splat(b), <a, a+1, ...>), splat(n)` into `setcc ult stepvector, splat(n - umin(n, b + a))`. It takes `a` from `BuildVectorSDNode::isArithmeticSequence`, which works modulo 2^EltBits and infers the start through undef lanes. Here lanes 60..63 hold 4,5,6,7, so it infers a = 4 - 60 = 200 (mod 256). The rewrite is only valid if every lane value a + i is exact, but 200 + 63 wraps past 255. As a result, lane i is compared as b + 200 + i instead of b + (i - 56), and all four lanes become false.
Suggested fix: before rewriting, require that `Start + (NumElts - 1)` does not overflow the element type: `uadd_ov` for unsigned compares, `sadd_ov` for signed ones (with NumElts - 1 representable). Validated locally: with this check the program prints `-4` in qemu on rv64gcv and rv64gc, the AGT check no longer fires, and 35 in-tree RISC-V RUN lines with ordered `icmp` plus `add nuw`/`nsw`/`or disjoint` give identical output.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs