Skip to content

[DebugInfo]Generate call-site information in swift #79308

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
Feb 28, 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
4 changes: 4 additions & 0 deletions include/swift/AST/IRGenOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ class IRGenOptions {
/// indexing info.
PathRemapper FilePrefixMap;

/// Indicates whether or not the frontend should generate callsite information
/// in the debug info.
bool DebugCallsiteInfo = false;

/// What level of debug info to generate.
IRGenDebugInfoLevel DebugInfoLevel : 2;

Expand Down
4 changes: 4 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -2191,6 +2191,10 @@ def cas_backend: Flag<["-"], "cas-backend">,
Flags<[FrontendOption, NoDriverOption]>,
HelpText<"Enable using CASBackend for object file output">;

def debug_callsite_info: Flag<["-"], "debug-callsite-info">,
Flags<[FrontendOption, NoDriverOption]>,
HelpText<"Generate callsite information in debug info">;

def cas_backend_mode: Joined<["-"], "cas-backend-mode=">,
Flags<[FrontendOption, NoDriverOption]>,
HelpText<"CASBackendMode for output kind">,
Expand Down
2 changes: 2 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3681,6 +3681,8 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
Opts.UseCASBackend |= Args.hasArg(OPT_cas_backend);
Opts.EmitCASIDFile |= Args.hasArg(OPT_cas_emit_casid_file);

Opts.DebugCallsiteInfo |= Args.hasArg(OPT_debug_callsite_info);

return false;
}

Expand Down
38 changes: 35 additions & 3 deletions lib/IRGen/IRGenDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
void emitPackCountParameter(IRGenFunction &IGF, llvm::Value *Metadata,
SILDebugVariable VarInfo);

/// Return flags which enable debug info emission for call sites, provided
/// that it is supported and enabled.
llvm::DINode::DIFlags getCallSiteRelatedAttrs() const;

/// Return the DIBuilder.
llvm::DIBuilder &getBuilder() { return DBuilder; }

Expand Down Expand Up @@ -3042,6 +3046,33 @@ void IRGenDebugInfoImpl::emitImport(ImportDecl *D) {
ImportedModules.insert(Imported.importedModule);
}

/// This is effectively \p clang::CGDebugInfo::getCallSiteRelatedAttrs().
llvm::DINode::DIFlags IRGenDebugInfoImpl::getCallSiteRelatedAttrs() const {

// Do not generate callsite attributes if unless the -gen-callsite-info flag
// is passed.
if (!Opts.DebugCallsiteInfo)
return llvm::DINode::FlagZero;

auto SwiftLangOpts = IGM.Context.LangOpts;
auto Loader = IGM.getSILModule().getASTContext().getClangModuleLoader();
auto *Importer = static_cast<ClangImporter *>(&*Loader);
auto &CGO = Importer->getCodeGenOpts();

// Do not generate callsite attributes if there is no debug info to be
// emitted.
if (CGO.getDebugInfo() == llvm::codegenoptions::NoDebugInfo ||
CGO.getDebugInfo() == llvm::codegenoptions::LocTrackingOnly)
return llvm::DINode::FlagZero;

// Callsite attributes are available in DWARFv5. However, for swift, lldb can
// accept these attributes as if they were part of DWARFv4.
if (Opts.DWARFVersion < 4)
return llvm::DINode::FlagZero;

return llvm::DINode::FlagAllCallsDescribed;
}

llvm::DISubprogram *IRGenDebugInfoImpl::emitFunction(SILFunction &SILFn,
llvm::Function *Fn) {
auto *DS = SILFn.getDebugScope();
Expand Down Expand Up @@ -3193,9 +3224,10 @@ IRGenDebugInfoImpl::emitFunction(const SILDebugScope *DS, llvm::Function *Fn,
}

// Construct the DISubprogram.
llvm::DISubprogram *SP = DBuilder.createFunction(
Scope, Name, LinkageName, File, Line, DIFnTy, ScopeLine, Flags, SPFlags,
TemplateParameters, Decl, Error);
llvm::DISubprogram *SP =
DBuilder.createFunction(Scope, Name, LinkageName, File, Line, DIFnTy,
ScopeLine, Flags | getCallSiteRelatedAttrs(),
SPFlags, TemplateParameters, Decl, Error);

if (Fn && !Fn->isDeclaration())
Fn->setSubprogram(SP);
Expand Down
7 changes: 7 additions & 0 deletions test/DebugInfo/call-site-info.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: %target-swift-frontend -debug-callsite-info -emit-ir -parse-as-library -g -O -module-name S %s -o - | %FileCheck %s
// 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
// CHECK: {{[0-9]+}} = distinct !DISubprogram(linkageName: {{.*}}, scope: !{{[0-9]+}}, file: !{{[0-9]+}}, type: !{{[0-9]+}}, flags: {{.*}} DIFlagAllCallsDescribed

public struct S {
public func f() {}
}