Issue 238711
Summary [DAGCombiner] visitVECTOR_DEINTERLEAVE folds the whole deinterleave to undef when only its first operand is undef
Labels miscompilation, llvm:SelectionDAG, generated by fuzzer
Assignees
Reporter YuanchengJiang
    The following code:

```llvm
; llc -O2 (any target). Run: clang -O2 repro.ll -o r && ./r     Expected: 44223311 Actual: 0
@out = global i32 0
@in = global <4 x i8> <i8 17, i8 34, i8 51, i8 68>
@fmt = private constant [4 x i8] c"%x\0A\00"
declare i32 @printf(ptr, ...)

define void @f(ptr %p, ptr %o) noinline {
  %v = load <4 x i8>, ptr %p
  ; the low half of the deinterleaved vector is poison, the high half is %v
  %x = shufflevector <4 x i8> %v, <4 x i8> poison, <8 x i32> <i32 poison, i32 poison, i32 poison, i32 poison, i32 0, i32 1, i32 2, i32 3>
  %d = call {<4 x i8>, <4 x i8>} @llvm.vector.deinterleave2.v8i8(<8 x i8> %x)
  %even = extractvalue {<4 x i8>, <4 x i8>} %d, 0   ; <poison, poison, v0, v2>
  %odd = extractvalue {<4 x i8>, <4 x i8>} %d, 1    ; <poison, poison, v1, v3>
  %r = shufflevector <4 x i8> %even, <4 x i8> %odd, <4 x i32> <i32 2, i32 3, i32 6, i32 7>   ; <v0, v2, v1, v3>
  store <4 x i8> %r, ptr %o
  ret void
}

define i32 @main() {
  call void @f(ptr @in, ptr @out)
  %r = load volatile i32, ptr @out
  call i32 (ptr, ...) @printf(ptr @fmt, i32 %r)
  %ok = icmp eq i32 %r, u0x44223311
  %rc = select i1 %ok, i32 0, i32 1
  ret i32 %rc
}
```

Resulted in this output:
```
$ clang -O2 repro.ll -o r && ./r       (x86-64; also -O0)
0
```
The expected output is `44223311`. `@f` is compiled to a bare `ret` and the store is deleted. The same happens on AArch64 (-O2), RISC-V +v, LoongArch +lasx, PPC64LE and MIPS64 +msa (all checked under qemu or by asm). Only AArch64 -O0 is correct, because GlobalISel handles it there.

Trunk: https://godbolt.org/z/88TbE3Y98 (llc trunk `-O2`, x86-64 and AArch64: `f:` is just `ret`).

Root cause: `DAGCombiner::visitVECTOR_DEINTERLEAVE` (llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp) canonicalizes "shuffle undef -> {undef, undef, ..}" when `N->getOperand(0).isUndef()`. The operands of ISD::VECTOR_DEINTERLEAVE are consecutive pieces of the deinterleaved vector, and every result takes lanes from all of them. Here `deinterleave2` of the `<8 x i8>` is split into `vector_deinterleave undef:v4i8, %v`. Both results take lanes 2 and 3 from the second operand, but the combine replaces both results with undef. The fold was added in #228858 ("[DAG] Add basic deinterleave/interleave(poison) -> poison combines"), whose description says "with all undef inputs". The sibling `visitVECTOR_INTERLEAVE` does test all operands.

Suggested fix: require every operand to be undef, as `visitVECTOR_INTERLEAVE` does:
```diff
-  SDValue Op0 = N->getOperand(0);
-
-  // Canonicalize shuffle undef -> {undef, undef, ..}
-  if (Op0.isUndef()) {
+  // Canonicalize deinterleave(undef, undef, ..) -> {undef, undef, ..}. The
+  // operands are consecutive pieces of the input and every result takes lanes
+  // from all of them, so all operands must be undef.
+  if (all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); })) {
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to