Skip to content

Commit 28fb2b3

Browse files
[LLVM][SelectionDAG] Reduce number of ComputeValueVTs variants. (#75614)
This is another step in the direction of fixing the `Fixed(0) != Scalable(0)` bugbear, although whilst weird I don't believe it's causing us any real issues.
1 parent 02e17ab commit 28fb2b3

File tree

6 files changed

+29
-67
lines changed

6 files changed

+29
-67
lines changed

llvm/include/llvm/CodeGen/Analysis.h

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,36 +62,31 @@ inline unsigned ComputeLinearIndex(Type *Ty,
6262
/// If Offsets is non-null, it points to a vector to be filled in
6363
/// with the in-memory offsets of each of the individual values.
6464
///
65-
void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty,
66-
SmallVectorImpl<EVT> &ValueVTs,
67-
SmallVectorImpl<TypeSize> *Offsets,
68-
TypeSize StartingOffset);
69-
void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty,
70-
SmallVectorImpl<EVT> &ValueVTs,
71-
SmallVectorImpl<TypeSize> *Offsets = nullptr,
72-
uint64_t StartingOffset = 0);
73-
void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty,
74-
SmallVectorImpl<EVT> &ValueVTs,
75-
SmallVectorImpl<uint64_t> *FixedOffsets,
76-
uint64_t StartingOffset);
77-
78-
/// Variant of ComputeValueVTs that also produces the memory VTs.
79-
void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty,
80-
SmallVectorImpl<EVT> &ValueVTs,
81-
SmallVectorImpl<EVT> *MemVTs,
82-
SmallVectorImpl<TypeSize> *Offsets,
83-
TypeSize StartingOffset);
8465
void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty,
8566
SmallVectorImpl<EVT> &ValueVTs,
8667
SmallVectorImpl<EVT> *MemVTs,
8768
SmallVectorImpl<TypeSize> *Offsets = nullptr,
88-
uint64_t StartingOffset = 0);
69+
TypeSize StartingOffset = TypeSize::getZero());
8970
void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty,
9071
SmallVectorImpl<EVT> &ValueVTs,
9172
SmallVectorImpl<EVT> *MemVTs,
9273
SmallVectorImpl<uint64_t> *FixedOffsets,
9374
uint64_t StartingOffset);
9475

76+
/// Variant of ComputeValueVTs that don't produce memory VTs.
77+
inline void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL,
78+
Type *Ty, SmallVectorImpl<EVT> &ValueVTs,
79+
SmallVectorImpl<TypeSize> *Offsets = nullptr,
80+
TypeSize StartingOffset = TypeSize::getZero()) {
81+
ComputeValueVTs(TLI, DL, Ty, ValueVTs, nullptr, Offsets, StartingOffset);
82+
}
83+
inline void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL,
84+
Type *Ty, SmallVectorImpl<EVT> &ValueVTs,
85+
SmallVectorImpl<uint64_t> *FixedOffsets,
86+
uint64_t StartingOffset) {
87+
ComputeValueVTs(TLI, DL, Ty, ValueVTs, nullptr, FixedOffsets, StartingOffset);
88+
}
89+
9590
/// computeValueLLTs - Given an LLVM IR type, compute a sequence of
9691
/// LLTs that represent all the individual underlying
9792
/// non-aggregate types that comprise it.

llvm/include/llvm/Support/TypeSize.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ class TypeSize : public details::FixedOrScalableQuantity<TypeSize, uint64_t> {
335335
static constexpr TypeSize getScalable(ScalarTy MinimumSize) {
336336
return TypeSize(MinimumSize, true);
337337
}
338+
static constexpr TypeSize getZero() { return TypeSize(0, false); }
338339

339340
// All code for this class below this point is needed because of the
340341
// temporary implicit conversion to uint64_t. The operator overloads are

llvm/lib/CodeGen/Analysis.cpp

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL,
8181
SmallVectorImpl<EVT> *MemVTs,
8282
SmallVectorImpl<TypeSize> *Offsets,
8383
TypeSize StartingOffset) {
84+
assert((Ty->isScalableTy() == StartingOffset.isScalable() ||
85+
StartingOffset.isZero()) &&
86+
"Offset/TypeSize mismatch!");
8487
// Given a struct type, recursively traverse the elements.
8588
if (StructType *STy = dyn_cast<StructType>(Ty)) {
8689
// If the Offsets aren't needed, don't query the struct layout. This allows
@@ -92,8 +95,8 @@ void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL,
9295
EE = STy->element_end();
9396
EI != EE; ++EI) {
9497
// Don't compute the element offset if we didn't get a StructLayout above.
95-
TypeSize EltOffset = SL ? SL->getElementOffset(EI - EB)
96-
: TypeSize::get(0, StartingOffset.isScalable());
98+
TypeSize EltOffset =
99+
SL ? SL->getElementOffset(EI - EB) : TypeSize::getZero();
97100
ComputeValueVTs(TLI, DL, *EI, ValueVTs, MemVTs, Offsets,
98101
StartingOffset + EltOffset);
99102
}
@@ -119,52 +122,12 @@ void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL,
119122
Offsets->push_back(StartingOffset);
120123
}
121124

