[X86] X86CompressEVEX: Incorrect VPMOVB2M + KMOV -> VPMOVMSKB transformation causes incorrect results
Commit 1caf2704dd6791baa4b958d6a666ea64ec24795d ("[X86] Allow EVEX compression for mask registers (#171980)") introduces a regression that causes incorrect code generation for AVX-512 vectorized code.
The transformation attempts to compress the following pattern:
vpmov*2m %xmm0, %k0 -> (erase)
kmov* %k0, %eax -> vmovmsk* %xmm0, %eax
However, this transformation produces incorrect results in certain code patterns involving masked operations in loops on AVX512 SKX+.
Assembly Difference
Before (correct) - commit b8f5cbba2abb:
switchit___vyi:
# %bb.0:
vpsllw $7, %xmm1, %xmm1
vpmovb2m %xmm1, %k0 ; <-- AVX-512 instruction
kmovd %k0, %eax ; <-- separate move from mask reg
andl $65534, %eax
je .LBB3_1
...
After (incorrect) - commit 1caf2704dd67:
switchit___vyi:
# %bb.0:
vpsllw $7, %xmm1, %xmm1
vpmovmskb %xmm1, %eax ; <-- Compressed to single instruction
andl $65534, %eax
je .LBB3_1
...
Compiler explorer link: https://ispc.godbolt.org/z/59Mnnvavf The test used in the reproducer produces incorrect results at runtime:
| Lane | Expected | Actual |
|---|---|---|
| 2 | 4.0 | 2.0 |
| 3 | 9.0 | 3.0 |
| 4 | 24.0 | 6.0 |
| 5 | 35.0 | 7.0 |
| 6 | 48.0 | 8.0 |
| 7 | 63.0 | 9.0 |
| 8 | 144.0 | 18.0 |
| ... | ... | ... |
@realqhc @RKSimon FYI
@llvm/issue-subscribers-backend-x86
Author: None (aneshlya)
The transformation attempts to compress the following pattern:
vpmov*2m %xmm0, %k0 -> (erase)
kmov* %k0, %eax -> vmovmsk* %xmm0, %eax
However, this transformation produces incorrect results in certain code patterns involving masked operations in loops on AVX512 SKX+.
Assembly Difference
Before (correct) - commit b8f5cbba2abb:
switchit___vyi:
# %bb.0:
vpsllw $7, %xmm1, %xmm1
vpmovb2m %xmm1, %k0 ; <-- AVX-512 instruction
kmovd %k0, %eax ; <-- separate move from mask reg
andl $65534, %eax
je .LBB3_1
...
After (incorrect) - commit 1caf2704dd67:
switchit___vyi:
# %bb.0:
vpsllw $7, %xmm1, %xmm1
vpmovmskb %xmm1, %eax ; <-- Compressed to single instruction
andl $65534, %eax
je .LBB3_1
...
Compiler explorer link: https://ispc.godbolt.org/z/59Mnnvavf The test used in the reproducer produces incorrect results at runtime:
| Lane | Expected | Actual |
|---|---|---|
| 2 | 4.0 | 2.0 |
| 3 | 9.0 | 3.0 |
| 4 | 24.0 | 6.0 |
| 5 | 35.0 | 7.0 |
| 6 | 48.0 | 8.0 |
| 7 | 63.0 | 9.0 |
| 8 | 144.0 | 18.0 |
| ... | ... | ... |
@aneshlya can you provide the reproducer as well please?
The vpmovb2m (extract v16i8 sign bits into mask and zero-extend) +kmovd (extract i32 mask bits - i16 active + i16 zero) -> vpmovmskb (extract v16i8 sign bits into gpr and zero-extend) fold should be OK, so either we have a problem with the size of the kmov vs movmsk instructions losing/emitting extra bits that should be zeroed or somehow the vpmov*2m had additional uses and shouldn't have been dropped.
> llc switch-19.ispc.ll -march=x86-64 -mcpu=skylake-avx512 -O1 -o switch-19.ispc.o -filetype=obj
> clang++ -O2 -m64 test_static.cpp -DTEST_SIG=2 -DTEST_WIDTH=16 switch-19.ispc.o -fPIE -o switch-19.run
The reproducer from "good" folder will not produce any output, just 0 error code. The reproducer from "bad" folder:
./switch-19.run
./switch-19.run: value 2 disagrees: returned 2.000000 [0x1p+1], expected 4.000000 [0x1p+2]
./switch-19.run: value 3 disagrees: returned 3.000000 [0x1.8p+1], expected 9.000000 [0x1.2p+3]
./switch-19.run: value 4 disagrees: returned 6.000000 [0x1.8p+2], expected 24.000000 [0x1.8p+4]
./switch-19.run: value 5 disagrees: returned 7.000000 [0x1.cp+2], expected 35.000000 [0x1.18p+5]
./switch-19.run: value 6 disagrees: returned 8.000000 [0x1p+3], expected 48.000000 [0x1.8p+5]
./switch-19.run: value 7 disagrees: returned 9.000000 [0x1.2p+3], expected 63.000000 [0x1.f8p+5]
./switch-19.run: value 8 disagrees: returned 18.000000 [0x1.2p+4], expected 144.000000 [0x1.2p+7]
./switch-19.run: value 9 disagrees: returned 20.000000 [0x1.4p+4], expected 180.000000 [0x1.68p+7]
./switch-19.run: value 10 disagrees: returned 22.000000 [0x1.6p+4], expected 220.000000 [0x1.b8p+7]
./switch-19.run: value 11 disagrees: returned 24.000000 [0x1.8p+4], expected 264.000000 [0x1.08p+8]
./switch-19.run: value 12 disagrees: returned 13.000000 [0x1.ap+3], expected 156.000000 [0x1.38p+7]
./switch-19.run: value 13 disagrees: returned 14.000000 [0x1.cp+3], expected 182.000000 [0x1.6cp+7]
./switch-19.run: value 14 disagrees: returned 15.000000 [0x1.ep+3], expected 210.000000 [0x1.a4p+7]
./switch-19.run: value 15 disagrees: returned 16.000000 [0x1p+4], expected 240.000000 [0x1.ep+7]
I'm on Intel(R) Xeon(R) Platinum 8360Y but I see it with newer HW as well.
@realqhc Are you able to look at this?
diff --git a/llvm/lib/Target/X86/X86CompressEVEX.cpp b/llvm/lib/Target/X86/X86CompressEVEX.cpp
--- a/llvm/lib/Target/X86/X86CompressEVEX.cpp (revision d4a59ea938d6e60659a4375dd862b3d19bcff2f4)
+++ b/llvm/lib/Target/X86/X86CompressEVEX.cpp (date 1767978363984)
@@ -229,6 +229,7 @@
SmallVectorImpl<MachineInstr *> &ToErase) {
const X86InstrInfo *TII = ST.getInstrInfo();
const TargetRegisterInfo *TRI = ST.getRegisterInfo();
+ MachineRegisterInfo *MRI = &MBB.getParent()->getRegInfo();
unsigned Opc = MI.getOpcode();
if (Opc != X86::VPMOVD2MZ128kr && Opc != X86::VPMOVD2MZ256kr &&
@@ -298,6 +299,16 @@
if (!KMovMI)
return false;
+
+ // Check if MaskReg is used in any other basic blocks
+ for (MachineOperand &UseMO : MRI->use_operands(MaskReg)) {
+ MachineInstr *UseMI = UseMO.getParent();
+ MachineBasicBlock *UseMBB = UseMI->getParent();
+
+ // If the use is in a different block, we cannot safely compress
+ if (UseMBB != &MBB)
+ return false;
+ }
// Apply the transformation
KMovMI->setDesc(TII->get(MovMskOpc));
The problem happens because MaskReg is used in other basic blocks and can be solved by additional checks. @RKSimon Could you provide some guidance in where to add test for this? I can create new PR with fix above and add test for this.
Probably best if you create llvm-project\llvm\test\CodeGen\X86\pr174871.ll
@aneshlya please can you confirm the new commit is OK - especially as we're so close to branching
Our tests are passing now. Thanks a lot for the fix!