Skip to content

[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
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions llvm/lib/Transforms/Vectorize/VectorCombine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -3519,6 +3520,97 @@ 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;

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) {
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() ||
!std::equal(NewMask.begin() + 1u, NewMask.end(), NewMask.begin()))
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));
auto NewCost = TTI.getShuffleCost(Kind, InputVT, NewMask, CostKind) +
TTI.getShuffleCost(Kind, InputVT, Mask1, CostKind);

if (NewCost > OldCost)
return false;

// Create new shuffles and narrowed phi.
auto Builder = IRBuilder(&I);
Builder.SetInsertPoint(Shuf);
Builder.SetCurrentDebugLocation(Shuf->getDebugLoc());
auto *PoisonVal = PoisonValue::get(InputVT);
auto *NewShuf0 = Builder.CreateShuffleVector(Op, PoisonVal, NewMask);

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() {
Expand Down Expand Up @@ -3597,6 +3689,9 @@ bool VectorCombine::run() {
case Instruction::BitCast:
MadeChange |= foldBitcastShuffle(I);
break;
case Instruction::PHI:
MadeChange |= shrinkPhiOfShuffles(I);
break;
default:
MadeChange |= shrinkType(I);
break;
Expand Down
Loading
Loading