Skip to content

Commit 4ed1606

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 4ed1606

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "clang/Basic/SourceLocation.h"
5656
#include "clang/Basic/SourceManager.h"
5757
#include "clang/Basic/TargetInfo.h"
58+
#include "clang/CodeGen/ModuleBuilder.h"
5859
#include "clang/Frontend/CompilerInstance.h"
5960
#include "clang/Lex/HeaderSearchOptions.h"
6061
#include "clang/Lex/Preprocessor.h"
@@ -284,6 +285,10 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
284285
void emitPackCountParameter(IRGenFunction &IGF, llvm::Value *Metadata,
285286
SILDebugVariable VarInfo);
286287

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

@@ -2966,6 +2971,33 @@ void IRGenDebugInfoImpl::emitImport(ImportDecl *D) {
29662971
ImportedModules.insert(Imported.importedModule);
29672972
}
29682973

2974+
/// This is effectively \p clang::CGDebugInfo::getCallSiteRelatedAttrs().
2975+
llvm::DINode::DIFlags IRGenDebugInfoImpl::getCallSiteRelatedAttrs() const {
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-related attributes are available in DWARF v5. Some debuggers,
2988+
// while not fully DWARF v5-compliant, may accept these attributes as if they
2989+
// were part of DWARF v4.
2990+
bool SupportsDWARFv4Ext =
2991+
CGO.DwarfVersion == 4 &&
2992+
(CGO.getDebuggerTuning() == llvm::DebuggerKind::LLDB ||
2993+
CGO.getDebuggerTuning() == llvm::DebuggerKind::GDB);
2994+
2995+
if (!SupportsDWARFv4Ext && CGO.DwarfVersion < 5)
2996+
return llvm::DINode::FlagZero;
2997+
2998+
return llvm::DINode::FlagAllCallsDescribed;
2999+
}
3000+
29693001
llvm::DISubprogram *IRGenDebugInfoImpl::emitFunction(SILFunction &SILFn,
29703002
llvm::Function *Fn) {
29713003
auto *DS = SILFn.getDebugScope();
@@ -3117,9 +3149,10 @@ IRGenDebugInfoImpl::emitFunction(const SILDebugScope *DS, llvm::Function *Fn,
31173149
}
31183150

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

31243157
if (Fn && !Fn->isDeclaration())
31253158
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)