| Issue |
237721
|
| Summary |
[VectorCombine] foldSelectShuffle pads the rebuilt udiv/sdiv/urem/srem divisor with poison lanes: the division becomes UB and the backend deletes it
|
| Labels |
miscompilation,
generated by fuzzer,
llvm:vectorcombine
|
| Assignees |
|
| Reporter |
YuanchengJiang
|
The following code:
```cpp
/* [VectorCombine] foldSelectShuffle pads the rebuilt binop's input shuffles with poison lanes even when the
* binop is a udiv/sdiv/urem/srem, so the new divisor has poison lanes (immediate UB). The backend then folds
* the whole udiv to undef: f returns t0*12 in every lane.
*
* Godbolt: compiler "armv8-a clang (trunk)" (or x86-64 clang (trunk) with --target=aarch64-linux-gnu), flags: -O2
* buggy f: movi v1.4s,#12 ; mla v1.4s,v0.4s,v1.4s ; mov v0.16b,v1.16b ; ret (no division at all)
* correct (-O2 -mllvm -disable-vector-combine): umull/umull2/uzp2/ushr #3 (x/10) for lanes 0,1 + mul by 12.
* Local run: clang --target=aarch64-linux-gnu -ffreestanding -O2 -S -emit-llvm repro.c; run it in an AArch64 emulator
* EXPECTED (clang -O0, or -O2 -mllvm -disable-vector-combine): 10 20 72 84 0 (exit 0)
* BUGGY (clang -O2, trunk c4a7814f8b): 1200 2400 72 84 1 (exit 1)
*/
#include <arm_neon.h>
int printf(const char *, ...);
__attribute__((noinline)) uint32x4_t f(uint32x4_t x) {
uint32x4_t t0 = x + 1;
uint32x4_t y = vabdq_u32((uint32x4_t){13, 15, 17, 19}, vdupq_n_u32(3)); /* {10,12,14,16} */
uint32x4_t d = __builtin_shufflevector(y, y, 0, 0, 1, 1); /* {10,10,12,12} */
uint32x4_t q = t0 / d;
uint32x4_t m = t0 * d;
return __builtin_shufflevector(q, m, 0, 1, 6, 7); /* {t0[0]/10, t0[1]/10, t0[2]*12, t0[3]*12} */
}
volatile unsigned in[4] = {99, 199, 5, 6};
int main(void) {
uint32x4_t x = {in[0], in[1], in[2], in[3]};
uint32x4_t r = f(x);
unsigned e[4] = {10, 20, 72, 84};
int bad = 0;
for (int i = 0; i < 4; i++) {
printf("%d\n", (int)r[i]); /* actual; expected 10 20 72 84 */
bad |= r[i] != e[i];
}
printf("%d\n", bad); /* 1 = MISMATCH */
return bad;
}
```
Resulted in this output:
```
$ ./repro # clang --target=aarch64-linux-gnu -O2, run in an AArch64 emulator
1200
2400
72
84
1 (MISMATCH, exit 1)
$ ./repro # same with -O2 -mllvm -disable-vector-combine (or -O0)
10
20
72
84
0
; f at -O2: no division at all
f:
movi v1.4s, #12
mla v1.4s, v0.4s, v1.4s
mov v0.16b, v1.16b
ret
```
| lane | expected | actual |
|------|----------|--------|
| 0 (t0/10) | 10 | 1200 |
| 1 (t0/10) | 20 | 2400 |
| 2 (t0*12) | 72 | 72 |
| 3 (t0*12) | 84 | 84 |
Trunk: https://godbolt.org/z/1anW9Wsnz (armv8-a clang trunk, -O2, assembly view; reproduces as of 2026-10-23)
Root cause: `VectorCombine::foldSelectShuffle` (llvm/lib/Transforms/Vectorize/VectorCombine.cpp) rewrites a select-like `shuffle(binop(a,b), binop(c,d))` by building new input shuffles and rebuilding the binops with `CreateBinOp(Op0->getOpcode(), ...)`. The new shuffles are padded with `PoisonMaskElem` in unused lanes, and there is no check for integer division or remainder. For `udiv`/`sdiv`/`urem`/`srem`, a poison lane in the divisor makes the whole instruction immediate UB. The sibling folds `foldShuffleOfBinops`, `foldPermuteOfBinops` and `foldShuffleToIdentity` all guard against this. Here, once the divisor becomes a constant in the DAG, `simplifyDivRem` sees the undef lane and folds the whole `udiv` to undef. The pass is target-independent, and the IR-level UB also appears on x86-64 and RISC-V.
Suggested fix: Return early from `foldSelectShuffle` when either binop `isIntDivRem()`, or pad the divisor shuffles with a defined lane instead of poison. The early return is validated locally (a local tree carries it, and the reproducer then prints `10 20 72 84 0`).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs