https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128913
Bug ID: 128913
Summary: [AArch64 SVE] wrong-code: constant .s predicate built
with .d trn1 leaves a lane unset (-O3
-msve-vector-bits=256)
Product: gcc
Version: 15.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: lcw at fb dot com
Target Milestone: ---
## Testcase (repro.c)
```c
#include <stdio.h>
__attribute__((noinline)) void select253(float *out, const float *a, const
float *b) {
long m = 253; /* 0b11111101: take b in every lane but lane 1 */
for (int i = 0; i < 8; i++) {
out[i] = (m & 1) ? b[i] : a[i];
m >>= 1;
}
}
int main(void) {
float a[8], b[8], out[8] = {0};
for (int i = 0; i < 8; i++) { a[i] = i; b[i] = 100 + i; }
select253(out, a, b);
int bad = 0;
for (int i = 0; i < 8; i++) {
float expected = ((253 >> i) & 1) ? b[i] : a[i];
if (out[i] != expected) { printf("lane %d: got %g, expected %g\n", i,
out[i], expected); bad = 1; }
}
puts(bad ? "MISCOMPILED" : "ok");
return bad;
}
```
## Commands
```
gcc -O3 -march=armv8-a+sve -msve-vector-bits=256 repro.c -o repro
qemu-aarch64 -cpu max,sve256=on ./repro
```
Output:
```
lane 5: got 0, expected 105
MISCOMPILED
```
Reproduces with GCC 13.4.0 and 15.2.0 (conda-forge builds). Correct at -O2, and
at -O3
without -msve-vector-bits=256. Of the 256 constant masks for this loop, exactly
two are
miscompiled: 2 and 253.
## Generated code (GCC 13.4.0)
```
select253:
...
ptrue p2.s, vl8
ptrue p0.d, vl4
trn1 p0.d, p0.d, p2.d
ptrue p2.b, vl1
ld1w z0.s, p0/z, [x2]
pfalse p1.b
trn1 p1.s, p1.s, p2.s
ptrue p2.b, vl32
ld1w z1.s, p1/z, [x1]
sel z0.s, p0, z0.s, z1.s
st1w z0.s, p2, [x0]
ret
```
p0 is meant to be the .s predicate for mask 253, i.e. lanes {1,0,1,1,1,1,1,1}.
`ptrue p0.d, vl4` only sets the predicate bit of the low .s lane of each .d
element
(only the low bit of each .d element is defined), and `trn1 p0.d, p0.d, p2.d`
takes .d
elements 0 and 2 from it and .d elements 1 and 3 from the all-true p2. Read as
a .s
predicate (by ld1w and sel), p0 is therefore lanes {1,0,1,1,1,0,1,1} (mask
221): lane 5,
the high .s half of .d element 2, is unset. sel then takes lane 5 from z1,
which was
loaded with p1 = {0,1,0,0,0,0,0,0}, so lane 5 of the result is 0.
The constant predicate appears to be synthesized in .d mode, whose upper .s
halves are
"don't care", and then used as a .s predicate.