Skip to content

[Debug Info] Do not emit children of bound generic types #80078

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
Mar 19, 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
42 changes: 25 additions & 17 deletions lib/IRGen/IRGenDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1226,8 +1226,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
return DITy;
}

std::pair<bool, Type>
getUnsubstituedType(Type Ty, StringRef MangledName) {
std::pair<bool, Type> getUnsubstitutedType(Type Ty, StringRef MangledName) {
if (!Ty)
return {false,{}};
// Go from Pair<Int, Double> to Pair<T, U>.
Expand All @@ -1252,7 +1251,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
llvm::DIScope *Scope, llvm::DIFile *File,
unsigned Line, llvm::DINode::DIFlags Flags) {
auto [IsUnsubstituted, UnsubstitutedTy] =
getUnsubstituedType(EnumTy, MangledName);
getUnsubstitutedType(EnumTy, MangledName);
auto UnsubstitutedDbgTy = DebugTypeInfo::getFromTypeInfo(
UnsubstitutedTy, IGM.getTypeInfoForUnlowered(UnsubstitutedTy), IGM);
if (IsUnsubstituted)
Expand Down Expand Up @@ -1301,7 +1300,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
Name);

auto [IsUnsubstitued, UnsubstitutedType] =
getUnsubstituedType(Type, MangledName);
getUnsubstitutedType(Type, MangledName);
auto UnsubstitutedDbgTy = DebugTypeInfo::getFromTypeInfo(
UnsubstitutedType, IGM.getTypeInfoForUnlowered(UnsubstitutedType), IGM);
if (IsUnsubstitued)
Expand All @@ -1326,19 +1325,24 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
}
}

// Recursive types such as `class A<B> { let a : A<A<B>> }` would produce an
// infinite chain of expansions for the type of `a`. Break these cycles by
// emitting any bound generics that still have type parameters as forward
// declarations.
if (Type->hasTypeParameter() || Type->hasPrimaryArchetype())
return createOpaqueStructWithSizedContainer(
Scope, Decl ? Decl->getNameStr() : "", File, Line, SizeInBits,
AlignInBits, Flags, MangledName, collectGenericParams(Type, true),
UnsubstitutedDITy);
// Generally, we don't emit members of a specialized bound generic, because
// these can be reconstructed by substituting the "template parameters" in
// the unspecialized type. We make an exception for inline arrays, because
// DWARF has special support for arrays.
if (Type->isInlineArray() && !Type->hasTypeParameter() &&
!Type->hasPrimaryArchetype())
// Create the substituted type.
return createStructType(Type, Decl, Scope, File, Line, SizeInBits,
AlignInBits, Flags, MangledName,
UnsubstitutedDITy);

// Create the substituted type.
return createStructType(Type, Decl, Scope, File, Line, SizeInBits,
AlignInBits, Flags, MangledName, UnsubstitutedDITy);
// Create the substituted type (without members).
llvm::DIType *SpecializedDITy = createOpaqueStructWithSizedContainer(
Scope, Decl ? Decl->getNameStr() : "", File, Line, SizeInBits,
AlignInBits, Flags, MangledName, collectGenericParams(Type),
UnsubstitutedDITy);
DBuilder.replaceTemporary(std::move(FwdDecl), SpecializedDITy);
return SpecializedDITy;
}

/// Create debug information for an enum with a raw type (enum E : Int {}).
Expand Down Expand Up @@ -1600,6 +1604,10 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {

llvm::Metadata *Elements[] = {DBuilder.createMemberType(
Scope, "", File, 0, SizeInBits, AlignInBits, 0, Flags, UniqueType)};
// FIXME: It's a limitation of LLVM that a forward declaration cannot have a
// specificationOf, so this attritbute is put on the sized container type
// instead. This is confusing consumers, and LLDB has to go out of its way
// to parse these confusing types as intended.
return DBuilder.createStructType(
Scope, "", File, Line, SizeInBits, AlignInBits, Flags,
/* DerivedFrom */ nullptr, DBuilder.getOrCreateArray(Elements),
Expand Down Expand Up @@ -2338,7 +2346,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
if (!isa<llvm::DICompositeType>(CachedType))
return true;
bool IsUnsubstituted =
getUnsubstituedType(DbgTy.getType(), getMangledName(DbgTy).Canonical)
getUnsubstitutedType(DbgTy.getType(), getMangledName(DbgTy).Canonical)
.first;
std::optional<uint64_t> SizeInBits;
if (!IsUnsubstituted)
Expand Down
8 changes: 5 additions & 3 deletions test/DebugInfo/BoundGenericStruct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public let s = S<Int>(t: 0)
// CHECK: ![[INTPARAM]] = !DITemplateTypeParameter(type: ![[INT:[0-9]+]])
// CHECK: ![[INT]] = !DICompositeType(tag: DW_TAG_structure_type, {{.*}}"$sSiD"

// DWARF-DAG: !DICompositeType(tag: DW_TAG_structure_type, {{.*}}templateParams: ![[PARAMS:[0-9]+]]{{.*}}identifier: "$s18BoundGenericStruct1SVySiGD"{{.*}}specification:
// DWARF-DAG: !DICompositeType(tag: DW_TAG_structure_type, {{.*}}name: "$s18BoundGenericStruct1SVySiGD"{{.*}}templateParams: ![[PARAMS:[0-9]+]]

// DWARF-DAG: ![[PARAMS]] = !{![[INTPARAM:[0-9]+]]}
// DWARF-DAG: ![[INTPARAM]] = !DITemplateTypeParameter(type: ![[INT:[0-9]+]])
Expand All @@ -34,14 +34,16 @@ public let inner = S2<Double>.Inner(t:4.2)
// CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "$s18BoundGenericStruct2S2VyxGD",
// CHECK-SAME: flags: DIFlagFwdDecl, runtimeLang: DW_LANG_Swift)

// DWARF-DAG: !DICompositeType(tag: DW_TAG_structure_type, name: "Inner", scope: ![[SCOPE1:[0-9]+]],{{.*}} size: 64, elements: ![[ELEMENTS1:[0-9]+]], {{.*}}templateParams: ![[PARAMS2:[0-9]+]], identifier: "$s18BoundGenericStruct2S2V5InnerVySd_GD", specification: ![[SPECIFICATION:[0-9]+]])
// DWARF-DAG: !DICompositeType(tag: DW_TAG_structure_type, name: "$s18BoundGenericStruct2S2V5InnerVySd_GD", scope: ![[SCOPE1:[0-9]+]], {{.*}}flags: DIFlagFwdDecl, runtimeLang: DW_LANG_Swift, templateParams: ![[PARAMS2:[0-9]+]])

// DWARF-DAG: ![[SCOPE1]] = !DICompositeType(tag: DW_TAG_structure_type, name: "$s18BoundGenericStruct2S2VyxGD",

// DWARF-DAG: ![[PARAMS2]] = !{![[PARAMS3:[0-9]+]]}
// DWARF-DAG: ![[PARAMS3]] = !DITemplateTypeParameter(type: ![[PARAMS4:[0-9]+]])
// DWARF-DAG: ![[PARAMS4]] = !DICompositeType(tag: DW_TAG_structure_type, name: "Double"{{.*}} size: 64, {{.*}}runtimeLang: DW_LANG_Swift,{{.*}} identifier: "$sSdD")

// DWARF-DAG: [[SPECIFICATION]] = !DICompositeType(tag: DW_TAG_structure_type, name: "Inner", {{.*}}runtimeLang: DW_LANG_Swift, identifier: "$s18BoundGenericStruct2S2V5InnerVyx_GD")
// DWARF-DAG: ![[SPECIFICATION:[0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type, name: "Inner", {{.*}}elements: ![[ELEMENTS1:[0-9]+]], runtimeLang: DW_LANG_Swift, identifier: "$s18BoundGenericStruct2S2V5InnerVyx_GD")
// DWARF-DAG: !DICompositeType(tag: DW_TAG_structure_type, {{.*}}line: 26, size: 64, {{.*}}specification: ![[SPECIFICATION]]

// DWARF-DAG: ![[ELEMENTS1]] = !{![[ELEMENTS2:[0-9]+]]}

Expand Down
4 changes: 2 additions & 2 deletions test/DebugInfo/embedded-recur-c-types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ typedef struct S2 {

public var v: UnsafeMutablePointer<S1_t>? = nil

// CHECK: !DICompositeType(tag: DW_TAG_structure_type, {{.*}} identifier: "$eSpySo4S1_taGD"
// CHECK: !DICompositeType(tag: DW_TAG_structure_type, {{.*}} identifier: "$eSpySo2S2VGD"
// CHECK-DAG: !DICompositeType(tag: DW_TAG_structure_type, {{.*}}name: "$eSpySo4S1_taGD"
// CHECK-DAG: !DICompositeType(tag: DW_TAG_structure_type, {{.*}}name: "$eSpySo2S2VGD"
10 changes: 5 additions & 5 deletions test/DebugInfo/typealias-recursive.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ struct Traits32: TraitsProtocol {
let v : MyClass<Traits32>? = nil


// This typedef is not necessary from a semantic point of view, but the result
// of a trade-off done to preserve type sugar (= not generally canonicalizing types).
// CHECK-DAG: !DIDerivedType(tag: DW_TAG_typedef, name: "$s4main7MyClassCyAC6TraitsayAA8Traits32V_GGSgD", {{.*}}baseType: ![[CANONICAL:[0-9]+]])
// CHECK-DAG: ![[CANONICAL]] = !DICompositeType(tag: DW_TAG_structure_type, {{.*}}identifier: "$s4main7MyClassCyAA8Traits32VGSgD", specification: ![[OPTIONAL:[0-9]+]])
// CHECK-DAG: ![[CANONICAL:.*]] = !DICompositeType(tag: DW_TAG_structure_type, {{.*}}identifier: "$s4main7MyClassCyAA8Traits32VGSgD", specification: ![[OPTIONAL:[0-9]+]])

// CHECK-DAG: ![[OPTIONAL]] = !DICompositeType(tag: DW_TAG_structure_type, name: "Optional"

// CHECK-DAG: !DICompositeType(tag: DW_TAG_structure_type, name: "MyClass", {{.*}}templateParams: ![[PARAMS:[0-9]+]], identifier: "$s4main7MyClassCyAA8Traits32VGD", specification: ![[SPEC:[0-9]+]])
// CHECK-DAG: ![[CONTAINER:.*]] = !DICompositeType(tag: DW_TAG_structure_type, {{.*}}line: 7, {{.*}}elements: ![[ELTS:[0-9]+]], runtimeLang: DW_LANG_Swift, specification: ![[SPEC:[0-9]+]])
// CHECK-DAG: ![[ELTS]] = !{![[MEMBER:[0-9]+]]}
// CHECK-DAG: ![[MEMBER]] = !DIDerivedType(tag: DW_TAG_member, {{.*}}baseType: ![[UNSIZED:[0-9]+]]
// CHECK-DAG: ![[UNSIZED]] = !DICompositeType(tag: DW_TAG_structure_type, name: "$s4main7MyClassCyAA8Traits32VGD", {{.*}}flags: DIFlagFwdDecl, runtimeLang: DW_LANG_Swift, templateParams: ![[PARAMS:[0-9]+]])
// CHECK-DAG: ![[PARAMS]] = !{![[PARAM:[0-9]+]]}
// CHECK-DAG: ![[PARAM]] = !DITemplateTypeParameter(type: ![[TRAITS32:[0-9]+]])
// CHECK-DAG: ![[TRAITS32]] = !DICompositeType(tag: DW_TAG_structure_type, name: "Traits32"
Expand Down
6 changes: 5 additions & 1 deletion test/DebugInfo/typealias_indirect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
// we cannot preserve that LocalAlias = Bool.

// CHECK: !DIDerivedType(tag: DW_TAG_typedef, name: "$s1a10ClassAliasaD"{{.*}}baseType: ![[LOCAL_BOOLTY:[0-9]+]]
// CHECK: ![[LOCAL_BOOLTY]] = !DICompositeType(tag: DW_TAG_structure_type, name: "MyClass", {{.*}}identifier: "$s1a7MyClassCyAA10LocalAliasaSbGD"
// CHECK: ![[LOCAL_BOOLTY]] = !DICompositeType(tag: DW_TAG_structure_type, {{.*}}elements: ![[ELTS:[0-9]+]], runtimeLang: DW_LANG_Swift, specification: ![[SPEC:[0-9]+]])
// CHECK-DAG: ![[ELTS]] = !{![[MEMBER:[0-9]+]]}
// CHECK-DAG: ![[MEMBER]] = !DIDerivedType(tag: DW_TAG_member, {{.*}}baseType: ![[UNSIZED:[0-9]+]]
// CHECK-DAG: ![[UNSIZED]] = !DICompositeType(tag: DW_TAG_structure_type, name: "$s1a7MyClassCyAA10LocalAliasaSbGD"
// CHECK-DAG: ![[SPEC]] = !DICompositeType(tag: DW_TAG_structure_type, name: "MyClass"{{.*}}identifier: "$s1a7MyClassCyxq_GD")

// FIXME: !DIDerivedType(tag: DW_TAG_typedef, name: "$s1a10LocalAliasaD", {{.*}}baseType: ![[BASETY:[0-9]+]]
// FIXME: ![[BASETY]]{{.*}}$sSbD
Expand Down