| Issue |
244566
|
| Summary |
[X86] VZEXT_MOVL is dropped using known-zero of poison lanes, losing a zero lane (miscompile)
|
| Labels |
backend:X86,
miscompilation
|
| Assignees |
|
| Reporter |
chfast
|
A shuffle that takes a lane from `zeroinitializer` loses the zero on AVX2 when the other lanes it zeroes come from an always-poison `ashr`.
```llvm
define <4 x i32> @f(<4 x i32> %x, ptr %p) {
%s = ashr <4 x i32> %x, <i32 9, i32 33, i32 33, i32 33>
%r = shufflevector <4 x i32> %s, <4 x i32> zeroinitializer, <4 x i32> <i32 0, i32 1, i32 6, i32 3>
store <4 x i32> %s, ptr %p
ret <4 x i32> %r
}
```
Lanes 1-3 of `%s` are poison (shift by 33). In `%r`, lanes 1 and 3 are poison, but lane 2 comes from the zero vector, so it must be 0.
Compiler Explorer: https://godbolt.org/z/G1EzW3P3e. It has llc trunk with `-mattr=+avx2` and `-mattr=+sse4.1`, the `-debug-_only_=dagcombine` trace, and a run of the same IR with a `main` added (`clang -O2 -mavx2 -Xclang -disable-llvm-passes`), which prints `r[2] = -1`.
`llubi` runs that IR (the store gets a 16-byte aligned `alloca`) without reporting UB and prints `r[2] = 0`; its trace has `%r = { i32 2, poison, i32 0, poison }`.
`llc -mtriple=x86_64-linux-gnu -mattr=+avx2`:
```asm
f:
vpsravd .LCPI0_0(%rip), %xmm0, %xmm0
vmovdqa %xmm0, (%rdi)
retq
```
`%r` is returned as `%s`. `vpsravd` with a count of 33 fills lane 2 with the sign bit, so `%r[2]` is -1 for a negative `%x[2]`. `-mattr=+avx512f,+avx512vl` gives the same code. `-mattr=+sse4.1` and `-mattr=+avx` zero the lane with a blend.
This driver, built with the object file from the command above, prints `r[0] = 2 (expected 2), r[2] = -1 (expected 0)`:
```c
#include <immintrin.h>
#include <stdio.h>
__m128i f(__m128i x, __m128i *p);
int main(void) {
__m128i p;
int r[4];
_mm_storeu_si128((__m128i *)r, f(_mm_setr_epi32(1024, -5, -7, -9), &p));
printf("r[0] = %d (expected 2), r[2] = %d (expected 0)\n", r[0], r[2]);
return r[2] != 0;
}
```
`-debug-_only_=dagcombine`:
```
Combining: t21: v4i32 = X86ISD::BLENDI t8, t10, TargetConstant:i8<4>
... into: t26: v4i32 = X86ISD::VZEXT_MOVL t8
Combining: t26: v4i32 = X86ISD::VZEXT_MOVL t8
Replacing.2 t26: v4i32 = X86ISD::VZEXT_MOVL t8
With: t8: v4i32 = sra t2, t23
```
The first step is fine: lanes 1 and 3 are poison, so zeroing them is a refinement. The second step is wrong. `X86TargetLowering::SimplifyDemandedVectorEltsForTargetNode` replaces `VZEXT_MOVL(Src)` with `Src` when the upper lanes of `Src` are known zero:
```cpp
case X86ISD::VZEXT_MOVL: {
// If upper demanded elements are already zero then we have nothing to do.
SDValue Src = ""
APInt DemandedUpperElts = DemandedElts;
DemandedUpperElts.clearLowBits(1);
if (TLO.DAG.MaskedVectorIsZero(Src, DemandedUpperElts, Depth + 1))
return TLO.CombineTo(Op, Src);
```
`computeKnownBits` reports the always-poison lanes of the `sra` as zero. So the fold returns `Src`, whose lane 2 is poison, while `VZEXT_MOVL` defines that lane as 0. Also requiring `isGuaranteedNotToBeUndefOrPoison(Src, DemandedUpperElts)` fixes this example; I did not run the regression tests with that change.
This is the same family as #241253 (`matchBinaryShuffle`, fixed by #243431) and #214388 (`isTargetShuffleEquivalent`, fixed by #217030, which added the same not-poison check there), but a different fold. It still reproduces on main at 6a68b4da94d8 and with LLVM 21.1.8. Found by fuzzing while reviewing #243097, which does not change it.
Assisted-by: Claude Code (Claude Opus 5.5)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs