Skip to content

[debuginfo][codeview] avoid using line zero artificial location for t… #72534

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
Mar 26, 2024
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 @@ -642,6 +642,10 @@ class IRGenOptions {
bool hasMultipleIRGenThreads() const { return !UseSingleModuleLLVMEmission && NumThreads > 1; }
bool shouldPerformIRGenerationInParallel() const { return !UseSingleModuleLLVMEmission && NumThreads != 0; }
bool hasMultipleIGMs() const { return hasMultipleIRGenThreads(); }

bool isDebugInfoCodeView() const {
return DebugInfoFormat == IRGenDebugInfoFormat::CodeView;
}
};

} // end namespace swift
Expand Down
16 changes: 13 additions & 3 deletions lib/IRGen/IRGenDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2661,12 +2661,18 @@ void IRGenDebugInfoImpl::addFailureMessageToCurrentLoc(IRBuilder &Builder,
else {
std::string FuncName = "Swift runtime failure: ";
FuncName += failureMsg;
llvm::DIFile *File = getOrCreateFile({}, {});
// CodeView consumers do not correctly handle an artificially generated
// file, thus use the original location's file as the file for the debug
// function, and prevent reuse of this debug function.
bool useCompilerGeneratedFile = !Opts.isDebugInfoCodeView();
llvm::DIFile *File =
useCompilerGeneratedFile ? getOrCreateFile({}, {}) : TrapLoc->getFile();
TrapSP = DBuilder.createFunction(
File, FuncName, StringRef(), File, 0,
DIFnTy, 0, llvm::DINode::FlagArtificial,
llvm::DISubprogram::SPFlagDefinition, nullptr, nullptr, nullptr);
RuntimeErrorFnCache.insert({failureMsg, llvm::TrackingMDNodeRef(TrapSP)});
if (useCompilerGeneratedFile)
RuntimeErrorFnCache.insert({failureMsg, llvm::TrackingMDNodeRef(TrapSP)});
}

ScopeCache[TrapSc] = llvm::TrackingMDNodeRef(TrapSP);
Expand All @@ -2675,7 +2681,11 @@ void IRGenDebugInfoImpl::addFailureMessageToCurrentLoc(IRBuilder &Builder,
assert(parentScopesAreSane(TrapSc) && "parent scope sanity check failed");

// Wrap the existing TrapLoc into the failure function.
auto DL = llvm::DILocation::get(IGM.getLLVMContext(), 0, 0, TrapSP, TrapLoc);
// Line 0 is invalid in CodeView, so use the line and column from the original
// trap location.
auto DL = llvm::DILocation::get(
IGM.getLLVMContext(), Opts.isDebugInfoCodeView() ? TrapLoc.getLine() : 0,
Opts.isDebugInfoCodeView() ? TrapLoc.getCol() : 0, TrapSP, TrapLoc);
Builder.SetCurrentDebugLocation(DL);
}

Expand Down
14 changes: 14 additions & 0 deletions lib/IRGen/IRGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,20 @@ Address IRGenFunction::emitAddressAtOffset(llvm::Value *base, Offset offset,

llvm::CallInst *IRBuilder::CreateNonMergeableTrap(IRGenModule &IGM,
StringRef failureMsg) {
if (IGM.DebugInfo && IGM.getOptions().isDebugInfoCodeView()) {
auto TrapLoc = getCurrentDebugLocation();
// Line 0 is invalid in CodeView, so create a new location that uses the
// line and column from the inlined location of the trap, that should
// correspond to its original source location.
if (TrapLoc.getLine() == 0 && TrapLoc.getInlinedAt()) {
auto DL = llvm::DILocation::getDistinct(
IGM.getLLVMContext(), TrapLoc.getInlinedAt()->getLine(),
TrapLoc.getInlinedAt()->getColumn(), TrapLoc.getScope(),
TrapLoc.getInlinedAt());
SetCurrentDebugLocation(DL);
}
}

if (IGM.IRGen.Opts.shouldOptimize()) {
// Emit unique side-effecting inline asm calls in order to eliminate
// the possibility that an LLVM optimization or code generation pass
Expand Down
6 changes: 5 additions & 1 deletion test/DebugInfo/doubleinlines.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ func callCondFail(arg: Builtin.Int1, msg: Builtin.RawPointer) {

// CHECK: define hidden swiftcc void @"$s13DoubleInlines12callCondFail3arg3msgyBi1__BptF"{{.*}} !dbg ![[FUNC:.*]] {
// CHECK: tail call void asm sideeffect "", "n"(i32 0) #3, !dbg ![[SCOPEONE:.*]]
// CHECK: tail call void @llvm.trap(), !dbg ![[LOCTRAP:.*]]

// CHECK: ![[FUNCSCOPEOTHER:.*]] = distinct !DISubprogram(name: "condFail",{{.*}}
// CHECK: ![[SCOPEONE]] = !DILocation(line: 0, scope: ![[SCOPETWO:.*]], inlinedAt: ![[SCOPETHREE:.*]])
// CHECK: ![[SCOPEONE]] = distinct !DILocation(line: 6, scope: ![[SCOPETWO:.*]], inlinedAt: ![[SCOPETHREE:.*]])
// CHECK: ![[SCOPETHREE]] = !DILocation(line: 6, scope: ![[FUNCSCOPE:.*]])
// CHECK: ![[FUNCSCOPE:[0-9]+]] = distinct !DILexicalBlock(scope: ![[FUNC]],
// CHECK: ![[LOCTRAP]] = !DILocation(line: 6, scope: ![[SCOPETRAP:.*]], inlinedAt: ![[SCOPEONE]])
// CHECK: ![[SCOPETRAP]] = distinct !DISubprogram(name: "Swift runtime failure: unknown program error"

import Builtin
7 changes: 3 additions & 4 deletions test/DebugInfo/linetable-codeview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,10 @@ func foo() {

// CHECK-DAG: ![[ADD]] = !DILocation(line: 6, scope:
// CHECK-DAG: ![[DIV]] = !DILocation(line: 7, scope:
// FIXME: The location of ``@llvm.trap`` should be in Integers.swift.gyb
// instead of being artificial.
// CHECK: ![[INLINEDADD]] = !DILocation(line: 0, scope: ![[FAILURE_FUNC:[0-9]+]], inlinedAt: ![[INLINELOC:[0-9]+]]

// CHECK: ![[INLINEDADD]] = !DILocation(line: 6, scope: ![[FAILURE_FUNC:[0-9]+]], inlinedAt: ![[INLINELOC:[0-9]+]]
// CHECK-DAG: !{{.*}} = distinct !DISubprogram(name: "Swift runtime failure: arithmetic overflow", scope: {{.*}}, flags: DIFlagArtificial, spFlags: DISPFlagDefinition, {{.*}})
// CHECK-DAG: ![[INLINELOC]] = !DILocation(line: 0, scope: !{{[0-9]+}}, inlinedAt: ![[ADD]]
// CHECK-DAG: ![[INLINELOC]] = distinct !DILocation(line: 6, scope: !{{[0-9]+}}, inlinedAt: ![[ADD]]

// NOTE: These prologue instructions are given artificial line locations for
// LLDB, but for CodeView they should have the location of the function
Expand Down