Issue 244153
Summary [AArch64][SVE] computeKnownBitsForTargetNode treats SHL_PRED/SRL_PRED/SRA_PRED amounts >= the element size as poison, but `svasr`/`svlsl`/`svlsr` define them: `(svasr_x(pg, neg, n >= 32)) & 1` folds to 0
Labels backend:AArch64, miscompilation, generated by fuzzer
Assignees
Reporter YuanchengJiang
    The following code:

```c
// clang --target=aarch64-linux-gnu -march=armv8-a+sve -O1 repro.c && ./a.out   (any SVE machine, or qemu-aarch64 -cpu max)
// Expected: 1 (correct at -O0). Actual at -O1/-O2/-O3: 0.
#include <arm_sve.h>
#include <stdio.h>
#include <stdint.h>

// SVE ASR is defined for shift amounts >= the element size: the result is the
// sign fill, so a negative value shifted by >= 32 is -1, and (-1 & 1) == 1.
__attribute__((noinline)) svint32_t f(svint32_t x, svuint32_t a) {
  svbool_t pg = svptrue_b32();
  svint32_t v = svorr_n_s32_x(pg, x, INT32_MIN);   // v is negative
  svuint32_t n = svorr_n_u32_x(pg, a, 32);         // n >= 32
  svint32_t s = svasr_s32_x(pg, v, n);             // all ones
  return s & 1; // 1
}

int main(void) {
  int32_t out[64];
 svst1_s32(svptrue_b32(), out, f(svdup_n_s32(5), svdup_n_u32(1)));
 printf("%d\n", out[0]);
  return out[0] != 1;
}
```

Resulted in this output:
```
$ clang --target=aarch64-linux-gnu -march=armv8-a+sve -O0 repro.c && qemu-aarch64 -cpu max ./a.out
1
$ clang --target=aarch64-linux-gnu -march=armv8-a+sve -O1 repro.c && qemu-aarch64 -cpu max ./a.out
0                                                    (exit 1; same at -O2/-O3)
```
At -O1 and above `f` compiles to `movi v0.2d, #0; ret`.

Trunk: https://godbolt.org/z/7c6Kzeah7 (AArch64 clang trunk `-O2 -march=armv8-a+sve`: `f` is `movi v0.2d, #0000000000000000` / `ret`; Godbolt cannot run SVE binaries, the run above is qemu 8.2 `-cpu max`)

Root cause: `AArch64TargetLowering::computeKnownBitsForTargetNode` (llvm/lib/Target/AArch64/AArch64ISelLowering.cpp, added by #200347) computes the known bits of an all-active `AArch64ISD::SHL_PRED` / `SRL_PRED` / `SRA_PRED` with `KnownBits::shl` / `lshr` / `ashr`. Those helpers follow ISD semantics: a shift amount >= the bit width is poison and is left out (an amount known to be out of range gives "all zero"). The predicated nodes are also formed straight from the `sve.lsl.u` / `sve.lsr.u` / `sve.asr.u` intrinsics (`performIntrinsicCombine`), which the ACLE `svlsl_x` / `svlsr_x` / `svasr_x` produce. There, amounts >= the element size are defined: LSL and LSR give 0, and ASR fills with the sign bit. For `asr(v, n)` with v negative and n >= 32, `KnownBits::ashr` returns all-zero, so `visitAND` folds `s & 1` to 0. The real value is -1 & 1 = 1. For LSL/LSR with an amount that may or may not be in range, the result omits the 0 outcome. For example, `sve.lsl.u(1, a & 32)` is known to have bit 0 set although it is 0 when a & 32 != 0.

Suggested fix: Clamp the ASR amount to `EltBits - 1`, which gives the sign fill. For LSL/LSR, add the zero result when the amount may be out of range:
```diff
-    if (Op.getOpcode() == AArch64ISD::SHL_PRED)
- Known = KnownBits::shl(KnownVal, KnownAmt);
-    else if (Op.getOpcode() == AArch64ISD::SRL_PRED)
-      Known = KnownBits::lshr(KnownVal, KnownAmt);
- else
+    // Unlike ISD shifts, SVE shifts are defined for amounts >= the element
+    // size (e.g. from the sve.lsl.u/lsr.u/asr.u intrinsics): LSL and LSR give
+    // zero and ASR fills with the sign bit. The KnownBits helpers treat such
+    // amounts as poison, so clamp the ASR amount and add the zero result.
+    unsigned EltBits = KnownVal.getBitWidth();
+ if (Op.getOpcode() == AArch64ISD::SRA_PRED) {
+      KnownAmt = KnownBits::umin(
+          KnownAmt, KnownBits::makeConstant(APInt(EltBits, EltBits - 1)));
       Known = KnownBits::ashr(KnownVal, KnownAmt);
+ break;
+    }
+    Known = Op.getOpcode() == AArch64ISD::SHL_PRED
+ ? KnownBits::shl(KnownVal, KnownAmt)
+                : KnownBits::lshr(KnownVal, KnownAmt);
+    if (KnownAmt.getMaxValue().uge(EltBits))
+      Known = Known.intersectWith(
+ KnownBits::makeConstant(APInt::getZero(EltBits)));
 break;
```
(fix.diff applies to 8b5ece820d.) With the fix, the repro prints 1 at -O0 through -O3. In the AArch64 CodeGen tests (736 sve/shift files, every llc RUN line), the output changes in none, so the USRA/SSRA matching from #200347 still works (its amounts are in-range constants). repro.ll has IR cases for a new test (`asr_c`, `asr_v`, `lsl_v`).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to