Skip to content

Commit e2f05f0

Browse files
committed
Only emit DWARF types for function pointers when -gdwarf-types is specified.
1 parent 6b1263b commit e2f05f0

File tree

5 files changed

+83
-61
lines changed

5 files changed

+83
-61
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ enum class IRGenDebugInfoKind : unsigned {
5050
LineTables, /// Line tables only.
5151
ASTTypes, /// Line tables + AST type references.
5252
DwarfTypes, /// Line tables + AST type references + DWARF types.
53-
Normal = DwarfTypes /// The setting LLDB prefers.
53+
Normal = ASTTypes /// The setting LLDB prefers.
5454
};
5555

5656
enum class IRGenEmbedMode : unsigned {

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,55 @@ llvm::DIType *IRGenDebugInfo::createDoublePointerSizedStruct(
12531253
llvm::dwarf::DW_LANG_Swift, nullptr, MangledName);
12541254
}
12551255

1256+
llvm::DIType *
1257+
IRGenDebugInfo::createFunctionPointer(DebugTypeInfo DbgTy, llvm::DIScope *Scope,
1258+
unsigned SizeInBits, unsigned AlignInBits,
1259+
unsigned Flags, StringRef MangledName) {
1260+
auto FwdDecl = llvm::TempDINode(DBuilder.createReplaceableCompositeType(
1261+
llvm::dwarf::DW_TAG_subroutine_type, MangledName, Scope, MainFile, 0,
1262+
llvm::dwarf::DW_LANG_Swift, SizeInBits, AlignInBits, Flags, MangledName));
1263+
1264+
auto TH = llvm::TrackingMDNodeRef(FwdDecl.get());
1265+
DITypeCache[DbgTy.getType()] = TH;
1266+
1267+
CanSILFunctionType FunTy;
1268+
TypeBase *BaseTy = DbgTy.getType();
1269+
if (auto *SILFnTy = dyn_cast<SILFunctionType>(BaseTy))
1270+
FunTy = CanSILFunctionType(SILFnTy);
1271+
// FIXME: Handling of generic parameters in SIL type lowering is in flux.
1272+
// DebugInfo doesn't appear to care about the generic context, so just
1273+
// throw it away before lowering.
1274+
else if (isa<GenericFunctionType>(BaseTy) ||
1275+
isa<PolymorphicFunctionType>(BaseTy)) {
1276+
auto *fTy = cast<AnyFunctionType>(BaseTy);
1277+
auto *nongenericTy =
1278+
FunctionType::get(fTy->getInput(), fTy->getResult(), fTy->getExtInfo());
1279+
1280+
FunTy = IGM.getLoweredType(nongenericTy).castTo<SILFunctionType>();
1281+
} else
1282+
FunTy = IGM.getLoweredType(BaseTy).castTo<SILFunctionType>();
1283+
auto Params = createParameterTypes(FunTy, DbgTy.getDeclContext());
1284+
1285+
auto FnTy = DBuilder.createSubroutineType(Params, Flags);
1286+
llvm::DIType *DITy;
1287+
if (FunTy->getRepresentation() == SILFunctionType::Representation::Thick) {
1288+
if (SizeInBits == 2 * CI.getTargetInfo().getPointerWidth(0))
1289+
// This is a FunctionPairTy: { i8*, %swift.refcounted* }.
1290+
DITy = createDoublePointerSizedStruct(Scope, MangledName, FnTy, MainFile,
1291+
0, Flags, MangledName);
1292+
else
1293+
// This is a generic function as noted above.
1294+
DITy = createOpaqueStruct(Scope, MangledName, MainFile, 0, SizeInBits,
1295+
AlignInBits, Flags, MangledName);
1296+
} else {
1297+
assert(SizeInBits == CI.getTargetInfo().getPointerWidth(0));
1298+
DITy = createPointerSizedStruct(Scope, MangledName, FnTy, MainFile, 0,
1299+
Flags, MangledName);
1300+
}
1301+
DBuilder.replaceTemporary(std::move(FwdDecl), DITy);
1302+
return DITy;
1303+
}
1304+
12561305
llvm::DIType *
12571306
IRGenDebugInfo::createOpaqueStruct(llvm::DIScope *Scope, StringRef Name,
12581307
llvm::DIFile *File, unsigned Line,
@@ -1545,51 +1594,13 @@ llvm::DIType *IRGenDebugInfo::createType(DebugTypeInfo DbgTy,
15451594
case TypeKind::SILFunction:
15461595
case TypeKind::Function:
15471596
case TypeKind::PolymorphicFunction:
1548-
case TypeKind::GenericFunction: {
1549-
auto FwdDecl = llvm::TempDINode(DBuilder.createReplaceableCompositeType(
1550-
llvm::dwarf::DW_TAG_subroutine_type, MangledName, Scope, File, 0,
1551-
llvm::dwarf::DW_LANG_Swift, SizeInBits, AlignInBits, Flags,
1552-
MangledName));
1553-
1554-
auto TH = llvm::TrackingMDNodeRef(FwdDecl.get());
1555-
DITypeCache[DbgTy.getType()] = TH;
1556-
1557-
CanSILFunctionType FunTy;
1558-
if (auto *SILFnTy = dyn_cast<SILFunctionType>(BaseTy))
1559-
FunTy = CanSILFunctionType(SILFnTy);
1560-
// FIXME: Handling of generic parameters in SIL type lowering is in flux.
1561-
// DebugInfo doesn't appear to care about the generic context, so just
1562-
// throw it away before lowering.
1563-
else if (isa<GenericFunctionType>(BaseTy) ||
1564-
isa<PolymorphicFunctionType>(BaseTy)) {
1565-
auto *fTy = cast<AnyFunctionType>(BaseTy);
1566-
auto *nongenericTy = FunctionType::get(fTy->getInput(), fTy->getResult(),
1567-
fTy->getExtInfo());
1568-
1569-
FunTy = IGM.getLoweredType(nongenericTy).castTo<SILFunctionType>();
1570-
} else
1571-
FunTy =
1572-
IGM.getLoweredType(BaseTy).castTo<SILFunctionType>();
1573-
auto Params = createParameterTypes(FunTy, DbgTy.getDeclContext());
1574-
1575-
auto FnTy = DBuilder.createSubroutineType(Params, Flags);
1576-
llvm::DIType *DITy;
1577-
if (FunTy->getRepresentation() == SILFunctionType::Representation::Thick) {
1578-
if (SizeInBits == 2 * CI.getTargetInfo().getPointerWidth(0))
1579-
// This is a FunctionPairTy: { i8*, %swift.refcounted* }.
1580-
DITy = createDoublePointerSizedStruct(Scope, MangledName, FnTy,
1581-
MainFile, 0, Flags, MangledName);
1582-
else
1583-
// This is a generic function as noted above.
1584-
DITy = createOpaqueStruct(Scope, MangledName, MainFile, 0, SizeInBits,
1585-
AlignInBits, Flags, MangledName);
1586-
} else {
1587-
assert(SizeInBits == CI.getTargetInfo().getPointerWidth(0));
1588-
DITy = createPointerSizedStruct(Scope, MangledName, FnTy, MainFile, 0,
1589-
Flags, MangledName);
1590-
}
1591-
DBuilder.replaceTemporary(std::move(FwdDecl), DITy);
1592-
return DITy;
1597+
case TypeKind::GenericFunction: {
1598+
if (Opts.DebugInfoKind > IRGenDebugInfoKind::ASTTypes)
1599+
return createFunctionPointer(DbgTy, Scope, SizeInBits, AlignInBits, Flags,
1600+
MangledName);
1601+
else
1602+
return createOpaqueStruct(Scope, MangledName, MainFile, 0, SizeInBits,
1603+
AlignInBits, Flags, MangledName);
15931604
}
15941605

15951606
case TypeKind::Enum: {

lib/IRGen/IRGenDebugInfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,12 @@ class IRGenDebugInfo {
318318
llvm::DIType *createDoublePointerSizedStruct(
319319
llvm::DIScope *Scope, StringRef Name, llvm::DIType *PointeeTy,
320320
llvm::DIFile *File, unsigned Line, unsigned Flags, StringRef MangledName);
321+
322+
/// Create DWARF debug info for a function pointer type.
323+
llvm::DIType *createFunctionPointer(DebugTypeInfo DbgTy, llvm::DIScope *Scope,
324+
unsigned SizeInBits, unsigned AlignInBits,
325+
unsigned Flags, StringRef MangledName);
326+
321327
/// Create an opaque struct with a mangled name.
322328
llvm::DIType *createOpaqueStruct(llvm::DIScope *Scope, StringRef Name,
323329
llvm::DIFile *File, unsigned Line,

test/DebugInfo/enum.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %target-swift-frontend -primary-file %s -emit-ir -g -o - | FileCheck %s
2+
// RUN: %target-swift-frontend -primary-file %s -emit-ir -gdwarf-types -o - | FileCheck %s --check-prefix=DWARF
23

34
// CHECK: ![[EMPTY:.*]] = !{}
45

@@ -62,8 +63,7 @@ public func foo(_ empty : Nothing) { }
6263
// CHECK-SAME: {{.*}}identifier: "_TtGO4enum4Rosex_")
6364
enum Rose<A> {
6465
case MkRose(() -> A, () -> [Rose<A>])
65-
// CHECK: !DICompositeType({{.*}}name: "Rose", {{.*}}elements: ![[ELTS]],
66-
// CHECK-SAME: {{.*}}identifier: "_TtGO4enum4RoseQq_S0__")
66+
// DWARF: !DICompositeType({{.*}}name: "Rose",{{.*}}identifier: "_TtGO4enum4RoseQq_S0__")
6767
case IORose(() -> Rose<A>)
6868
}
6969

@@ -72,8 +72,7 @@ func foo<T>(_ x : Rose<T>) -> Rose<T> { return x }
7272
// CHECK: !DICompositeType({{.*}}name: "Tuple", {{.*}}elements: ![[ELTS:[0-9]+]],
7373
// CHECK-SAME: {{.*}}identifier: "_TtGO4enum5Tuplex_")
7474
public enum Tuple<P> {
75-
// CHECK: !DICompositeType({{.*}}name: "Tuple", {{.*}}elements: ![[ELTS]],
76-
// CHECK-SAME: {{.*}}identifier: "_TtGO4enum5TupleQq_S0__")
75+
// DWARF: !DICompositeType({{.*}}name: "Tuple",{{.*}}identifier: "_TtGO4enum5TupleQq_S0__")
7776
case C(P, () -> Tuple)
7877
}
7978

test/DebugInfo/fnptr.swift

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// RUN: %target-swift-frontend %s -emit-ir -g -o - | FileCheck %s
1+
// RUN: %target-swift-frontend %s -emit-ir -gdwarf-types -o - | FileCheck %s
2+
// RUN: %target-swift-frontend %s -emit-ir -g -o - | FileCheck %s --check-prefix=AST
23

34
// CHECK-DAG: ![[SINODE:.*]] = !DICompositeType(tag: DW_TAG_structure_type, name: "Int64",{{.*}} identifier: [[SI:.*]])
45
// CHECK-DAG: ![[SFNODE:.*]] = !DICompositeType(tag: DW_TAG_structure_type, name: "Float",{{.*}} identifier: [[SF:.*]])
@@ -9,19 +10,22 @@ func bar() {
910
func baz(_ i: Float) -> Int64 { return 0; }
1011
func barz(_ i: Float, _ j: Float) -> Int64 { return 0; }
1112
func main() -> Int64 {
12-
13-
// CHECK-DAG: !DILocalVariable(name: "bar_function_pointer",{{.*}} line: [[@LINE+1]],{{.*}} type: ![[BARPT:[0-9]+]]
14-
var bar_function_pointer = bar
13+
// CHECK-DAG: !DILocalVariable(name: "bar_fnptr",{{.*}} line: [[@LINE+3]],{{.*}} type: ![[BARPT:[0-9]+]]
14+
// AST-DAG: !DILocalVariable(name: "bar_fnptr",{{.*}} line: [[@LINE+2]],{{.*}} type: ![[BAR_T:[0-9]+]]
15+
// AST-DAG: ![[BAR_T]] = !DICompositeType({{.*}}, identifier: "_TtFT_T_")
16+
var bar_fnptr = bar
1517
// CHECK-DAG: ![[BARPT]] = !DICompositeType(tag: DW_TAG_structure_type, {{.*}} elements: ![[BARMEMBERS:[0-9]+]]
1618
// CHECK-DAG: ![[BARMEMBERS]] = !{![[BARMEMBER:.*]], {{.*}}}
1719
// CHECK-DAG: ![[BARMEMBER]] = !DIDerivedType(tag: DW_TAG_member,{{.*}} baseType: ![[BARPTR:[0-9]+]]
1820
// CHECK-DAG: ![[BARPTR]] = !DIDerivedType(tag: DW_TAG_pointer_type,{{.*}} baseType: ![[BART:[0-9]+]]
1921
// CHECK-DAG: ![[BART]] = !DISubroutineType(types: ![[BARARGS:[0-9]+]])
2022
// CHECK-DAG: ![[BARARGS]] = !{![[VOID:.*]]}
2123
// CHECK-DAG: ![[VOID]] = {{.*}}identifier: "_TtT_"
22-
bar_function_pointer();// Set breakpoint here
24+
bar_fnptr();
2325

24-
// CHECK-DAG: !DILocalVariable(name: "baz_function_pointer",{{.*}} type: ![[BAZPT:[0-9]+]]
26+
// CHECK-DAG: !DILocalVariable(name: "baz_fnptr",{{.*}} type: ![[BAZPT:[0-9]+]]
27+
// AST-DAG: !DILocalVariable(name: "baz_fnptr",{{.*}} type: ![[BAZ_T:[0-9]+]]
28+
// AST-DAG: ![[BAZ_T]] = !DICompositeType({{.*}}, identifier: "_TtFSfVs5Int64")
2529
// CHECK-DAG: ![[BAZPT]] = !DICompositeType(tag: DW_TAG_structure_type, {{.*}} elements: ![[BAZMEMBERS:[0-9]+]]
2630
// CHECK-DAG: ![[BAZMEMBERS]] = !{![[BAZMEMBER:.*]], {{.*}}}
2731
// CHECK-DAG: ![[BAZMEMBER]] = !DIDerivedType(tag: DW_TAG_member,{{.*}} baseType: ![[BAZPTR:[0-9]+]]
@@ -30,18 +34,20 @@ func main() -> Int64 {
3034
// CHECK-DAG: ![[BAZARGS]] = !{![[INT:.*]], ![[FLOAT:.*]]}
3135
// CHECK-DAG: ![[INT]] = {{.*}}identifier: "_TtVs5Int64"
3236
// CHECK-DAG: ![[FLOAT]] = {{.*}}identifier: "_TtSf"
33-
var baz_function_pointer = baz
34-
baz_function_pointer(2.89)
37+
var baz_fnptr = baz
38+
baz_fnptr(2.89)
3539

36-
// CHECK-DAG: !DILocalVariable(name: "barz_function_pointer",{{.*}} type: ![[BARZPT:[0-9]+]]
40+
// CHECK-DAG: !DILocalVariable(name: "barz_fnptr",{{.*}} type: ![[BARZPT:[0-9]+]]
41+
// AST_DAG: !DILocalVariable(name: "barz_fnptr",{{.*}} type: ![[BARZ_T:[0-9]+]]
42+
// AST-DAG: ![[BARZ_T:[0-9]+]] = !DICompositeType({{.*}}, identifier: "_TtFTSfSf_Vs5Int64")
3743
// CHECK-DAG: ![[BARZPT]] = !DICompositeType(tag: DW_TAG_structure_type,{{.*}} elements: ![[BARZMEMBERS:[0-9]+]]
3844
// CHECK-DAG: ![[BARZMEMBERS]] = !{![[BARZMEMBER:.*]], {{.*}}}
3945
// CHECK-DAG: ![[BARZMEMBER]] = !DIDerivedType(tag: DW_TAG_member,{{.*}} baseType: ![[BARZPTR:[0-9]+]]
4046
// CHECK-DAG: ![[BARZPTR]] = !DIDerivedType(tag: DW_TAG_pointer_type,{{.*}} baseType: ![[BARZT:[0-9]+]]
4147
// CHECK-DAG: ![[BARZT]] = !DISubroutineType(types: ![[BARZARGS:.*]])
4248
// CHECK-DAG: ![[BARZARGS]] = !{![[INT]], ![[FLOAT]], ![[FLOAT]]}
43-
var barz_function_pointer = barz
44-
return barz_function_pointer(2.89, -1.0)
49+
var barz_fnptr = barz
50+
return barz_fnptr(2.89, -1.0)
4551
}
4652

4753
main()

0 commit comments

Comments
 (0)