Skip to content

Commit 4bc672e

Browse files
committed
[DebugInfo] Deduplicate location and scope in debug variables (NFC)
1 parent b944e38 commit 4bc672e

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

lib/SIL/IR/SILInstructions.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,15 @@ AllocStackInst *AllocStackInst::create(SILDebugLocation Loc,
266266
IsLexical_t isLexical,
267267
IsFromVarDecl_t isFromVarDecl,
268268
UsesMoveableValueDebugInfo_t wasMoved) {
269+
// Don't store the same information twice.
270+
if (Var) {
271+
if (Var->Loc == Loc.getLocation())
272+
Var->Loc = {};
273+
if (Var->Scope == Loc.getScope())
274+
Var->Scope = nullptr;
275+
if (Var->Type == elementType)
276+
Var->Type = {};
277+
}
269278
SmallVector<SILValue, 8> TypeDependentOperands;
270279
collectTypeDependentOperands(TypeDependentOperands, F,
271280
elementType.getASTType());
@@ -460,6 +469,13 @@ DebugValueInst *DebugValueInst::create(SILDebugLocation DebugLoc,
460469
SILDebugVariable Var, bool poisonRefs,
461470
UsesMoveableValueDebugInfo_t wasMoved,
462471
bool trace) {
472+
// Don't store the same information twice.
473+
if (Var.Loc == DebugLoc.getLocation())
474+
Var.Loc = {};
475+
if (Var.Scope == DebugLoc.getScope())
476+
Var.Scope = nullptr;
477+
if (Var.Type == Operand->getType().getObjectType())
478+
Var.Type = {};
463479
void *buf = allocateDebugVarCarryingInst<DebugValueInst>(M, Var);
464480
return ::new (buf)
465481
DebugValueInst(DebugLoc, Operand, Var, poisonRefs, wasMoved, trace);
@@ -474,9 +490,8 @@ DebugValueInst::createAddr(SILDebugLocation DebugLoc, SILValue Operand,
474490
if (!isa<AllocStackInst>(Operand))
475491
Var.DIExpr.prependElements(
476492
{SILDIExprElement::createOperator(SILDIExprOperator::Dereference)});
477-
void *buf = allocateDebugVarCarryingInst<DebugValueInst>(M, Var);
478-
return ::new (buf) DebugValueInst(DebugLoc, Operand, Var,
479-
/*poisonRefs=*/false, wasMoved, trace);
493+
return DebugValueInst::create(DebugLoc, Operand, M, Var,
494+
/*poisonRefs=*/false, wasMoved, trace);
480495
}
481496

482497
bool DebugValueInst::exprStartsWithDeref() const {

test/DebugInfo/copyforward.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ bb0(%0 : $*Optional<Element>, %1 : $*CollectionOfOne<Element>.Iterator):
1717
%3 = alloc_stack [lexical] [var_decl] $Optional<Element>, let, name "result", loc "backward.swift":67:9, scope 2
1818
%4 = struct_element_addr %1 : $*CollectionOfOne<Element>.Iterator, #CollectionOfOne.Iterator._elements, loc "backward.swift":67:18, scope 1
1919

20-
// CHECK: debug_value %{{[0-9]+}} : $*Optional<Element>, let, (name "result", loc "backward.swift":67:9, scope 2), expr op_deref, loc "backward.swift":67:18, scope 2
20+
// CHECK: debug_value %{{[0-9]+}} : $*Optional<Element>, let, (name "result", loc "backward.swift":67:9), expr op_deref, loc "backward.swift":67:18, scope 2
2121

2222
copy_addr %4 to [init] %3 : $*Optional<Element>, loc "backward.swift":67:18, scope 1
2323
%6 = alloc_stack $Optional<Element>, loc "backward.swift":68:17, scope 2

test/DebugInfo/debug_info_expression.sil

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ bb0:
2525
%3 = struct_element_addr %2 : $*MyStruct, #MyStruct.x, loc "file.swift":9:17, scope 1
2626
// CHECK: %[[FIELD_X:.*]] = getelementptr {{.*}} %[[MY_STRUCT]]
2727
// CHECK-SIL: debug_value %{{[0-9]+}} : $*Builtin.Int64
28-
// CHECK-SIL-SAME: (name "my_struct", loc "file.swift":8:9, scope {{[0-9]+}})
28+
// CHECK-SIL-SAME: (name "my_struct", loc "file.swift":8:9)
2929
// CHECK-SIL-SAME: type $MyStruct, expr op_deref:op_fragment:#MyStruct.x
3030
debug_value %3 : $*Builtin.Int64, var, (name "my_struct", loc "file.swift":8:9, scope 1), type $MyStruct, expr op_deref:op_fragment:#MyStruct.x, loc "file.swift":9:17, scope 1
3131

@@ -51,7 +51,7 @@ bb0:
5151
// CHECK: %[[MY_STRUCT:.+]] = alloca %{{.*}}MyStruct
5252
// CHECK: llvm.dbg.declare(metadata ptr %[[MY_STRUCT]], metadata ![[VAR_DECL_MD:[0-9]+]]
5353
// CHECK-SIL: alloc_stack $Int, var
54-
// CHECK-SIL-SAME: (name "my_struct", loc "file.swift":15:9, scope {{[0-9]+}})
54+
// CHECK-SIL-SAME: (name "my_struct", loc "file.swift":15:9)
5555
// CHECK-SIL-SAME: type $MyStruct, expr op_fragment:#MyStruct.x
5656
%field_x = alloc_stack $Int, var, (name "my_struct", loc "file.swift":15:9, scope 2), type $MyStruct, expr op_fragment:#MyStruct.x, loc "file.swift":16:17, scope 2
5757
// CHECK: %[[FIELD_X:.+]] = alloca %TSi

test/DebugInfo/sroa_mem2reg.sil

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ bb0(%0 : $Int64, %1 : $Int64):
5757
// CHECK-MEM2REG: %[[FIELD_Y:[0-9]+]] = struct_extract %[[STRUCT]] : $MyStruct, #MyStruct.y, loc "sroa.swift":8:21
5858
store %1 to %15 : $*Int64, loc "sroa.swift":10:17, scope 2
5959
// Make sure the struct fields' SSA values are properly connected to the source variables via op_fragment
60-
// CHECK-MEM2REG: debug_value %[[FIELD_X]] : $Int64, var, (name "my_struct", loc "sroa.swift":8:9, scope 2), type $*MyStruct, expr op_fragment:#MyStruct.x
61-
// CHECK-MEM2REG: debug_value %[[FIELD_Y]] : $Int64, var, (name "my_struct", loc "sroa.swift":8:9, scope 2), type $*MyStruct, expr op_fragment:#MyStruct.y
60+
// CHECK-MEM2REG: debug_value %[[FIELD_X]] : $Int64, var, (name "my_struct", loc "sroa.swift":8:9), type $*MyStruct, expr op_fragment:#MyStruct.x
61+
// CHECK-MEM2REG: debug_value %[[FIELD_Y]] : $Int64, var, (name "my_struct", loc "sroa.swift":8:9), type $*MyStruct, expr op_fragment:#MyStruct.y
6262
// CHECK-IR: call void @llvm.dbg.value(metadata i64 %0
6363
// CHECK-IR-SAME: metadata ![[STRUCT_MD:[0-9]+]]
6464
// CHECK-IR-SAME: !DIExpression(DW_OP_LLVM_fragment, 0, 64)
6565
// CHECK-IR: call void @llvm.dbg.value(metadata i64 %1
6666
// CHECK-IR-SAME: metadata ![[STRUCT_MD]]
6767
// CHECK-IR-SAME: !DIExpression(DW_OP_LLVM_fragment, 64, 64)
6868
// Make sure function arguments' SSA values are also properly connected to the source variables
69-
// CHECK-MEM2REG: debug_value %0 : $Int64, var, (name "my_struct", loc "sroa.swift":8:9, scope 2), type $*MyStruct, expr op_fragment:#MyStruct.x
70-
// CHECK-MEM2REG: debug_value %1 : $Int64, var, (name "my_struct", loc "sroa.swift":8:9, scope 2), type $*MyStruct, expr op_fragment:#MyStruct.y
69+
// CHECK-MEM2REG: debug_value %0 : $Int64, var, (name "my_struct", loc "sroa.swift":8:9), type $*MyStruct, expr op_fragment:#MyStruct.x
70+
// CHECK-MEM2REG: debug_value %1 : $Int64, var, (name "my_struct", loc "sroa.swift":8:9), type $*MyStruct, expr op_fragment:#MyStruct.y
7171
// CHECK-IR: call void @llvm.dbg.value(metadata i64 %0, metadata ![[ARG1_MD:[0-9]+]]
7272
// CHECK-IR: call void @llvm.dbg.value(metadata i64 %1, metadata ![[ARG2_MD:[0-9]+]]
7373
dealloc_stack %4 : $*MyStruct, loc "sroa.swift":8:9, scope 2

0 commit comments

Comments
 (0)