Skip to content

Commit adb130c

Browse files
[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)
1 parent 5bd643e commit adb130c

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

llvm/include/llvm/Support/TypeSize.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,22 @@ template <typename LeafTy, typename ValueTy> class FixedOrScalableQuantity {
100100
: Quantity(Quantity), Scalable(Scalable) {}
101101

102102
friend constexpr LeafTy &operator+=(LeafTy &LHS, const LeafTy &RHS) {
103-
assert(LHS.Scalable == RHS.Scalable && "Incompatible types");
103+
assert((LHS.Quantity == 0 || RHS.Quantity == 0 ||
104+
LHS.Scalable == RHS.Scalable) &&
105+
"Incompatible types");
104106
LHS.Quantity += RHS.Quantity;
107+
if (!RHS.isZero())
108+
LHS.Scalable = RHS.Scalable;
105109
return LHS;
106110
}
107111

108112
friend constexpr LeafTy &operator-=(LeafTy &LHS, const LeafTy &RHS) {
109-
assert(LHS.Scalable == RHS.Scalable && "Incompatible types");
113+
assert((LHS.Quantity == 0 || RHS.Quantity == 0 ||
114+
LHS.Scalable == RHS.Scalable) &&
115+
"Incompatible types");
110116
LHS.Quantity -= RHS.Quantity;
117+
if (!RHS.isZero())
118+
LHS.Scalable = RHS.Scalable;
111119
return LHS;
112120
}
113121

@@ -315,6 +323,8 @@ class TypeSize : public details::FixedOrScalableQuantity<TypeSize, uint64_t> {
315323
: FixedOrScalableQuantity(V) {}
316324

317325
public:
326+
constexpr TypeSize() : FixedOrScalableQuantity(0, false) {}
327+
318328
constexpr TypeSize(ScalarTy Quantity, bool Scalable)
319329
: FixedOrScalableQuantity(Quantity, Scalable) {}
320330

llvm/unittests/Support/TypeSizeTest.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,27 @@ static_assert(INT64_C(2) * TSFixed32 == TypeSize::getFixed(64));
8181
static_assert(UINT64_C(2) * TSFixed32 == TypeSize::getFixed(64));
8282
static_assert(alignTo(TypeSize::getFixed(7), 8) == TypeSize::getFixed(8));
8383

84+
static_assert(TypeSize() == TypeSize::getFixed(0));
85+
static_assert(TypeSize::getFixed(0) != TypeSize::getScalable(0));
86+
static_assert(TypeSize::getFixed(0).isZero());
87+
static_assert(TypeSize::getScalable(0).isZero());
88+
static_assert(TypeSize::getFixed(0) ==
89+
(TypeSize::getFixed(4) - TypeSize::getFixed(4)));
90+
static_assert(TypeSize::getScalable(0) ==
91+
(TypeSize::getScalable(4) - TypeSize::getScalable(4)));
92+
static_assert(TypeSize::getFixed(0) + TypeSize::getScalable(8) ==
93+
TypeSize::getScalable(8));
94+
static_assert(TypeSize::getScalable(8) + TypeSize::getFixed(0) ==
95+
TypeSize::getScalable(8));
96+
static_assert(TypeSize::getFixed(8) + TypeSize::getScalable(0) ==
97+
TypeSize::getFixed(8));
98+
static_assert(TypeSize::getScalable(0) + TypeSize::getFixed(8) ==
99+
TypeSize::getFixed(8));
100+
static_assert(TypeSize::getScalable(8) - TypeSize::getFixed(0) ==
101+
TypeSize::getScalable(8));
102+
static_assert(TypeSize::getFixed(8) - TypeSize::getScalable(0) ==
103+
TypeSize::getFixed(8));
104+
84105
TEST(TypeSize, FailIncompatibleTypes) {
85106
EXPECT_DEBUG_DEATH(TypeSize::getFixed(8) + TypeSize::getScalable(8),
86107
"Incompatible types");

0 commit comments

Comments
 (0)