Skip to content

Emit extra inhabitants in debug info for basic types #69335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions lib/IRGen/DebugTypeInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ DebugTypeInfo::DebugTypeInfo(swift::Type Ty, llvm::Type *FragmentStorageTy,
llvm::Optional<Size::int_type> SizeInBits,
Alignment Align, bool HasDefaultAlignment,
bool IsMetadata, bool SizeIsFragmentSize,
bool IsFixedBuffer)
bool IsFixedBuffer,
std::optional<uint32_t> NumExtraInhabitants)
: Type(Ty.getPointer()), FragmentStorageType(FragmentStorageTy),
SizeInBits(SizeInBits), Align(Align),
DefaultAlignment(HasDefaultAlignment), IsMetadataType(IsMetadata),
SizeIsFragmentSize(SizeIsFragmentSize), IsFixedBuffer(IsFixedBuffer) {
SizeInBits(SizeInBits), NumExtraInhabitants(NumExtraInhabitants),
Align(Align), DefaultAlignment(HasDefaultAlignment),
IsMetadataType(IsMetadata), SizeIsFragmentSize(SizeIsFragmentSize),
IsFixedBuffer(IsFixedBuffer) {
assert(Align.getValue() != 0);
}

Expand All @@ -52,17 +54,22 @@ DebugTypeInfo DebugTypeInfo::getFromTypeInfo(swift::Type Ty, const TypeInfo &TI,
bool IsFragmentTypeInfo) {
llvm::Optional<Size::int_type> SizeInBits;
llvm::Type *StorageType = TI.getStorageType();
std::optional<uint32_t> NumExtraInhabitants;
if (StorageType->isSized())
SizeInBits = IGM.DataLayout.getTypeSizeInBits(StorageType);
else if (TI.isFixedSize()) {
const FixedTypeInfo &FixTy = *cast<const FixedTypeInfo>(&TI);
Size::int_type Size = FixTy.getFixedSize().getValue() * 8;
SizeInBits = Size;
}
if (TI.isFixedSize()) {
const FixedTypeInfo &FixTy = *cast<const FixedTypeInfo>(&TI);
NumExtraInhabitants = FixTy.getFixedExtraInhabitantCount(IGM);
}
assert(TI.getStorageType() && "StorageType is a nullptr");
return DebugTypeInfo(Ty.getPointer(), StorageType, SizeInBits,
TI.getBestKnownAlignment(), ::hasDefaultAlignment(Ty),
false, IsFragmentTypeInfo);
false, IsFragmentTypeInfo, false, NumExtraInhabitants);
}

