Issue 235519
Summary [InstCombine] Missed identity vector reconstruction through scalar PHIs
Labels new issue
Assignees
Reporter Sahil170595
    InstCombine misses an identity reconstruction when all lanes of a vector pass through scalar PHIs and are then inserted back in their original order. The original incoming vectors could instead pass through one vector PHI. In this example, existing bitcast folds could then leave one i32 PHI.

Reproduced with LLVM main at 7af12b1edf38e8b991e372f1f9aae87faa7f2d83 (LLVM 24.0.0git, Release with assertions), and with Triton's pinned LLVM 18f9e623e4c891ba31ff817c07197707ac9bc593.

Command:

```sh
opt -passes=instcombine -S repro.ll
```

Reproducer:

```llvm
declare void @use_word(i32)

define void @reconstruct_byte_phis(i32 %initial, ptr %src, i1 %continue) {
entry:
  %initial.bytes = bitcast i32 %initial to <4 x i8>
  %a0 = extractelement <4 x i8> %initial.bytes, i64 0
  %a1 = extractelement <4 x i8> %initial.bytes, i64 1
  %a2 = extractelement <4 x i8> %initial.bytes, i64 2
  %a3 = extractelement <4 x i8> %initial.bytes, i64 3
  br label %loop

loop:
  %p0 = phi i8 [ %a0, %entry ], [ %b0, %latch ]
  %p1 = phi i8 [ %b1, %latch ], [ %a1, %entry ]
  %p2 = phi i8 [ %a2, %entry ], [ %b2, %latch ]
  %p3 = phi i8 [ %a3, %entry ], [ %b3, %latch ]
  %v0 = insertelement <4 x i8> poison, i8 %p0, i64 0
 %v1 = insertelement <4 x i8> %v0, i8 %p1, i64 1
  %v2 = insertelement <4 x i8> %v1, i8 %p2, i64 2
  %v3 = insertelement <4 x i8> %v2, i8 %p3, i64 3
  %packed = bitcast <4 x i8> %v3 to i32
  call void @use_word(i32 %packed)
  br label %latch

latch:
  %next = load i32, ptr %src, align 4
  %next.bytes = bitcast i32 %next to <4 x i8>
  %b0 = extractelement <4 x i8> %next.bytes, i64 0
  %b1 = extractelement <4 x i8> %next.bytes, i64 1
  %b2 = extractelement <4 x i8> %next.bytes, i64 2
 %b3 = extractelement <4 x i8> %next.bytes, i64 3
  br i1 %continue, label %loop, label %exit

exit:
  ret void
}
```

InstCombine keeps four i8 PHIs, the extracts, and the insert chain. The expected core of the loop is:

```llvm
loop:
  %packed = phi i32 [ %initial, %entry ], [ %next, %latch ]
  call void @use_word(i32 %packed)
  br label %latch
```

`-passes='default<O3>' -vectorize-slp=false` also retains the scalar PHIs. `-passes='instcombine,slp-vectorizer,instcombine'` can fold this example, but the useful missing case here is a target-independent combine that does not require enabling SLP.

This comes from https://github.com/triton-lang/triton/issues/12486. Triton disables LLVM SLP by default for NVIDIA kernels and uses its NVPTX vectorizer. In the affected SM8x prefetch loop, the separate byte carries cause excess register pressure. A narrow downstream identity fold in https://github.com/triton-lang/triton/pull/12487 changes the original L40S reproducer from 255 registers / 140 spills to 236 registers / zero spills. Those measurements are for the downstream Triton fix; they are not performance claims for an LLVM patch. Reproduction scripts and measurements: https://gist.github.com/Sahil170595/3ada0b14afebbdf89f8cb25562412a86.

Related precedents:

- https://github.com/llvm/llvm-project/issues/46404 describes the analogous struct reconstruction problem. `foldAggregateConstructionIntoAggregateReuse` already handles aggregate reconstruction through PHIs.
- https://github.com/llvm/llvm-project/pull/115627 preserves extract/insert patterns for vector combining, but does not handle this scalar-PHI case.

I am working on a small vector counterpart in InstCombine: match a complete identity insert chain, require the scalar PHIs to share a block and be used only by the reconstruction, and verify that every incoming edge extracts the corresponding lanes from the same original vector. This would reuse those incoming vectors without adding edge operations or target-specific assumptions.

Thanks to Hughshine for the original Triton reproducer and diagnosis, and to lezcano for suggesting that the fold belongs upstream.

Assisted-by: OpenAI Codex

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to