| Issue |
235610
|
| Summary |
[AArch64][InstCombine] instCombineSVEDupqLane ignores the lane index of sve.dupq.lane
|
| Labels |
backend:AArch64,
miscompilation,
SVE,
llvm:instcombine,
generated by fuzzer
|
| Assignees |
|
| Reporter |
YuanchengJiang
|
The following code:
```c
// AArch64 SVE miscompile: InstCombine (instCombineSVEDupqLane) rewrites svdupq_lane(svset_neonq(v, q), idx)
// into a splat of q's repeating pattern without looking at idx. For idx != 0 the result must come from
// quadword idx of v (or be zero when idx >= the number of quadwords).
// Godbolt: clang --target=aarch64-linux-gnu -march=armv8-a+sve -O1 (also -O2/-O3)
// Expected output (any vector length): 0 0 0 0 / bad=0
#include <arm_sve.h>
#include <arm_neon_sve_bridge.h>
int printf(const char *, ...);
__attribute__((noinline)) svint32_t f(int a, int b) {
int32x4_t q = {a, b, a, b};
// quadword 1 of (zeros with q in quadword 0) is zero (or out of range -> zero at VL=128)
return svdupq_lane_s32(svset_neonq_s32(svdup_n_s32(0), q), 1);
}
int main(void) {
volatile int a = 7, b = 9;
svint32_t r = f(a, b);
int x0 = svlasta_s32(svpfalse_b(), r);
int x1 = svlastb_s32(svptrue_pat_b32(SV_VL2), r);
int x2 = svlastb_s32(svptrue_pat_b32(SV_VL3), r);
int x3 = svlastb_s32(svptrue_pat_b32(SV_VL4), r);
printf("%d %d %d %d\n", x0, x1, x2, x3);
int bad = x0 | x1 | x2 | x3;
printf("bad=%d\n", bad != 0);
return bad != 0;
}
```
Resulted in this output:
```
7 9 7 9
bad=1
```
(expected `0 0 0 0`; clang `--target=aarch64-linux-gnu -march=armv8-a+sve -O1`..`-O3`, run under qemu-aarch64 `-cpu max,sve{128,256,512}=on`; `-O0` prints `0 0 0 0`.)
`svdupq_lane_s32(x, 1)` must broadcast quadword 1 of `x` (zeros here: `x` is zero outside quadword 0; with a 128-bit vector length the index is out of range, which also gives zeros). At -O1 and above `f` becomes `fmov s0, w0; mov v0.s[1], w1; mov z0.d, d0`, a broadcast of quadword 0.
Trunk: https://godbolt.org/z/GeqK5v15G
Root cause: `instCombineSVEDupqLane` (AArch64TargetTransformInfo.cpp) matches `dupq.lane(vector.insert(Default, <insertelement chain>, 0), Idx)` and replaces it with a splat of the simplified insertelement pattern, but never looks at `Idx` (operand 1). The result is only correct for `Idx == 0`. For any other index the intrinsic reads a different quadword (from `Default`, or zeros when `Idx` is at least the number of quadwords, as the TBL-based lowering does), and for a non-constant index the fold is wrong whenever the index is non-zero.
Suggested fix: only apply the fold when `II.getOperand(1)` is the constant 0. Not validated locally.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs