| Issue |
244166
|
| Summary |
[Mips] MSA performORCombine folds `c ? splat(-1) : x` (expanded as or/and/xor) to splat(-1), dropping the select
|
| Labels |
backend:MIPS,
miscompilation,
generated by fuzzer
|
| Assignees |
|
| Reporter |
YuanchengJiang
|
The following code:
```cpp
// MIPS MSA: (a < b ? (v4i16)-1 : x) + x always takes the all-ones arm (the select is folded away).
// clang --target=mipsel-linux-gnu -march=mips32r5 -mmsa -mfp64 -O2 (also -O1/-O3, mips64r5/r6 -mmsa; -O0 is correct)
int printf(const char *, ...);
typedef short v4i16 __attribute__((vector_size(8)));
__attribute__((noinline)) void f(v4i16 *p, v4i16 *q, int a, int b) {
v4i16 x = *p;
*q = (a < b ? (v4i16){-1, -1, -1, -1} : x) + x;
}
int main(void) {
v4i16 in = {1, 2, 3, 4}, out;
f(&in, &out, 1, 0); /* 1 < 0 is false: expect x + x = {2, 4, 6, 8} */
printf("%d %d %d %d (expected 2 4 6 8)\n", out[0], out[1], out[2], out[3]);
int bad = out[0] != 2 || out[1] != 4 || out[2] != 6 || out[3] != 8;
printf(bad ? "MISCOMPILE\n" : "OK\n");
return bad;
}
```
Resulted in this output (`clang --target=mipsel-linux-gnu -march=mips32r5 -mmsa -mfp64 -O2`, run under `qemu-mipsel -cpu P5600` through an equivalent freestanding program, `repro_freestanding.c`, that prints in hex):
```
0000 0001 0002 0003 expected 0002 0004 0006 0008
MISCOMPILE
```
| build | `f(&in, &out, 1, 0)` with in = {1,2,3,4} |
|---|---|
| expected (`1 < 0` is false: x + x) / `-O0` | 2 4 6 8 |
| `-O1` / `-O2` / `-O3` with `-mmsa` | **0 1 2 3** (i.e. -1 + x) |
The generated code no longer computes the condition at all: `ldi.b $w0, -1; addv.h ...`. Without `-mmsa` the code is correct.
Trunk: https://godbolt.org/z/h9jKbb973
Root cause: `performORCombine` (MipsSEISelLowering.cpp) looks for `(or (and Cond, IfSet), (and ~Cond, IfClr))` to form a VSELECT/bit-select. If `Op0Op0` or `Op0Op1` is a constant splat, it sets `IsConstantMask = true` and records `Mask`, even when the matching inverse constant is not found and `IfClr` stays unset. The later non-constant search then matches `or(and(M, C), and(X, xor(M, -1)))` with `Cond = M` and `IfSet = C`. Because the stale all-ones `Mask` and `IsConstantMask` are still set, the degenerate "mask is all ones" fold fires and returns `IfSet`, so the select disappears. The or/and/xor shape comes from expanding a widened v4i16 select.
Suggested fix: set `IsConstantMask` only when the inverse constant is actually found, or reset it before the non-constant search. Validated locally (reset when `IfClr` is unset): the program prints `2 4 6 8` in qemu, the AGT check no longer fires, and 185 llc RUN lines of the MSA vector tests give unchanged output.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs