-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[VectorCombine][AMDGPU] Narrow Phi of Shuffles. #140188
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
PeddleSpam
wants to merge
11
commits into
llvm:main
Choose a base branch
from
PeddleSpam:narrow_phis
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
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f331e87
[VectorCombine][AMDGPU] Narrow Phi of Shuffles.
4a6a0f2
Add VectorCombine transform and update tests.
c712df5
Address review comments.
78d6a2e
Address review comments.
e4f333b
Formatting.
bb89c13
Retrigger checks.
d0b3d68
Merge branch 'main' into narrow_phis
PeddleSpam cae4e9b
Address comments.
aca0aaa
Address review comments.
2b72aa9
Add tests for each target.
b23b785
Update tests.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,6 +132,7 @@ class VectorCombine { | |
bool foldSelectShuffle(Instruction &I, bool FromReduction = false); | ||
bool foldInterleaveIntrinsics(Instruction &I); | ||
bool shrinkType(Instruction &I); | ||
bool shrinkPhiOfShuffles(Instruction &I); | ||
|
||
void replaceValue(Value &Old, Value &New) { | ||
LLVM_DEBUG(dbgs() << "VC: Replacing: " << Old << '\n'); | ||
|
@@ -3519,6 +3520,95 @@ bool VectorCombine::foldInterleaveIntrinsics(Instruction &I) { | |
return true; | ||
} | ||
|
||
/// Attempt to narrow a phi of shufflevector instructions where the two incoming | ||
/// values have the same operands but different masks. If the two shuffle masks | ||
/// are offsets of one another we can use one branch to rotate the incoming | ||
/// vector and perform one larger shuffle after the phi. | ||
bool VectorCombine::shrinkPhiOfShuffles(Instruction &I) { | ||
auto *Phi = dyn_cast<PHINode>(&I); | ||
if (!Phi || Phi->getNumIncomingValues() != 2u) | ||
return false; | ||
|
||
Value *Op = nullptr; | ||
ArrayRef<int> Mask0; | ||
ArrayRef<int> Mask1; | ||
|
||
if (!match(Phi->getOperand(0u), | ||
m_OneUse(m_Shuffle(m_Value(Op), m_Poison(), m_Mask(Mask0)))) || | ||
!match(Phi->getOperand(1u), | ||
m_OneUse(m_Shuffle(m_Specific(Op), m_Poison(), m_Mask(Mask1))))) | ||
return false; | ||
PeddleSpam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
auto *Shuf = cast<ShuffleVectorInst>(Phi->getOperand(0u)); | ||
|
||
// Ensure result vectors are wider than the argument vector. | ||
auto *InputVT = cast<FixedVectorType>(Op->getType()); | ||
auto *ResultVT = cast<FixedVectorType>(Shuf->getType()); | ||
auto const InputNumElements = InputVT->getNumElements(); | ||
|
||
if (InputNumElements >= ResultVT->getNumElements()) | ||
return false; | ||
|
||
// Take the difference of the two shuffle masks at each index. Ignore poison | ||
// values at the same index in both masks. | ||
SmallVector<int, 16> NewMask; | ||
NewMask.reserve(Mask0.size()); | ||
|
||
for (auto I = 0u; I < Mask0.size(); ++I) { | ||
PeddleSpam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (Mask0[I] >= 0 && Mask1[I] >= 0) | ||
NewMask.push_back(Mask0[I] - Mask1[I]); | ||
else if (Mask0[I] == -1 && Mask1[I] == -1) | ||
continue; | ||
else | ||
return false; | ||
} | ||
|
||
// Ensure all elements of the new mask are equal. If the difference between | ||
// the incoming mask elements is the same, the two must be constant offsets | ||
// of one another. | ||
if (NewMask.empty() || !all_equal(NewMask)) | ||
return false; | ||
|
||
// Create new mask using difference of the two incoming masks. | ||
int MaskOffset = NewMask[0u]; | ||
unsigned Index = (InputNumElements - MaskOffset) % InputNumElements; | ||
NewMask.clear(); | ||
|
||
for (unsigned I = 0u; I < InputNumElements; ++I) { | ||
NewMask.push_back(Index); | ||
Index = (Index + 1u) % InputNumElements; | ||
} | ||
|
||
// Calculate costs for worst cases and compare. | ||
auto const Kind = TTI::SK_PermuteSingleSrc; | ||
auto OldCost = std::max(TTI.getShuffleCost(Kind, InputVT, Mask0, CostKind), | ||
TTI.getShuffleCost(Kind, InputVT, Mask1, CostKind)); | ||
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. getShuffleCost has changed to take a dst and src type - you need to merge with trunk |
||
auto NewCost = TTI.getShuffleCost(Kind, InputVT, NewMask, CostKind) + | ||
TTI.getShuffleCost(Kind, InputVT, Mask1, CostKind); | ||
|
||
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. Add debug message:
|
||
if (NewCost > OldCost) | ||
return false; | ||
|
||
// Create new shuffles and narrowed phi. | ||
auto Builder = IRBuilder(Shuf); | ||
Builder.SetCurrentDebugLocation(Shuf->getDebugLoc()); | ||
auto *PoisonVal = PoisonValue::get(InputVT); | ||
auto *NewShuf0 = Builder.CreateShuffleVector(Op, PoisonVal, NewMask); | ||
PeddleSpam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Builder.SetInsertPoint(Phi); | ||
Builder.SetCurrentDebugLocation(Phi->getDebugLoc()); | ||
auto *NewPhi = Builder.CreatePHI(NewShuf0->getType(), 2u); | ||
NewPhi->addIncoming(NewShuf0, Phi->getIncomingBlock(0u)); | ||
NewPhi->addIncoming(Op, Phi->getIncomingBlock(1u)); | ||
|
||
Builder.SetInsertPoint(*NewPhi->getInsertionPointAfterDef()); | ||
PoisonVal = PoisonValue::get(NewPhi->getType()); | ||
auto *NewShuf1 = Builder.CreateShuffleVector(NewPhi, PoisonVal, Mask1); | ||
|
||
replaceValue(*Phi, *NewShuf1); | ||
return true; | ||
} | ||
|
||
/// This is the entry point for all transforms. Pass manager differences are | ||
/// handled in the callers of this function. | ||
bool VectorCombine::run() { | ||
|
@@ -3597,6 +3687,9 @@ bool VectorCombine::run() { | |
case Instruction::BitCast: | ||
MadeChange |= foldBitcastShuffle(I); | ||
break; | ||
case Instruction::PHI: | ||
MadeChange |= shrinkPhiOfShuffles(I); | ||
break; | ||
default: | ||
MadeChange |= shrinkType(I); | ||
break; | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.