[PATCH] D90713: [TypeSize] Extend UnivariateLinearPolyBase with add/sub methods

Sander de Smalen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 3 13:38:13 PST 2020


sdesmalen created this revision.
sdesmalen added reviewers: ctetreau, vkmr.
Herald added a subscriber: dexonsmith.
Herald added a project: LLVM.
sdesmalen requested review of this revision.

This patch adds add/sub methods to ElementCount and TypeSize to allow:

  TypeSize::getFixed(8).add(8)     <=> TypeSize::getFixed(16)
  TypeSize::getFixed(16).sub(8)    <=> TypeSize::getFixed(8)
  TypeSize::getScalable(8).add(8)  <=> TypeSize::getScalable(16)
  TypeSize::getScalable(16).sub(8) <=> TypeSize::getScalable(8)

This patch implements parts of the POC in D90342 <https://reviews.llvm.org/D90342>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D90713

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


Index: llvm/unittests/Support/LinearPolyBaseTest.cpp
===================================================================
--- llvm/unittests/Support/LinearPolyBaseTest.cpp
+++ llvm/unittests/Support/LinearPolyBaseTest.cpp
@@ -135,6 +135,11 @@
   Univariate3D X(42, 0);
   X += Univariate3D(42, 0);
   EXPECT_EQ(X, Univariate3D(84, 0));
+
+  // Test 'add' method
+  EXPECT_EQ(Univariate3D(42, 0).add(1), Univariate3D(43, 0));
+  EXPECT_EQ(Univariate3D(42, 1).add(2), Univariate3D(44, 1));
+  EXPECT_EQ(Univariate3D(42, 2).add(3), Univariate3D(45, 2));
 }
 
 TEST(UnivariateLinearPolyBase, Univariate3D_Sub) {
@@ -148,6 +153,11 @@
   Univariate3D X(84, 0);
   X -= Univariate3D(42, 0);
   EXPECT_EQ(X, Univariate3D(42, 0));
+
+  // Test 'sub' method
+  EXPECT_EQ(Univariate3D(43, 0).sub(1), Univariate3D(42, 0));
+  EXPECT_EQ(Univariate3D(44, 1).sub(2), Univariate3D(42, 1));
+  EXPECT_EQ(Univariate3D(45, 2).sub(3), Univariate3D(42, 2));
 }
 
 TEST(UnivariateLinearPolyBase, Univariate3D_Scale) {
Index: llvm/include/llvm/Support/TypeSize.h
===================================================================
--- llvm/include/llvm/Support/TypeSize.h
+++ llvm/include/llvm/Support/TypeSize.h
@@ -177,7 +177,7 @@
   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) {}
 
   friend LeafTy &operator+=(LeafTy &LHS, const LeafTy &RHS) {
@@ -235,6 +235,23 @@
   ScalarTy getValue(unsigned Dim) const {
     return Dim == UnivariateDim ? Value : 0;
   }
+
+  // Aliases for operator+ and operator- for completion, since we also
+  // support add/sub with a scalar type as well.
+  LeafTy add(const LeafTy &RHS) { return *this + RHS; }
+  LeafTy sub(const LeafTy &RHS) { return *this - RHS; }
+
+  /// Add \p RHS to the value at the univariate dimension.
+  LeafTy add(ScalarTy RHS) {
+    return static_cast<LeafTy>(
+        UnivariateLinearPolyBase(Value + RHS, UnivariateDim));
+  }
+
+  /// Subtract \p RHS from the value at the univariate dimension.
+  LeafTy sub(ScalarTy RHS) {
+    return static_cast<LeafTy>(
+        UnivariateLinearPolyBase(Value - RHS, UnivariateDim));
+  }
 };
 
 
@@ -253,6 +270,11 @@
 /// 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 };
@@ -261,7 +283,11 @@
   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));
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D90713.302677.patch
Type: text/x-patch
Size: 3249 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201103/975f0885/attachment.bin>


More information about the llvm-commits mailing list