[PATCH] D123554: GlobalISel: Add LLT helper to multiply vector sizes

Matt Arsenault via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 11 16:23:11 PDT 2022


arsenm created this revision.
arsenm added reviewers: paquette, dsanders, aemerson, bogner, aditya_nandakumar.
Herald added subscribers: dexonsmith, rovka.
Herald added a project: All.
arsenm requested review of this revision.
Herald added a subscriber: wdng.
Herald added a project: LLVM.

https://reviews.llvm.org/D123554

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


Index: llvm/unittests/CodeGen/LowLevelTypeTest.cpp
===================================================================
--- llvm/unittests/CodeGen/LowLevelTypeTest.cpp
+++ llvm/unittests/CodeGen/LowLevelTypeTest.cpp
@@ -319,4 +319,49 @@
             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));
+}
 }
Index: llvm/include/llvm/Support/TypeSize.h
===================================================================
--- llvm/include/llvm/Support/TypeSize.h
+++ llvm/include/llvm/Support/TypeSize.h
@@ -362,6 +362,11 @@
         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())),
Index: llvm/include/llvm/Support/LowLevelTypeImpl.h
===================================================================
--- llvm/include/llvm/Support/LowLevelTypeImpl.h
+++ llvm/include/llvm/Support/LowLevelTypeImpl.h
@@ -207,6 +207,18 @@
     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 {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D123554.422069.patch
Type: text/x-patch
Size: 3968 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220411/89c3e7bb/attachment.bin>


More information about the llvm-commits mailing list