-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
[LV][VPlan] Add initial support for CSA vectorization #106560
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -269,6 +269,10 @@ class LoopVectorizationLegality { | |||||
/// induction descriptor. | ||||||
using InductionList = MapVector<PHINode *, InductionDescriptor>; | ||||||
|
||||||
/// CSAList contains the CSA descriptors for all the CSAs that were found | ||||||
/// in the loop, rooted by their phis. | ||||||
using CSAList = MapVector<PHINode *, CSADescriptor>; | ||||||
|
||||||
/// RecurrenceSet contains the phi nodes that are recurrences other than | ||||||
/// inductions and reductions. | ||||||
using RecurrenceSet = SmallPtrSet<const PHINode *, 8>; | ||||||
|
@@ -321,6 +325,12 @@ class LoopVectorizationLegality { | |||||
/// Returns True if V is a Phi node of an induction variable in this loop. | ||||||
bool isInductionPhi(const Value *V) const; | ||||||
|
||||||
/// Returns the CSAs found in the loop. | ||||||
const CSAList &getCSAs() const { return CSAs; } | ||||||
|
||||||
/// Returns true if Phi is the root of a CSA in the loop. | ||||||
bool isCSAPhi(PHINode *Phi) const { return CSAs.count(Phi) != 0; } | ||||||
|
||||||
/// Returns a pointer to the induction descriptor, if \p Phi is an integer or | ||||||
/// floating point induction. | ||||||
const InductionDescriptor *getIntOrFpInductionDescriptor(PHINode *Phi) const; | ||||||
|
@@ -550,6 +560,10 @@ class LoopVectorizationLegality { | |||||
void addInductionPhi(PHINode *Phi, const InductionDescriptor &ID, | ||||||
SmallPtrSetImpl<Value *> &AllowedExit); | ||||||
|
||||||
// Updates the vetorization state by adding \p Phi to the CSA list. | ||||||
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.
Suggested change
|
||||||
void addCSAPhi(PHINode *Phi, const CSADescriptor &CSADesc, | ||||||
SmallPtrSetImpl<Value *> &AllowedExit); | ||||||
|
||||||
/// The loop that we evaluate. | ||||||
Loop *TheLoop; | ||||||
|
||||||
|
@@ -594,6 +608,9 @@ class LoopVectorizationLegality { | |||||
/// variables can be pointers. | ||||||
InductionList Inductions; | ||||||
|
||||||
/// Holds the conditional scalar assignments | ||||||
CSAList CSAs; | ||||||
|
||||||
/// Holds all the casts that participate in the update chain of the induction | ||||||
/// variables, and that have been proven to be redundant (possibly under a | ||||||
/// runtime guard). These casts can be ignored when creating the vectorized | ||||||
|
This file contains 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 | ||||
---|---|---|---|---|---|---|
|
@@ -6,7 +6,8 @@ | |||||
// | ||||||
//===----------------------------------------------------------------------===// | ||||||
// | ||||||
// This file "describes" induction and recurrence variables. | ||||||
// This file "describes" induction, recurrence, and conditional scalar | ||||||
// assignment (CSA) variables. | ||||||
// | ||||||
//===----------------------------------------------------------------------===// | ||||||
|
||||||
|
@@ -1570,3 +1571,58 @@ bool InductionDescriptor::isInductionPHI( | |||||
D = InductionDescriptor(StartValue, IK_PtrInduction, Step); | ||||||
return true; | ||||||
} | ||||||
|
||||||
/// Return CSADescriptor that describes a CSA that matches one of these | ||||||
/// patterns: | ||||||
/// phi loop_inv, (select cmp, value, phi) | ||||||
/// phi loop_inv, (select cmp, phi, value) | ||||||
/// phi (select cmp, value, phi), loop_inv | ||||||
/// phi (select cmp, phi, value), loop_inv | ||||||
/// If the CSA does not match any of these paterns, return a CSADescriptor | ||||||
/// that describes an InvalidCSA. | ||||||
bool CSADescriptor::isCSAPhi(PHINode *Phi, Loop *TheLoop, CSADescriptor &CSA) { | ||||||
|
||||||
// Must be a scalar | ||||||
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.
Suggested change
|
||||||
Type *Type = Phi->getType(); | ||||||
if (!Type->isIntegerTy() && !Type->isFloatingPointTy() && | ||||||
!Type->isPointerTy()) | ||||||
return false; | ||||||
|
||||||
// Match phi loop_inv, (select cmp, value, phi) | ||||||
// or phi loop_inv, (select cmp, phi, value) | ||||||
// or phi (select cmp, value, phi), loop_inv | ||||||
// or phi (select cmp, phi, value), loop_inv | ||||||
if (Phi->getNumIncomingValues() != 2) | ||||||
return false; | ||||||
auto SelectInstIt = find_if(Phi->incoming_values(), [&Phi](const Use &U) { | ||||||
return match(U.get(), m_Select(m_Value(), m_Specific(Phi), m_Value())) || | ||||||
match(U.get(), m_Select(m_Value(), m_Value(), m_Specific(Phi))); | ||||||
}); | ||||||
if (SelectInstIt == Phi->incoming_values().end()) | ||||||
return false; | ||||||
auto LoopInvIt = find_if(Phi->incoming_values(), [&](Use &U) { | ||||||
return U.get() != *SelectInstIt && TheLoop->isLoopInvariant(U.get()); | ||||||
}); | ||||||
if (LoopInvIt == Phi->incoming_values().end()) | ||||||
return false; | ||||||
|
||||||
// Phi or Sel must be used only outside the loop, | ||||||
// excluding if Phi use Sel or Sel use Phi | ||||||
auto IsOnlyUsedOutsideLoop = [&](Value *V, Value *Ignore) { | ||||||
return all_of(V->users(), [Ignore, TheLoop](User *U) { | ||||||
if (U == Ignore) | ||||||
return true; | ||||||
if (auto *I = dyn_cast<Instruction>(U)) | ||||||
return !TheLoop->contains(I); | ||||||
return true; | ||||||
}); | ||||||
}; | ||||||
Instruction *Select = cast<SelectInst>(SelectInstIt->get()); | ||||||
Value *LoopInv = LoopInvIt->get(); | ||||||
if (!IsOnlyUsedOutsideLoop(Phi, Select) || | ||||||
!IsOnlyUsedOutsideLoop(Select, Phi)) | ||||||
return false; | ||||||
|
||||||
CSA = CSADescriptor(Phi, Select, LoopInv); | ||||||
return true; | ||||||
} |
This file contains 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 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 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 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 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 |
---|---|---|
|
@@ -174,8 +174,8 @@ class VPBuilder { | |
new VPInstruction(Opcode, Operands, WrapFlags, DL, Name)); | ||
} | ||
|
||
VPValue *createNot(VPValue *Operand, DebugLoc DL = {}, | ||
const Twine &Name = "") { | ||
VPInstruction *createNot(VPValue *Operand, DebugLoc DL = {}, | ||
const Twine &Name = "") { | ||
return createInstruction(VPInstruction::Not, {Operand}, DL, Name); | ||
} | ||
Comment on lines
-177
to
180
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. |
||
|
||
|
@@ -231,6 +231,37 @@ class VPBuilder { | |
Ptr, Offset, VPRecipeWithIRFlags::GEPFlagsTy(true), DL, Name)); | ||
} | ||
|
||
VPInstruction *createCSAMaskPhi(VPValue *InitMask, DebugLoc DL, | ||
const Twine &Name) { | ||
return createInstruction(VPInstruction::CSAMaskPhi, {InitMask}, DL, Name); | ||
} | ||
|
||
VPInstruction *createAnyOf(VPValue *Cond, DebugLoc DL, const Twine &Name) { | ||
return createInstruction(VPInstruction::AnyOf, {Cond}, DL, Name); | ||
} | ||
|
||
VPInstruction *createCSAMaskSel(VPValue *Cond, VPValue *MaskPhi, | ||
VPValue *AnyOf, DebugLoc DL, | ||
const Twine &Name) { | ||
return createInstruction(VPInstruction::CSAMaskSel, {Cond, MaskPhi, AnyOf}, | ||
DL, Name); | ||
} | ||
|
||
VPInstruction *createAnyOfEVL(VPValue *Cond, VPValue *EVL, DebugLoc DL, | ||
const Twine &Name) { | ||
return createInstruction(VPInstruction::AnyOfEVL, {Cond, EVL}, DL, Name); | ||
} | ||
|
||
VPInstruction *createCSAVLPhi(DebugLoc DL, const Twine &Name) { | ||
return createInstruction(VPInstruction::CSAVLPhi, {}, DL, Name); | ||
} | ||
|
||
VPInstruction *createCSAVLSel(VPValue *AnyOfEVL, VPValue *VLPhi, VPValue *EVL, | ||
DebugLoc DL, const Twine &Name) { | ||
return createInstruction(VPInstruction::CSAVLSel, {AnyOfEVL, VLPhi, EVL}, | ||
DL, Name); | ||
} | ||
|
||
VPDerivedIVRecipe *createDerivedIV(InductionDescriptor::InductionKind Kind, | ||
FPMathOperator *FPBinOp, VPValue *Start, | ||
VPCanonicalIVPHIRecipe *CanonicalIV, | ||
|
Oops, something went wrong.
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.
I don't think CAS is a very common term, would be good to have a more descriptive name if possible
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.
Intel has used the term conditional scalar assingmnet. I have abbreviated it as CSA for short. I have documented the acronym in the code in this patch in multiple places
I thought that
ConditionalScalarAssignmentDescriptor
,createConditionalScalarAssignmentMaskPhi
, andVPConditionalScalarAssignmentDescriptorExtractScalarRecipe
were quite long for example.Do you have any suggestion on what you'd like it to be named? Is expanding
CSA
toConditionalScalarAssignment
everywhere your preference?For now, I've tried to be proactive in ab17128.