Skip to content

[Instructions] cache computed shufflevector properties #115536

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
64 changes: 44 additions & 20 deletions llvm/include/llvm/IR/Instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -1886,6 +1886,20 @@ DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
// ShuffleVectorInst Class
//===----------------------------------------------------------------------===//

struct ShuffleMaskAttrs {
bool SingleSource : 1;
bool Identity : 1;
bool Reverse : 1;
bool ZeroEltSplat : 1;
bool Select : 1;
bool Transpose : 1;
bool Splice : 1;
int SpliceIndex;
};

static_assert(sizeof(ShuffleMaskAttrs) <= sizeof(uint64_t),
"ShuffleMaskAttrs is too large!");

constexpr int PoisonMaskElem = -1;

/// This instruction constructs a fixed permutation of two
Expand All @@ -1903,6 +1917,27 @@ class ShuffleVectorInst : public Instruction {

SmallVector<int, 4> ShuffleMask;
Constant *ShuffleMaskForBitcode;
ShuffleMaskAttrs ShuffleAttrs;

// (Re)compute the shuffle mask attributes.
void computeShuffleAttrs() {
ShuffleAttrs = {}; // reset
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed, as you're reassigning all members anyway.

ShuffleAttrs.SingleSource =
!changesLength() && isSingleSourceMask(ShuffleMask, ShuffleMask.size());
ShuffleAttrs.Identity =
!changesLength() && isIdentityMask(ShuffleMask, ShuffleMask.size());
ShuffleAttrs.Select =
!changesLength() && isSelectMask(ShuffleMask, ShuffleMask.size());
ShuffleAttrs.Reverse =
!changesLength() && isReverseMask(ShuffleMask, ShuffleMask.size());
ShuffleAttrs.ZeroEltSplat =
!changesLength() && isZeroEltSplatMask(ShuffleMask, ShuffleMask.size());
ShuffleAttrs.Transpose =
!changesLength() && isTransposeMask(ShuffleMask, ShuffleMask.size());
ShuffleAttrs.Splice =
!changesLength() &&
isSpliceMask(ShuffleMask, ShuffleMask.size(), ShuffleAttrs.SpliceIndex);
}

protected:
// Note: Instruction needs to be a friend here to call cloneImpl.
Expand Down Expand Up @@ -2013,10 +2048,7 @@ class ShuffleVectorInst : public Instruction {
/// vector without changing the length of that vector.
/// Example: shufflevector <4 x n> A, <4 x n> B, <3,0,undef,3>
/// TODO: Optionally allow length-changing shuffles.
bool isSingleSource() const {
return !changesLength() &&
isSingleSourceMask(ShuffleMask, ShuffleMask.size());
}
bool isSingleSource() const { return ShuffleAttrs.SingleSource; }

/// Return true if this shuffle mask chooses elements from exactly one source
/// vector without lane crossings. A shuffle using this mask is not
Expand Down Expand Up @@ -2047,7 +2079,7 @@ class ShuffleVectorInst : public Instruction {
if (isa<ScalableVectorType>(getType()))
return false;

return !changesLength() && isIdentityMask(ShuffleMask, ShuffleMask.size());
return ShuffleAttrs.Identity;
}

/// Return true if this shuffle lengthens exactly one source vector with
Expand Down Expand Up @@ -2087,9 +2119,7 @@ class ShuffleVectorInst : public Instruction {
/// This returns false if the mask does not choose from both input vectors.
/// In that case, the shuffle is better classified as an identity shuffle.
/// TODO: Optionally allow length-changing shuffles.
bool isSelect() const {
return !changesLength() && isSelectMask(ShuffleMask, ShuffleMask.size());
}
bool isSelect() const { return ShuffleAttrs.Select; }

/// Return true if this shuffle mask swaps the order of elements from exactly
/// one source vector.
Expand All @@ -2108,9 +2138,7 @@ class ShuffleVectorInst : public Instruction {
/// one source vector.
/// Example: shufflevector <4 x n> A, <4 x n> B, <3,undef,1,undef>
/// TODO: Optionally allow length-changing shuffles.
bool isReverse() const {
return !changesLength() && isReverseMask(ShuffleMask, ShuffleMask.size());
}
bool isReverse() const { return ShuffleAttrs.Reverse; }

/// Return true if this shuffle mask chooses all elements with the same value
/// as the first element of exactly one source vector.
Expand All @@ -2131,10 +2159,7 @@ class ShuffleVectorInst : public Instruction {
/// Example: shufflevector <4 x n> A, <4 x n> B, <undef,0,undef,0>
/// TODO: Optionally allow length-changing shuffles.
/// TODO: Optionally allow splats from other elements.
bool isZeroEltSplat() const {
return !changesLength() &&
isZeroEltSplatMask(ShuffleMask, ShuffleMask.size());
}
bool isZeroEltSplat() const { return ShuffleAttrs.ZeroEltSplat; }

/// Return true if this shuffle mask is a transpose mask.
/// Transpose vector masks transpose a 2xn matrix. They read corresponding
Expand Down Expand Up @@ -2181,9 +2206,7 @@ class ShuffleVectorInst : public Instruction {
/// merge or interleave. See the description for isTransposeMask() for the
/// exact specification.
/// Example: shufflevector <4 x n> A, <4 x n> B, <0,4,2,6>
bool isTranspose() const {
return !changesLength() && isTransposeMask(ShuffleMask, ShuffleMask.size());
}
bool isTranspose() const { return ShuffleAttrs.Transpose; }

/// Return true if this shuffle mask is a splice mask, concatenating the two
/// inputs together and then extracts an original width vector starting from
Expand All @@ -2204,8 +2227,9 @@ class ShuffleVectorInst : public Instruction {
/// then extracts an original width vector starting from the splice index.
/// Example: shufflevector <4 x n> A, <4 x n> B, <1,2,3,4>
bool isSplice(int &Index) const {
return !changesLength() &&
isSpliceMask(ShuffleMask, ShuffleMask.size(), Index);
if (ShuffleAttrs.Splice)
Index = ShuffleAttrs.SpliceIndex;
return ShuffleAttrs.Splice;
}

/// Return true if this shuffle mask is an extract subvector mask.
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/IR/Instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,7 @@ void ShuffleVectorInst::getShuffleMask(const Constant *Mask,
void ShuffleVectorInst::setShuffleMask(ArrayRef<int> Mask) {
ShuffleMask.assign(Mask.begin(), Mask.end());
ShuffleMaskForBitcode = convertShuffleMaskForBitcode(Mask, getType());
computeShuffleAttrs();
}

Constant *ShuffleVectorInst::convertShuffleMaskForBitcode(ArrayRef<int> Mask,
Expand Down
Loading