Skip to content

[DebugInfo] Prevent salvaged debug vars from being marked artificial #38942

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
Aug 20, 2021
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
17 changes: 17 additions & 0 deletions lib/IRGen/IRGenDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2542,6 +2542,23 @@ void IRGenDebugInfoImpl::emitDbgIntrinsic(
!llvm::FindDbgDeclareUses(Storage).empty())
return;

// Fragment DIExpression cannot cover the whole variable
// or going out-of-bound.
if (auto Fragment = Expr->getFragmentInfo())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we understand why we generate a fragment that covers the entire variable? Does this happen because we SROA a struct with a single member? Could we catch that case in IRGenDebugInfoImpl::emitVariableDeclaration() and not emit a DW_OP_LLVM_fragment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this only happens when the struct only has one member. Originally I was going to solve this by preserving the bail out logic here and avoid emitting DW_OP_LLVM_fragment some place earlier as suggested. But then I found that it will be easier to just drop the fragment part here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That works!

if (auto VarSize = Var->getSizeInBits()) {
unsigned FragSize = Fragment->SizeInBits;
unsigned FragOffset = Fragment->OffsetInBits;
if (FragOffset + FragSize > *VarSize ||
FragSize == *VarSize) {
// Drop the fragment part
assert(Expr->isValid());
// Since this expression is valid, DW_OP_LLVM_fragment
// and its arguments must be the last 3 elements.
auto OrigElements = Expr->getElements();
Expr = DBuilder.createExpression(OrigElements.drop_back(3));
}
}

// A dbg.declare is only meaningful if there is a single alloca for
// the variable that is live throughout the function.
if (auto *Alloca = dyn_cast<llvm::AllocaInst>(Storage)) {
Expand Down
3 changes: 1 addition & 2 deletions lib/SILOptimizer/Utils/InstOptUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2119,8 +2119,7 @@ void swift::salvageDebugInfo(SILInstruction *I) {
DestVal.getDefiningInstruction())) {
if (auto VarInfo = ASI->getVarInfo())
SILBuilder(SI, ASI->getDebugScope())
.createDebugValue(RegularLocation::getAutoGeneratedLocation(),
SI->getSrc(), *VarInfo);
.createDebugValue(SI->getLoc(), SI->getSrc(), *VarInfo);
}
}
}
17 changes: 17 additions & 0 deletions test/DebugInfo/debug_info_expression.sil
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ struct MyStruct {
var y: Builtin.Int64
}

struct SmallStruct {
var z : Builtin.Int64
}

sil_scope 1 { loc "file.swift":7:6 parent @test_fragment : $@convention(thin) () -> () }

// Testing op_fragment w/ debug_value_addr
Expand All @@ -16,6 +20,8 @@ bb0:
%2 = alloc_stack $MyStruct, var, name "my_struct", loc "file.swift":8:9, scope 1
// CHECK: %[[MY_STRUCT:.+]] = alloca %{{.*}}MyStruct
// CHECK: llvm.dbg.declare(metadata {{.*}}* %[[MY_STRUCT]], metadata ![[VAR_DECL_MD:[0-9]+]]
// CHECK: %[[SMALL_STRUCT:.+]] = alloca %{{.*}}SmallStruct
// CHECK: llvm.dbg.declare(metadata {{.*}}* %[[SMALL_STRUCT]], metadata ![[SMALL_VAR_DECL_MD:[0-9]+]]
%3 = struct_element_addr %2 : $*MyStruct, #MyStruct.x, loc "file.swift":9:17, scope 1
// CHECK: %[[FIELD_X:.*]] = getelementptr {{.*}} %[[MY_STRUCT]]
// CHECK-SIL: debug_value_addr %{{[0-9]+}} : $*Builtin.Int64
Expand All @@ -25,6 +31,17 @@ bb0:
// CHECK: llvm.dbg.value(metadata {{.*}}* %[[FIELD_X]], metadata ![[VAR_DECL_MD]]
// CHECK-SAME: !DIExpression(DW_OP_deref, DW_OP_LLVM_fragment, 0, 64)
// CHECK-NOT: ), !dbg ![[VAR_DECL_MD]]

%4 = alloc_stack $SmallStruct, var, name "small_struct", loc "file.swift":10:11, scope 1
%5 = struct_element_addr %4 : $*SmallStruct, #SmallStruct.z, loc "file.swift":11:13, scope 1
// CHECK: %[[FIELD_Z:.*]] = getelementptr {{.*}} %[[SMALL_STRUCT]]
// If the fragment covers the whole struct, we're not generating the
// DW_OP_LLVM_fragment part.
// CHECK: llvm.dbg.value(metadata {{.*}}* %[[FIELD_Z]], metadata ![[SMALL_VAR_DECL_MD]]
// CHECK-SAME: !DIExpression(DW_OP_deref)
debug_value_addr %5 : $*Builtin.Int64, var, (name "small_struct", loc "file.swift":10:11, scope 1), type $SmallStruct, expr op_fragment:#SmallStruct.z, loc "file.swift":11:13, scope 1
dealloc_stack %4 : $*SmallStruct

dealloc_stack %2 : $*MyStruct
%r = tuple()
return %r : $()
Expand Down
9 changes: 9 additions & 0 deletions test/DebugInfo/sroa_mem2reg.sil
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// RUN: %target-sil-opt -enable-sil-verify-all -sil-print-debuginfo -sroa -mem2reg %s -o %t.sil
// RUN: %FileCheck --check-prefix=CHECK-MEM2REG %s --input-file=%t.sil
// RUN: %target-swiftc_driver -Xfrontend -disable-debugger-shadow-copies -g -emit-ir %t.sil | %FileCheck --check-prefix=CHECK-IR %s
// RUN: %target-swiftc_driver -Xfrontend -disable-debugger-shadow-copies -g -c %t.sil -o %t.o
// RUN: %llvm-dwarfdump --debug-info %t.o | %FileCheck --check-prefix=CHECK-DWARF %s
sil_stage canonical

import Builtin
Expand Down Expand Up @@ -78,3 +80,10 @@ bb0(%0 : $Int64, %1 : $Int64):
// CHECK-IR-SAME: line: 7
// CHECK-IR: ![[ARG2_MD]] = !DILocalVariable(name: "in_y", arg: 2
// CHECK-IR-SAME: line: 7

// CHECK-DWARF-LABEL: DW_AT_name ("foo")
// CHECK-DWARF: DW_TAG_variable
// CHECK-DWARF: DW_AT_name ("my_struct")
// Shouldn't be marked artificial
// CHECK-DWARF-NOT: DW_AT_artificial (true)
// CHECK-DWARF: DW_TAG_{{.*}}