[go: nahoru, domu]

Skip to content

Commit

Permalink
[VectorUtils] Add llvm::scaleShuffleMaskElts wrapper for narrowShuffl…
Browse files Browse the repository at this point in the history
…eMaskElts/widenShuffleMaskElts, NFC. (#96646)

Using the target number of vector elements, scaleShuffleMaskElts will try to use narrowShuffleMaskElts/widenShuffleMaskElts to scale the shuffle mask accordingly.

Working on #58895 I didn't want to create yet another case where we have to handle both re-scaling cases.
  • Loading branch information
RKSimon committed Jun 26, 2024
1 parent 8681bb8 commit 5b4000d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
7 changes: 7 additions & 0 deletions llvm/include/llvm/Analysis/VectorUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ void narrowShuffleMaskElts(int Scale, ArrayRef<int> Mask,
bool widenShuffleMaskElts(int Scale, ArrayRef<int> Mask,
SmallVectorImpl<int> &ScaledMask);

/// Attempt to narrow/widen the \p Mask shuffle mask to the \p NumDstElts target
/// width. Internally this will call narrowShuffleMaskElts/widenShuffleMaskElts.
/// This will assert unless NumDstElts is a multiple of Mask.size (or vice-versa).
/// Returns false on failure, and ScaledMask will be in an undefined state.
bool scaleShuffleMaskElts(unsigned NumDstElts, ArrayRef<int> Mask,
SmallVectorImpl<int> &ScaledMask);

/// Repetitively apply `widenShuffleMaskElts()` for as long as it succeeds,
/// to get the shuffle mask with widest possible elements.
void getShuffleMaskWithWidestElts(ArrayRef<int> Mask,
Expand Down
25 changes: 25 additions & 0 deletions llvm/lib/Analysis/VectorUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,31 @@ bool llvm::widenShuffleMaskElts(int Scale, ArrayRef<int> Mask,
return true;
}

bool llvm::scaleShuffleMaskElts(unsigned NumDstElts, ArrayRef<int> Mask,
SmallVectorImpl<int> &ScaledMask) {
unsigned NumSrcElts = Mask.size();
assert(NumSrcElts > 0 && NumDstElts > 0 && "Unexpected scaling factor");

// Fast-path: if no scaling, then it is just a copy.
if (NumSrcElts == NumDstElts) {
ScaledMask.assign(Mask.begin(), Mask.end());
return true;
}

// Ensure we can find a whole scale factor.
assert(((NumSrcElts % NumDstElts) == 0 || (NumDstElts % NumSrcElts) == 0) &&
"Unexpected scaling factor");

if (NumSrcElts > NumDstElts) {
int Scale = NumSrcElts / NumDstElts;
return widenShuffleMaskElts(Scale, Mask, ScaledMask);
}

int Scale = NumDstElts / NumSrcElts;
narrowShuffleMaskElts(Scale, Mask, ScaledMask);
return true;
}

void llvm::getShuffleMaskWithWidestElts(ArrayRef<int> Mask,
SmallVectorImpl<int> &ScaledMask) {
std::array<SmallVector<int, 16>, 2> TmpMasks;
Expand Down
10 changes: 1 addition & 9 deletions llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2837,15 +2837,7 @@ Instruction *InstCombinerImpl::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
auto *XType = cast<FixedVectorType>(X->getType());
unsigned XNumElts = XType->getNumElements();
SmallVector<int, 16> ScaledMask;
if (XNumElts >= VWidth) {
assert(XNumElts % VWidth == 0 && "Unexpected vector bitcast");
narrowShuffleMaskElts(XNumElts / VWidth, Mask, ScaledMask);
} else {
assert(VWidth % XNumElts == 0 && "Unexpected vector bitcast");
if (!widenShuffleMaskElts(VWidth / XNumElts, Mask, ScaledMask))
ScaledMask.clear();
}
if (!ScaledMask.empty()) {
if (scaleShuffleMaskElts(XNumElts, Mask, ScaledMask)) {
// If the shuffled source vector simplifies, cast that value to this
// shuffle's type.
if (auto *V = simplifyShuffleVectorInst(X, UndefValue::get(XType),
Expand Down

0 comments on commit 5b4000d

Please sign in to comment.