Skip to content

Commit 1576c08

Browse files
committed
Store size in DebugTypeInfo in Bits (NFC)
1 parent e74aeed commit 1576c08

File tree

3 files changed

+68
-47
lines changed

3 files changed

+68
-47
lines changed

lib/IRGen/DebugTypeInfo.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,26 @@ using namespace swift;
2525
using namespace irgen;
2626

2727
DebugTypeInfo::DebugTypeInfo(swift::Type Ty, llvm::Type *FragmentStorageTy,
28-
Optional<Size> size, Alignment align,
28+
Optional<Size::int_type> SizeInBits,
29+
Alignment Align, bool HasDefaultAlignment,
30+
bool IsMetadata, bool SizeIsFragmentSize)
31+
: Type(Ty.getPointer()), FragmentStorageType(FragmentStorageTy),
32+
SizeInBits(SizeInBits), Align(Align),
33+
DefaultAlignment(HasDefaultAlignment), IsMetadataType(IsMetadata),
34+
SizeIsFragmentSize(SizeIsFragmentSize) {
35+
assert(Align.getValue() != 0);
36+
}
37+
38+
DebugTypeInfo::DebugTypeInfo(swift::Type Ty, llvm::Type *FragmentStorageTy,
39+
Optional<Size> SizeInBytes, Alignment Align,
2940
bool HasDefaultAlignment, bool IsMetadata,
3041
bool SizeIsFragmentSize)
31-
: Type(Ty.getPointer()), FragmentStorageType(FragmentStorageTy), size(size),
32-
align(align), DefaultAlignment(HasDefaultAlignment),
42+
: Type(Ty.getPointer()), FragmentStorageType(FragmentStorageTy),
43+
Align(Align), DefaultAlignment(HasDefaultAlignment),
3344
IsMetadataType(IsMetadata), SizeIsFragmentSize(SizeIsFragmentSize) {
34-
assert(align.getValue() != 0);
45+
if (SizeInBytes)
46+
SizeInBits = SizeInBytes->getValue() * 8;
47+
assert(Align.getValue() != 0);
3548
}
3649

3750
/// Determine whether this type has a custom @_alignment attribute.
@@ -99,8 +112,7 @@ DebugTypeInfo DebugTypeInfo::getTypeMetadata(swift::Type Ty,
99112
}
100113

101114
DebugTypeInfo DebugTypeInfo::getForwardDecl(swift::Type Ty) {
102-
DebugTypeInfo DbgTy(Ty.getPointer(), nullptr, {}, Alignment(1), true,
103-
false, false);
115+
DebugTypeInfo DbgTy(Ty.getPointer());
104116
return DbgTy;
105117
}
106118

@@ -144,9 +156,9 @@ DebugTypeInfo DebugTypeInfo::getErrorResult(swift::Type Ty,
144156
}
145157

146158
bool DebugTypeInfo::operator==(DebugTypeInfo T) const {
147-
return (getType() == T.getType() &&
148-
size == T.size &&
149-
align == T.align);
159+
return getType() == T.getType() &&
160+
SizeInBits == T.SizeInBits &&
161+
Align == T.Align;
150162
}
151163