DebugTypeInfo DebugTypeInfo::getLocalVariable(VarDecl *Decl, swift::Type Ty,
Expand Down
7 changes: 6 additions & 1 deletion lib/IRGen/DebugTypeInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class DebugTypeInfo {
/// the storage type for undefined variables.
llvm::Type *FragmentStorageType = nullptr;
llvm::Optional<Size::int_type> SizeInBits;
std::optional<uint32_t> NumExtraInhabitants;
Alignment Align;
bool DefaultAlignment = true;
bool IsMetadataType = false;
Expand All @@ -57,7 +58,8 @@ class DebugTypeInfo {
llvm::Optional<Size::int_type> SizeInBits = {},
Alignment AlignInBytes = Alignment(1),
bool HasDefaultAlignment = true, bool IsMetadataType = false,
bool IsFragmentTypeInfo = false, bool IsFixedBuffer = false);
bool IsFragmentTypeInfo = false, bool IsFixedBuffer = false,
std::optional<uint32_t> NumExtraInhabitants = {});

/// Create type for a local variable.
static DebugTypeInfo getLocalVariable(VarDecl *Decl, swift::Type Ty,
Expand Down Expand Up @@ -119,6 +121,9 @@ class DebugTypeInfo {
bool hasDefaultAlignment() const { return DefaultAlignment; }
bool isSizeFragmentSize() const { return SizeIsFragmentSize; }
bool isFixedBuffer() const { return IsFixedBuffer; }
std::optional<uint32_t> getNumExtraInhabitants() const {
return NumExtraInhabitants;
}

bool operator==(DebugTypeInfo T) const;
bool operator!=(DebugTypeInfo T) const;
Expand Down
14 changes: 12 additions & 2 deletions lib/IRGen/IRGenDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
? 0
: DbgTy.getAlignment().getValue() * SizeOfByte;
unsigned Encoding = 0;
uint32_t NumExtraInhabitants = 0;
llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;

TypeBase *BaseTy = DbgTy.getType();
Expand All @@ -1386,13 +1387,17 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
Encoding = llvm::dwarf::DW_ATE_unsigned;
if (auto CompletedDbgTy = CompletedDebugTypeInfo::get(DbgTy))
SizeInBits = getSizeOfBasicType(*CompletedDbgTy);
if (auto DbgTyNumExtraInhabitants = DbgTy.getNumExtraInhabitants())
NumExtraInhabitants = *DbgTyNumExtraInhabitants;
break;
}

case TypeKind::BuiltinIntegerLiteral: {
Encoding = llvm::dwarf::DW_ATE_unsigned; // ?
if (auto CompletedDbgTy = CompletedDebugTypeInfo::get(DbgTy))
SizeInBits = getSizeOfBasicType(*CompletedDbgTy);
if (auto DbgTyNumExtraInhabitants = DbgTy.getNumExtraInhabitants())
NumExtraInhabitants = *DbgTyNumExtraInhabitants;
break;
}

Expand All @@ -1401,6 +1406,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
// Assuming that the bitwidth and FloatTy->getFPKind() are identical.
SizeInBits = FloatTy->getBitWidth();
Encoding = llvm::dwarf::DW_ATE_float;
if (auto DbgTyNumExtraInhabitants = DbgTy.getNumExtraInhabitants())
NumExtraInhabitants = *DbgTyNumExtraInhabitants;
break;
}

Expand Down Expand Up @@ -1726,7 +1733,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
DebugTypeInfo AliasedDbgTy(
AliasedTy, DbgTy.getFragmentStorageType(), DbgTy.getRawSizeInBits(),
DbgTy.getAlignment(), DbgTy.hasDefaultAlignment(), false,
DbgTy.isSizeFragmentSize(), DbgTy.isFixedBuffer());
DbgTy.isSizeFragmentSize(), DbgTy.isFixedBuffer(),
DbgTy.getNumExtraInhabitants());
return DBuilder.createTypedef(getOrCreateType(AliasedDbgTy), MangledName,
File, 0, Scope);
}
Expand Down Expand Up @@ -1774,7 +1782,9 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
DbgTy.getType()->dump(llvm::dbgs()); llvm::dbgs() << "\n");
MangledName = "<unknown>";
}
return DBuilder.createBasicType(MangledName, SizeInBits, Encoding);
return DBuilder.createBasicType(MangledName, SizeInBits, Encoding,
llvm::DINode::FlagZero,
NumExtraInhabitants);
}

/// Determine if there exists a name mangling for the given type.
Expand Down
2 changes: 1 addition & 1 deletion test/DebugInfo/bool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
func markUsed<T>(_ t: T) {}

// Int1 uses 1 bit, but is aligned at 8 bits.
// CHECK: !DIBasicType(name: "$sBi1_D", size: 1, encoding: DW_ATE_unsigned)
// CHECK: !DIBasicType(name: "$sBi1_D", size: 1, encoding: DW_ATE_unsigned, num_extra_inhabitants: 254)
// Bool has a fixed layout with a storage size of 1 byte and 7 "spare" bits.
// CHECK_G: !DICompositeType(tag: DW_TAG_structure_type, name: "Bool",
// CHECK_G-SAME: size: 8
Expand Down