[llvm] [VPlan] Introduce vputils::getSingleScalarClone (PR #161667)
Ramkumar Ramachandra via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 3 00:00:24 PST 2025
https://github.com/artagnon updated https://github.com/llvm/llvm-project/pull/161667
>From fa43d675df5f8fba7b308d3efb64b4e3e1c1e785 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Thu, 2 Oct 2025 14:21:01 +0100
Subject: [PATCH] [VPlan] Introduce vputils::getSingleScalarClone
---
.../Transforms/Vectorize/VPlanTransforms.cpp | 26 ++++---------------
llvm/lib/Transforms/Vectorize/VPlanUtils.cpp | 22 ++++++++++++++++
llvm/lib/Transforms/Vectorize/VPlanUtils.h | 4 +++
3 files changed, 31 insertions(+), 21 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index b45536869c5af..87bea593bddc0 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -214,19 +214,7 @@ static bool sinkScalarOperands(VPlan &Plan) {
if (NeedsDuplicating) {
if (ScalarVFOnly)
continue;
- VPSingleDefRecipe *Clone;
- if (auto *SinkCandidateRepR =
- dyn_cast<VPReplicateRecipe>(SinkCandidate)) {
- // TODO: Handle converting to uniform recipes as separate transform,
- // then cloning should be sufficient here.
- Instruction *I = SinkCandidate->getUnderlyingInstr();
- Clone = new VPReplicateRecipe(I, SinkCandidate->operands(), true,
- nullptr /*Mask*/, *SinkCandidateRepR);
- // TODO: add ".cloned" suffix to name of Clone's VPValue.
- } else {
- Clone = SinkCandidate->clone();
- }
-
+ VPSingleDefRecipe *Clone = vputils::getSingleScalarClone(SinkCandidate);
Clone->insertBefore(SinkCandidate);
SinkCandidate->replaceUsesWithIf(Clone, [SinkTo](VPUser &U, unsigned) {
return cast<VPRecipeBase>(&U)->getParent() != SinkTo;
@@ -693,8 +681,7 @@ static void legalizeAndOptimizeInductions(VPlan &Plan) {
if (!vputils::isSingleScalar(Def) && !vputils::onlyFirstLaneUsed(Def))
continue;
- auto *Clone = new VPReplicateRecipe(Def->getUnderlyingInstr(),
- Def->operands(), /*IsUniform*/ true);
+ VPSingleDefRecipe *Clone = vputils::getSingleScalarClone(Def);
Clone->insertAfter(Def);
Def->replaceAllUsesWith(Clone);
}
@@ -1399,9 +1386,8 @@ static void narrowToSingleScalarRecipes(VPlan &Plan) {
auto *RepOrWidenR = cast<VPSingleDefRecipe>(&R);
if (RepR && isa<StoreInst>(RepR->getUnderlyingInstr()) &&
vputils::isSingleScalar(RepR->getOperand(1))) {
- auto *Clone = new VPReplicateRecipe(
- RepOrWidenR->getUnderlyingInstr(), RepOrWidenR->operands(),
- true /*IsSingleScalar*/, nullptr /*Mask*/, *RepR /*Metadata*/);
+ auto *Clone =
+ cast<VPReplicateRecipe>(vputils::getSingleScalarClone(RepOrWidenR));
Clone->insertBefore(RepOrWidenR);
unsigned ExtractOpc =
vputils::isUniformAcrossVFsAndUFs(RepR->getOperand(1))
@@ -1426,9 +1412,7 @@ static void narrowToSingleScalarRecipes(VPlan &Plan) {
}))
continue;
- auto *Clone = new VPReplicateRecipe(RepOrWidenR->getUnderlyingInstr(),
- RepOrWidenR->operands(),
- true /*IsSingleScalar*/);
+ VPSingleDefRecipe *Clone = vputils::getSingleScalarClone(RepOrWidenR);
Clone->insertBefore(RepOrWidenR);
RepOrWidenR->replaceAllUsesWith(Clone);
if (isDeadRecipe(*RepOrWidenR))
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
index c6380d30ab2e2..e86473e5501e5 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
@@ -351,6 +351,28 @@ vputils::getRecipesForUncountableExit(VPlan &Plan,
return UncountableCondition;
}
+VPSingleDefRecipe *vputils::getSingleScalarClone(VPSingleDefRecipe *R) {
+ // TODO: add ".cloned" suffix to name of Clone's VPValue.
+ return TypeSwitch<VPSingleDefRecipe *, VPSingleDefRecipe *>(R)
+ .Case<VPInstruction, VPWidenRecipe, VPWidenSelectRecipe,
+ VPWidenCallRecipe, VPReplicateRecipe>([](auto *I) {
+ return new VPReplicateRecipe(I->getUnderlyingInstr(), I->operands(),
+ /*IsSingleScalar*/ true,
+ /*Mask*/ nullptr,
+ /*Metadata*/ *I);
+ })
+ .Case<VPWidenGEPRecipe>([](auto *I) {
+ // WidenGEP does not have metadata.
+ return new VPReplicateRecipe(I->getUnderlyingInstr(), I->operands(),
+ /*IsSingleScalar*/ true, /*Mask*/ nullptr);
+ })
+ .Case<VPScalarIVStepsRecipe>([](auto *I) { return I->clone(); })
+ .Default([](auto *I) {
+ llvm_unreachable("Don't know how to convert to single-scalar");
+ return nullptr;
+ });
+}
+
bool VPBlockUtils::isHeader(const VPBlockBase *VPB,
const VPDominatorTree &VPDT) {
auto *VPBB = dyn_cast<VPBasicBlock>(VPB);
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.h b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
index c21a0e70c1392..a4ff446b559b2 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
@@ -71,6 +71,10 @@ std::optional<VPValue *>
getRecipesForUncountableExit(VPlan &Plan,
SmallVectorImpl<VPRecipeBase *> &Recipes,
SmallVectorImpl<VPRecipeBase *> &GEPs);
+
+/// Returns a single-scalar version of \p R, creating a fresh single-scalar
+/// VPReplicateRecipe or just cloning the recipe.
+VPSingleDefRecipe *getSingleScalarClone(VPSingleDefRecipe *R);
} // namespace vputils
//===----------------------------------------------------------------------===//
More information about the llvm-commits
mailing list