| Issue |
240293
|
| Summary |
[AArch64] Big-endian: store of the high half of v8i16/v4i32/v8f16/v4f32 emits ST1 {v.d}[1], which reverses the elements (regression from #190320)
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
20000419
|
On aarch64_be, storing the upper half of a 128-bit vector with elements smaller than 64 bits writes the elements in reverse order. This is a regression in clang 23.
```c
typedef unsigned short v8u16 __attribute__((vector_size(16)));
typedef unsigned short v4u16 __attribute__((vector_size(8)));
void store_hi(const v8u16 *in, v4u16 *out) {
v8u16 v = *in;
*out = __builtin_shufflevector(v, v, 4, 5, 6, 7);
}
```
`clang --target=aarch64_be-linux-gnu -O2 -S`:
```asm
// clang 22
ld1 { v0.8h }, [x0]
ext v0.16b, v0.16b, v0.16b, #8
st1 { v0.4h }, [x1]
// clang 23, 24
ld1 { v0.8h }, [x0]
st1 { v0.d }[1], [x1]
```
Godbolt: https://godbolt.org/z/EMKG75E93
With input `{0,1,2,3,4,5,6,7}`, clang 23 and 24 store `{7,6,5,4}` instead of `{4,5,6,7}` (run under `qemu-aarch64_be`; little endian and clang 22 store `{4,5,6,7}`). The same pattern also breaks `-O0` code that goes through memory, for example `vget_lane_u32(vget_high_u32(v), 0)` on `{1,2,3,4}` returns 4 instead of 3, and `vget_high_u16` returns the wrong element as well.
At `-O0` this also breaks NEON intrinsics whose `vget_high` result goes through the stack (`-march=armv8.2-a+dotprod`, results read with `vgetq_lane_u32`/`vget_lane_u32`):
- With `g16[i] = (uint16_t)(0x0102 + 0x0304*i + 7*i*i)`, `vmull_high_u16(vld1q_u16(g16), vld1q_u16(g16 + 8))` should give `0x022b9d44, 0x02efcfe9, 0x03cfe4e4, 0x04cd0529`. aarch64_be `-O0` returns these four products in reverse order, because `vget_high_u16` already returns `g16[7], g16[6], g16[5], g16[4]` before the multiply.
- With `g8[i] = (uint8_t)(0x11*i + 0x0d)`, `vdot_u32(vdup_n_u32(0x77), vget_low_u8(vld1q_u8(g8)), vget_high_u8(vld1q_u8(g8 + 16)))` should give `0x78b5, 0xbd45` but aarch64_be `-O0` gives `0x6b53, 0x137d3`; lane 0 of `vget_high_u8` is `g8[31]` (`0x1c`) instead of `g8[24]` (`0xa5`).
Both are correct at `-O1` and above and on little endian. In both `-O0` cases the high half is stored with `st1 { v0.d }[1]` before `umull`/`udot`. The `vdot*_lane` miscompile at `-O1` and above is a different problem, tracked in #166190.
`ST1 { Vt.d }[1]` stores one 64-bit element, so on big-endian its bytes are in 64-bit order, not in the element order a `v4i16`/`v2i32` store needs. #190320 added `St1Lane128SubvecPat` in `AArch64InstrInfo.td` for `v16i8`, `v8i16`, `v4i32`, `v2i64`, `v8f16`, `v8bf16`, `v4f32` and `v2f64` without a predicate, while the neighbouring vector store patterns are gated on `IsLE` (big-endian has to use `ST1` with the element size). On big-endian the pattern is only correct for `v2i64`/`v2f64`, which have one element per half. `v16i8` is not correct either: storing bytes 8..15 of `"abcdefghijklmnop"` writes `"ponmlkji"`.
Suggested fix (not tested): put the `v16i8`/`v8i16`/`v4i32`/`v8f16`/`v8bf16`/`v4f32` instances under `let Predicates = [IsLE]`, or on big-endian use `ST1i64` only for `v2i64`/`v2f64`.
Versions: clang 22 correct; clang 23 and 24.0.0 (trunk cf44dd9e317e) wrong at `-O0` and `-O2`. `run.c` with `start_a64.S` and `mc.c` is a freestanding runner for `qemu-aarch64_be`.
Disclaimer: This test case was found with LLM-assisted targeted testing, and the report was written with LLM assistance.
Assisted-by: Grok
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs