Skip to content

Commit 8df1abe

Browse files
authored
Merge pull request #72158 from Snowy1803/simplify-dbg-type-size
[DebugInfo] Move type size information to CompletedDebugTypeInfo
2 parents d887249 + 653e8fd commit 8df1abe

File tree

5 files changed

+100
-127
lines changed

5 files changed

+100
-127
lines changed

include/swift/SIL/SILDebugInfoExpression.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -296,16 +296,6 @@ class SILDebugInfoExpression {
296296
return Elements.size() &&
297297
Elements[0].getAsOperator() == SILDIExprOperator::Dereference;
298298
}
299-
300-
/// Return true if this DIExpression has a fragment (at the end)
301-
bool hasFragment() const {
302-
return (Elements.size() >= 2 &&
303-
Elements[Elements.size() - 2].getAsOperator() ==
304-
SILDIExprOperator::Fragment) ||
305-
(Elements.size() >= 3 &&
306-
Elements[Elements.size() - 3].getAsOperator() ==
307-
SILDIExprOperator::TupleFragment);
308-
}
309299
};
310300

311301
/// Returns the hashcode for the di expr element.

lib/IRGen/DebugTypeInfo.cpp

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,13 @@ using namespace swift;
2626
using namespace irgen;
2727

2828
DebugTypeInfo::DebugTypeInfo(swift::Type Ty, llvm::Type *FragmentStorageTy,
29-
std::optional<Size::int_type> SizeInBits,
3029
Alignment Align, bool HasDefaultAlignment,
31-
bool IsMetadata, bool SizeIsFragmentSize,
32-
bool IsFixedBuffer,
30+
bool IsMetadata, bool IsFixedBuffer,
3331
std::optional<uint32_t> NumExtraInhabitants)
3432
: Type(Ty.getPointer()), FragmentStorageType(FragmentStorageTy),
35-
SizeInBits(SizeInBits), NumExtraInhabitants(NumExtraInhabitants),
33+
NumExtraInhabitants(NumExtraInhabitants),
3634
Align(Align), DefaultAlignment(HasDefaultAlignment),
37-
IsMetadataType(IsMetadata), SizeIsFragmentSize(SizeIsFragmentSize),
38-
IsFixedBuffer(IsFixedBuffer) {
35+
IsMetadataType(IsMetadata), IsFixedBuffer(IsFixedBuffer) {
3936
assert(Align.getValue() != 0);
4037
}
4138

@@ -50,32 +47,23 @@ static bool hasDefaultAlignment(swift::Type Ty) {
5047
}
5148

5249
DebugTypeInfo DebugTypeInfo::getFromTypeInfo(swift::Type Ty, const TypeInfo &TI,
53-
IRGenModule &IGM,
54-
bool IsFragmentTypeInfo) {
55-
std::optional<Size::int_type> SizeInBits;
50+
IRGenModule &IGM) {
5651
llvm::Type *StorageType = TI.getStorageType();
5752
std::optional<uint32_t> NumExtraInhabitants;
58-
if (StorageType->isSized())
59-
SizeInBits = IGM.DataLayout.getTypeSizeInBits(StorageType);
60-
else if (TI.isFixedSize()) {
61-
const FixedTypeInfo &FixTy = *cast<const FixedTypeInfo>(&TI);
62-
Size::int_type Size = FixTy.getFixedSize().getValue() * 8;
63-
SizeInBits = Size;
64-
}
6553
if (TI.isFixedSize()) {
6654
const FixedTypeInfo &FixTy = *cast<const FixedTypeInfo>(&TI);
6755
NumExtraInhabitants = FixTy.getFixedExtraInhabitantCount(IGM);
6856
}
6957
assert(TI.getStorageType() && "StorageType is a nullptr");
70-
return DebugTypeInfo(Ty.getPointer(), StorageType, SizeInBits,
58+
return DebugTypeInfo(Ty.getPointer(), StorageType,
7159
TI.getBestKnownAlignment(), ::hasDefaultAlignment(Ty),
72-
false, IsFragmentTypeInfo, false, NumExtraInhabitants);
60+
/* IsMetadataType = */ false,
61+
/* IsFixedBuffer = */ false, NumExtraInhabitants);
7362
}
7463

7564
DebugTypeInfo DebugTypeInfo::getLocalVariable(VarDecl *Decl, swift::Type Ty,
7665
const TypeInfo &Info,
77-
IRGenModule &IGM,
78-
bool IsFragmentTypeInfo) {
66+
IRGenModule &IGM) {
7967

8068
auto DeclType = Decl->getInterfaceType();
8169
auto RealType = Ty;
@@ -89,14 +77,15 @@ DebugTypeInfo DebugTypeInfo::getLocalVariable(VarDecl *Decl, swift::Type Ty,
8977
// the type hasn't been mucked with by an optimization pass.
9078
auto *Type = Sugared->isEqual(RealType) ? DeclType.getPointer()
9179
: RealType.getPointer();
92-
return getFromTypeInfo(Type, Info, IGM, IsFragmentTypeInfo);
80+
return getFromTypeInfo(Type, Info, IGM);
9381
}
9482

9583
DebugTypeInfo DebugTypeInfo::getGlobalMetadata(swift::Type Ty,
9684
llvm::Type *StorageTy, Size size,
9785
Alignment align) {
98-
DebugTypeInfo DbgTy(Ty.getPointer(), StorageTy, size.getValue() * 8, align,
99-
true, false, false);
86+
DebugTypeInfo DbgTy(Ty.getPointer(), StorageTy, align,
87+
/* HasDefaultAlignment = */ true,
88+
/* IsMetadataType = */ false);
10089
assert(StorageTy && "StorageType is a nullptr");
10190
assert(!DbgTy.isContextArchetype() &&
10291
"type metadata cannot contain an archetype");
@@ -106,8 +95,9 @@ DebugTypeInfo DebugTypeInfo::getGlobalMetadata(swift::Type Ty,
10695
DebugTypeInfo DebugTypeInfo::getTypeMetadata(swift::Type Ty,
10796
llvm::Type *StorageTy, Size size,
10897
Alignment align) {
109-
DebugTypeInfo DbgTy(Ty.getPointer(), StorageTy, size.getValue() * 8, align,
110-
true, true, false);
98+
DebugTypeInfo DbgTy(Ty.getPointer(), StorageTy, align,
99+
/* HasDefaultAlignment = */ true,
100+
/* IsMetadataType = */ true);
111101
assert(StorageTy && "StorageType is a nullptr");
112102
assert(!DbgTy.isContextArchetype() &&
113103
"type metadata cannot contain an archetype");
@@ -132,7 +122,7 @@ DebugTypeInfo DebugTypeInfo::getGlobal(SILGlobalVariable *GV,
132122
Type = DeclType.getPointer();
133123
}
134124
auto &TI = IGM.getTypeInfoForUnlowered(Type);
135-
DebugTypeInfo DbgTy = getFromTypeInfo(Type, TI, IGM, false);
125+
DebugTypeInfo DbgTy = getFromTypeInfo(Type, TI, IGM);
136126
assert(FragmentStorageType && "FragmentStorageType is a nullptr");
137127
assert(!DbgTy.isContextArchetype() &&
138128
"type of global variable cannot be an archetype");
@@ -152,8 +142,9 @@ DebugTypeInfo::getGlobalFixedBuffer(SILGlobalVariable *GV,
152142
if (DeclType->isEqual(LowTy))
153143
Type = DeclType.getPointer();
154144
}
155-
DebugTypeInfo DbgTy(Type, FragmentStorageType, SizeInBytes.getValue() * 8,
156-
Align, ::hasDefaultAlignment(Type), false, false, true);
145+
DebugTypeInfo DbgTy(Type, FragmentStorageType,
146+
Align, ::hasDefaultAlignment(Type),
147+
/* IsMetadataType = */ false, /* IsFixedBuffer = */ true);
157148
assert(FragmentStorageType && "FragmentStorageType is a nullptr");
158149
assert(!DbgTy.isContextArchetype() &&
159150
"type of global variable cannot be an archetype");
@@ -164,8 +155,9 @@ DebugTypeInfo DebugTypeInfo::getObjCClass(ClassDecl *theClass,
164155
llvm::Type *FragmentStorageType,
165156
Size SizeInBytes, Alignment align) {
166157
DebugTypeInfo DbgTy(theClass->getInterfaceType().getPointer(),
167-
FragmentStorageType, SizeInBytes.getValue() * 8, align,
168-
true, false, false);
158+
FragmentStorageType, align,
159+
/* HasDefaultAlignment = */ true,
160+
/* IsMetadataType = */ false);
169161
assert(FragmentStorageType && "FragmentStorageType is a nullptr");
170162
assert(!DbgTy.isContextArchetype() &&
171163
"type of objc class cannot be an archetype");
@@ -175,13 +167,12 @@ DebugTypeInfo DebugTypeInfo::getObjCClass(ClassDecl *theClass,
175167
DebugTypeInfo DebugTypeInfo::getErrorResult(swift::Type Ty,
176168
IRGenModule &IGM) {
177169
auto &TI = IGM.getTypeInfoForUnlowered(Ty);
178-
DebugTypeInfo DbgTy = getFromTypeInfo(Ty, TI, IGM, false);
170+
DebugTypeInfo DbgTy = getFromTypeInfo(Ty, TI, IGM);
179171
return DbgTy;
180172
}
181173

182174
bool DebugTypeInfo::operator==(DebugTypeInfo T) const {
183175
return getType() == T.getType() &&
184-
SizeInBits == T.SizeInBits &&
185176
Align == T.Align;
186177
}
187178

@@ -204,8 +195,6 @@ TypeDecl *DebugTypeInfo::getDecl() const {
204195
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
205196
LLVM_DUMP_METHOD void DebugTypeInfo::dump() const {
206197
llvm::errs() << "[";
207-
if (SizeInBits)
208-
llvm::errs() << "SizeInBits " << *SizeInBits << " ";
209198
llvm::errs() << "Alignment " << Align.getValue() << "] ";
210199
if (auto *Type = getType())
211200
Type->dump(llvm::errs());
@@ -217,3 +206,19 @@ LLVM_DUMP_METHOD void DebugTypeInfo::dump() const {
217206
llvm::errs() << "forward-declared\n";
218207
}
219208
#endif
209+
210+
std::optional<CompletedDebugTypeInfo>
211+
CompletedDebugTypeInfo::getFromTypeInfo(swift::Type Ty, const TypeInfo &Info, IRGenModule &IGM) {
212+
auto *StorageType = IGM.getStorageTypeForUnlowered(Ty);
213+
std::optional<uint64_t> SizeInBits;
214+
if (StorageType->isSized())
215+
SizeInBits = IGM.DataLayout.getTypeSizeInBits(StorageType);
216+
else if (Info.isFixedSize()) {
217+
const FixedTypeInfo &FixTy = *cast<const FixedTypeInfo>(&Info);
218+
Size::int_type Size = FixTy.getFixedSize().getValue() * 8;
219+
SizeInBits = Size;
220+
}
221+
222+
return CompletedDebugTypeInfo::get(
223+
DebugTypeInfo::getFromTypeInfo(Ty, Info, IGM), SizeInBits);
224+
}

lib/IRGen/DebugTypeInfo.h

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,23 @@ class DebugTypeInfo {
4444
/// Needed to determine the size of basic types and to determine
4545
/// the storage type for undefined variables.
4646
llvm::Type *FragmentStorageType = nullptr;
47-
std::optional<Size::int_type> SizeInBits;
4847
std::optional<uint32_t> NumExtraInhabitants;
4948
Alignment Align;
5049
bool DefaultAlignment = true;
5150
bool IsMetadataType = false;
52-
bool SizeIsFragmentSize = false;
5351
bool IsFixedBuffer = false;
5452

5553
public:
5654
DebugTypeInfo() = default;
5755
DebugTypeInfo(swift::Type Ty, llvm::Type *StorageTy = nullptr,
58-
std::optional<Size::int_type> SizeInBits = {},
5956
Alignment AlignInBytes = Alignment(1),
6057
bool HasDefaultAlignment = true, bool IsMetadataType = false,
61-
bool IsFragmentTypeInfo = false, bool IsFixedBuffer = false,
58+
bool IsFixedBuffer = false,
6259
std::optional<uint32_t> NumExtraInhabitants = {});
6360

6461
/// Create type for a local variable.
6562
static DebugTypeInfo getLocalVariable(VarDecl *Decl, swift::Type Ty,
66-
const TypeInfo &Info, IRGenModule &IGM,
67-
bool IsFragmentTypeInfo);
63+
const TypeInfo &Info, IRGenModule &IGM);
6864
/// Create type for global type metadata.
6965
static DebugTypeInfo getGlobalMetadata(swift::Type Ty, llvm::Type *StorageTy,
7066
Size size, Alignment align);
@@ -77,8 +73,7 @@ class DebugTypeInfo {
7773

7874
/// Create a standalone type from a TypeInfo object.
7975
static DebugTypeInfo getFromTypeInfo(swift::Type Ty, const TypeInfo &Info,
80-
IRGenModule &IGM,
81-
bool IsFragmentTypeInfo);
76+
IRGenModule &IGM);
8277
/// Global variables.
8378
static DebugTypeInfo getGlobal(SILGlobalVariable *GV,
8479
llvm::Type *StorageType, IRGenModule &IGM);
@@ -105,21 +100,12 @@ class DebugTypeInfo {
105100
return false;
106101
}
107102

108-
llvm::Type *getFragmentStorageType() const {
109-
if (SizeInBits && *SizeInBits == 0)
110-
assert(FragmentStorageType && "only defined types may have a size");
111-
return FragmentStorageType;
112-
}
113-
std::optional<Size::int_type> getTypeSizeInBits() const {
114-
return SizeIsFragmentSize ? std::nullopt : SizeInBits;
115-
}
116-
std::optional<Size::int_type> getRawSizeInBits() const { return SizeInBits; }
103+
llvm::Type *getFragmentStorageType() const { return FragmentStorageType; }
117104
Alignment getAlignment() const { return Align; }
118105
bool isNull() const { return Type == nullptr; }
119106
bool isForwardDecl() const { return FragmentStorageType == nullptr; }
120107
bool isMetadataType() const { return IsMetadataType; }
121108
bool hasDefaultAlignment() const { return DefaultAlignment; }
122-
bool isSizeFragmentSize() const { return SizeIsFragmentSize; }
123109
bool isFixedBuffer() const { return IsFixedBuffer; }
124110
std::optional<uint32_t> getNumExtraInhabitants() const {
125111
return NumExtraInhabitants;
@@ -134,22 +120,23 @@ class DebugTypeInfo {
134120

135121
/// A DebugTypeInfo with a defined size (that may be 0).
136122
class CompletedDebugTypeInfo : public DebugTypeInfo {
137-
CompletedDebugTypeInfo(DebugTypeInfo DbgTy) : DebugTypeInfo(DbgTy) {}
123+
Size::int_type SizeInBits;
124+
125+
CompletedDebugTypeInfo(DebugTypeInfo DbgTy, Size::int_type SizeInBits)
126+
: DebugTypeInfo(DbgTy), SizeInBits(SizeInBits) {}
138127

139128
public:
140-
static std::optional<CompletedDebugTypeInfo> get(DebugTypeInfo DbgTy) {
141-
if (!DbgTy.getRawSizeInBits() || DbgTy.isSizeFragmentSize())
129+
static std::optional<CompletedDebugTypeInfo>
130+
get(DebugTypeInfo DbgTy, std::optional<Size::int_type> SizeInBits) {
131+
if (!SizeInBits)
142132
return {};
143-
return CompletedDebugTypeInfo(DbgTy);
133+
return CompletedDebugTypeInfo(DbgTy, *SizeInBits);
144134
}
145135

146136
static std::optional<CompletedDebugTypeInfo>
147-
getFromTypeInfo(swift::Type Ty, const TypeInfo &Info, IRGenModule &IGM) {
148-
return CompletedDebugTypeInfo::get(
149-
DebugTypeInfo::getFromTypeInfo(Ty, Info, IGM, /*IsFragment*/ false));
150-
}
137+
getFromTypeInfo(swift::Type Ty, const TypeInfo &Info, IRGenModule &IGM);
151138

152-
Size::int_type getSizeInBits() const { return *SizeInBits; }
139+
Size::int_type getSizeInBits() const { return SizeInBits; }
153140
};
154141

155142
}
@@ -164,8 +151,8 @@ template <> struct DenseMapInfo<swift::irgen::DebugTypeInfo> {
164151
}
165152
static swift::irgen::DebugTypeInfo getTombstoneKey() {
166153
return swift::irgen::DebugTypeInfo(
167-
llvm::DenseMapInfo<swift::TypeBase *>::getTombstoneKey(), nullptr, 0,
168-
swift::irgen::Alignment(), false, false, false);
154+
llvm::DenseMapInfo<swift::TypeBase *>::getTombstoneKey(), nullptr,
155+
swift::irgen::Alignment(), /* HasDefaultAlignment = */ false);
169156
}
170157
static unsigned getHashValue(swift::irgen::DebugTypeInfo Val) {
171158
return DenseMapInfo<swift::CanType>::getHashValue(Val.getType());

0 commit comments

Comments
 (0)