Skip to content

Commit a5cd278

Browse files
committed
[IR] Improve member ShuffleVectorInst::isReplicationMask()
When we have an actual shuffle, we can impose the additional restriction that the mask replicates the elements of the first operand, so we know the replication factor as a ratio of output and op0 vector sizes.
1 parent 6d48e25 commit a5cd278

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

llvm/include/llvm/IR/Instructions.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2373,14 +2373,7 @@ class ShuffleVectorInst : public Instruction {
23732373
}
23742374

23752375
/// Return true if this shuffle mask is a replication mask.
2376-
bool isReplicationMask(int &ReplicationFactor, int &VF) const {
2377-
// Not possible to express a shuffle mask for a scalable vector for this
2378-
// case.
2379-
if (isa<ScalableVectorType>(getType()))
2380-
return false;
2381-
2382-
return isReplicationMask(ShuffleMask, ReplicationFactor, VF);
2383-
}
2376+
bool isReplicationMask(int &ReplicationFactor, int &VF) const;
23842377

23852378
/// Change values in a shuffle permute mask assuming the two vector operands
23862379
/// of length InVecNumElts have swapped position.

llvm/lib/IR/Instructions.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2502,6 +2502,21 @@ bool ShuffleVectorInst::isReplicationMask(ArrayRef<int> Mask,
25022502
return false;
25032503
}
25042504

2505+
bool ShuffleVectorInst::isReplicationMask(int &ReplicationFactor,
2506+
int &VF) const {
2507+
// Not possible to express a shuffle mask for a scalable vector for this
2508+
// case.
2509+
if (isa<ScalableVectorType>(getType()))
2510+
return false;
2511+
2512+
VF = cast<FixedVectorType>(Op<0>()->getType())->getNumElements();
2513+
if (ShuffleMask.size() % VF != 0)
2514+
return false;
2515+
ReplicationFactor = ShuffleMask.size() / VF;
2516+
2517+
return isReplicationMaskWithParams(ShuffleMask, ReplicationFactor, VF);
2518+
}
2519+
25052520
//===----------------------------------------------------------------------===//
25062521
// InsertValueInst Class
25072522
//===----------------------------------------------------------------------===//

llvm/unittests/IR/InstructionsTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,16 @@ TEST(InstructionsTest, ShuffleMaskIsReplicationMask) {
11261126
ReplicatedMask, GuessedReplicationFactor, GuessedVF));
11271127
EXPECT_EQ(GuessedReplicationFactor, ReplicationFactor);
11281128
EXPECT_EQ(GuessedVF, VF);
1129+
1130+
for (int OpVF : seq_inclusive(VF, 2 * VF + 1)) {
1131+
LLVMContext Ctx;
1132+
Type *OpVFTy = FixedVectorType::get(IntegerType::getInt1Ty(Ctx), OpVF);
1133+
Value *Op = ConstantVector::getNullValue(OpVFTy);
1134+
ShuffleVectorInst *SVI = new ShuffleVectorInst(Op, Op, ReplicatedMask);
1135+
EXPECT_EQ(SVI->isReplicationMask(GuessedReplicationFactor, GuessedVF),
1136+
OpVF == VF);
1137+
delete SVI;
1138+
}
11291139
}
11301140
}
11311141
}

0 commit comments

Comments
 (0)