152164
bool DebugTypeInfo::operator!=(DebugTypeInfo T) const { return !operator==(T); }
@@ -168,9 +180,9 @@ TypeDecl *DebugTypeInfo::getDecl() const {
168180
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
169181
LLVM_DUMP_METHOD void DebugTypeInfo::dump() const {
170182
llvm::errs() << "[";
171-
if (size)
172-
llvm::errs() << "Size " << size->getValue() << " ";
173-
llvm::errs() << "Alignment " << align.getValue() << "] ";
183+
if (SizeInBits)
184+
llvm::errs() << "SizeInBits " << *SizeInBits << " ";
185+
llvm::errs() << "Alignment " << Align.getValue() << "] ";
174186
getType()->dump(llvm::errs());
175187

176188
if (FragmentStorageType) {

lib/IRGen/DebugTypeInfo.h

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,19 @@ class DebugTypeInfo {
4343
/// Needed to determine the size of basic types and to determine
4444
/// the storage type for undefined variables.
4545
llvm::Type *FragmentStorageType = nullptr;
46-
Optional<Size> size;
47-
Alignment align;
46+
Optional<Size::int_type> SizeInBits;
47+
Alignment Align;
4848
bool DefaultAlignment = true;
4949
bool IsMetadataType = false;
5050
bool SizeIsFragmentSize;
5151

5252
public:
5353
DebugTypeInfo() = default;
54+
DebugTypeInfo(swift::Type Ty, llvm::Type *StorageTy = nullptr,
55+
Optional<Size::int_type> SizeInBits = {},
56+
Alignment AlignInBytes = Alignment(1),
57+
bool HasDefaultAlignment = true, bool IsMetadataType = false,
58+
bool IsFragmentTypeInfo = false);
5459
DebugTypeInfo(swift::Type Ty, llvm::Type *StorageTy,
5560
Optional<Size> SizeInBytes, Alignment AlignInBytes,
5661
bool HasDefaultAlignment, bool IsMetadataType,
@@ -99,16 +104,16 @@ class DebugTypeInfo {
99104
}
100105

101106
llvm::Type *getFragmentStorageType() const {
102-
if (size && size->isZero())
107+
if (SizeInBits && *SizeInBits == 0)
103108
assert(FragmentStorageType && "only defined types may have a size");
104109
return FragmentStorageType;
105110
}
106-
Optional<Size> getTypeSize() const {
107-
return SizeIsFragmentSize ? llvm::None : size;
111+
Optional<Size::int_type> getTypeSizeInBits() const {
112+
return SizeIsFragmentSize ? llvm::None : SizeInBits;
108113
}
109-
Optional<Size> getRawSize() const { return size; }
110-
void setSize(Size NewSize) { size = NewSize; }
111-
Alignment getAlignment() const { return align; }
114+
Optional<Size::int_type> getRawSizeInBits() const { return SizeInBits; }
115+
void setSizeInBits(Size::int_type NewSize) { SizeInBits = NewSize; }
116+
Alignment getAlignment() const { return Align; }
112117
bool isNull() const { return Type == nullptr; }
113118
bool isForwardDecl() const { return FragmentStorageType == nullptr; }
114119
bool isMetadataType() const { return IsMetadataType; }
@@ -128,7 +133,7 @@ class CompletedDebugTypeInfo : public DebugTypeInfo {
128133

129134
public:
130135
static Optional<CompletedDebugTypeInfo> get(DebugTypeInfo DbgTy) {
131-
if (!DbgTy.getRawSize() || DbgTy.isSizeFragmentSize())
136+
if (!DbgTy.getRawSizeInBits() || DbgTy.isSizeFragmentSize())
132137
return {};
133138
return CompletedDebugTypeInfo(DbgTy);
134139
}
@@ -139,7 +144,7 @@ class CompletedDebugTypeInfo : public DebugTypeInfo {
139144
DebugTypeInfo::getFromTypeInfo(Ty, Info, /*IsFragment*/ false));
140145
}
141146

142-
Size::int_type getSizeValue() const { return size->getValue(); }
147+
Size::int_type getSizeInBits() const { return *SizeInBits; }
143148
};
144149

145150
}

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -845,12 +845,12 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
845845
return cast<llvm::DIFile>(Scope);
846846
}
847847

848-
static Size getStorageSize(const llvm::DataLayout &DL,
849-
ArrayRef<llvm::Value *> Storage) {
850-
unsigned size = 0;
848+
static unsigned getStorageSizeInBits(const llvm::DataLayout &DL,
849+
ArrayRef<llvm::Value *> Storage) {
850+
unsigned SizeInBits = 0;
851851
for (llvm::Value *Piece : Storage)
852-
size += DL.getTypeSizeInBits(Piece->getType());
853-
return Size(size);
852+
SizeInBits += DL.getTypeSizeInBits(Piece->getType());
853+
return SizeInBits;
854854
}
855855

856856
StringRef getMangledName(DebugTypeInfo DbgTy) {
@@ -948,10 +948,9 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
948948
llvm::DINode::DIFlags Flags) {
949949
unsigned SizeOfByte = CI.getTargetInfo().getCharWidth();
950950
auto *Ty = getOrCreateType(DbgTy);
951-
auto *DITy = DBuilder.createMemberType(
952-
Scope, Name, File, 0,
953-
SizeOfByte * DbgTy.getSizeValue(), 0, OffsetInBits,
954-
Flags, Ty);
951+
auto *DITy =
952+
DBuilder.createMemberType(Scope, Name, File, 0, DbgTy.getSizeInBits(),
953+
0, OffsetInBits, Flags, Ty);
955954
OffsetInBits += getSizeInBits(Ty);
956955
OffsetInBits = llvm::alignTo(OffsetInBits,
957956
SizeOfByte * DbgTy.getAlignment().getValue());
@@ -1017,8 +1016,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
10171016
llvm::DIFile *File, unsigned Line,
10181017
llvm::DINode::DIFlags Flags) {
10191018
StringRef Name = Decl->getName().str();
1020-
unsigned SizeOfByte = CI.getTargetInfo().getCharWidth();
1021-
unsigned SizeInBits = DbgTy.getSizeValue() * SizeOfByte;
1019+
unsigned SizeInBits = DbgTy.getSizeInBits();
10221020
// Default, since Swift doesn't allow specifying a custom alignment.
10231021
unsigned AlignInBits = 0;
10241022

@@ -1050,8 +1048,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
10501048
// the storage size from the enum.
10511049
ElemDbgTy = CompletedDebugTypeInfo::get(
10521050
DebugTypeInfo(Decl->getRawType(), DbgTy.getFragmentStorageType(),
1053-
DbgTy.getRawSize(), DbgTy.getAlignment(), true, false,
1054-
DbgTy.isSizeFragmentSize()));
1051+
DbgTy.getRawSizeInBits(), DbgTy.getAlignment(), true,
1052+
false, DbgTy.isSizeFragmentSize()));
10551053
else if (auto ArgTy = ElemDecl->getArgumentInterfaceType()) {
10561054
// A discriminated union. This should really be described as a
10571055
// DW_TAG_variant_type. For now only describing the data.
@@ -1089,15 +1087,14 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
10891087

10901088
llvm::DIType *getOrCreateDesugaredType(Type Ty, DebugTypeInfo DbgTy) {
10911089
DebugTypeInfo BlandDbgTy(
1092-
Ty, DbgTy.getFragmentStorageType(), DbgTy.getRawSize(),
1090+
Ty, DbgTy.getFragmentStorageType(), DbgTy.getRawSizeInBits(),
10931091
DbgTy.getAlignment(), DbgTy.hasDefaultAlignment(),
10941092
DbgTy.isMetadataType(), DbgTy.isSizeFragmentSize());
10951093
return getOrCreateType(BlandDbgTy);
10961094
}
10971095

10981096
uint64_t getSizeOfBasicType(CompletedDebugTypeInfo DbgTy) {
1099-
uint64_t SizeOfByte = CI.getTargetInfo().getCharWidth();
1100-
uint64_t BitWidth = DbgTy.getSizeValue() * SizeOfByte;
1097+
uint64_t BitWidth = DbgTy.getSizeInBits();
11011098
llvm::Type *StorageType = DbgTy.getFragmentStorageType()
11021099
? DbgTy.getFragmentStorageType()
11031100
: IGM.DataLayout.getSmallestLegalIntType(
@@ -1332,8 +1329,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
13321329
uint64_t SizeOfByte = CI.getTargetInfo().getCharWidth();
13331330
// FIXME: SizeInBits is redundant with DbgTy, remove it.
13341331
uint64_t SizeInBits = 0;
1335-
if (DbgTy.getTypeSize())
1336-
SizeInBits = DbgTy.getTypeSize()->getValue() * SizeOfByte;
1332+
if (DbgTy.getTypeSizeInBits())
1333+
SizeInBits = *DbgTy.getTypeSizeInBits();
13371334
unsigned AlignInBits = DbgTy.hasDefaultAlignment()
13381335
? 0
13391336
: DbgTy.getAlignment().getValue() * SizeOfByte;
@@ -1448,7 +1445,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
14481445
SizeInBits, AlignInBits, Flags, nullptr,
14491446
llvm::dwarf::DW_LANG_Swift, MangledName);
14501447
StringRef Name = Decl->getName().str();
1451-
if (DbgTy.getTypeSize())
1448+
if (DbgTy.getTypeSizeInBits())
14521449
return createOpaqueStruct(Scope, Name, File, FwdDeclLine, SizeInBits,
14531450
AlignInBits, Flags, MangledName);
14541451
return DBuilder.createForwardDecl(
@@ -1688,7 +1685,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
16881685
// For TypeAlias types, the DeclContext for the aliased type is
16891686
// in the decl of the alias type.
16901687
DebugTypeInfo AliasedDbgTy(AliasedTy, DbgTy.getFragmentStorageType(),
1691-
DbgTy.getRawSize(), DbgTy.getAlignment(),
1688+
DbgTy.getRawSizeInBits(), DbgTy.getAlignment(),
16921689
DbgTy.hasDefaultAlignment(), false,
16931690
DbgTy.isSizeFragmentSize());
16941691
return DBuilder.createTypedef(getOrCreateType(AliasedDbgTy), MangledName,
@@ -1783,9 +1780,16 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
17831780
if (auto *DITy = getTypeOrNull(DbgTy.getType())) {
17841781
// FIXME: Enable this assertion.
17851782
#if SWIFT_DEBUGINFO_CACHE_VERIFICATION
1786-
if (auto CachedSize = DbgTy.getTypeSize()) {
1787-
if (unsigned Size = getSizeInBits(DITy))
1788-
assert(llvm::alignTo(Size, 8) / 8 == CachedSize->getValue());
1783+
if (auto CachedSizeInBits = DbgTy.getTypeSizeInBits()) {
1784+
if (unsigned SizeInBits = getSizeInBits(DITy)) {
1785+
if (SizeInBits != *CachedSizeInBits) {
1786+
DITy->dump();
1787+
DbgTy.dump();
1788+
llvm::errs() << "SizeInBits = " << SizeInBits << "\n";
1789+
llvm::errs() << "CachedSizeInBits = " << *CachedSizeInBits << "\n";
1790+
}
1791+
assert(SizeInBits == *CachedSizeInBits);
1792+
}
17891793
}
17901794
#endif
17911795
return DITy;
@@ -2530,8 +2534,8 @@ void IRGenDebugInfoImpl::emitVariableDeclaration(
25302534
if (DbgTy.getType()->hasOpenedExistential())
25312535
return;
25322536

2533-
if (!DbgTy.getTypeSize())
2534-
DbgTy.setSize(getStorageSize(IGM.DataLayout, Storage));
2537+
if (!DbgTy.getTypeSizeInBits())
2538+
DbgTy.setSizeInBits(getStorageSizeInBits(IGM.DataLayout, Storage));
25352539

25362540
auto *Scope = dyn_cast_or_null<llvm::DILocalScope>(getOrCreateScope(DS));
25372541
assert(Scope && "variable has no local scope");

0 commit comments

Comments
 (0)