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

Merged
merged 14 commits into from
Aug 12, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
99 changes: 99 additions & 0 deletions llvm/lib/Transforms/Vectorize/VectorCombine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class VectorCombine {
bool foldInterleaveIntrinsics(Instruction &I);
bool shrinkType(Instruction &I);
bool shrinkLoadForShuffles(Instruction &I);
bool shrinkPhiOfShuffles(Instruction &I);

void replaceValue(Value &Old, Value &New) {
LLVM_DEBUG(dbgs() << "VC: Replacing: " << Old << '\n');
Expand Down Expand Up @@ -3994,6 +3995,101 @@ bool VectorCombine::shrinkLoadForShuffles(Instruction &I) {
return false;
}

// 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 [M0, M1] : zip(Mask0, Mask1)) {
if (M0 >= 0 && M1 >= 0)
NewMask.push_back(M0 - M1);
else if (M0 == -1 && M1 == -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, ResultVT, InputVT, Mask0, CostKind),
TTI.getShuffleCost(Kind, ResultVT, InputVT, Mask1, CostKind));
auto NewCost = TTI.getShuffleCost(Kind, InputVT, InputVT, NewMask, CostKind) +
TTI.getShuffleCost(Kind, ResultVT, InputVT, Mask1, CostKind);

LLVM_DEBUG(dbgs() << "Found a phi of mergeable shuffles: " << I
<< "\n OldCost: " << OldCost << " vs NewCost: " << NewCost
<< "\n");

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);
Worklist.push(cast<Instruction>(NewShuf0));

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 @@ -4081,6 +4177,9 @@ bool VectorCombine::run() {
case Instruction::Xor:
MadeChange |= foldBitOpOfCastops(I);
break;
case Instruction::PHI:
MadeChange |= shrinkPhiOfShuffles(I);
break;
default:
MadeChange |= shrinkType(I);
break;
Expand Down
Loading
Loading