| Issue |
244528
|
| Summary |
[fuzzer-found][PowerPC] combineBVOfVecSExt ignores the sign_extend_inreg width: `(signed char)v[i]` of a `short` vector becomes `vextsh2w`
|
| Labels |
backend:PowerPC,
miscompilation,
generated by fuzzer
|
| Assignees |
|
| Reporter |
YuanchengJiang
|
The following code:
```cpp
// clang --target=powerpc64le-linux-gnu -mcpu=pwr9 -O0 repro.c (lanes 1,3,5,7 trigger on LE)
// clang --target=powerpc64-linux-gnu -mcpu=pwr9 -O0 repro.c (lanes 0,2,4,6 trigger on BE)
// Each lane is (signed char) of a short lane, so 0x0080 must become -128, but vextsh2w sign-extends the
// whole halfword and gives +128.
int printf(const char *, ...);
typedef short v8s __attribute__((vector_size(16)));
typedef int v4i __attribute__((vector_size(16)));
__attribute__((noinline)) v4i even(v8s v) {
return (v4i){(signed char)v[0], (signed char)v[2], (signed char)v[4], (signed char)v[6]};
}
__attribute__((noinline)) v4i odd(v8s v) {
return (v4i){(signed char)v[1], (signed char)v[3], (signed char)v[5], (signed char)v[7]};
}
int main(void) {
v8s v = {0x80, 0x80, 0x7F, 0x7F, 0x1FF, 0x1FF, 0x100, 0x100};
int want[4] = {-128, 127, -1, 0};
v4i e = even(v), o = odd(v);
int bad = 0;
for (int i = 0; i < 4; i++) {
if (e[i] != want[i] || o[i] != want[i]) {
printf("lane %d: even %d odd %d, expected %d\n", i, e[i], o[i], want[i]);
bad = 1;
}
}
printf(bad ? "FAIL\n" : "PASS\n");
return bad;
}
```
Resulted in this output (powerpc64, `-mcpu=pwr9 -O0`, run in qemu-ppc64 `-cpu power9`):
```
lane 0: even 128 odd -128, expected -128
lane 2: even 511 odd -1, expected -1
lane 3: even 256 odd 0, expected 0
FAIL
```
| function | lanes | expected | actual (BE) |
|---|---|---|---|
| `even()` | v[0],v[2],v[4],v[6] = 0x80, 0x7F, 0x1FF, 0x100 | -128 127 -1 0 | 128 127 511 256 |
| `odd()` | v[1],v[3],v[5],v[7] | -128 127 -1 0 | -128 127 -1 0 |
On little-endian powerpc64le, `odd()` is the wrong one: both functions are wrong when their lane indices need a shuffle. The wrong instruction is `vextsh2w` (halfword to word); the correct one is `vextsb2w`. The bug shows at `-O0` (the default debug build). At `-O1`+, the IR reaching codegen is a shufflevector plus sext and the code is correct.
Trunk: https://godbolt.org/z/crnz8nEW7
Root cause: `combineBVOfVecSExt` (PPCISelLowering.cpp) turns a BUILD_VECTOR of `sign_extend_inreg(any_extend(extract_vector_elt V, i))` into a shuffle plus a `vexts*2*` instruction. It picks the instruction from V's element type (i16) and the result type (i32). It never checks that the `sign_extend_inreg` extends from V's element type: here it extends from i8. Sign-extending the whole halfword instead of the low byte gives 128 for 0x0080.
Suggested fix: in `isSExtOfVecExtract`, for SIGN_EXTEND_INREG require `cast<VTSDNode>(Op.getOperand(1))->getVT() == Input element type`. Validated locally: with the check, qemu prints `-128 127 -1 0` for both functions, the AGT check no longer fires, and 67 llc RUN lines of the related PowerPC tests give identical output.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs