[AArch64] tryToConvertShuffleOfTbl2ToTbl4 merges two tbl2 into one tbl4 without keeping out-of-range indices out of range
The following code:
// AArch64: shufflevector(tbl2(a,b,m1), tbl2(c,d,m2)) -> tbl4(a,b,c,d,m') ignores out-of-range tbl2 indices.
// clang --target=aarch64-linux-gnu -O2 repro.c (Godbolt: armv8-a clang trunk, -O2; execution needs an AArch64 host)
// Expected output: "0 0 0" (tbl2 returns 0 for indices >= 32). AArch64 -O1/-O2 prints "3 4 255"-style values from c/d.
#include <arm_neon.h>
int printf(const char *, ...);
__attribute__((noinline)) uint8x16_t f(uint8x16_t a, uint8x16_t b, uint8x16_t c, uint8x16_t d, unsigned char v) {
uint8x16_t m1 = vdupq_n_u8(v); /* variable index (40 at run time) */
const uint8x16_t m2 = {0, 4, 8, 12, 16, 20, 24, 255, 0, 0, 0, 0, 0, 0, 0, 0}; /* 255: out of range */
uint8x16x2_t ab = {{a, b}}, cd = {{c, d}};
uint8x16_t t1 = vqtbl2q_u8(ab, m1), t2 = vqtbl2q_u8(cd, m2);
return vcombine_u8(vget_low_u8(t1), vget_low_u8(t2));
}
int main(void) {
uint8x16_t a = vdupq_n_u8(1), b = vdupq_n_u8(2), c = vdupq_n_u8(3), d = vdupq_n_u8(4);
volatile unsigned char v = 40;
uint8x16_t r = f(a, b, c, d, v);
int e0 = vgetq_lane_u8(r, 0), e15 = vgetq_lane_u8(r, 15);
printf("%d\n", e0); /* expected 0: index 40 is out of range for a 2-register table */
printf("%d\n", e15); /* expected 0: index 255 is out of range */
return !(e0 == 0 && e15 == 0);
}
Resulted in this output (clang --target=aarch64-linux-gnu -O2; executed on Godbolt trunk and in an AArch64 emulator):
3
2
| build | lane 0 (mask index 40) | lane 15 (mask index 255) |
|---|---|---|
expected (two tbl2) / -O0 |
0 | 0 |
-O1 / -O2 / -O3 |
3 (byte of c) |
2 (byte of b) |
The two tbl2 and the shuffle are merged into one tbl {v0.16b, v1.16b, v2.16b, v3.16b} with a 4-register table.
Trunk: https://godbolt.org/z/xEKfGEfY9
Root cause: tryToConvertShuffleOfTbl2ToTbl4 (AArch64ISelLowering.cpp) folds shufflevector(tbl2(a,b,m1), tbl2(c,d,m2)) into tbl4(a,b,c,d,m'). For lanes from the first tbl2 it copies m1[j], and for lanes from the second it uses m2[j] + 32. A 2-register tbl returns 0 for an index >= 32, but a 4-register tbl only does so for >= 64. So:
- a first-mask index in [32, 64), whether constant or variable as in this example, now reads
c/dinstead of giving 0; - a second-mask index >= 224 (e.g. 255) becomes 256..287, which is 0..31 as a byte, and reads
a/b.
LLVM's own test arm64-tbl.ll (shuffled_tbl2_to_tbl4_nonconst_first_mask*) encodes the wrong code.
Suggested fix: only fold when every used first-mask element is a constant. Keep an out-of-range constant out of range by mapping indices >= 32 to 255, and do the same for the second mask (indices >= 32 map to 255; the others get +32). Validated locally: the program prints 0 0, the AGT check no longer fires, and among 157 llc RUN lines of the AArch64 tests that mention tbl, the only output changes are the two shuffled_tbl2_to_tbl4_nonconst_first_mask* functions that encode this bug.
@llvm/issue-subscribers-backend-aarch64
Author: Jiang Yuancheng (YuanchengJiang)
// AArch64: shufflevector(tbl2(a,b,m1), tbl2(c,d,m2)) -> tbl4(a,b,c,d,m') ignores out-of-range tbl2 indices.
// clang --target=aarch64-linux-gnu -O2 repro.c (Godbolt: armv8-a clang trunk, -O2; execution needs an AArch64 host)
// Expected output: "0 0 0" (tbl2 returns 0 for indices >= 32). AArch64 -O1/-O2 prints "3 4 255"-style values from c/d.
#include <arm_neon.h>
int printf(const char *, ...);
__attribute__((noinline)) uint8x16_t f(uint8x16_t a, uint8x16_t b, uint8x16_t c, uint8x16_t d, unsigned char v) {
uint8x16_t m1 = vdupq_n_u8(v); /* variable index (40 at run time) */
const uint8x16_t m2 = {0, 4, 8, 12, 16, 20, 24, 255, 0, 0, 0, 0, 0, 0, 0, 0}; /* 255: out of range */
uint8x16x2_t ab = {{a, b}}, cd = {{c, d}};
uint8x16_t t1 = vqtbl2q_u8(ab, m1), t2 = vqtbl2q_u8(cd, m2);
return vcombine_u8(vget_low_u8(t1), vget_low_u8(t2));
}
int main(void) {
uint8x16_t a = vdupq_n_u8(1), b = vdupq_n_u8(2), c = vdupq_n_u8(3), d = vdupq_n_u8(4);
volatile unsigned char v = 40;
uint8x16_t r = f(a, b, c, d, v);
int e0 = vgetq_lane_u8(r, 0), e15 = vgetq_lane_u8(r, 15);
printf("%d\n", e0); /* expected 0: index 40 is out of range for a 2-register table */
printf("%d\n", e15); /* expected 0: index 255 is out of range */
return !(e0 == 0 && e15 == 0);
}
Resulted in this output (clang --target=aarch64-linux-gnu -O2; executed on Godbolt trunk and in an AArch64 emulator):
3
2
| build | lane 0 (mask index 40) | lane 15 (mask index 255) |
|---|---|---|
expected (two tbl2) / -O0 |
0 | 0 |
-O1 / -O2 / -O3 |
3 (byte of c) |
2 (byte of b) |
The two tbl2 and the shuffle are merged into one tbl {v0.16b, v1.16b, v2.16b, v3.16b} with a 4-register table.
Trunk: https://godbolt.org/z/xEKfGEfY9
Root cause: tryToConvertShuffleOfTbl2ToTbl4 (AArch64ISelLowering.cpp) folds shufflevector(tbl2(a,b,m1), tbl2(c,d,m2)) into tbl4(a,b,c,d,m'). For lanes from the first tbl2 it copies m1[j], and for lanes from the second it uses m2[j] + 32. A 2-register tbl returns 0 for an index >= 32, but a 4-register tbl only does so for >= 64. So:
- a first-mask index in [32, 64), whether constant or variable as in this example, now reads
c/dinstead of giving 0; - a second-mask index >= 224 (e.g. 255) becomes 256..287, which is 0..31 as a byte, and reads
a/b.
LLVM's own test arm64-tbl.ll (shuffled_tbl2_to_tbl4_nonconst_first_mask*) encodes the wrong code.
Suggested fix: only fold when every used first-mask element is a constant. Keep an out-of-range constant out of range by mapping indices >= 32 to 255, and do the same for the second mask (indices >= 32 map to 255; the others get +32). Validated locally: the program prints 0 0, the AGT check no longer fires, and among 157 llc RUN lines of the AArch64 tests that mention tbl, the only output changes are the two shuffled_tbl2_to_tbl4_nonconst_first_mask* functions that encode this bug.
Reproduced on an AArch64 macOS host with Homebrew clang 22.1.8 / 23.1.4 and Apple clang 21.0.0. Sweeping all 256 values of the variable index at -O2:
- lane taken from the first mask (variable index): wrong for 32 values (32..63), which now read from
c/d - lane taken from the second mask (constant 255): wrong in all 256 runs, since 255 + 32 wraps to 31
-O0 is correct in all cases.
A related problem in the same loop: a poison shuffle lane (-1) passes the ShuffleMask[I] < 16 check and reaches Mask1.getOperand(-1). llc -O2 -mtriple=aarch64 22.1.8 and 23.1.4 segfault in LowerVECTOR_SHUFFLE on:
declare <16 x i8> @llvm.aarch64.neon.tbl2.v16i8(<16 x i8>, <16 x i8>, <16 x i8>)
define <16 x i8> @g(<16 x i8> %a, <16 x i8> %b, <16 x i8> %c, <16 x i8> %d) {
%t1 = call <16 x i8> @llvm.aarch64.neon.tbl2.v16i8(<16 x i8> %a, <16 x i8> %b, <16 x i8> <i8 0, i8 1, i8 2, i8 3, i8 4, i8 5, i8 6, i8 7, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0>)
%t2 = call <16 x i8> @llvm.aarch64.neon.tbl2.v16i8(<16 x i8> %c, <16 x i8> %d, <16 x i8> <i8 0, i8 1, i8 2, i8 3, i8 4, i8 5, i8 6, i8 7, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0>)
%r = shufflevector <16 x i8> %t1, <16 x i8> %t2, <16 x i32> <i32 poison, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23>
ret <16 x i8> %r
}
Possible fix: only merge when every selected mask element is a constant or undef, map indices >= 32 to 255, and handle negative shuffle lanes. This changes the expected output of the *_nonconst_first_mask* tests in arm64-tbl.ll, which currently pass the variable index through unchanged. I have checked the index mapping exhaustively in a small model; I have not built LLVM with the change yet.