-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[VPlan] Introduce vputils::getSingleScalarClone #161667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
artagnon
wants to merge
3
commits into
llvm:main
Choose a base branch
from
artagnon:vplan-get-single-scalar-clone
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-llvm-transforms Author: Ramkumar Ramachandra (artagnon) ChangesFull diff: https://github.com/llvm/llvm-project/pull/161667.diff 3 Files Affected:
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index acdb37996a443..2b3f4af8fa3ad 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -189,19 +189,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;
@@ -666,8 +654,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);
}
@@ -1309,15 +1296,14 @@ 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);
auto *Ext = new VPInstruction(VPInstruction::ExtractLastElement,
{Clone->getOperand(0)});
Ext->insertBefore(Clone);
Clone->setOperand(0, Ext);
- RepR->eraseFromParent();
+ RepOrWidenR->eraseFromParent();
continue;
}
@@ -1332,9 +1318,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);
}
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
index 059993043dcda..8a59bfc7abfe0 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
@@ -250,3 +250,22 @@ vputils::getRecipesForUncountableExit(VPlan &Plan,
return UncountableCondition;
}
+
+VPSingleDefRecipe *vputils::getSingleScalarClone(VPSingleDefRecipe *R) {
+ return TypeSwitch<VPSingleDefRecipe *, VPSingleDefRecipe *>(R)
+ .Case<VPInstruction, VPWidenRecipe, VPWidenCastRecipe,
+ 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) { return nullptr; });
+}
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.h b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
index 0222b0aa81063..4979ade0c2764 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
@@ -116,6 +116,10 @@ std::optional<VPValue *>
getRecipesForUncountableExit(VPlan &Plan,
SmallVectorImpl<VPRecipeBase *> &Recipes,
SmallVectorImpl<VPRecipeBase *> &GEPs);
+
+/// Returns a single-scalar version of \p R if possible, creating a fresh
+/// single-scalar VPReplicateRecipe or just cloning the recipe.
+VPSingleDefRecipe *getSingleScalarClone(VPSingleDefRecipe *R);
} // namespace vputils
//===----------------------------------------------------------------------===//
|
@llvm/pr-subscribers-vectorizers Author: Ramkumar Ramachandra (artagnon) ChangesFull diff: https://github.com/llvm/llvm-project/pull/161667.diff 3 Files Affected:
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index acdb37996a443..2b3f4af8fa3ad 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -189,19 +189,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;
@@ -666,8 +654,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);
}
@@ -1309,15 +1296,14 @@ 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);
auto *Ext = new VPInstruction(VPInstruction::ExtractLastElement,
{Clone->getOperand(0)});
Ext->insertBefore(Clone);
Clone->setOperand(0, Ext);
- RepR->eraseFromParent();
+ RepOrWidenR->eraseFromParent();
continue;
}
@@ -1332,9 +1318,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);
}
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
index 059993043dcda..8a59bfc7abfe0 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
@@ -250,3 +250,22 @@ vputils::getRecipesForUncountableExit(VPlan &Plan,
return UncountableCondition;
}
+
+VPSingleDefRecipe *vputils::getSingleScalarClone(VPSingleDefRecipe *R) {
+ return TypeSwitch<VPSingleDefRecipe *, VPSingleDefRecipe *>(R)
+ .Case<VPInstruction, VPWidenRecipe, VPWidenCastRecipe,
+ 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) { return nullptr; });
+}
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.h b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
index 0222b0aa81063..4979ade0c2764 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
@@ -116,6 +116,10 @@ std::optional<VPValue *>
getRecipesForUncountableExit(VPlan &Plan,
SmallVectorImpl<VPRecipeBase *> &Recipes,
SmallVectorImpl<VPRecipeBase *> &GEPs);
+
+/// Returns a single-scalar version of \p R if possible, creating a fresh
+/// single-scalar VPReplicateRecipe or just cloning the recipe.
+VPSingleDefRecipe *getSingleScalarClone(VPSingleDefRecipe *R);
} // namespace vputils
//===----------------------------------------------------------------------===//
|
Gentle ping. |
1 similar comment
Gentle ping. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.