[llvm] 5ee9ef8 - [TypeSize] Extend UnivariateLinearPolyBase with getWithIncrement/Decrement methods

Sander de Smalen via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 6 01:02:08 PST 2020


Author: Sander de Smalen
Date: 2020-11-06T09:01:19Z
New Revision: 5ee9ef85190cff09c4d3718d8683af1900a33348

URL: https://github.com/llvm/llvm-project/commit/5ee9ef85190cff09c4d3718d8683af1900a33348
DIFF: https://github.com/llvm/llvm-project/commit/5ee9ef85190cff09c4d3718d8683af1900a33348.diff

LOG: [TypeSize] Extend UnivariateLinearPolyBase with getWithIncrement/Decrement methods

This patch adds getWithIncrement/getWithDecrement methods to
ElementCount and TypeSize to allow:

  TypeSize::getFixed(8).getWithIncrement(8)     <=> TypeSize::getFixed(16)
  TypeSize::getFixed(16).getWithDecrement(8)    <=> TypeSize::getFixed(8)
  TypeSize::getScalable(8).getWithIncrement(8)  <=> TypeSize::getScalable(16)
  TypeSize::getScalable(16).getWithDecrement(8) <=> TypeSize::getScalable(8)

This patch implements parts of the POC in D90342.

Reviewed By: ctetreau, dmgreen

Differential Revision: https://reviews.llvm.org/D90713

Added: 
    

Modified: 
    llvm/include/llvm/Support/TypeSize.h
    llvm/unittests/Support/LinearPolyBaseTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/TypeSize.h b/llvm/include/llvm/Support/TypeSize.h
index be40be2fc580..d277affdbb23 100644
--- a/llvm/include/llvm/Support/TypeSize.h
+++ b/llvm/include/llvm/Support/TypeSize.h
@@ -169,7 +169,7 @@ class UnivariateLinearPolyBase {
   ScalarTy Value;         // The value at the univeriate dimension.
   unsigned UnivariateDim; // The univeriate dimension.
 
-  UnivariateLinearPolyBase(ScalarTy &Val, unsigned UnivariateDim)
+  UnivariateLinearPolyBase(ScalarTy Val, unsigned UnivariateDim)
       : Value(Val), UnivariateDim(UnivariateDim) {
     assert(UnivariateDim < Dimensions && "Dimension out of range");
   }
@@ -229,6 +229,18 @@ class UnivariateLinearPolyBase {
   ScalarTy getValue(unsigned Dim) const {
     return Dim == UnivariateDim ? Value : 0;
   }
+
+  /// Add \p RHS to the value at the univariate dimension.
+  LeafTy getWithIncrement(ScalarTy RHS) {
+    return static_cast<LeafTy>(
+        UnivariateLinearPolyBase(Value + RHS, UnivariateDim));
+  }
+
+  /// Subtract \p RHS from the value at the univariate dimension.
+  LeafTy getWithDecrement(ScalarTy RHS) {
+    return static_cast<LeafTy>(
+        UnivariateLinearPolyBase(Value - RHS, UnivariateDim));
+  }
 };
 
 
@@ -247,6 +259,11 @@ class UnivariateLinearPolyBase {
 /// fixed-sized or it is scalable-sized, but it cannot be both.
 template <typename LeafTy>
 class LinearPolySize : public UnivariateLinearPolyBase<LeafTy> {
+  // Make the parent class a friend, so that it can access the protected
+  // conversion/copy-constructor for UnivariatePolyBase<LeafTy> ->
+  // LinearPolySize<LeafTy>.
+  friend class UnivariateLinearPolyBase<LeafTy>;
+
 public:
   using ScalarTy = typename UnivariateLinearPolyBase<LeafTy>::ScalarTy;
   enum Dims : unsigned { FixedDim = 0, ScalableDim = 1 };
@@ -255,7 +272,11 @@ class LinearPolySize : public UnivariateLinearPolyBase<LeafTy> {
   LinearPolySize(ScalarTy MinVal, Dims D)
       : UnivariateLinearPolyBase<LeafTy>(MinVal, D) {}
 
+  LinearPolySize(const UnivariateLinearPolyBase<LeafTy> &V)
+      : UnivariateLinearPolyBase<LeafTy>(V) {}
+
 public:
+
   static LeafTy getFixed(ScalarTy MinVal) {
     return static_cast<LeafTy>(LinearPolySize(MinVal, FixedDim));
   }

diff  --git a/llvm/unittests/Support/LinearPolyBaseTest.cpp b/llvm/unittests/Support/LinearPolyBaseTest.cpp
index 407e9b6ae80f..6942ea2a14ea 100644
--- a/llvm/unittests/Support/LinearPolyBaseTest.cpp
+++ b/llvm/unittests/Support/LinearPolyBaseTest.cpp
@@ -140,6 +140,11 @@ TEST(UnivariateLinearPolyBase, Univariate3D_Add) {
   Univariate3D X(42, 0);
   X += Univariate3D(42, 0);
   EXPECT_EQ(X, Univariate3D(84, 0));
+
+  // Test 'getWithIncrement' method
+  EXPECT_EQ(Univariate3D(42, 0).getWithIncrement(1), Univariate3D(43, 0));
+  EXPECT_EQ(Univariate3D(42, 1).getWithIncrement(2), Univariate3D(44, 1));
+  EXPECT_EQ(Univariate3D(42, 2).getWithIncrement(3), Univariate3D(45, 2));
 }
 
 TEST(UnivariateLinearPolyBase, Univariate3D_Sub) {
@@ -153,6 +158,11 @@ TEST(UnivariateLinearPolyBase, Univariate3D_Sub) {
   Univariate3D X(84, 0);
   X -= Univariate3D(42, 0);
   EXPECT_EQ(X, Univariate3D(42, 0));
+
+  // Test 'getWithDecrement' method
+  EXPECT_EQ(Univariate3D(43, 0).getWithDecrement(1), Univariate3D(42, 0));
+  EXPECT_EQ(Univariate3D(44, 1).getWithDecrement(2), Univariate3D(42, 1));
+  EXPECT_EQ(Univariate3D(45, 2).getWithDecrement(3), Univariate3D(42, 2));
 }
 
 TEST(UnivariateLinearPolyBase, Univariate3D_Scale) {


        


More information about the llvm-commits mailing list