| Issue |
235607
|
| Summary |
[ARM][InstCombine] MVE `pred.v2i(pred.i2v(x))` is folded to `x`, keeping the upper 16 bits that VMSR/VMRS P0 drop
|
| Labels |
backend:ARM,
miscompilation,
llvm:instcombine,
generated by fuzzer
|
| Assignees |
|
| Reporter |
YuanchengJiang
|
The following code:
IR-only reproducer: ACLE passes and returns `mve_pred16_t` as 16 bits, so in C the i32 reaching `pred.i2v` has no upper bits set; several C forms were tried. Run with `opt -passes=instcombine`, then `llc -O2` (thumbv8.1m.main, +mve.fp).
```llvm
target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"
target triple = "thumbv8.1m.main-none-unknown-eabihf"
; r = ~x & 0xffff (VPNOT of P0 = x); y = ~r = (x & 0xffff) | 0xffff0000;
; v2i(i2v(y)) reads P0 back: only the low 16 bits survive, so @f returns x & 0xffff.
define i32 @f(i32 %x) #0 {
%p = call <16 x i1> @llvm.arm.mve.pred.i2v.v16i1(i32 %x)
%n = xor <16 x i1> %p, splat (i1 true)
%r = call i32 @llvm.arm.mve.pred.v2i.v16i1(<16 x i1> %n)
%y = xor i32 %r, -1
%q = call <4 x i1> @llvm.arm.mve.pred.i2v.v4i1(i32 %y)
%s = call i32 @llvm.arm.mve.pred.v2i.v4i1(<4 x i1> %q)
ret i32 %s
}
declare <16 x i1> @llvm.arm.mve.pred.i2v.v16i1(i32)
declare i32 @llvm.arm.mve.pred.v2i.v16i1(<16 x i1>)
declare <4 x i1> @llvm.arm.mve.pred.i2v.v4i1(i32)
declare i32 @llvm.arm.mve.pred.v2i.v4i1(<4 x i1>)
attributes #0 = { nounwind "target-features"="+mve.fp" }
```
Resulted in this output (before and after InstCombine, both compiled with `llc -O2` and run under `qemu-arm -cpu cortex-m55` with the driver `driver.c`; expected `f(x) = x & 0xffff`):
```
== before
f(00000000) = 00000000 ok
f(0000ffff) = 0000ffff ok
f(12345678) = 00005678 ok
f(ffff0000) = 00000000 ok
f(0000f0f0) = 0000f0f0 ok
== after
f(00000000) = ffff0000 WRONG (expected low 16 bits of x)
f(0000ffff) = ffffffff WRONG (expected low 16 bits of x)
f(12345678) = ffff5678 WRONG (expected low 16 bits of x)
f(ffff0000) = ffff0000 WRONG (expected low 16 bits of x)
f(0000f0f0) = fffff0f0 WRONG (expected low 16 bits of x)
```
| x | expected (x & 0xffff) / before InstCombine | after InstCombine |
|---|---|---|
| 0x00000000 | 0x00000000 | **0xffff0000** |
| 0x0000ffff | 0x0000ffff | **0xffffffff** |
| 0x12345678 | 0x00005678 | **0xffff5678** |
After InstCombine the code is `vmsr p0, r0; vpnot; vmrs r0, p0; mvns r0, r0` instead of `vmsr p0, r0; vpnot; vpnot; vmrs r0, p0`: the second round trip through P0, which clears the upper 16 bits, is gone.
Trunk: https://godbolt.org/z/EWboqMjK4 (opt). llc trunk on the two IRs: before https://godbolt.org/z/bj3xbrn5Y, after https://godbolt.org/z/1Yfj3vPn8
Root cause: `ARMTTIImpl::instCombineIntrinsic` folds `arm_mve_pred_v2i(arm_mve_pred_i2v(X))` to `X`. `pred.i2v` uses only the low 16 bits of `X` (VMSR P0), and `pred.v2i` returns a value in [0, 65536) (the same function attaches `range(i32 0, 65536)` to it). So the fold is only correct when the upper 16 bits of `X` are known zero. Here `X` is `~v2i(...)`, whose upper bits are all ones.
Suggested fix: replace the result with `and X, 0xffff`, or fold only when the upper 16 bits of `X` are known zero. Not validated locally. Note that llc's DAG also folds a same-type `v2i(i2v(x))` to `x` (`m.ll` @g compiles to `bx lr`), so the same check may be needed there.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs