[llvm] 12d79b1 - GlobalISel: Add LLT helper to multiply vector sizes

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 19 18:04:43 PDT 2022


Author: Matt Arsenault
Date: 2022-04-19T21:04:32-04:00
New Revision: 12d79b1514b8036a6079495a45091c9bfb8db1a6

URL: https://github.com/llvm/llvm-project/commit/12d79b1514b8036a6079495a45091c9bfb8db1a6
DIFF: https://github.com/llvm/llvm-project/commit/12d79b1514b8036a6079495a45091c9bfb8db1a6.diff

LOG: GlobalISel: Add LLT helper to multiply vector sizes

Added: 
    

Modified: 
    llvm/include/llvm/Support/LowLevelTypeImpl.h
    llvm/include/llvm/Support/TypeSize.h
    llvm/unittests/CodeGen/LowLevelTypeTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/LowLevelTypeImpl.h b/llvm/include/llvm/Support/LowLevelTypeImpl.h
index dd286f5228fe5..186a7e5930ecd 100644
--- a/llvm/include/llvm/Support/LowLevelTypeImpl.h
+++ b/llvm/include/llvm/Support/LowLevelTypeImpl.h
@@ -207,6 +207,18 @@ class LLT {
     return scalar(getScalarSizeInBits() / Factor);
   }
 
+  /// Produce a vector type that is \p Factor times bigger, preserving the
+  /// element type. For a scalar or pointer, this will produce a new vector with
+  /// \p Factor elements.
+  LLT multiplyElements(int Factor) const {
+    if (isVector()) {
+      return scalarOrVector(getElementCount().multiplyCoefficientBy(Factor),
+                            getElementType());
+    }
+
+    return fixed_vector(Factor, *this);
+  }
+
   bool isByteSized() const { return getSizeInBits().isKnownMultipleOf(8); }
 
   unsigned getScalarSizeInBits() const {

diff  --git a/llvm/include/llvm/Support/TypeSize.h b/llvm/include/llvm/Support/TypeSize.h
index 6bddb602e8c19..96f33c1e3d8cc 100644
--- a/llvm/include/llvm/Support/TypeSize.h
+++ b/llvm/include/llvm/Support/TypeSize.h
@@ -362,6 +362,11 @@ class LinearPolySize : public UnivariateLinearPolyBase<LeafTy> {
         LinearPolySize::get(getKnownMinValue() / RHS, isScalable()));
   }
 
+  LeafTy multiplyCoefficientBy(ScalarTy RHS) const {
+    return static_cast<LeafTy>(
+        LinearPolySize::get(getKnownMinValue() * RHS, isScalable()));
+  }
+
   LeafTy coefficientNextPowerOf2() const {
     return static_cast<LeafTy>(LinearPolySize::get(
         static_cast<ScalarTy>(llvm::NextPowerOf2(getKnownMinValue())),

diff  --git a/llvm/unittests/CodeGen/LowLevelTypeTest.cpp b/llvm/unittests/CodeGen/LowLevelTypeTest.cpp
index bf629c5d5e35f..ef0a43971d4b7 100644
--- a/llvm/unittests/CodeGen/LowLevelTypeTest.cpp
+++ b/llvm/unittests/CodeGen/LowLevelTypeTest.cpp
@@ -319,4 +319,49 @@ TEST(LowLevelTypeTest, Divide) {
             LLT::fixed_vector(4, LLT::pointer(1, 64)).divide(2));
 }
 
+TEST(LowLevelTypeTest, MultiplyElements) {
+  // Basic scalar->vector cases
+  EXPECT_EQ(LLT::fixed_vector(2, 16), LLT::scalar(16).multiplyElements(2));
+  EXPECT_EQ(LLT::fixed_vector(3, 16), LLT::scalar(16).multiplyElements(3));
+  EXPECT_EQ(LLT::fixed_vector(4, 32), LLT::scalar(32).multiplyElements(4));
+  EXPECT_EQ(LLT::fixed_vector(4, 7), LLT::scalar(7).multiplyElements(4));
+
+  // Basic vector to vector cases
+  EXPECT_EQ(LLT::fixed_vector(4, 32),
+            LLT::fixed_vector(2, 32).multiplyElements(2));
+  EXPECT_EQ(LLT::fixed_vector(9, 32),
+            LLT::fixed_vector(3, 32).multiplyElements(3));
+
+  // Pointer to vector of pointers
+  EXPECT_EQ(LLT::fixed_vector(2, LLT::pointer(0, 32)),
+            LLT::pointer(0, 32).multiplyElements(2));
+  EXPECT_EQ(LLT::fixed_vector(3, LLT::pointer(1, 32)),
+            LLT::pointer(1, 32).multiplyElements(3));
+  EXPECT_EQ(LLT::fixed_vector(4, LLT::pointer(1, 64)),
+            LLT::pointer(1, 64).multiplyElements(4));
+
+  // Vector of pointers to vector of pointers
+  EXPECT_EQ(LLT::fixed_vector(8, LLT::pointer(1, 64)),
+            LLT::fixed_vector(2, LLT::pointer(1, 64)).multiplyElements(4));
+  EXPECT_EQ(LLT::fixed_vector(9, LLT::pointer(1, 32)),
+            LLT::fixed_vector(3, LLT::pointer(1, 32)).multiplyElements(3));
+
+  // Scalable vectors
+  EXPECT_EQ(LLT::scalable_vector(4, 16),
+            LLT::scalable_vector(2, 16).multiplyElements(2));
+  EXPECT_EQ(LLT::scalable_vector(6, 16),
+            LLT::scalable_vector(2, 16).multiplyElements(3));
+  EXPECT_EQ(LLT::scalable_vector(9, 16),
+            LLT::scalable_vector(3, 16).multiplyElements(3));
+  EXPECT_EQ(LLT::scalable_vector(4, 32),
+            LLT::scalable_vector(2, 32).multiplyElements(2));
+  EXPECT_EQ(LLT::scalable_vector(256, 32),
+            LLT::scalable_vector(8, 32).multiplyElements(32));
+
+  // Scalable vectors of pointers
+  EXPECT_EQ(LLT::scalable_vector(4, LLT::pointer(0, 32)),
+            LLT::scalable_vector(2, LLT::pointer(0, 32)).multiplyElements(2));
+  EXPECT_EQ(LLT::scalable_vector(32, LLT::pointer(1, 64)),
+            LLT::scalable_vector(8, LLT::pointer(1, 64)).multiplyElements(4));
+}
 }


        


More information about the llvm-commits mailing list