Issue 244169
Summary [AArch64] tryToConvertShuffleOfTbl2ToTbl4 merges two tbl2 into one tbl4 without keeping out-of-range indices out of range
Labels backend:AArch64, miscompilation, generated by fuzzer
Assignees
Reporter YuanchengJiang
    The following code:

```cpp
// 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`/`d` instead 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-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to