-
Notifications
You must be signed in to change notification settings - Fork 13.7k
[VectorCombine] Shrink loads used in shufflevector rebroadcasts #128938
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
18
commits into
llvm:main
Choose a base branch
from
PeddleSpam:shrink_loads
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.
+513
−36
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
3a75016
[AggressiveInstCombine] Shrink loads used in shufflevector rebroadcasts.
f566b7a
Add implementation and update tests.
f5c8bf5
Fix broken tests.
157074c
Ignore non-simple loads.
d6c00c0
Move transform to VectorCombine and update tests.
4d6873f
Address review comments and update tests.
1861c7d
Code formatting.
6534370
Add cost analysis for shufflevector ops and update tests.
5b63d6f
Code formatting.
f2c6b30
Address comments.
4dc36ee
Fix conflict with subvector load widening.
1092f5b
Fix build bot false positive for undef.
265f960
Address comments.
070b6f8
Merge branch 'main' into shrink_loads
PeddleSpam 6ca4bfa
Address review comments.
0310170
Address comments.
6a9dd0b
Formatting.
019e39f
Correct renaming and remove function-style casts.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
#include "llvm/ADT/DenseMap.h" | ||
#include "llvm/ADT/STLExtras.h" | ||
#include "llvm/ADT/ScopeExit.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/ADT/Statistic.h" | ||
#include "llvm/Analysis/AssumptionCache.h" | ||
#include "llvm/Analysis/BasicAliasAnalysis.h" | ||
|
@@ -32,8 +33,10 @@ | |
#include "llvm/Transforms/Utils/Local.h" | ||
#include "llvm/Transforms/Utils/LoopUtils.h" | ||
#include <numeric> | ||
#include <optional> | ||
#include <queue> | ||
#include <set> | ||
#include <tuple> | ||
|
||
#define DEBUG_TYPE "vector-combine" | ||
#include "llvm/Transforms/Utils/InstructionWorklist.h" | ||
|
@@ -131,6 +134,7 @@ class VectorCombine { | |
bool foldSelectShuffle(Instruction &I, bool FromReduction = false); | ||
bool foldInterleaveIntrinsics(Instruction &I); | ||
bool shrinkType(Instruction &I); | ||
bool shrinkLoadForShuffles(Instruction &I); | ||
|
||
void replaceValue(Value &Old, Value &New) { | ||
LLVM_DEBUG(dbgs() << "VC: Replacing: " << Old << '\n'); | ||
|
@@ -3483,6 +3487,125 @@ bool VectorCombine::foldInterleaveIntrinsics(Instruction &I) { | |
return true; | ||
} | ||
|
||
// Attempt to shrink loads that are only used by shufflevector instructions. | ||
bool VectorCombine::shrinkLoadForShuffles(Instruction &I) { | ||
auto *OldLoad = dyn_cast<LoadInst>(&I); | ||
if (!OldLoad || !OldLoad->isSimple()) | ||
return false; | ||
|
||
auto *OldLoadTy = dyn_cast<FixedVectorType>(OldLoad->getType()); | ||
if (!OldLoadTy) | ||
return false; | ||
|
||
unsigned const OldNumElements = OldLoadTy->getNumElements(); | ||
|
||
// Search all uses of load. If all uses are shufflevector instructions, and | ||
// the second operands are all poison values, find the minimum and maximum | ||
// indices of the vector elements referenced by all shuffle masks. | ||
// Otherwise return `std::nullopt`. | ||
using IndexRange = std::pair<int, int>; | ||
auto GetIndexRangeInShuffles = [&]() -> std::optional<IndexRange> { | ||
IndexRange OutputRange = IndexRange(OldNumElements, -1); | ||
for (auto &Use : I.uses()) { | ||
// Ensure all uses match the required pattern. | ||
User *Shuffle = Use.getUser(); | ||
ArrayRef<int> Mask; | ||
|
||
if (!match(Shuffle, | ||
m_Shuffle(m_Specific(OldLoad), m_Undef(), m_Mask(Mask)))) | ||
return std::nullopt; | ||
|
||
// Ignore shufflevector instructions that have no uses. | ||
if (Shuffle->use_empty()) | ||
continue; | ||
|
||
// Find the min and max indices used by the shufflevector instruction. | ||
for (int Index : Mask) { | ||
if (Index >= 0 && Index < static_cast<int>(OldNumElements)) { | ||
OutputRange.first = std::min(Index, OutputRange.first); | ||
OutputRange.second = std::max(Index, OutputRange.second); | ||
} | ||
} | ||
} | ||
|
||
if (OutputRange.second < OutputRange.first) | ||
return std::nullopt; | ||
|
||
return OutputRange; | ||
}; | ||
|
||
// Get the range of vector elements used by shufflevector instructions. | ||
if (auto Indices = GetIndexRangeInShuffles()) { | ||
unsigned const NewNumElements = Indices->second + 1u; | ||
|
||
// If the range of vector elements is smaller than the full load, attempt | ||
// to create a smaller load. | ||
if (NewNumElements < OldNumElements) { | ||
auto Builder = IRBuilder(&I); | ||
Builder.SetCurrentDebugLocation(I.getDebugLoc()); | ||
|
||
// Calculate costs of old and new ops. | ||
Type *ElemTy = OldLoadTy->getElementType(); | ||
FixedVectorType *NewLoadTy = FixedVectorType::get(ElemTy, NewNumElements); | ||
Value *PtrOp = OldLoad->getPointerOperand(); | ||
|
||
InstructionCost OldCost = TTI.getMemoryOpCost( | ||
Instruction::Load, OldLoad->getType(), OldLoad->getAlign(), | ||
OldLoad->getPointerAddressSpace(), CostKind); | ||
InstructionCost NewCost = | ||
TTI.getMemoryOpCost(Instruction::Load, NewLoadTy, OldLoad->getAlign(), | ||
OldLoad->getPointerAddressSpace(), CostKind); | ||
|
||
using UseEntry = std::pair<ShuffleVectorInst *, std::vector<int>>; | ||
auto NewUses = SmallVector<UseEntry, 4u>(); | ||
auto SizeDiff = OldNumElements - NewNumElements; | ||
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. (style) don't use auto except for casts/iterators |
||
|
||
for (auto &Use : I.uses()) { | ||
auto *Shuffle = cast<ShuffleVectorInst>(Use.getUser()); | ||
auto OldMask = Shuffle->getShuffleMask(); | ||
|
||
// Create entry for new use. | ||
NewUses.push_back({Shuffle, {}}); | ||
auto &NewMask = NewUses.back().second; | ||
for (auto Index : OldMask) | ||
NewMask.push_back(Index >= static_cast<int>(OldNumElements) | ||
? Index - SizeDiff | ||
: Index); | ||
|
||
// Update costs. | ||
OldCost += TTI.getShuffleCost(TTI::SK_PermuteSingleSrc, OldLoadTy, | ||
OldMask, CostKind); | ||
NewCost += TTI.getShuffleCost(TTI::SK_PermuteSingleSrc, NewLoadTy, | ||
NewMask, CostKind); | ||
} | ||
|
||
if (OldCost < NewCost || !NewCost.isValid()) | ||
return false; | ||
|
||
// Create new load of smaller vector. | ||
auto *NewLoad = cast<LoadInst>( | ||
Builder.CreateAlignedLoad(NewLoadTy, PtrOp, OldLoad->getAlign())); | ||
NewLoad->copyMetadata(I); | ||
|
||
// Replace all uses. | ||
for (auto &Use : NewUses) { | ||
auto *Shuffle = Use.first; | ||
auto &NewMask = Use.second; | ||
|
||
Builder.SetInsertPoint(Shuffle); | ||
Builder.SetCurrentDebugLocation(Shuffle->getDebugLoc()); | ||
auto *NewShuffle = Builder.CreateShuffleVector( | ||
NewLoad, PoisonValue::get(NewLoadTy), NewMask); | ||
|
||
replaceValue(*Shuffle, *NewShuffle); | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/// This is the entry point for all transforms. Pass manager differences are | ||
/// handled in the callers of this function. | ||
bool VectorCombine::run() { | ||
|
@@ -3558,6 +3681,9 @@ bool VectorCombine::run() { | |
MadeChange |= foldSelectShuffle(I); | ||
MadeChange |= foldShuffleToIdentity(I); | ||
break; | ||
case Instruction::Load: | ||
MadeChange |= shrinkLoadForShuffles(I); | ||
break; | ||
case Instruction::BitCast: | ||
MadeChange |= foldBitcastShuffle(I); | ||
break; | ||
|
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
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.