| Issue |
238712
|
| Summary |
[AArch64] vectorToScalarBitmask without NEON (streaming-compatible SVE): `bitcast <16 x i1> to i16` drops lanes 8..15 because their mask bits 1<<8..1<<15 are truncated to i8
|
| Labels |
backend:AArch64,
miscompilation,
generated by fuzzer
|
| Assignees |
|
| Reporter |
YuanchengJiang
|
The following code:
```c
// clang --target=aarch64-linux-gnu -march=armv9-a+sme -O2 repro.c && ./a.out (also -O0, -march=armv8-a+sve; any SME/SVE machine or qemu-aarch64 -cpu max)
// Expected: ff00 (lanes 8..15 negative). Actual in the __arm_streaming_compatible function: 0. Without the attribute: ff00.
#include <stdio.h>
typedef unsigned short u16;
typedef signed char v16i8 __attribute__((vector_size(16)));
typedef _Bool v16b __attribute__((ext_vector_type(16)));
__attribute__((noinline)) u16 movemask(v16i8 a) __arm_streaming_compatible {
v16b m = __builtin_convertvector(a < 0, v16b);
return __builtin_bit_cast(u16, m);
}
int main(void) {
v16i8 a = {0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1};
u16 r = movemask(a);
printf("%x\n", r);
return r != 0xff00;
}
```
Resulted in this output:
```
$ clang --target=aarch64-linux-gnu -march=armv9-a+sme -O2 repro.c && qemu-aarch64 -cpu max ./a.out
0 (expected ff00; same at -O0 and with -march=armv8-a / armv8-a+sve)
```
The same function without `__arm_streaming_compatible` prints `ff00`. The code is `cmplt` / `mov z0.b, p1/z, #-1` / `and` with a byte mask / `uaddv d0, p0, z0.b`. Lanes 8..15 are ANDed with 0, so they never reach the sum. A smaller IR case is in repro.ll: an all-true `<16 x i1>` gives 255 instead of 65535 with `llc -mtriple=aarch64 -mattr=+sve2 -force-streaming-compatible`.
Trunk: https://godbolt.org/z/E7xa3d6xE (AArch64 clang trunk `-march=armv9-a+sme -O2`: `movemask` is the `and` + `uaddv` sequence above)
Root cause: `vectorToScalarBitmask` (llvm/lib/Target/AArch64/AArch64ISelLowering.cpp) has a v16i8 special case that ANDs with two halves of 1, 2, ..., 128 and packs them with three ADDPs. It is guarded by `isNeonAvailable()`. In streaming-compatible code NEON is not available, so v16i8 falls into the generic path. That path builds the mask constants `1 << I` for I = 0..15 into a `v16i8` BUILD_VECTOR, which truncates 1<<8..1<<15 to 0, and then does a VECREDUCE_ADD.
Suggested fix: Bail out of the generic path when the lane type cannot hold one bit per lane. The caller (`combineBitcast...` / the store combine) then uses the default expansion:
```diff
- // All other vector sizes.
+ // All other vector sizes. Each lane needs a bit of its own in the lane type;
+ // v16i8 without NEON (streaming SVE) would truncate the masks of lanes 8..15.
unsigned NumEl = VecVT.getVectorNumElements();
+ if (VecVT.getScalarSizeInBits() < NumEl)
+ return SDValue();
```
(fix.diff applies to 8b5ece820d. Another option is an SVE form of the two-half packing.)
The in-tree tests already encode the bug. In `sve-streaming-mode-fixed-length-masked-{load,store,expandload}.ll`, the `-force-streaming-compatible` CHECK lines of `masked_load_v16i8` and its siblings build the lane mask from lanes 0..7 only (`and w9, w9, #0xffffff80` sign-fills lane 7). The later `tbnz w8, #8..#15` tests therefore read lane 7's bit, so a masked load or store of `<16 x i8>` uses lane 7's mask for lanes 8..15. With the fix these tests change to the generic per-lane mask and need regenerating. No other AArch64 streaming test output changes except for equivalent vselect code.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs