Skip to content

Commit c4549d3

Browse files
[DebugInfo]Generate call-site information in swift
This patch adds support for emitting the flag llvm::DINode::FlagAllCallsDescribed when generating LLVM IR from the Swift compiler to get call-site information for swift source code.
1 parent 4937d0a commit c4549d3

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,10 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
284284
void emitPackCountParameter(IRGenFunction &IGF, llvm::Value *Metadata,
285285
SILDebugVariable VarInfo);
286286

287+
/// Return flags which enable debug info emission for call sites, provided
288+
/// that it is supported and enabled.
289+
llvm::DINode::DIFlags getCallSiteRelatedAttrs() const;
290+
287291
/// Return the DIBuilder.
288292
llvm::DIBuilder &getBuilder() { return DBuilder; }
289293

@@ -2966,6 +2970,33 @@ void IRGenDebugInfoImpl::emitImport(ImportDecl *D) {
29662970
ImportedModules.insert(Imported.importedModule);
29672971
}
29682972

2973+
/// This is effectively \p clang::CGDebugInfo::getCallSiteRelatedAttrs().
2974+
llvm::DINode::DIFlags IRGenDebugInfoImpl::getCallSiteRelatedAttrs() const {
2975+
auto Optimize = IGM.getClangASTContext().getLangOpts().Optimize;
2976+
auto Loader = IGM.getSILModule().getASTContext().getClangModuleLoader();
2977+
auto *Importer = static_cast<ClangImporter *>(&*Loader);
2978+
auto &CGO = Importer->getCodeGenOpts();
2979+
2980+
// Call site-related attributes are only useful in optimized programs, and
2981+
// when there's a possibility of debugging backtraces.
2982+
if (!Optimize || CGO.getDebugInfo() == llvm::codegenoptions::NoDebugInfo ||
2983+
CGO.getDebugInfo() == llvm::codegenoptions::LocTrackingOnly)
2984+
return llvm::DINode::FlagZero;
2985+
2986+
// Call site-related attributes are available in DWARF v5. Some debuggers,
2987+
// while not fully DWARF v5-compliant, may accept these attributes as if they
2988+
// were part of DWARF v4.
2989+
bool SupportsDWARFv4Ext =
2990+
CGO.DwarfVersion == 4 &&
2991+
(CGO.getDebuggerTuning() == llvm::DebuggerKind::LLDB ||
2992+
CGO.getDebuggerTuning() == llvm::DebuggerKind::GDB);
2993+
2994+
if (!SupportsDWARFv4Ext && CGO.DwarfVersion < 5)
2995+
return llvm::DINode::FlagZero;
2996+
2997+
return llvm::DINode::FlagAllCallsDescribed;
2998+
}
2999+
29693000
llvm::DISubprogram *IRGenDebugInfoImpl::emitFunction(SILFunction &SILFn,
29703001
llvm::Function *Fn) {
29713002
auto *DS = SILFn.getDebugScope();
@@ -3117,9 +3148,10 @@ IRGenDebugInfoImpl::emitFunction(const SILDebugScope *DS, llvm::Function *Fn,
31173148
}
31183149

31193150
// Construct the DISubprogram.
3120-
llvm::DISubprogram *SP = DBuilder.createFunction(
3121-
Scope, Name, LinkageName, File, Line, DIFnTy, ScopeLine, Flags, SPFlags,
3122-
TemplateParameters, Decl, Error);
3151+
llvm::DISubprogram *SP =
3152+
DBuilder.createFunction(Scope, Name, LinkageName, File, Line, DIFnTy,
3153+
ScopeLine, Flags | getCallSiteRelatedAttrs(),
3154+
SPFlags, TemplateParameters, Decl, Error);
31233155

31243156
if (Fn && !Fn->isDeclaration())
31253157
Fn->setSubprogram(SP);

test/DebugInfo/call-site-info.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %target-swift-frontend -emit-ir -parse-as-library -g -O -module-name S %s -o - | %FileCheck %s
2+
3+
// CHECK: {{[0-9]+}} = distinct !DISubprogram(name: "f", linkageName: {{.*}}, scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: !{{[0-9]+}}, scopeLine: {{[0-9]+}}, flags: DIFlagAllCallsDescribed
4+
5+
6+
// CHECK: {{[0-9]+}} = distinct !DISubprogram(linkageName: {{.*}}, scope: !{{[0-9]+}}, file: !{{[0-9]+}}, type: !{{[0-9]+}}, flags: {{.*}} DIFlagAllCallsDescribed
7+
8+
9+
public struct S {
10+
public func f() {}
11+
}

0 commit comments

Comments
 (0)