[llvm] adb130c - [llvm][TypeSize] Consider TypeSize of '0' to be fixed/scalable-agnostic. (#72994)

via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 27 06:04:58 PST 2023


Author: Sander de Smalen
Date: 2023-11-27T14:04:52Z
New Revision: adb130ccad67a18eb15506822e9cfcb1cb00cb45

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

LOG: [llvm][TypeSize] Consider TypeSize of '0' to be fixed/scalable-agnostic. (#72994)

This patch allows adding any quantity to a zero-initialized TypeSize,
such
that e.g.:

  TypeSize::Scalable(0) + TypeSize::Fixed(4) == TypeSize::Fixed(4)
  TypeSize::Fixed(0) + TypeSize::Scalable(4) == TypeSize::Scalable(4)

This makes it easier to implement add-reductions using TypeSize where
the 'scalable' flag is not yet known before starting the reduction.

(this PR follows on from #72979)

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/TypeSize.h b/llvm/include/llvm/Support/TypeSize.h
index ada98d809fc236f..08ab7c9024f7544 100644
--- a/llvm/include/llvm/Support/TypeSize.h
+++ b/llvm/include/llvm/Support/TypeSize.h
@@ -100,14 +100,22 @@ template <typename LeafTy, typename ValueTy> class FixedOrScalableQuantity {
       : Quantity(Quantity), Scalable(Scalable) {}
 
   friend constexpr LeafTy &operator+=(LeafTy &LHS, const LeafTy &RHS) {
-    assert(LHS.Scalable == RHS.Scalable && "Incompatible types");
+    assert((LHS.Quantity == 0 || RHS.Quantity == 0 ||
+            LHS.Scalable == RHS.Scalable) &&
+           "Incompatible types");
     LHS.Quantity += RHS.Quantity;
+    if (!RHS.isZero())
+      LHS.Scalable = RHS.Scalable;
     return LHS;
   }
 
   friend constexpr LeafTy &operator-=(LeafTy &LHS, const LeafTy &RHS) {
-    assert(LHS.Scalable == RHS.Scalable && "Incompatible types");
+    assert((LHS.Quantity == 0 || RHS.Quantity == 0 ||
+            LHS.Scalable == RHS.Scalable) &&
+           "Incompatible types");
     LHS.Quantity -= RHS.Quantity;
+    if (!RHS.isZero())
+      LHS.Scalable = RHS.Scalable;
     return LHS;
   }
 
@@ -315,6 +323,8 @@ class TypeSize : public details::FixedOrScalableQuantity<TypeSize, uint64_t> {
       : FixedOrScalableQuantity(V) {}
 
 public:
+  constexpr TypeSize() : FixedOrScalableQuantity(0, false) {}
+
   constexpr TypeSize(ScalarTy Quantity, bool Scalable)
       : FixedOrScalableQuantity(Quantity, Scalable) {}
 

diff  --git a/llvm/unittests/Support/TypeSizeTest.cpp b/llvm/unittests/Support/TypeSizeTest.cpp
index 33169a3d8b198c0..503dc5d99b1823d 100644
--- a/llvm/unittests/Support/TypeSizeTest.cpp
+++ b/llvm/unittests/Support/TypeSizeTest.cpp
@@ -81,6 +81,27 @@ static_assert(INT64_C(2) * TSFixed32 == TypeSize::getFixed(64));
 static_assert(UINT64_C(2) * TSFixed32 == TypeSize::getFixed(64));
 static_assert(alignTo(TypeSize::getFixed(7), 8) == TypeSize::getFixed(8));
 
+static_assert(TypeSize() == TypeSize::getFixed(0));
+static_assert(TypeSize::getFixed(0) != TypeSize::getScalable(0));
+static_assert(TypeSize::getFixed(0).isZero());
+static_assert(TypeSize::getScalable(0).isZero());
+static_assert(TypeSize::getFixed(0) ==
+              (TypeSize::getFixed(4) - TypeSize::getFixed(4)));
+static_assert(TypeSize::getScalable(0) ==
+              (TypeSize::getScalable(4) - TypeSize::getScalable(4)));
+static_assert(TypeSize::getFixed(0) + TypeSize::getScalable(8) ==
+              TypeSize::getScalable(8));
+static_assert(TypeSize::getScalable(8) + TypeSize::getFixed(0) ==
+              TypeSize::getScalable(8));
+static_assert(TypeSize::getFixed(8) + TypeSize::getScalable(0) ==
+              TypeSize::getFixed(8));
+static_assert(TypeSize::getScalable(0) + TypeSize::getFixed(8) ==
+              TypeSize::getFixed(8));
+static_assert(TypeSize::getScalable(8) - TypeSize::getFixed(0) ==
+              TypeSize::getScalable(8));
+static_assert(TypeSize::getFixed(8) - TypeSize::getScalable(0) ==
+              TypeSize::getFixed(8));
+
 TEST(TypeSize, FailIncompatibleTypes) {
   EXPECT_DEBUG_DEATH(TypeSize::getFixed(8) + TypeSize::getScalable(8),
                      "Incompatible types");


        


More information about the llvm-commits mailing list