-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[LV] Use original trip-count as the vector-trip-count if use predicated EVL instructions for tail-folding. #132675
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
NexMing
wants to merge
4
commits into
llvm:main
Choose a base branch
from
NexMing:evl-vplan
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.
+874
−1,824
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b34c7c0
[LV] Use original trip count as the vector trip count if use predicat…
NexMing c331045
[NFC] Simplify code
NexMing ef2e776
Update llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
NexMing 738264e
Remove unnecessary VPEVLBasedIVPHIRecipe.
NexMing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2021,10 +2021,9 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) { | |
} | ||
} | ||
|
||
/// Add a VPEVLBasedIVPHIRecipe and related recipes to \p Plan and | ||
/// replaces all uses except the canonical IV increment of | ||
/// VPCanonicalIVPHIRecipe with a VPEVLBasedIVPHIRecipe. VPCanonicalIVPHIRecipe | ||
/// is used only for loop iterations counting after this transformation. | ||
/// Add a VPInstruction::ExplicitVectorLength and related recipes to \p Plan and | ||
/// adjust the increment of the canonical induction variable to an explicit | ||
/// vector length. | ||
/// | ||
/// The function uses the following definitions: | ||
/// %StartV is the canonical induction start value. | ||
|
@@ -2035,29 +2034,25 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) { | |
/// ... | ||
/// | ||
/// vector.body: | ||
/// ... | ||
/// %EVLPhi = EXPLICIT-VECTOR-LENGTH-BASED-IV-PHI [ %StartV, %vector.ph ], | ||
/// [ %NextEVLIV, %vector.body ] | ||
/// %AVL = sub original TC, %EVLPhi | ||
/// %IVPhi = CANONICAL-INDUCTION ir<0>, %IV.NEXT | ||
/// %AVL = sub original TC, %IVPhi | ||
/// %VPEVL = EXPLICIT-VECTOR-LENGTH %AVL | ||
/// ... | ||
/// %NextEVLIV = add IVSize (cast i32 %VPEVVL to IVSize), %EVLPhi | ||
/// %IV.NEXT = add %IVPhi, IVSize (cast i32 %VPEVL to IVSize) | ||
/// ... | ||
/// | ||
/// If MaxSafeElements is provided, the function adds the following recipes: | ||
/// vector.ph: | ||
/// ... | ||
/// | ||
/// vector.body: | ||
/// ... | ||
/// %EVLPhi = EXPLICIT-VECTOR-LENGTH-BASED-IV-PHI [ %StartV, %vector.ph ], | ||
/// [ %NextEVLIV, %vector.body ] | ||
/// %IVPhi = CANONICAL-INDUCTION ir<0>, %IV.NEXT | ||
/// %AVL = sub original TC, %EVLPhi | ||
/// %cmp = cmp ult %AVL, MaxSafeElements | ||
/// %SAFE_AVL = select %cmp, %AVL, MaxSafeElements | ||
/// %VPEVL = EXPLICIT-VECTOR-LENGTH %SAFE_AVL | ||
/// ... | ||
/// %NextEVLIV = add IVSize (cast i32 %VPEVL to IVSize), %EVLPhi | ||
/// %IV.NEXT = add %IVPhi, IVSize (cast i32 %VPEVL to IVSize) | ||
/// ... | ||
/// | ||
bool VPlanTransforms::tryAddExplicitVectorLength( | ||
|
@@ -2073,15 +2068,11 @@ bool VPlanTransforms::tryAddExplicitVectorLength( | |
return false; | ||
|
||
auto *CanonicalIVPHI = Plan.getCanonicalIV(); | ||
VPValue *StartV = CanonicalIVPHI->getStartValue(); | ||
|
||
// Create the ExplicitVectorLengthPhi recipe in the main loop. | ||
auto *EVLPhi = new VPEVLBasedIVPHIRecipe(StartV, DebugLoc()); | ||
EVLPhi->insertAfter(CanonicalIVPHI); | ||
VPBuilder Builder(Header, Header->getFirstNonPhi()); | ||
// Compute original TC - IV as the AVL (application vector length). | ||
VPValue *AVL = Builder.createNaryOp( | ||
Instruction::Sub, {Plan.getTripCount(), EVLPhi}, DebugLoc(), "avl"); | ||
Instruction::Sub, {&Plan.getVectorTripCount(), CanonicalIVPHI}, | ||
DebugLoc(), "avl"); | ||
if (MaxSafeElements) { | ||
// Support for MaxSafeDist for correct loop emission. | ||
VPValue *AVLSafe = Plan.getOrAddLiveIn( | ||
|
@@ -2094,27 +2085,20 @@ bool VPlanTransforms::tryAddExplicitVectorLength( | |
|
||
auto *CanonicalIVIncrement = | ||
cast<VPInstruction>(CanonicalIVPHI->getBackedgeValue()); | ||
Builder.setInsertPoint(CanonicalIVIncrement); | ||
VPSingleDefRecipe *OpVPEVL = VPEVL; | ||
if (unsigned IVSize = CanonicalIVPHI->getScalarType()->getScalarSizeInBits(); | ||
IVSize != 32) { | ||
OpVPEVL = Builder.createScalarCast( | ||
IVSize < 32 ? Instruction::Trunc : Instruction::ZExt, OpVPEVL, | ||
CanonicalIVPHI->getScalarType(), CanonicalIVIncrement->getDebugLoc()); | ||
} | ||
auto *NextEVLIV = Builder.createOverflowingOp( | ||
Instruction::Add, {OpVPEVL, EVLPhi}, | ||
{CanonicalIVIncrement->hasNoUnsignedWrap(), | ||
CanonicalIVIncrement->hasNoSignedWrap()}, | ||
CanonicalIVIncrement->getDebugLoc(), "index.evl.next"); | ||
EVLPhi->addOperand(NextEVLIV); | ||
|
||
transformRecipestoEVLRecipes(Plan, *VPEVL); | ||
|
||
// Replace all uses of VPCanonicalIVPHIRecipe by | ||
// VPEVLBasedIVPHIRecipe except for the canonical IV increment. | ||
CanonicalIVPHI->replaceAllUsesWith(EVLPhi); | ||
// Adjust the increment of the canonical induction variable to an explicit | ||
// vector length. | ||
CanonicalIVIncrement->setOperand(0, CanonicalIVPHI); | ||
CanonicalIVIncrement->setOperand(1, OpVPEVL); | ||
// TODO: support unroll factor > 1. | ||
Plan.setUF(1); | ||
return true; | ||
|
@@ -2298,17 +2282,14 @@ void VPlanTransforms::convertToConcreteRecipes(VPlan &Plan) { | |
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>( | ||
vp_depth_first_deep(Plan.getEntry()))) { | ||
for (VPRecipeBase &R : make_early_inc_range(VPBB->phis())) { | ||
if (!isa<VPCanonicalIVPHIRecipe, VPEVLBasedIVPHIRecipe>(&R)) | ||
continue; | ||
auto *PhiR = cast<VPHeaderPHIRecipe>(&R); | ||
StringRef Name = | ||
isa<VPCanonicalIVPHIRecipe>(PhiR) ? "index" : "evl.based.iv"; | ||
auto *ScalarR = new VPInstruction( | ||
Instruction::PHI, {PhiR->getStartValue(), PhiR->getBackedgeValue()}, | ||
PhiR->getDebugLoc(), Name); | ||
ScalarR->insertBefore(PhiR); | ||
PhiR->replaceAllUsesWith(ScalarR); | ||
PhiR->eraseFromParent(); | ||
if (auto *PhiR = dyn_cast<VPCanonicalIVPHIRecipe>(&R)) { | ||
auto *ScalarR = new VPInstruction( | ||
Instruction::PHI, {PhiR->getStartValue(), PhiR->getBackedgeValue()}, | ||
PhiR->getDebugLoc(), "index"); | ||
ScalarR->insertBefore(PhiR); | ||
PhiR->replaceAllUsesWith(ScalarR); | ||
PhiR->eraseFromParent(); | ||
} | ||
Comment on lines
+2285
to
+2292
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. VPCanonicalIVPHIRecipe's step must be loop invariant, otherwise it isn't an induction. If you want to do this transform, you must replace the canonical IV with a plain scalar phi recipe, like done in |
||
} | ||
} | ||
} | ||
|
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look like it is the right place to update. The vector trip count is guaranteed to be a multiple of VF.
The caller would need updating to not use it/request when folding the tail with EVL