Issue 244524
Summary [fuzzer-found][AArch64] convertMulToShlAdd turns `mul <2 x i64> x, splat(INT64_MIN)` into a shift by 64 (poison), so the multiply and its store are deleted
Labels backend:AArch64, miscompilation, generated by fuzzer
Assignees
Reporter YuanchengJiang
    The following code:

IR-only reproducer: when the IR passes see `x * INT64_MIN`, they turn it into `shl x, 63`. The bug needs the splat constant to appear only during SelectionDAG lowering; here it comes from a `select` of constants, as in the fuzzer input `fuzz-640375251.ll`. `main` prints the two lanes of `{1, 3} * INT64_MIN`.

```llvm
@fmt = private constant [11 x i8] c"%llx %llx\0A\00"
declare i32 @printf(ptr, ...)
define void @f(ptr %pa, ptr %po) noinline {
  %a = load <2 x i64>, ptr %pa, align 16
  %c = icmp eq <2 x i64> splat (i64 -9223372036854775808), <i64 1, i64 -9223372036854775808>
  %k = select <2 x i1> %c, <2 x i64> <i64 1, i64 -9223372036854775808>, <2 x i64> splat (i64 -9223372036854775808)
  %m = mul <2 x i64> %a, %k
  store <2 x i64> %m, ptr %po, align 16
  ret void
}
define i32 @main() {
  %pa = alloca <2 x i64>, align 16
  %po = alloca <2 x i64>, align 16
  store <2 x i64> <i64 1, i64 3>, ptr %pa
  store <2 x i64> zeroinitializer, ptr %po
  call void @f(ptr %pa, ptr %po)
  %r = load <2 x i64>, ptr %po
  %x = extractelement <2 x i64> %r, i32 0
  %y = extractelement <2 x i64> %r, i32 1
  call i32 (ptr, ...) @printf(ptr @fmt, i64 %x, i64 %y)
  ret i32 0
}
```

Resulted in this output (`llc -O2 -mtriple=aarch64`, run on an AArch64 emulator):
```
0 0
```

| build | output |
|---|---|
| expected (`llc -O0`) | `8000000000000000 8000000000000000` |
| `llc -O2` | `0 0` |

Wrong code: the whole function is deleted, so the result is never stored.
```
f:
        ret
```

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

Root cause: `convertMulToShlAdd` (AArch64ISelLowering.cpp, added in #183827) lowers a v2i64 multiply by a splat C. It takes `abs(C)`, strips the trailing zeros, and uses `x << (k + tz) +/- (x << tz)` when the odd part is 2^k ± 1. For C = INT64_MIN, `abs()` returns INT64_MIN again, `countr_zero` is 63 and the odd part is 1. Then `1 + 1` is a power of two, so the SUB form is chosen with a shift amount of `1 + 63 = 64`. A vector shift by 64 is poison, so the multiply folds to undef and the store is removed. The correct result is `x << 63`.

Suggested fix: in `convertMulToShlAdd`, bail out (or emit `x << (BW-1)`) when the computed shift amount is >= the element width. Validated locally: with the bail-out the program prints `8000000000000000 8000000000000000`, the AGT check no longer fires, and the 44 llc RUN lines of the 22 AArch64 tests that multiply `<2 x i64>` give unchanged output.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to