Skip to content

Commit 6357d12

Browse files
Merge pull request #73009 from Snowy1803/debuginfo-small-fixes
[Debuginfo] Small fixes
2 parents e3bd21a + 4a543c8 commit 6357d12

File tree

7 files changed

+78
-18
lines changed

7 files changed

+78
-18
lines changed

include/swift/SIL/SILCloner.h

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,16 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
338338
registerLocalArchetypeRemapping(archetypeTy, replacementTy);
339339
}
340340

341-
// SILCloner will take care of debug scope on the instruction
342-
// and this helper will remap the auxiliary debug scope too, if there is any.
343-
void remapDebugVarInfo(DebugVarCarryingInst DbgVarInst) {
344-
if (!DbgVarInst)
341+
/// SILCloner will take care of debug scope on the instruction
342+
/// and this helper will remap the auxiliary debug scope too, if there is any.
343+
void remapDebugVariable(std::optional<SILDebugVariable> &VarInfo) {
344+
if (!VarInfo)
345345
return;
346-
auto VarInfo = DbgVarInst.getVarInfo();
347-
if (VarInfo && VarInfo->Scope)
348-
DbgVarInst.setDebugVarScope(getOpScope(VarInfo->Scope));
346+
if (VarInfo->Type)
347+
VarInfo->Type = getOpType(*VarInfo->Type);
348+
// Don't remap locations for debug values.
349+
if (VarInfo->Scope)
350+
VarInfo->Scope = getOpScope(VarInfo->Scope);
349351
}
350352

351353
ProtocolConformanceRef getOpConformance(Type ty,
@@ -879,8 +881,7 @@ SILCloner<ImplClass>::visitAllocStackInst(AllocStackInst *Inst) {
879881
Loc = MandatoryInlinedLocation::getAutoGeneratedLocation();
880882
VarInfo = std::nullopt;
881883
}
882-
if (VarInfo && VarInfo->Type)
883-
VarInfo->Type = getOpType(*VarInfo->Type);
884+
remapDebugVariable(VarInfo);
884885
auto *NewInst = getBuilder().createAllocStack(
885886
Loc, getOpType(Inst->getElementType()), VarInfo,
886887
Inst->hasDynamicLifetime(), Inst->isLexical(), Inst->isFromVarDecl(),
@@ -890,7 +891,6 @@ SILCloner<ImplClass>::visitAllocStackInst(AllocStackInst *Inst) {
890891
true
891892
#endif
892893
);
893-
remapDebugVarInfo(DebugVarCarryingInst(NewInst));
894894
recordClonedInstruction(Inst, NewInst);
895895
}
896896

@@ -1428,14 +1428,12 @@ SILCloner<ImplClass>::visitDebugValueInst(DebugValueInst *Inst) {
14281428
return;
14291429

14301430
// Since we want the debug info to survive, we do not remap the location here.
1431-
SILDebugVariable VarInfo = *Inst->getVarInfo();
1431+
std::optional<SILDebugVariable> VarInfo = Inst->getVarInfo();
14321432
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
1433-
if (VarInfo.Type)
1434-
VarInfo.Type = getOpType(*VarInfo.Type);
1433+
remapDebugVariable(VarInfo);
14351434
auto *NewInst = getBuilder().createDebugValue(
1436-
Inst->getLoc(), getOpValue(Inst->getOperand()), VarInfo,
1435+
Inst->getLoc(), getOpValue(Inst->getOperand()), *VarInfo,
14371436
Inst->poisonRefs(), Inst->usesMoveableValueDebugInfo(), Inst->hasTrace());
1438-
remapDebugVarInfo(DebugVarCarryingInst(NewInst));
14391437
recordClonedInstruction(Inst, NewInst);
14401438
}
14411439
template<typename ImplClass>

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3180,7 +3180,7 @@ void IRGenDebugInfoImpl::emitVariableDeclaration(
31803180

31813181
// Create the descriptor for the variable.
31823182
unsigned DVarLine = DInstLine;
3183-
uint16_t DVarCol = 0;
3183+
uint16_t DVarCol = DInstLoc.Column;
31843184
if (VarInfo.Loc) {
31853185
auto DVarLoc = getStartLocation(VarInfo.Loc);
31863186
DVarLine = DVarLoc.Line;

lib/SIL/IR/SILPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
14121412
else
14131413
*this << ", var";
14141414

1415-
if ((Var->Loc || Var->Scope) && SM) {
1415+
if ((Var->Loc || Var->Scope) && SM && Ctx.printDebugInfo()) {
14161416
*this << ", (name \"" << Var->Name << '"';
14171417
if (Var->Loc)
14181418
printDebugLocRef(*Var->Loc, *SM);

lib/SILOptimizer/SILCombiner/SILCombine.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@ void SwiftPassInvocation::eraseInstruction(SILInstruction *inst) {
615615
if (silCombiner) {
616616
silCombiner->eraseInstFromFunction(*inst);
617617
} else {
618+
swift::salvageDebugInfo(inst);
618619
if (inst->isStaticInitializerInst()) {
619620
inst->getParent()->erase(inst, *getPassManager()->getModule());
620621
} else {

lib/SILOptimizer/Transforms/SILSROA.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ void SROAMemoryUseAnalyzer::chopUpAlloca(std::vector<AllocStackInst *> &Worklist
298298
SILInstruction *User = Operand->getUser();
299299
auto *DVI = dyn_cast<DebugValueInst>(User);
300300
assert(DVI && "getDebugUses should only return DebugValueInst");
301-
SILBuilderWithScope B(DVI);
301+
SILBuilder B(DVI, DVI->getDebugScope());
302302
std::optional<SILDebugVariable> DVIVarInfo = DVI->getVarInfo();
303303
assert(DVIVarInfo && "debug_value without debug info");
304304

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %target-sil-opt %s -dead-store-elimination -enable-sil-verify-all | %FileCheck %s
2+
3+
// REQUIRES: swift_in_compiler
4+
5+
sil_stage canonical
6+
7+
import Swift
8+
import Builtin
9+
10+
// We should be able to replace the local store with a debug_value.
11+
// The dead store elimination pass is written in Swift, and should call
12+
// salvage debug info when removing the store.
13+
14+
// CHECK-LABEL: @trivial_local_dead_store
15+
// CHECK: debug_value %{{[0-9]+}} : $Int, var, name "a"
16+
// CHECK: return
17+
sil hidden @trivial_local_dead_store : $@convention(thin) () -> () {
18+
bb0:
19+
%0 = alloc_stack $Int, var, name "a"
20+
%1 = integer_literal $Builtin.Int64, 1
21+
%2 = struct $Int (%1 : $Builtin.Int64)
22+
store %2 to %0 : $*Int
23+
%4 = tuple ()
24+
dealloc_stack %0 : $*Int
25+
return %4 : $()
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// RUN: %target-swiftc_driver -g -emit-ir %s | %FileCheck %s
2+
sil_stage canonical
3+
4+
import Builtin
5+
import Swift
6+
7+
sil_scope 2 { loc "simple.swift":1:2 parent @test_debug_value : $@convention(thin) (Builtin.Int64) -> () }
8+
9+
// CHECK-LABEL: @test_debug_value
10+
// CHECK-SAME: !dbg ![[FUNC_DI:[0-9]+]]
11+
sil hidden @test_debug_value : $@convention(thin) (Builtin.Int64) -> () {
12+
bb0(%0 : $Builtin.Int64):
13+
// CHECK: @llvm.dbg.declare(metadata ptr
14+
// CHECK-SAME: metadata ![[VAR_DI:[0-9]+]]
15+
// CHECK-SAME: ), !dbg ![[LOC_DI:[0-9]+]]
16+
debug_value %0 : $Builtin.Int64, var, name "x", loc "simple.swift":1:16, scope 2
17+
%zero = integer_literal $Builtin.Int64, 0
18+
debug_value %zero : $Builtin.Int64, var, (name "x", loc "simple.swift":1:16, scope 2), loc "simple.swift":3:4, scope 2
19+
%1 = tuple ()
20+
return %1 : $()
21+
}
22+
23+
// One debug_value uses the VarInfo way, the other uses the instruction way.
24+
// They should both resolve to the same variable, so we should only have one x.
25+
26+
// CHECK: !DISubprogram(name: "test_debug_value"
27+
// CHECK-SAME: retainedNodes: ![[RETAINED:[0-9]+]]
28+
29+
// Only one variable should be retained
30+
// CHECK: ![[RETAINED]] = !{![[VAR_DI]]}
31+
32+
// CHECK-NOT: !DILocalVariable(name: "x"
33+
// CHECK: ![[VAR_DI]] = !DILocalVariable(name: "x"
34+
// CHECK-SAME: line: 1
35+
// CHECK-NOT: !DILocalVariable(name: "x"

0 commit comments

Comments
 (0)