[llvm] [VPlan] Add VPBuilder methods to create (First|Last)ActiveLane (NFC). (PR #195479)
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Sat May 2 13:24:04 PDT 2026
https://github.com/fhahn created https://github.com/llvm/llvm-project/pull/195479
Add dedicaed helpers to builder to slightly simplify code a use-sites.
>From 0f21a5c79019d88dd953cd8adeba69751b003abe Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Sat, 2 May 2026 20:48:46 +0100
Subject: [PATCH] [VPlan] Add VPBuilder methods to create
(First|Last)ActiveLane (NFC).
Add dedicaed helpers to builder to slightly simplify code a use-sites.
---
.../Vectorize/LoopVectorizationPlanner.h | 14 ++++++++++++++
.../lib/Transforms/Vectorize/VPlanConstruction.cpp | 3 +--
llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp | 13 +++++--------
3 files changed, 20 insertions(+), 10 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h b/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h
index 7bdd5a3b34e8e..814a414a52ac8 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h
@@ -201,6 +201,20 @@ class VPBuilder {
Opcode, Operands, ResultTy, Flags, {}, DL, Name));
}
+ VPInstruction *createFirstActiveLane(ArrayRef<VPValue *> Masks,
+ DebugLoc DL = DebugLoc::getUnknown(),
+ const Twine &Name = "") {
+ return tryInsertInstruction(new VPInstruction(
+ VPInstruction::FirstActiveLane, Masks, {}, {}, DL, Name));
+ }
+
+ VPInstruction *createLastActiveLane(ArrayRef<VPValue *> Masks,
+ DebugLoc DL = DebugLoc::getUnknown(),
+ const Twine &Name = "") {
+ return tryInsertInstruction(new VPInstruction(VPInstruction::LastActiveLane,
+ Masks, {}, {}, DL, Name));
+ }
+
VPInstruction *createOverflowingOp(
unsigned Opcode, ArrayRef<VPValue *> Operands,
VPRecipeWithIRFlags::WrapFlagsTy WrapFlags = {false, false},
diff --git a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
index e4f2ae41fd468..e20d5d947ac54 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
@@ -1384,8 +1384,7 @@ void VPlanTransforms::foldTailByMasking(VPlan &Plan) {
continue;
// Compute the index of the last active lane.
- VPValue *LastActiveLane =
- Builder.createNaryOp(VPInstruction::LastActiveLane, HeaderMask);
+ VPValue *LastActiveLane = Builder.createLastActiveLane(HeaderMask);
auto *Ext =
Builder.createNaryOp(VPInstruction::ExtractLane, {LastActiveLane, Op});
R.getVPSingleValue()->replaceAllUsesWith(Ext);
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 9b34bd1f092a0..6f349a96a92d3 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -1057,8 +1057,7 @@ static VPValue *optimizeEarlyExitInductionUser(VPlan &Plan,
VPBuilder B(cast<VPBasicBlock>(PredVPBB));
DebugLoc DL = cast<VPInstruction>(Op)->getDebugLoc();
- VPValue *FirstActiveLane =
- B.createNaryOp(VPInstruction::FirstActiveLane, Mask, DL);
+ VPValue *FirstActiveLane = B.createFirstActiveLane(Mask, DL);
Type *FirstActiveLaneType = TypeInfo.inferScalarType(FirstActiveLane);
FirstActiveLane = B.createScalarZExtOrTrunc(FirstActiveLane, CanonicalIVType,
FirstActiveLaneType, DL);
@@ -3968,9 +3967,8 @@ void VPlanTransforms::convertToConcreteRecipes(VPlan &Plan) {
}
// Create FirstActiveLane on the inverted masks.
- VPValue *FirstInactiveLane = Builder.createNaryOp(
- VPInstruction::FirstActiveLane, NotMasks,
- LastActiveL->getDebugLoc(), "first.inactive.lane");
+ VPValue *FirstInactiveLane = Builder.createFirstActiveLane(
+ NotMasks, LastActiveL->getDebugLoc(), "first.inactive.lane");
// Subtract 1 to get the last active lane.
VPValue *One =
@@ -4165,9 +4163,8 @@ void VPlanTransforms::handleUncountableEarlyExits(VPlan &Plan,
Exits.size() == 1 ? VectorEarlyExitVPBBs[0]
: Plan.createVPBasicBlock("vector.early.exit.check");
VPBuilder DispatchBuilder(DispatchVPBB, DispatchVPBB->begin());
- VPValue *FirstActiveLane =
- DispatchBuilder.createNaryOp(VPInstruction::FirstActiveLane, {Combined},
- DebugLoc::getUnknown(), "first.active.lane");
+ VPValue *FirstActiveLane = DispatchBuilder.createFirstActiveLane(
+ {Combined}, DebugLoc::getUnknown(), "first.active.lane");
// For each early exit, disconnect the original exiting block
// (early.exiting.I) from the exit block (ir-bb<exit.I>) and route through a
More information about the llvm-commits
mailing list