[llvm] e202236 - [IR] Add IRBuilderBase::CreateVectorSplat(ElementCount EC) variant
Simon Pilgrim via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 2 08:55:58 PDT 2020
Author: Simon Pilgrim
Date: 2020-08-02T16:55:38+01:00
New Revision: e20223672100ed4826827412b80a605c759538da
URL: https://github.com/llvm/llvm-project/commit/e20223672100ed4826827412b80a605c759538da
DIFF: https://github.com/llvm/llvm-project/commit/e20223672100ed4826827412b80a605c759538da.diff
LOG: [IR] Add IRBuilderBase::CreateVectorSplat(ElementCount EC) variant
As discussed on D81500, this adds a more general ElementCount variant of the build helper and converts the (non-scalable) unsigned NumElts variant to use it internally.
Added:
Modified:
llvm/include/llvm/IR/IRBuilder.h
llvm/lib/IR/IRBuilder.cpp
llvm/unittests/Analysis/VectorUtilsTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h
index ffec4ff64ca6..d467789132ac 100644
--- a/llvm/include/llvm/IR/IRBuilder.h
+++ b/llvm/include/llvm/IR/IRBuilder.h
@@ -2484,6 +2484,10 @@ class IRBuilderBase {
/// NumElts elements.
Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "");
+ /// Return a vector value that contains \arg V broadcasted to \p
+ /// EC elements.
+ Value *CreateVectorSplat(ElementCount EC, Value *V, const Twine &Name = "");
+
/// Return a value that has been extracted from a larger integer type.
Value *CreateExtractInteger(const DataLayout &DL, Value *From,
IntegerType *ExtractedTy, uint64_t Offset,
diff --git a/llvm/lib/IR/IRBuilder.cpp b/llvm/lib/IR/IRBuilder.cpp
index 1fffce015f70..f223f2c6a2b3 100644
--- a/llvm/lib/IR/IRBuilder.cpp
+++ b/llvm/lib/IR/IRBuilder.cpp
@@ -996,17 +996,22 @@ Value *IRBuilderBase::CreateStripInvariantGroup(Value *Ptr) {
Value *IRBuilderBase::CreateVectorSplat(unsigned NumElts, Value *V,
const Twine &Name) {
- assert(NumElts > 0 && "Cannot splat to an empty vector!");
+ ElementCount EC(NumElts, false);
+ return CreateVectorSplat(EC, V, Name);
+}
+
+Value *IRBuilderBase::CreateVectorSplat(ElementCount EC, Value *V,
+ const Twine &Name) {
+ assert(EC.Min > 0 && "Cannot splat to an empty vector!");
// First insert it into an undef vector so we can shuffle it.
Type *I32Ty = getInt32Ty();
- Value *Undef = UndefValue::get(FixedVectorType::get(V->getType(), NumElts));
+ Value *Undef = UndefValue::get(VectorType::get(V->getType(), EC));
V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
Name + ".splatinsert");
// Shuffle the value across the desired number of elements.
- Value *Zeros =
- ConstantAggregateZero::get(FixedVectorType::get(I32Ty, NumElts));
+ Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, EC));
return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
}
diff --git a/llvm/unittests/Analysis/VectorUtilsTest.cpp b/llvm/unittests/Analysis/VectorUtilsTest.cpp
index 69e5285e8731..731ebdfe16f5 100644
--- a/llvm/unittests/Analysis/VectorUtilsTest.cpp
+++ b/llvm/unittests/Analysis/VectorUtilsTest.cpp
@@ -93,6 +93,9 @@ TEST_F(BasicTest, isSplat) {
Value *SplatC = IRB.CreateVectorSplat(5, ScalarC);
EXPECT_TRUE(isSplatValue(SplatC));
+ Value *SplatC_SVE = IRB.CreateVectorSplat(ElementCount(5, true), ScalarC);
+ EXPECT_TRUE(isSplatValue(SplatC_SVE));
+
// FIXME: Constant splat analysis does not allow undef elements.
Constant *SplatWithUndefC = ConstantVector::get({ScalarC, UndefScalar});
EXPECT_FALSE(isSplatValue(SplatWithUndefC));
More information about the llvm-commits
mailing list