[llvm] 0655422 - Reapply "[VPlan] Replace first-lane uses of BuildVector." (#206774) (#207512)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Jul 4 06:55:00 PDT 2026
Author: Florian Hahn
Date: 2026-07-04T13:54:55Z
New Revision: 06554227c7005f90fee66925d9acbe249329b005
URL: https://github.com/llvm/llvm-project/commit/06554227c7005f90fee66925d9acbe249329b005
DIFF: https://github.com/llvm/llvm-project/commit/06554227c7005f90fee66925d9acbe249329b005.diff
LOG: Reapply "[VPlan] Replace first-lane uses of BuildVector." (#206774) (#207512)
This reverts commit 638d71403a8598bb061b874ab39b8f9c6f0a97f0.
Recommit with fix for root cause of the revert:
VPWidenRecipe::onlyFirstLaneUsed was incorrectly assuming all operands
defined outside loop region are invariant, which is not true (e.g.
BuildVector). Test added in e173245deeaa2340.
Original message:
Replace uses of the first lane of a BuildVector with the first operand.
Added:
Modified:
llvm/lib/Transforms/Vectorize/VPlan.h
llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
llvm/test/Transforms/LoopVectorize/VPlan/buildvector-first-lane-only.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index 3e7be2d67733a..b2d179d8e6d98 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -1868,7 +1868,7 @@ class LLVM_ABI_FOR_TEST VPWidenRecipe : public VPRecipeWithIRFlags,
assert(is_contained(operands(), Op) &&
"Op must be an operand of the recipe");
return Opcode == Instruction::Select && Op == getOperand(0) &&
- Op->isDefinedOutsideLoopRegions();
+ isa<VPIRValue>(Op);
}
};
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index e5d16ccbec434..bd260a0d12362 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -1745,6 +1745,14 @@ static void simplifyRecipe(VPSingleDefRecipe *Def) {
return;
}
+ // Replace uses of a BuildVector by users that only use its first lane with
+ // its first operand directly.
+ if (match(Def, m_BuildVector())) {
+ Def->replaceUsesWithIf(Def->getOperand(0), [Def](VPUser &U, unsigned) {
+ return U.usesFirstLaneOnly(Def);
+ });
+ }
+
// Look through broadcast of single-scalar when used as select conditions; in
// that case the scalar condition can be used directly.
if (match(Def,
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/buildvector-first-lane-only.ll b/llvm/test/Transforms/LoopVectorize/VPlan/buildvector-first-lane-only.ll
index 04f3aa3accbc8..297eccd66c2d5 100644
--- a/llvm/test/Transforms/LoopVectorize/VPlan/buildvector-first-lane-only.ll
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/buildvector-first-lane-only.ll
@@ -20,19 +20,15 @@ define i16 @last_active_lane_live_out(i32 %x) {
; CHECK-NEXT: Successor(s): middle.block
; CHECK-EMPTY:
; CHECK-NEXT: middle.block:
-; CHECK-NEXT: vp<[[VP6:%[0-9]+]]> = SCALAR-STEPS ir<0>, ir<1>, ir<4>, ir<1>
-; CHECK-NEXT: vp<[[VP7:%[0-9]+]]> = SCALAR-STEPS ir<0>, ir<1>, ir<4>, ir<2>
-; CHECK-NEXT: vp<[[VP8:%[0-9]+]]> = SCALAR-STEPS ir<0>, ir<1>, ir<4>, ir<3>
-; CHECK-NEXT: EMIT vp<[[VP9:%[0-9]+]]> = buildvector ir<0>, vp<[[VP6]]>, vp<[[VP7]]>, vp<[[VP8]]>
-; CHECK-NEXT: EMIT vp<%active.lane.mask> = active lane mask vp<[[VP9]]>, ir<2>, ir<1>
-; CHECK-NEXT: EMIT vp<[[VP10:%[0-9]+]]> = not vp<%active.lane.mask>
-; CHECK-NEXT: EMIT vp<%first.inactive.lane> = first-active-lane vp<[[VP10]]>
+; CHECK-NEXT: EMIT vp<%active.lane.mask> = active lane mask ir<0>, ir<2>, ir<1>
+; CHECK-NEXT: EMIT vp<[[VP6:%[0-9]+]]> = not vp<%active.lane.mask>
+; CHECK-NEXT: EMIT vp<%first.inactive.lane> = first-active-lane vp<[[VP6]]>
; CHECK-NEXT: EMIT vp<%last.active.lane> = sub vp<%first.inactive.lane>, ir<1>
-; CHECK-NEXT: EMIT vp<[[VP11:%[0-9]+]]> = extractelement vp<[[VP5]]>, vp<%last.active.lane>
+; CHECK-NEXT: EMIT vp<[[VP7:%[0-9]+]]> = extractelement vp<[[VP5]]>, vp<%last.active.lane>
; CHECK-NEXT: Successor(s): ir-bb<exit>
; CHECK-EMPTY:
; CHECK-NEXT: ir-bb<exit>:
-; CHECK-NEXT: IR %t.lcssa = phi i16 [ %t, %loop ] (extra operand: vp<[[VP11]]> from middle.block)
+; CHECK-NEXT: IR %t.lcssa = phi i16 [ %t, %loop ] (extra operand: vp<[[VP7]]> from middle.block)
; CHECK-NEXT: No successors
; CHECK-NEXT: }
;
More information about the llvm-commits
mailing list