Skip to content

Commit c1a2077

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 725bd91 commit c1a2077

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 30 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,28 @@ 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 SwiftLangOpts = IGM.Context.LangOpts;
2976+
auto Optimize = IGM.getClangASTContext().getLangOpts().Optimize;
2977+
auto Loader = IGM.getSILModule().getASTContext().getClangModuleLoader();
2978+
auto *Importer = static_cast<ClangImporter *>(&*Loader);
2979+
auto &CGO = Importer->getCodeGenOpts();
2980+
2981+
// Call site-related attributes are only useful in optimized programs, and
2982+
// when there's a possibility of debugging backtraces.
2983+
if (!Optimize || CGO.getDebugInfo() == llvm::codegenoptions::NoDebugInfo ||
2984+
CGO.getDebugInfo() == llvm::codegenoptions::LocTrackingOnly)
2985+
return llvm::DINode::FlagZero;
2986+
2987+
// Call site attributes are available in DWARFv5. However, for swift, lldb can
2988+
// accept these attributes as if they were part of DWARFv4.
2989+
if (Opts.DWARFVersion < 4)
2990+
return llvm::DINode::FlagZero;
2991+
2992+
return llvm::DINode::FlagAllCallsDescribed;
2993+
}
2994+
29692995
llvm::DISubprogram *IRGenDebugInfoImpl::emitFunction(SILFunction &SILFn,
29702996
llvm::Function *Fn) {
29712997
auto *DS = SILFn.getDebugScope();
@@ -3117,9 +3143,10 @@ IRGenDebugInfoImpl::emitFunction(const SILDebugScope *DS, llvm::Function *Fn,
31173143
}
31183144

31193145
// Construct the DISubprogram.
3120-
llvm::DISubprogram *SP = DBuilder.createFunction(
3121-
Scope, Name, LinkageName, File, Line, DIFnTy, ScopeLine, Flags, SPFlags,
3122-
TemplateParameters, Decl, Error);
3146+
llvm::DISubprogram *SP =
3147+
DBuilder.createFunction(Scope, Name, LinkageName, File, Line, DIFnTy,
3148+
ScopeLine, Flags | getCallSiteRelatedAttrs(),
3149+
SPFlags, TemplateParameters, Decl, Error);
31233150

31243151
if (Fn && !Fn->isDeclaration())
31253152
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)