Skip to content

[Debug info] Retain all type aliases in asttypes mode #78901

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 3 commits into from
Jan 29, 2025
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
60 changes: 24 additions & 36 deletions lib/IRGen/DebugTypeInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@
using namespace swift;
using namespace irgen;

DebugTypeInfo::DebugTypeInfo(swift::Type Ty, llvm::Type *FragmentStorageTy,
Alignment Align, bool HasDefaultAlignment,
bool IsMetadata, bool IsFixedBuffer,
DebugTypeInfo::DebugTypeInfo(swift::Type Ty, Alignment Align,
bool HasDefaultAlignment, bool IsMetadata,
bool IsFixedBuffer,
std::optional<uint32_t> NumExtraInhabitants)
: Type(Ty.getPointer()), FragmentStorageType(FragmentStorageTy),
NumExtraInhabitants(NumExtraInhabitants),
: Type(Ty.getPointer()), NumExtraInhabitants(NumExtraInhabitants),
Align(Align), DefaultAlignment(HasDefaultAlignment),
IsMetadataType(IsMetadata), IsFixedBuffer(IsFixedBuffer) {
assert(Align.getValue() != 0);
Expand All @@ -49,15 +48,14 @@ static bool hasDefaultAlignment(swift::Type Ty) {

DebugTypeInfo DebugTypeInfo::getFromTypeInfo(swift::Type Ty, const TypeInfo &TI,
IRGenModule &IGM) {
llvm::Type *StorageType = TI.getStorageType();
std::optional<uint32_t> NumExtraInhabitants;
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,
TI.getBestKnownAlignment(), ::hasDefaultAlignment(Ty),
return DebugTypeInfo(Ty.getPointer(), TI.getBestKnownAlignment(),
::hasDefaultAlignment(Ty),
/* IsMetadataType = */ false,
/* IsFixedBuffer = */ false, NumExtraInhabitants);
}
Expand All @@ -81,25 +79,21 @@ DebugTypeInfo DebugTypeInfo::getLocalVariable(VarDecl *Decl, swift::Type Ty,
return getFromTypeInfo(Type, Info, IGM);
}

DebugTypeInfo DebugTypeInfo::getGlobalMetadata(swift::Type Ty,
llvm::Type *StorageTy, Size size,
DebugTypeInfo DebugTypeInfo::getGlobalMetadata(swift::Type Ty, Size size,
Alignment align) {
DebugTypeInfo DbgTy(Ty.getPointer(), StorageTy, align,
DebugTypeInfo DbgTy(Ty.getPointer(), align,
/* HasDefaultAlignment = */ true,
/* IsMetadataType = */ false);
assert(StorageTy && "StorageType is a nullptr");
assert(!DbgTy.isContextArchetype() &&
"type metadata cannot contain an archetype");
return DbgTy;
}

DebugTypeInfo DebugTypeInfo::getTypeMetadata(swift::Type Ty,
llvm::Type *StorageTy, Size size,
DebugTypeInfo DebugTypeInfo::getTypeMetadata(swift::Type Ty, Size size,
Alignment align) {
DebugTypeInfo DbgTy(Ty.getPointer(), StorageTy, align,
DebugTypeInfo DbgTy(Ty.getPointer(), align,
/* HasDefaultAlignment = */ true,
/* IsMetadataType = */ true);
assert(StorageTy && "StorageType is a nullptr");
assert(!DbgTy.isContextArchetype() &&
"type metadata cannot contain an archetype");
return DbgTy;
Expand All @@ -111,7 +105,6 @@ DebugTypeInfo DebugTypeInfo::getForwardDecl(swift::Type Ty) {
}

DebugTypeInfo DebugTypeInfo::getGlobal(SILGlobalVariable *GV,
llvm::Type *FragmentStorageType,
IRGenModule &IGM) {
// Prefer the original, potentially sugared version of the type if
// the type hasn't been mucked with by an optimization pass.
Expand All @@ -124,16 +117,14 @@ DebugTypeInfo DebugTypeInfo::getGlobal(SILGlobalVariable *GV,
}
auto &TI = IGM.getTypeInfoForUnlowered(Type);
DebugTypeInfo DbgTy = getFromTypeInfo(Type, TI, IGM);
assert(FragmentStorageType && "FragmentStorageType is a nullptr");
assert(!DbgTy.isContextArchetype() &&
"type of global variable cannot be an archetype");
return DbgTy;
}

DebugTypeInfo
DebugTypeInfo::getGlobalFixedBuffer(SILGlobalVariable *GV,
llvm::Type *FragmentStorageType,
Size SizeInBytes, Alignment Align) {
DebugTypeInfo DebugTypeInfo::getGlobalFixedBuffer(SILGlobalVariable *GV,
Size SizeInBytes,
Alignment Align) {
// Prefer the original, potentially sugared version of the type if
// the type hasn't been mucked with by an optimization pass.
auto LowTy = GV->getLoweredType().getASTType();
Expand All @@ -143,23 +134,18 @@ DebugTypeInfo::getGlobalFixedBuffer(SILGlobalVariable *GV,
if (DeclType->isEqual(LowTy))
Type = DeclType.getPointer();
}
DebugTypeInfo DbgTy(Type, FragmentStorageType,
Align, ::hasDefaultAlignment(Type),
DebugTypeInfo DbgTy(Type, Align, ::hasDefaultAlignment(Type),
/* IsMetadataType = */ false, /* IsFixedBuffer = */ true);
assert(FragmentStorageType && "FragmentStorageType is a nullptr");
assert(!DbgTy.isContextArchetype() &&
"type of global variable cannot be an archetype");
return DbgTy;
}

DebugTypeInfo DebugTypeInfo::getObjCClass(ClassDecl *theClass,
llvm::Type *FragmentStorageType,
Size SizeInBytes, Alignment align) {
DebugTypeInfo DbgTy(theClass->getInterfaceType().getPointer(),
FragmentStorageType, align,
DebugTypeInfo DebugTypeInfo::getObjCClass(ClassDecl *theClass, Size SizeInBytes,
Alignment align) {
DebugTypeInfo DbgTy(theClass->getInterfaceType().getPointer(), align,
/* HasDefaultAlignment = */ true,
/* IsMetadataType = */ false);
assert(FragmentStorageType && "FragmentStorageType is a nullptr");
assert(!DbgTy.isContextArchetype() &&
"type of objc class cannot be an archetype");
return DbgTy;
Expand Down Expand Up @@ -193,23 +179,25 @@ TypeDecl *DebugTypeInfo::getDecl() const {
return nullptr;
}

bool DebugTypeInfo::isForwardDecl() const {
return isNull() || (!isa<TypeAliasType>(getType()));
}

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVM_DUMP_METHOD void DebugTypeInfo::dump() const {
llvm::errs() << "[";
llvm::errs() << "Alignment " << Align.getValue() << "] ";
if (auto *Type = getType())
Type->dump(llvm::errs());

if (FragmentStorageType) {
llvm::errs() << "FragmentStorageType=";
FragmentStorageType->dump();
} else
if (isForwardDecl())
llvm::errs() << "forward-declared\n";
}
#endif

std::optional<CompletedDebugTypeInfo>
CompletedDebugTypeInfo::getFromTypeInfo(swift::Type Ty, const TypeInfo &Info, IRGenModule &IGM) {
CompletedDebugTypeInfo::getFromTypeInfo(swift::Type Ty, const TypeInfo &Info,
IRGenModule &IGM) {
auto *StorageType = IGM.getStorageTypeForUnlowered(Ty);
std::optional<uint64_t> SizeInBits;
if (StorageType->isSized())
Expand Down
26 changes: 10 additions & 16 deletions lib/IRGen/DebugTypeInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class DebugTypeInfo {
TypeBase *Type = nullptr;
/// Needed to determine the size of basic types and to determine
/// the storage type for undefined variables.
llvm::Type *FragmentStorageType = nullptr;
std::optional<uint32_t> NumExtraInhabitants;
Alignment Align;
bool DefaultAlignment = true;
Expand All @@ -52,8 +51,7 @@ class DebugTypeInfo {

public:
DebugTypeInfo() = default;
DebugTypeInfo(swift::Type Ty, llvm::Type *StorageTy = nullptr,
Alignment AlignInBytes = Alignment(1),
DebugTypeInfo(swift::Type Ty, Alignment AlignInBytes = Alignment(1),
bool HasDefaultAlignment = true, bool IsMetadataType = false,
bool IsFixedBuffer = false,
std::optional<uint32_t> NumExtraInhabitants = {});
Expand All @@ -62,11 +60,11 @@ class DebugTypeInfo {
static DebugTypeInfo getLocalVariable(VarDecl *Decl, swift::Type Ty,
const TypeInfo &Info, IRGenModule &IGM);
/// Create type for global type metadata.
static DebugTypeInfo getGlobalMetadata(swift::Type Ty, llvm::Type *StorageTy,
Size size, Alignment align);
static DebugTypeInfo getGlobalMetadata(swift::Type Ty, Size size,
Alignment align);
/// Create type for an artificial metadata variable.
static DebugTypeInfo getTypeMetadata(swift::Type Ty, llvm::Type *StorageTy,
Size size, Alignment align);
static DebugTypeInfo getTypeMetadata(swift::Type Ty, Size size,
Alignment align);

/// Create a forward declaration for a type whose size is unknown.
static DebugTypeInfo getForwardDecl(swift::Type Ty);
Expand All @@ -75,14 +73,11 @@ class DebugTypeInfo {
static DebugTypeInfo getFromTypeInfo(swift::Type Ty, const TypeInfo &Info,
IRGenModule &IGM);
/// Global variables.
static DebugTypeInfo getGlobal(SILGlobalVariable *GV,
llvm::Type *StorageType, IRGenModule &IGM);
static DebugTypeInfo getGlobal(SILGlobalVariable *GV, IRGenModule &IGM);
static DebugTypeInfo getGlobalFixedBuffer(SILGlobalVariable *GV,
llvm::Type *StorageType,
Size SizeInBytes, Alignment align);
/// ObjC classes.
static DebugTypeInfo getObjCClass(ClassDecl *theClass,
llvm::Type *StorageType, Size size,
static DebugTypeInfo getObjCClass(ClassDecl *theClass, Size size,
Alignment align);
/// Error type.
static DebugTypeInfo getErrorResult(swift::Type Ty, IRGenModule &IGM);
Expand All @@ -100,10 +95,9 @@ class DebugTypeInfo {
return false;
}

llvm::Type *getFragmentStorageType() const { return FragmentStorageType; }
Alignment getAlignment() const { return Align; }
bool isNull() const { return Type == nullptr; }
bool isForwardDecl() const { return FragmentStorageType == nullptr; }
bool isNull() const { return !Type; }
bool isForwardDecl() const;
bool isMetadataType() const { return IsMetadataType; }
bool hasDefaultAlignment() const { return DefaultAlignment; }
bool isFixedBuffer() const { return IsFixedBuffer; }
Expand Down Expand Up @@ -151,7 +145,7 @@ template <> struct DenseMapInfo<swift::irgen::DebugTypeInfo> {
}
static swift::irgen::DebugTypeInfo getTombstoneKey() {
return swift::irgen::DebugTypeInfo(
llvm::DenseMapInfo<swift::TypeBase *>::getTombstoneKey(), nullptr,
llvm::DenseMapInfo<swift::TypeBase *>::getTombstoneKey(),
swift::irgen::Alignment(), /* HasDefaultAlignment = */ false);
}
static unsigned getHashValue(swift::irgen::DebugTypeInfo Val) {
Expand Down
30 changes: 13 additions & 17 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2741,10 +2741,9 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
}

DebugTypeInfo DbgTy =
inFixedBuffer
? DebugTypeInfo::getGlobalFixedBuffer(
var, globalTy, fixedSize, fixedAlignment)
: DebugTypeInfo::getGlobal(var, globalTy, *this);
inFixedBuffer ? DebugTypeInfo::getGlobalFixedBuffer(var, fixedSize,
fixedAlignment)
: DebugTypeInfo::getGlobal(var, *this);

gvar = createVariable(*this, link, globalTy, fixedAlignment, DbgTy, loc, name);
}
Expand Down Expand Up @@ -4738,8 +4737,8 @@ Address IRGenModule::getAddrOfObjCClassRef(ClassDecl *theClass) {
assert(ObjCInterop && "getting address of ObjC class ref in no-interop mode");

LinkEntity entity = LinkEntity::forObjCClassRef(theClass);
auto DbgTy = DebugTypeInfo::getObjCClass(
theClass, ObjCClassPtrTy, getPointerSize(), getPointerAlignment());
auto DbgTy = DebugTypeInfo::getObjCClass(theClass, getPointerSize(),
getPointerAlignment());
auto addr = getAddrOfLLVMVariable(entity, ConstantInit(), DbgTy);

// Define it lazily.
Expand All @@ -4764,8 +4763,8 @@ llvm::Constant *IRGenModule::getAddrOfObjCClass(ClassDecl *theClass,
assert(ObjCInterop && "getting address of ObjC class in no-interop mode");
assert(!theClass->isForeign());
LinkEntity entity = LinkEntity::forObjCClass(theClass);
auto DbgTy = DebugTypeInfo::getObjCClass(
theClass, ObjCClassPtrTy, getPointerSize(), getPointerAlignment());
auto DbgTy = DebugTypeInfo::getObjCClass(theClass, getPointerSize(),
getPointerAlignment());
auto addr = getAddrOfLLVMVariable(entity, forDefinition, DbgTy);
return addr;
}
Expand All @@ -4784,8 +4783,8 @@ IRGenModule::getAddrOfMetaclassObject(ClassDecl *decl,
? LinkEntity::forObjCMetaclass(decl)
: LinkEntity::forSwiftMetaclassStub(decl);

auto DbgTy = DebugTypeInfo::getObjCClass(
decl, ObjCClassPtrTy, getPointerSize(), getPointerAlignment());
auto DbgTy = DebugTypeInfo::getObjCClass(decl, getPointerSize(),
getPointerAlignment());
auto addr = getAddrOfLLVMVariable(entity, forDefinition, DbgTy);
return addr;
}
Expand All @@ -4800,8 +4799,8 @@ IRGenModule::getAddrOfCanonicalSpecializedGenericMetaclassObject(
auto entity =
LinkEntity::forSpecializedGenericSwiftMetaclassStub(concreteType);

auto DbgTy = DebugTypeInfo::getObjCClass(
theClass, ObjCClassPtrTy, getPointerSize(), getPointerAlignment());
auto DbgTy = DebugTypeInfo::getObjCClass(theClass, getPointerSize(),
getPointerAlignment());
auto addr = getAddrOfLLVMVariable(entity, forDefinition, DbgTy);
return addr;
}
Expand Down Expand Up @@ -5149,10 +5148,8 @@ llvm::GlobalValue *IRGenModule::defineTypeMetadata(
TypeMetadataAddress::AddressPoint);
}

auto DbgTy = DebugTypeInfo::getGlobalMetadata(
MetatypeType::get(concreteType),
entity.getDefaultDeclarationType(*this)->getPointerTo(), Size(0),
Alignment(1));
auto DbgTy = DebugTypeInfo::getGlobalMetadata(MetatypeType::get(concreteType),
Size(0), Alignment(1));

// Define the variable.
llvm::GlobalVariable *var = cast<llvm::GlobalVariable>(
Expand Down Expand Up @@ -5316,7 +5313,6 @@ IRGenModule::getAddrOfTypeMetadata(CanType concreteType,
break;
}
DbgTy = DebugTypeInfo::getGlobalMetadata(MetatypeType::get(concreteType),
defaultVarTy->getPointerTo(),
Size(0), Alignment(1));

ConstantReference addr;
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void IRGenModule::emitSILGlobalVariable(SILGlobalVariable *var) {
// variable directly, don't actually emit it; just return undef.
if (ti.isKnownEmpty(expansion)) {
if (DebugInfo && var->getDecl()) {
auto DbgTy = DebugTypeInfo::getGlobal(var, Int8Ty, *this);
auto DbgTy = DebugTypeInfo::getGlobal(var, *this);
DebugInfo->emitGlobalVariableDeclaration(nullptr, var->getDecl()->getName().str(),
"", DbgTy,
var->getLinkage() != SILLinkage::Public &&
Expand Down
Loading