Skip to content

Commit 456b5a0

Browse files
committed
[DebugInfo] Move type size information to CompletedDebugTypeInfo (NFC)
1 parent 75015b5 commit 456b5a0

File tree

3 files changed

+56
-60
lines changed

3 files changed

+56
-60
lines changed

lib/IRGen/DebugTypeInfo.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ 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,
3130
bool IsMetadata, bool SizeIsFragmentSize,
3231
bool IsFixedBuffer,
3332
std::optional<uint32_t> NumExtraInhabitants)
3433
: Type(Ty.getPointer()), FragmentStorageType(FragmentStorageTy),
35-
SizeInBits(SizeInBits), NumExtraInhabitants(NumExtraInhabitants),
34+
NumExtraInhabitants(NumExtraInhabitants),
3635
Align(Align), DefaultAlignment(HasDefaultAlignment),
3736
IsMetadataType(IsMetadata), SizeIsFragmentSize(SizeIsFragmentSize),
3837
IsFixedBuffer(IsFixedBuffer) {
@@ -52,22 +51,14 @@ static bool hasDefaultAlignment(swift::Type Ty) {
5251
DebugTypeInfo DebugTypeInfo::getFromTypeInfo(swift::Type Ty, const TypeInfo &TI,
5352
IRGenModule &IGM,
5453
bool IsFragmentTypeInfo) {
55-
std::optional<Size::int_type> SizeInBits;
5654
llvm::Type *StorageType = TI.getStorageType();
5755
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-
}
6556
if (TI.isFixedSize()) {
6657
const FixedTypeInfo &FixTy = *cast<const FixedTypeInfo>(&TI);
6758
NumExtraInhabitants = FixTy.getFixedExtraInhabitantCount(IGM);
6859
}
6960
assert(TI.getStorageType() && "StorageType is a nullptr");
70-
return DebugTypeInfo(Ty.getPointer(), StorageType, SizeInBits,
61+
return DebugTypeInfo(Ty.getPointer(), StorageType,
7162
TI.getBestKnownAlignment(), ::hasDefaultAlignment(Ty),
7263
false, IsFragmentTypeInfo, false, NumExtraInhabitants);
7364
}
@@ -95,8 +86,7 @@ DebugTypeInfo DebugTypeInfo::getLocalVariable(VarDecl *Decl, swift::Type Ty,
9586
DebugTypeInfo DebugTypeInfo::getGlobalMetadata(swift::Type Ty,
9687
llvm::Type *StorageTy, Size size,
9788
Alignment align) {
98-
DebugTypeInfo DbgTy(Ty.getPointer(), StorageTy, size.getValue() * 8, align,
99-
true, false, false);
89+
DebugTypeInfo DbgTy(Ty.getPointer(), StorageTy, align, true, false, false);
10090
assert(StorageTy && "StorageType is a nullptr");
10191
assert(!DbgTy.isContextArchetype() &&
10292
"type metadata cannot contain an archetype");
@@ -106,8 +96,7 @@ DebugTypeInfo DebugTypeInfo::getGlobalMetadata(swift::Type Ty,
10696
DebugTypeInfo DebugTypeInfo::getTypeMetadata(swift::Type Ty,
10797
llvm::Type *StorageTy, Size size,
10898
Alignment align) {
109-
DebugTypeInfo DbgTy(Ty.getPointer(), StorageTy, size.getValue() * 8, align,
110-
true, true, false);
99+
DebugTypeInfo DbgTy(Ty.getPointer(), StorageTy, align, true, true, false);
111100
assert(StorageTy && "StorageType is a nullptr");
112101
assert(!DbgTy.isContextArchetype() &&
113102
"type metadata cannot contain an archetype");
@@ -152,7 +141,7 @@ DebugTypeInfo::getGlobalFixedBuffer(SILGlobalVariable *GV,
152141
if (DeclType->isEqual(LowTy))
153142
Type = DeclType.getPointer();
154143
}
155-
DebugTypeInfo DbgTy(Type, FragmentStorageType, SizeInBytes.getValue() * 8,
144+
DebugTypeInfo DbgTy(Type, FragmentStorageType,
156145
Align, ::hasDefaultAlignment(Type), false, false, true);
157146
assert(FragmentStorageType && "FragmentStorageType is a nullptr");
158147
assert(!DbgTy.isContextArchetype() &&
@@ -164,8 +153,7 @@ DebugTypeInfo DebugTypeInfo::getObjCClass(ClassDecl *theClass,
164153
llvm::Type *FragmentStorageType,
165154
Size SizeInBytes, Alignment align) {
166155
DebugTypeInfo DbgTy(theClass->getInterfaceType().getPointer(),
167-
FragmentStorageType, SizeInBytes.getValue() * 8, align,
168-
true, false, false);
156+
FragmentStorageType, align, true, false, false);
169157
assert(FragmentStorageType && "FragmentStorageType is a nullptr");
170158
assert(!DbgTy.isContextArchetype() &&
171159
"type of objc class cannot be an archetype");
@@ -181,7 +169,6 @@ DebugTypeInfo DebugTypeInfo::getErrorResult(swift::Type Ty,
181169

182170
bool DebugTypeInfo::operator==(DebugTypeInfo T) const {
183171
return getType() == T.getType() &&
184-
SizeInBits == T.SizeInBits &&
185172
Align == T.Align;
186173
}
187174

@@ -204,8 +191,6 @@ TypeDecl *DebugTypeInfo::getDecl() const {
204191
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
205192
LLVM_DUMP_METHOD void DebugTypeInfo::dump() const {
206193
llvm::errs() << "[";
207-
if (SizeInBits)
208-
llvm::errs() << "SizeInBits " << *SizeInBits << " ";
209194
llvm::errs() << "Alignment " << Align.getValue() << "] ";
210195
if (auto *Type = getType())
211196
Type->dump(llvm::errs());
@@ -217,3 +202,20 @@ LLVM_DUMP_METHOD void DebugTypeInfo::dump() const {
217202
llvm::errs() << "forward-declared\n";
218203
}
219204
#endif
205+
206+
std::optional<CompletedDebugTypeInfo>
207+
CompletedDebugTypeInfo::getFromTypeInfo(swift::Type Ty, const TypeInfo &Info, IRGenModule &IGM) {
208+
auto *StorageType = IGM.getStorageTypeForUnlowered(Ty);
209+
std::optional<uint64_t> SizeInBits;
210+
if (StorageType->isSized())
211+
SizeInBits = IGM.DataLayout.getTypeSizeInBits(StorageType);
212+
else if (Info.isFixedSize()) {
213+
const FixedTypeInfo &FixTy = *cast<const FixedTypeInfo>(&Info);
214+
Size::int_type Size = FixTy.getFixedSize().getValue() * 8;
215+
SizeInBits = Size;
216+
}
217+
218+
return CompletedDebugTypeInfo::get(
219+
DebugTypeInfo::getFromTypeInfo(Ty, Info, IGM, /*IsFragment*/ false),
220+
SizeInBits);
221+
}

lib/IRGen/DebugTypeInfo.h

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ 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;
@@ -55,7 +54,6 @@ class DebugTypeInfo {
5554
public:
5655
DebugTypeInfo() = default;
5756
DebugTypeInfo(swift::Type Ty, llvm::Type *StorageTy = nullptr,
58-
std::optional<Size::int_type> SizeInBits = {},
5957
Alignment AlignInBytes = Alignment(1),
6058
bool HasDefaultAlignment = true, bool IsMetadataType = false,
6159
bool IsFragmentTypeInfo = false, bool IsFixedBuffer = false,
@@ -105,12 +103,7 @@ class DebugTypeInfo {
105103
return false;
106104
}
107105

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> getRawSizeInBits() const { return SizeInBits; }
106+
llvm::Type *getFragmentStorageType() const { return FragmentStorageType; }
114107
Alignment getAlignment() const { return Align; }
115108
bool isNull() const { return Type == nullptr; }
116109
bool isForwardDecl() const { return FragmentStorageType == nullptr; }
@@ -131,22 +124,23 @@ class DebugTypeInfo {
131124

132125
/// A DebugTypeInfo with a defined size (that may be 0).
133126
class CompletedDebugTypeInfo : public DebugTypeInfo {
134-
CompletedDebugTypeInfo(DebugTypeInfo DbgTy) : DebugTypeInfo(DbgTy) {}
127+
Size::int_type SizeInBits;
128+
129+
CompletedDebugTypeInfo(DebugTypeInfo DbgTy, Size::int_type SizeInBits)
130+
: DebugTypeInfo(DbgTy), SizeInBits(SizeInBits) {}
135131

136132
public:
137-
static std::optional<CompletedDebugTypeInfo> get(DebugTypeInfo DbgTy) {
138-
if (!DbgTy.getRawSizeInBits() || DbgTy.isSizeFragmentSize())
133+
static std::optional<CompletedDebugTypeInfo>
134+
get(DebugTypeInfo DbgTy, std::optional<Size::int_type> SizeInBits) {
135+
if (!SizeInBits)
139136
return {};
140-
return CompletedDebugTypeInfo(DbgTy);
137+
return CompletedDebugTypeInfo(DbgTy, *SizeInBits);
141138
}
142139

143140
static std::optional<CompletedDebugTypeInfo>
144-
getFromTypeInfo(swift::Type Ty, const TypeInfo &Info, IRGenModule &IGM) {
145-
return CompletedDebugTypeInfo::get(
146-
DebugTypeInfo::getFromTypeInfo(Ty, Info, IGM, /*IsFragment*/ false));
147-
}
141+
getFromTypeInfo(swift::Type Ty, const TypeInfo &Info, IRGenModule &IGM);
148142

149-
Size::int_type getSizeInBits() const { return *SizeInBits; }
143+
Size::int_type getSizeInBits() const { return SizeInBits; }
150144
};
151145

152146
}
@@ -161,7 +155,7 @@ template <> struct DenseMapInfo<swift::irgen::DebugTypeInfo> {
161155
}
162156
static swift::irgen::DebugTypeInfo getTombstoneKey() {
163157
return swift::irgen::DebugTypeInfo(
164-
llvm::DenseMapInfo<swift::TypeBase *>::getTombstoneKey(), nullptr, 0,
158+
llvm::DenseMapInfo<swift::TypeBase *>::getTombstoneKey(), nullptr,
165159
swift::irgen::Alignment(), false, false, false);
166160
}
167161
static unsigned getHashValue(swift::irgen::DebugTypeInfo Val) {

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,11 +1044,10 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
10441044
llvm::DINode::DIFlags Flags) {
10451045
unsigned SizeOfByte = CI.getTargetInfo().getCharWidth();
10461046
auto *Ty = getOrCreateType(DbgTy);
1047+
auto SizeInBits = getSizeInBits(Ty);
10471048
auto *DITy = DBuilder.createMemberType(
1048-
Scope, Name, File, 0,
1049-
DbgTy.getRawSizeInBits() ? *DbgTy.getRawSizeInBits() : 0, 0,
1050-
OffsetInBits, Flags, Ty);
1051-
OffsetInBits += getSizeInBits(Ty);
1049+
Scope, Name, File, 0, SizeInBits, 0, OffsetInBits, Flags, Ty);
1050+
OffsetInBits += SizeInBits;
10521051
OffsetInBits = llvm::alignTo(OffsetInBits,
10531052
SizeOfByte * DbgTy.getAlignment().getValue());
10541053
return DITy;
@@ -1350,14 +1349,14 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
13501349
// Create debug information for an enum with no raw type.
13511350
llvm::DICompositeType *
13521351
createUnsubstitutedVariantType(DebugTypeInfo DbgTy, EnumDecl *Decl,
1353-
StringRef MangledName, unsigned AlignInBits,
1352+
StringRef MangledName,
1353+
unsigned SizeInBits, unsigned AlignInBits,
13541354
llvm::DIScope *Scope, llvm::DIFile *File,
13551355
unsigned Line, llvm::DINode::DIFlags Flags) {
13561356
assert(!Decl->getRawType() &&
13571357
"Attempting to create variant debug info from raw enum!");
13581358

13591359
StringRef Name = Decl->getName().str();
1360-
unsigned SizeInBits = DbgTy.getRawSizeInBits().value_or(0);
13611360
auto NumExtraInhabitants = DbgTy.getNumExtraInhabitants();
13621361

13631362
// A variant part should actually be a child to a DW_TAG_structure_type
@@ -1419,8 +1418,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
14191418

14201419
llvm::DIType *getOrCreateDesugaredType(Type Ty, DebugTypeInfo DbgTy) {
14211420
DebugTypeInfo BlandDbgTy(Ty, DbgTy.getFragmentStorageType(),
1422-
DbgTy.getRawSizeInBits(), DbgTy.getAlignment(),
1423-
DbgTy.hasDefaultAlignment(),
1421+
DbgTy.getAlignment(), DbgTy.hasDefaultAlignment(),
14241422
DbgTy.isMetadataType(), DbgTy.isSizeFragmentSize(),
14251423
DbgTy.isFixedBuffer());
14261424
return getOrCreateType(BlandDbgTy);
@@ -1687,11 +1685,12 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
16871685
// emitting the storage size of the struct, but it may be necessary
16881686
// to emit the (target!) size of the underlying basic type.
16891687
uint64_t SizeOfByte = CI.getTargetInfo().getCharWidth();
1690-
// FIXME: SizeInBits is redundant with DbgTy, remove it.
1691-
auto *llvmty = IGM.getStorageTypeForUnlowered(DbgTy.getType());
1688+
auto CompletedDbgTy = CompletedDebugTypeInfo::getFromTypeInfo(
1689+
DbgTy.getType(), IGM.getTypeInfoForUnlowered(DbgTy.getType()), IGM);
16921690
std::optional<uint64_t> SizeInBitsOrNull;
1693-
if (llvmty->isSized())
1694-
SizeInBitsOrNull = IGM.DataLayout.getTypeSizeInBits(llvmty);
1691+
if (CompletedDbgTy)
1692+
SizeInBitsOrNull = CompletedDbgTy->getSizeInBits();
1693+
16951694
uint64_t SizeInBits = SizeInBitsOrNull.value_or(0);
16961695
unsigned AlignInBits = DbgTy.hasDefaultAlignment()
16971696
? 0
@@ -1720,14 +1719,14 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
17201719
case TypeKind::BuiltinPackIndex:
17211720
case TypeKind::BuiltinInteger: {
17221721
Encoding = llvm::dwarf::DW_ATE_unsigned;
1723-
if (auto CompletedDbgTy = CompletedDebugTypeInfo::get(DbgTy))
1722+
if (CompletedDbgTy)
17241723
SizeInBits = getSizeOfBasicType(*CompletedDbgTy);
17251724
break;
17261725
}
17271726

17281727
case TypeKind::BuiltinIntegerLiteral: {
17291728
Encoding = llvm::dwarf::DW_ATE_unsigned; // ?
1730-
if (auto CompletedDbgTy = CompletedDebugTypeInfo::get(DbgTy))
1729+
if (CompletedDbgTy)
17311730
SizeInBits = getSizeOfBasicType(*CompletedDbgTy);
17321731
break;
17331732
}
@@ -2015,7 +2014,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
20152014
auto L = getFileAndLocation(Decl);
20162015
unsigned FwdDeclLine = 0;
20172016
if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes)
2018-
if (auto CompletedDbgTy = CompletedDebugTypeInfo::get(DbgTy))
2017+
if (CompletedDbgTy)
20192018
return createEnumType(*CompletedDbgTy, Decl, MangledName, AlignInBits,
20202019
Scope, L.File, L.Line, Flags);
20212020
return createOpaqueStruct(Scope, Decl->getName().str(), L.File,
@@ -2041,8 +2040,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
20412040
UnsubstitutedTy->mapTypeOutOfContext(), {});
20422041
if (DeclTypeMangledName == MangledName) {
20432042
return createUnsubstitutedVariantType(DbgTy, Decl, MangledName,
2044-
AlignInBits, Scope, File, FwdDeclLine,
2045-
Flags);
2043+
SizeInBits, AlignInBits, Scope, File,
2044+
FwdDeclLine, Flags);
20462045
}
20472046
// Force the creation of the unsubstituted type, don't create it
20482047
// directly so it goes through all the caching/verification logic.
@@ -2095,7 +2094,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
20952094
// For TypeAlias types, the DeclContext for the aliased type is
20962095
// in the decl of the alias type.
20972096
DebugTypeInfo AliasedDbgTy(
2098-
AliasedTy, DbgTy.getFragmentStorageType(), DbgTy.getRawSizeInBits(),
2097+
AliasedTy, DbgTy.getFragmentStorageType(),
20992098
DbgTy.getAlignment(), DbgTy.hasDefaultAlignment(), false,
21002099
DbgTy.isSizeFragmentSize(), DbgTy.isFixedBuffer(),
21012100
DbgTy.getNumExtraInhabitants());
@@ -2190,10 +2189,11 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
21902189
bool sanityCheckCachedType(DebugTypeInfo DbgTy, llvm::DIType *CachedType) {
21912190
if (DbgTy.isForwardDecl())
21922191
return true;
2193-
auto *StorageType = IGM.getStorageTypeForUnlowered(DbgTy.getType());
2192+
auto CompletedDbgTy = CompletedDebugTypeInfo::getFromTypeInfo(
2193+
DbgTy.getType(), IGM.getTypeInfoForUnlowered(DbgTy.getType()), IGM);
21942194
std::optional<uint64_t> SizeInBits;
2195-
if (StorageType->isSized())
2196-
SizeInBits = IGM.DataLayout.getTypeSizeInBits(StorageType);
2195+
if (CompletedDbgTy)
2196+
SizeInBits = CompletedDbgTy->getSizeInBits();
21972197
unsigned CachedSizeInBits = getSizeInBits(CachedType);
21982198
if ((SizeInBits && CachedSizeInBits != *SizeInBits) ||
21992199
(!SizeInBits && CachedSizeInBits)) {

0 commit comments

Comments
 (0)