122-
void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL,
123-
Type *Ty, SmallVectorImpl<EVT> &ValueVTs,
124-
SmallVectorImpl<TypeSize> *Offsets,
125-
TypeSize StartingOffset) {
126-
return ComputeValueVTs(TLI, DL, Ty, ValueVTs, /*MemVTs=*/nullptr, Offsets,
127-
StartingOffset);
128-
}
129-
130-
void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL,
131-
Type *Ty, SmallVectorImpl<EVT> &ValueVTs,
132-
SmallVectorImpl<TypeSize> *Offsets,
133-
uint64_t StartingOffset) {
134-
TypeSize Offset = TypeSize::get(StartingOffset, Ty->isScalableTy());
135-
return ComputeValueVTs(TLI, DL, Ty, ValueVTs, Offsets, Offset);
136-
}
137-
138-
void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL,
139-
Type *Ty, SmallVectorImpl<EVT> &ValueVTs,
140-
SmallVectorImpl<uint64_t> *FixedOffsets,
141-
uint64_t StartingOffset) {
142-
TypeSize Offset = TypeSize::get(StartingOffset, Ty->isScalableTy());
143-
if (FixedOffsets) {
144-
SmallVector<TypeSize, 4> Offsets;
145-
ComputeValueVTs(TLI, DL, Ty, ValueVTs, &Offsets, Offset);
146-
for (TypeSize Offset : Offsets)
147-
FixedOffsets->push_back(Offset.getFixedValue());
148-
} else {
149-
ComputeValueVTs(TLI, DL, Ty, ValueVTs, nullptr, Offset);
150-
}
151-
}
152-
153-
void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL,
154-
Type *Ty, SmallVectorImpl<EVT> &ValueVTs,
155-
SmallVectorImpl<EVT> *MemVTs,
156-
SmallVectorImpl<TypeSize> *Offsets,
157-
uint64_t StartingOffset) {
158-
TypeSize Offset = TypeSize::get(StartingOffset, Ty->isScalableTy());
159-
return ComputeValueVTs(TLI, DL, Ty, ValueVTs, MemVTs, Offsets, Offset);
160-
}
161-
162125
void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL,
163126
Type *Ty, SmallVectorImpl<EVT> &ValueVTs,
164127
SmallVectorImpl<EVT> *MemVTs,
165128
SmallVectorImpl<uint64_t> *FixedOffsets,
166129
uint64_t StartingOffset) {
167-
TypeSize Offset = TypeSize::get(StartingOffset, Ty->isScalableTy());
130+
TypeSize Offset = TypeSize::getFixed(StartingOffset);
168131
if (FixedOffsets) {
169132
SmallVector<TypeSize, 4> Offsets;
170133
ComputeValueVTs(TLI, DL, Ty, ValueVTs, MemVTs, &Offsets, Offset);

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4332,7 +4332,7 @@ void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
43324332
Type *Ty = I.getType();
43334333
SmallVector<EVT, 4> ValueVTs, MemVTs;
43344334
SmallVector<TypeSize, 4> Offsets;
4335-
ComputeValueVTs(TLI, DAG.getDataLayout(), Ty, ValueVTs, &MemVTs, &Offsets, 0);
4335+
ComputeValueVTs(TLI, DAG.getDataLayout(), Ty, ValueVTs, &MemVTs, &Offsets);
43364336
unsigned NumValues = ValueVTs.size();
43374337
if (NumValues == 0)
43384338
return;
@@ -4500,7 +4500,7 @@ void SelectionDAGBuilder::visitStore(const StoreInst &I) {
45004500
SmallVector<EVT, 4> ValueVTs, MemVTs;
45014501
SmallVector<TypeSize, 4> Offsets;
45024502
ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4503-
SrcV->getType(), ValueVTs, &MemVTs, &Offsets, 0);
4503+
SrcV->getType(), ValueVTs, &MemVTs, &Offsets);
45044504
unsigned NumValues = ValueVTs.size();
45054505
if (NumValues == 0)
45064506
return;

llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ static Instruction *unpackLoadToAggregate(InstCombinerImpl &IC, LoadInst &LI) {
777777
auto *Zero = ConstantInt::get(IdxType, 0);
778778

779779
Value *V = PoisonValue::get(T);
780-
TypeSize Offset = TypeSize::get(0, ET->isScalableTy());
780+
TypeSize Offset = TypeSize::getZero();
781781
for (uint64_t i = 0; i < NumElements; i++) {
782782
Value *Indices[2] = {
783783
Zero,
@@ -1303,7 +1303,7 @@ static bool unpackStoreToAggregate(InstCombinerImpl &IC, StoreInst &SI) {
13031303
auto *IdxType = Type::getInt64Ty(T->getContext());
13041304
auto *Zero = ConstantInt::get(IdxType, 0);
13051305

1306-
TypeSize Offset = TypeSize::get(0, AT->getElementType()->isScalableTy());
1306+
TypeSize Offset = TypeSize::getZero();
13071307
for (uint64_t i = 0; i < NumElements; i++) {
13081308
Value *Indices[2] = {
13091309
Zero,

llvm/unittests/Support/TypeSizeTest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,12 @@ static_assert(UINT64_C(2) * TSFixed32 == TypeSize::getFixed(64));
8282
static_assert(alignTo(TypeSize::getFixed(7), 8) == TypeSize::getFixed(8));
8383

8484
static_assert(TypeSize() == TypeSize::getFixed(0));
85+
static_assert(TypeSize::getZero() == TypeSize::getFixed(0));
86+
static_assert(TypeSize::getZero() != TypeSize::getScalable(0));
8587
static_assert(TypeSize::getFixed(0) != TypeSize::getScalable(0));
8688
static_assert(TypeSize::getFixed(0).isZero());
8789
static_assert(TypeSize::getScalable(0).isZero());
90+
static_assert(TypeSize::getZero().isZero());
8891
static_assert(TypeSize::getFixed(0) ==
8992
(TypeSize::getFixed(4) - TypeSize::getFixed(4)));
9093
static_assert(TypeSize::getScalable(0) ==

0 commit comments

Comments
 (0)