Skip to content

Commit 57b1664

Browse files
committed
Debug Info: Distinguish between types of unknown size and types with size=0.
DebugInfoTypes didn't properly distinguish between types with an unknown size and used 0 as the default value. This can cause types to show up in DWARF with a size of 0, which will prevent LLDB from looking at the type further. In practice, this meant that many resilient types (for example, SwiftUI Views) couldn't be displayed in the debugger and just showed up as empty. This patch does three things: 0. Make size in DebugTypeInfo optional. 1. Introduce CompletedDebugTypeInfo, a subclass of DebugTypeInfo where size is guaranteed to be non-empty. 2. Change some APIs that only make sense with a size to use CompletedDebugTypeInfo. This caused some churn because I needed to refactor the get*Elements() functions back into their callers to facilitate the new early-exit path. These functions are only called when compiling with -gdwarf-types. 3. Change createOpaqueStructWithSizedContainer() to create a size-less forward declaration instead of a zero-sized type for the inner type. rdar://76973844 (cherry picked from commit b8e6eeb)
1 parent 274adc9 commit 57b1664

File tree

5 files changed

+182
-140
lines changed

5 files changed

+182
-140
lines changed

lib/IRGen/DebugTypeInfo.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
using namespace swift;
2525
using namespace irgen;
2626

27-
DebugTypeInfo::DebugTypeInfo(swift::Type Ty, llvm::Type *StorageTy, Size size,
28-
Alignment align, bool HasDefaultAlignment,
29-
bool IsMetadata)
27+
DebugTypeInfo::DebugTypeInfo(swift::Type Ty, llvm::Type *StorageTy,
28+
Optional<Size> size, Alignment align,
29+
bool HasDefaultAlignment, bool IsMetadata)
3030
: Type(Ty.getPointer()), StorageType(StorageTy), size(size), align(align),
3131
DefaultAlignment(HasDefaultAlignment), IsMetadataType(IsMetadata) {
3232
assert(align.getValue() != 0);
@@ -43,14 +43,10 @@ static bool hasDefaultAlignment(swift::Type Ty) {
4343

4444
DebugTypeInfo DebugTypeInfo::getFromTypeInfo(swift::Type Ty,
4545
const TypeInfo &Info) {
46-
Size size;
46+
Optional<Size> size;
4747
if (Info.isFixedSize()) {
4848
const FixedTypeInfo &FixTy = *cast<const FixedTypeInfo>(&Info);
4949
size = FixTy.getFixedSize();
50-
} else {
51-
// FIXME: Handle NonFixedTypeInfo here or assert that we won't
52-
// encounter one.
53-
size = Size(0);
5450
}
5551
assert(Info.getStorageType() && "StorageType is a nullptr");
5652
return DebugTypeInfo(Ty.getPointer(), Info.getStorageType(), size,
@@ -160,10 +156,12 @@ TypeDecl *DebugTypeInfo::getDecl() const {
160156

161157
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
162158
LLVM_DUMP_METHOD void DebugTypeInfo::dump() const {
163-
llvm::errs() << "[Size " << size.getValue() << " Alignment "
164-
<< align.getValue() << "] ";
165-
159+
llvm::errs() << "[";
160+
if (size)
161+
llvm::errs() << "Size " << size->getValue() << " ";
162+
llvm::errs() << "Alignment " << align.getValue() << "] ";
166163
getType()->dump(llvm::errs());
164+
167165
if (StorageType) {
168166
llvm::errs() << "StorageType=";
169167
StorageType->dump();

lib/IRGen/DebugTypeInfo.h

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,23 @@ class TypeInfo;
3636
/// This data structure holds everything needed to emit debug info
3737
/// for a type.
3838
class DebugTypeInfo {
39+
protected:
3940
/// The type we need to emit may be different from the type
4041
/// mentioned in the Decl, for example, stripped of qualifiers.
4142
TypeBase *Type = nullptr;
4243
/// Needed to determine the size of basic types and to determine
4344
/// the storage type for undefined variables.
4445
llvm::Type *StorageType = nullptr;
45-
Size size = Size(0);
46-
Alignment align = Alignment();
46+
Optional<Size> size;
47+
Alignment align;
4748
bool DefaultAlignment = true;
4849
bool IsMetadataType = false;
4950

5051
public:
5152
DebugTypeInfo() = default;
52-
DebugTypeInfo(swift::Type Ty, llvm::Type *StorageTy, Size SizeInBytes,
53-
Alignment AlignInBytes, bool HasDefaultAlignment,
54-
bool IsMetadataType);
53+
DebugTypeInfo(swift::Type Ty, llvm::Type *StorageTy,
54+
Optional<Size> SizeInBytes, Alignment AlignInBytes,
55+
bool HasDefaultAlignment, bool IsMetadataType);
5556

5657
/// Create type for a local variable.
5758
static DebugTypeInfo getLocalVariable(VarDecl *Decl,
@@ -92,24 +93,44 @@ class DebugTypeInfo {
9293
}
9394

9495
llvm::Type *getStorageType() const {
95-
assert((StorageType || size.isZero()) &&
96-
"only defined types may have a size");
96+
if (size && size->isZero())
97+
assert(StorageType && "only defined types may have a size");
9798
return StorageType;
9899
}
99-
Size getSize() const { return size; }
100+
Optional<Size> getSize() const { return size; }
100101
void setSize(Size NewSize) { size = NewSize; }
101102
Alignment getAlignment() const { return align; }
102103
bool isNull() const { return Type == nullptr; }
103104
bool isForwardDecl() const { return StorageType == nullptr; }
104105
bool isMetadataType() const { return IsMetadataType; }
105106
bool hasDefaultAlignment() const { return DefaultAlignment; }
106-
107+
107108
bool operator==(DebugTypeInfo T) const;
108109
bool operator!=(DebugTypeInfo T) const;
109110
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
110111
LLVM_DUMP_METHOD void dump() const;
111112
#endif
112113
};
114+
115+
/// A DebugTypeInfo with a defined size (that may be 0).
116+
class CompletedDebugTypeInfo : public DebugTypeInfo {
117+
CompletedDebugTypeInfo(DebugTypeInfo DbgTy) : DebugTypeInfo(DbgTy) {}
118+
public:
119+
static Optional<CompletedDebugTypeInfo> get(DebugTypeInfo DbgTy) {
120+
if (!DbgTy.getSize())
121+
return {};
122+
return CompletedDebugTypeInfo(DbgTy);
123+
}
124+
125+
static Optional<CompletedDebugTypeInfo>
126+
getFromTypeInfo(swift::Type Ty, const TypeInfo &Info) {
127+
return CompletedDebugTypeInfo::get(
128+
DebugTypeInfo::getFromTypeInfo(Ty, Info));
129+
}
130+
131+
Size::int_type getSizeValue() const { return size.getValue().getValue(); }
132+
};
133+
113134
}
114135
}
115136

0 commit comments

Comments
 (0)