[llvm] [VPlan] Pass result type to VPBuilder::createSingleScalarOp (NFC). (PR #227659)
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 30 04:11:47 PDT 2026
https://github.com/fhahn created https://github.com/llvm/llvm-project/pull/227659
This removes another use of the underlying value to get the result type for casts. For other recipes, it just adds an additional consistency check for now.
>From 1f1c681bdef6ace257d5d191da665e0d3e5343cf Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Sun, 20 Sep 2026 13:57:29 +0100
Subject: [PATCH] [VPlan] Pass result type to VPBuilder::createSingleScalarOp
(NFC).
This removes another use of the underlying value to get the result type
for casts. For other recipes, it just adds an additional consistency
check for now.
---
.../Vectorize/LoopVectorizationPlanner.h | 19 ++++++++++---------
.../Transforms/Vectorize/LoopVectorize.cpp | 2 +-
.../Transforms/Vectorize/VPlanTransforms.cpp | 10 ++++++----
llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp | 5 +++--
4 files changed, 20 insertions(+), 16 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h b/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h
index 3c07a6e159656..733b8647a06e3 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h
@@ -499,21 +499,22 @@ template <typename InserterTy> class VPBuilderBase : public InserterTy {
/// Create a single-scalar recipe with \p Opcode and \p Operands without
/// inserting it.
- static VPSingleDefRecipe *createSingleScalarOp(unsigned Opcode,
- ArrayRef<VPValue *> Operands,
- VPValue *Mask,
- const VPIRFlags &Flags,
- const VPIRMetadata &Metadata,
- DebugLoc DL, Instruction *UV) {
+ static VPSingleDefRecipe *
+ createSingleScalarOp(unsigned Opcode, ArrayRef<VPValue *> Operands,
+ VPValue *Mask, const VPIRFlags &Flags,
+ const VPIRMetadata &Metadata, DebugLoc DL,
+ Type *ResultTy, Instruction *UV) {
if (Instruction::isCast(Opcode)) {
assert(!Mask && "Cast cannot be predicated");
auto *VPI = new VPInstruction(Opcode, Operands, Flags, Metadata, DL,
- UV->getName(), UV->getType());
+ UV->getName(), ResultTy);
VPI->setUnderlyingValue(UV);
return VPI;
}
- return new VPReplicateRecipe(UV, Operands, /*IsSingleScalar=*/true, Mask,
- Flags, Metadata, DL);
+ auto *RepR = new VPReplicateRecipe(UV, Operands, /*IsSingleScalar=*/true,
+ Mask, Flags, Metadata, DL);
+ assert(RepR->getScalarType() == ResultTy && "unexpected result type");
+ return RepR;
}
VPScalarIVStepsRecipe *
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 0986d8ea9e59d..45edf702d7092 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -6296,7 +6296,7 @@ VPSingleDefRecipe *VPRecipeBuilder::handleReplication(VPInstruction *VPI,
if (IsUniform) {
return VPBuilder::createSingleScalarOp(
VPI->getOpcode(), VPI->operandsWithoutMask(), BlockInMask, *VPI, *VPI,
- VPI->getDebugLoc(), I);
+ VPI->getDebugLoc(), VPI->getScalarType(), I);
}
auto *Recipe = new VPReplicateRecipe(I, VPI->operandsWithoutMask(),
/*IsSingleScalar=*/false, BlockInMask,
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 7a4134018565d..d61b5eae71dd1 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -399,7 +399,8 @@ static bool sinkScalarOperands(VPlan &Plan) {
Clone = VPBuilder::createSingleScalarOp(
SinkCandidateRepR->getOpcode(), SinkCandidate->operands(),
/*Mask=*/nullptr, *SinkCandidateRepR, *SinkCandidateRepR,
- SinkCandidate->getDebugLoc(), SinkCandidate->getUnderlyingInstr());
+ SinkCandidate->getDebugLoc(), SinkCandidate->getScalarType(),
+ SinkCandidate->getUnderlyingInstr());
// TODO: add ".cloned" suffix to name of Clone's VPValue.
} else {
Clone = SinkCandidate->clone();
@@ -813,7 +814,7 @@ static void legalizeAndOptimizeInductions(VPlan &Plan) {
auto *Clone = VPBuilder::createSingleScalarOp(
Def->getUnderlyingInstr()->getOpcode(), Def->operands(),
/*Mask=*/nullptr, *Def, getMetadataOf(Def), DebugLoc::getUnknown(),
- Def->getUnderlyingInstr());
+ Def->getScalarType(), Def->getUnderlyingInstr());
Clone->insertAfter(Def);
Def->replaceAllUsesWith(Clone);
Def->eraseFromParent();
@@ -1902,7 +1903,8 @@ static void narrowToSingleScalarRecipes(VPlan &Plan) {
auto *Clone = VPBuilder::createSingleScalarOp(
vputils::getOpcode(RepOrWidenR), RepOrWidenR->operands(),
/*Mask=*/nullptr, *RepOrWidenR, getMetadataOf(RepOrWidenR),
- DebugLoc::getUnknown(), RepOrWidenR->getUnderlyingInstr());
+ DebugLoc::getUnknown(), RepOrWidenR->getScalarType(),
+ RepOrWidenR->getUnderlyingInstr());
Clone->insertBefore(RepOrWidenR);
RepOrWidenR->replaceAllUsesWith(Clone);
if (vputils::isDeadRecipe(*RepOrWidenR))
@@ -5773,7 +5775,7 @@ void VPlanTransforms::makeScalarizationDecisions(VPlan &Plan, VFRange &Range) {
auto *Recipe = VPBuilder::createSingleScalarOp(
VPI.getOpcode(), VPI.operandsWithoutMask(), /*Mask=*/nullptr, VPI,
- VPI, VPI.getDebugLoc(), I);
+ VPI, VPI.getDebugLoc(), VPI.getScalarType(), I);
Recipe->insertBefore(&VPI);
VPI.replaceAllUsesWith(Recipe);
VPI.eraseFromParent();
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp b/llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp
index 1be9bdf6c01ae..d1e30ab0cff65 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp
@@ -657,7 +657,7 @@ cloneForLane(VPlan &Plan, VPBuilder &Builder, Type *IdxTy,
// Mask from the operands?)
New = VPBuilder::createSingleScalarOp(
RepR->getOpcode(), NewOps, /*Mask=*/nullptr, *RepR, *RepR,
- RepR->getDebugLoc(), RepR->getUnderlyingInstr());
+ RepR->getDebugLoc(), RepR->getScalarType(), RepR->getUnderlyingInstr());
} else {
New = DefR->clone();
for (const auto &[Idx, Op] : enumerate(NewOps)) {
@@ -736,7 +736,8 @@ static void convertRecipesInRegionBlocksToSingleScalar(VPlan &Plan, Type *IdxTy,
if (auto *RepR = dyn_cast<VPReplicateRecipe>(&OldR)) {
auto *NewR = VPBuilder::createSingleScalarOp(
RepR->getOpcode(), to_vector(RepR->operands()), /*Mask=*/nullptr,
- *RepR, *RepR, OldDL, RepR->getUnderlyingInstr());
+ *RepR, *RepR, OldDL, RepR->getScalarType(),
+ RepR->getUnderlyingInstr());
NewR->insertBefore(RepR);
RepR->replaceAllUsesWith(NewR);
RepR->eraseFromParent();
More information about the llvm-commits
mailing list