Issue 235611
Summary [AArch64][InstCombine] SVE shift/divide intrinsics simplified with IR shl/lshr/ashr/sdiv/udiv semantics (asr(x,x) -> 0, x/x -> 1)
Labels backend:AArch64, miscompilation, SVE, llvm:instcombine, generated by fuzzer
Assignees
Reporter YuanchengJiang
    The following code:

```c
// AArch64 SVE miscompile: InstCombine simplifies predicated SVE shift / divide intrinsics with the IR
// semantics of shl/lshr/ashr/sdiv/udiv (out-of-range shift amount and division by zero are poison/UB in IR,
// but fully defined for the SVE instructions):
//   svasr_x(pg, x, x)          -> 0   (wrong for x < 0: ASR by >= 16 gives -1)
//   svlsr_x(pg, 15, x << 4)    -> 15  (wrong for x<<4 != 0: LSR by >= 16 gives 0)
//   svdiv_x(pg, x, x)          -> 1 (wrong for x == 0: SDIV by 0 gives 0)
// Godbolt: clang --target=aarch64-linux-gnu -march=armv8-a+sve -O1 (also -O2/-O3)
// Expected output: -1 0 0 / bad=0
#include <arm_sve.h>
int printf(const char *, ...);

__attribute__((noinline)) short asr_self(short x) {
  svint16_t v = svdup_n_s16(x);
  return svlasta_s16(svpfalse_b(), svasr_s16_x(svptrue_b16(), v, svreinterpret_u16_s16(v)));
}
__attribute__((noinline)) unsigned short lsr_mask(unsigned short x) {
  svuint16_t s = svdup_n_u16((unsigned short)(x << 4));
  return svlasta_u16(svpfalse_b(), svlsr_u16_x(svptrue_b16(), svdup_n_u16(15), s));
}
__attribute__((noinline)) int div_self(int x) {
 svint32_t v = svdup_n_s32(x);
  return svlasta_s32(svpfalse_b(), svdiv_s32_x(svptrue_b32(), v, v));
}
int main(void) {
  volatile short a = -1;
  volatile unsigned short b = 1;
  volatile int c = 0;
  int r1 = asr_self(a), r2 = lsr_mask(b), r3 = div_self(c);
  printf("%d %d %d\n", r1, r2, r3);
  int bad = r1 != -1 || r2 != 0 || r3 != 0;
 printf("bad=%d\n", bad);
  return bad;
}
```

Resulted in this output:
```
0 15 1
bad=1
```
(expected `-1 0 0`; clang `--target=aarch64-linux-gnu -march=armv8-a+sve -O1`..`-O3`, run under qemu-aarch64 `-cpu max,sve{128,256,512}=on`; `-O0` prints `-1 0 0`.)

| function | input | expected (SVE semantics) | actual |
|---|---|---|---|
| `svasr_s16_x(pg, x, x)` | x = -1 | -1 (ASR by 0xffff >= 16 fills with the sign) | 0 |
| `svlsr_u16_x(pg, 15, dup(x << 4))` | x = 1 | 0 (LSR by 16) | 15 |
| `svdiv_s32_x(pg, x, x)` | x = 0 | 0 (SDIV by zero is 0) | 1 |

At -O2 all three functions fold to constants (`mov w0, wzr` / `mov w0, #15` / `mov w0, #1`).

Trunk: https://godbolt.org/z/KxvE1E3Gn

Root cause: `simplifySVEIntrinsicBinOp` (AArch64TargetTransformInfo.cpp) simplifies `sve.asr/lsl/lsr/sdiv/udiv` (and their `_u` forms) by calling InstSimplify's `simplifyBinOp` with the matching IR opcode (`AShr`/`Shl`/`LShr`/`SDiv`/`UDiv`, set via `setMatchingIROpcode`). For these opcodes IR has poison/UB inputs (shift amount >= bit width, division by zero, INT_MIN / -1) on which the SVE instructions are fully defined, and InstSimplify exploits them: `ashr X, X -> 0`, `sdiv X, X -> 1`, `udiv X, X -> 1`, `lshr C, (Y << 4) -> C` (the amount "must" be 0). The existing guard (`isa<UndefValue>(SimpleII)`) only rejects simplifications whose result is poison for every input, not ones that are valid only on the IR-defined inputs.

Suggested fix: do not use `simplifyBinOp` for the shift/divide opcodes (or only when the operands are constants), or first prove the IR preconditions (shift amount known < bit width, divisor known non-zero and not -1 with INT_MIN dividend). Not validated locally.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to