| Issue |
240463
|
| Summary |
[X86] Miscompile: spill folding drops KMOVDkk zero-extension when v32i1 is inserted into a zero v64i1 mask
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
alexey-bataev
|
`llc -O1` and above, with AVX512BW, lose the zero-extension of a `v32i1` mask into `v64i1` when the result is live across a call. Lanes 32..63, which the IR sets to zero, come back with the original compare bits. `-O0` is correct. Reproduces with trunk `llc` (24.0.0git).
Reproducer, `repro.ll`:
```llvm
target triple = "x86_64-unknown-linux-gnu"
declare void @g()
define i1 @f(<64 x i8> %a, <64 x i8> %b) {
%m = icmp ne <64 x i8> %a, %b
%s = shufflevector <64 x i1> %m, <64 x i1> zeroinitializer, <64 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31, i32 64, i32 65, i32 66, i32 67, i32 68, i32 69, i32 70, i32 71, i32 72, i32 73, i32 74, i32 75, i32 76, i32 77, i32 78, i32 79, i32 80, i32 81, i32 82, i32 83, i32 84, i32 85, i32 86, i32 87, i32 88, i32 89, i32 90, i32 91, i32 92, i32 93, i32 94, i32 95>
%i = bitcast <64 x i1> %s to i64
call void @g()
%r = icmp eq i64 %i, 0
ret i1 %r
}
```
`llc -O2 -mattr=+avx512bw repro.ll -o -`:
```
f:
pushq %rax
vpcmpneqb %zmm1, %zmm0, %k0
kmovq %k0, (%rsp) # 8-byte Spill
vzeroupper
callq g@PLT
kmovq (%rsp), %k0 # 8-byte Reload
kortestq %k0, %k0
sete %al
popq %rcx
retq
```
Nothing clears bits 32..63 of `%k0` before the spill, so `kortestq` tests all 64 compare lanes. Without the call, the same function gets `kmovd %k0, %k0` before `kortestq`.
Runtime check, with inputs that differ only in lane 40:
```c
#include <stdio.h>
#include <immintrin.h>
_Bool f(__m512i, __m512i);
void g(void) {}
int main(void) {
char a[64] = {0}, b[64] = {0};
b[40] = 1;
printf("%d (expected 1)\n", f(_mm512_loadu_si512(a), _mm512_loadu_si512(b)));
}
```
```
$ llc -O0 -mattr=+avx512bw -filetype=obj repro.ll -o repro.o && clang -mavx512bw main.c repro.o && ./a.out
1 (expected 1)
$ llc -O2 -mattr=+avx512bw -filetype=obj repro.ll -o repro.o && clang -mavx512bw main.c repro.o && ./a.out
0 (expected 1)
```
Analysis:
The `insert_subvector (v64i1 zeroinitializer), v32i1, 0` pattern in X86InstrVecCompiler.td selects to `COPY_TO_REGCLASS (KMOVDkk VK32:$mask), VK64`. After the register coalescer joins the surrounding COPYs, this is `%4:vk64 = KMOVDkk %2:vk64`. `%4` is live across the call, so greedy spills it, and `InlineSpiller::spillAroundUses` folds the spill store into the def (`-debug-_only_=regalloc`):
```
folded: 64r KMOVQmk %stack.0, 1, $noreg, 0, $noreg, %2:vk64 :: (store (s64) into %stack.0)
```
The KMOVDkk is deleted and the unmodified 64-bit source is stored.
The fold is allowed because `KMOV*kk` are defined with `isMoveReg = 1` (avx512_mask_mov in X86InstrAVX512.td). As a result `X86InstrInfo::isCopyInstrImpl` reports them as copies, and `TargetInstrInfo::foldMemoryOperand` folds any `isCopyInstr` into a plain spill or reload of the register class, here VK64. With BWI the mask registers are 64 bits wide, and KMOVBkk, KMOVWkk and KMOVDkk zero the bits above their width. Once their operands are VK64, they are not full copies.
This looks like the mask-register counterpart of c26dfc81e254c ("[HACK] X86: Disable isCopyInstrImpl for undef subreg..."), which handled a similar `MOV32rr` case. That fix does not apply here, because mask registers have no sub-registers.
A possible fix is to stop `X86InstrInfo::isCopyInstrImpl` from reporting the zero-extending forms as copies: KMOVBkk and KMOVDkk always, KMOVWkk when BWI is available, and their _EVEX variants. Without BWI, KMOVWkk is the full mask copy that copyPhysReg emits, so it has to stay a copy.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs