Skip to content

Commit e159c0f

Browse files
Merge pull request #39345 from adrian-prantl/83163549
Enter a new debug scope for every let binding
2 parents 6766824 + 8bb292e commit e159c0f

File tree

6 files changed

+25
-22
lines changed

6 files changed

+25
-22
lines changed

lib/SILGen/SILGenFunction.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
308308
std::vector<PatternMatchContext*> SwitchStack;
309309
/// Keep track of our current nested scope.
310310
///
311-
/// The boolean tracks whether this is a 'guard' scope, which should be
311+
/// The boolean tracks whether this is a binding scope, which should be
312312
/// popped automatically when we leave the innermost BraceStmt scope.
313313
std::vector<llvm::PointerIntPair<const SILDebugScope *, 1>> DebugScopeStack;
314314

@@ -583,19 +583,20 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
583583

584584
/// Enter the debug scope for \p Loc, creating it if necessary.
585585
///
586-
/// \param isGuardScope If true, this is a scope for the bindings introduced by
587-
/// a 'guard' statement. This scope ends when the next innermost BraceStmt ends.
588-
void enterDebugScope(SILLocation Loc, bool isGuardScope=false) {
589-
auto *Parent =
590-
DebugScopeStack.size() ? DebugScopeStack.back().getPointer() : F.getDebugScope();
586+
/// \param isBindingScope If true, this is a scope for the bindings introduced
587+
/// by a let expression. This scope ends when the next innermost BraceStmt
588+
/// ends.
589+
void enterDebugScope(SILLocation Loc, bool isBindingScope = false) {
590+
auto *Parent = DebugScopeStack.size() ? DebugScopeStack.back().getPointer()
591+
: F.getDebugScope();
591592
auto *DS = Parent;
592593
// Don't create a pointless scope for the function body's BraceStmt.
593594
if (!DebugScopeStack.empty())
594595
// Don't nest a scope for Loc under Parent unless it's actually different.
595596
if (RegularLocation(DS->getLoc()) != RegularLocation(Loc))
596597
DS = new (SGM.M)
597598
SILDebugScope(RegularLocation(Loc), &getFunction(), DS);
598-
DebugScopeStack.emplace_back(DS, isGuardScope);
599+
DebugScopeStack.emplace_back(DS, isBindingScope);
599600
B.setCurrentDebugScope(DS);
600601
}
601602

lib/SILGen/SILGenStmt.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -784,9 +784,6 @@ void StmtEmitter::visitGuardStmt(GuardStmt *S) {
784784
// Emit the condition bindings, branching to the bodyBB if they fail.
785785
auto NumFalseTaken = SGF.loadProfilerCount(S->getBody());
786786
auto NumNonTaken = SGF.loadProfilerCount(S);
787-
// Begin a new 'guard' scope, which is popped when the next innermost debug
788-
// scope ends.
789-
SGF.enterDebugScope(S, /*isGuardScope=*/true);
790787
SGF.emitStmtCondition(S->getCond(), bodyBB, S, NumNonTaken, NumFalseTaken);
791788
}
792789

lib/SILGen/SwitchEnumBuilder.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ void SwitchEnumBuilder::emit() && {
140140
// Don't allow cleanups to escape the conditional block.
141141
SwitchCaseFullExpr presentScope(builder.getSILGenFunction(),
142142
CleanupLocation(loc), branchDest);
143+
// Begin a new binding scope, which is popped when the next innermost debug
144+
// scope ends. The cleanup location loc isn't the perfect source location
145+
// but it's close enough.
146+
builder.getSILGenFunction().enterDebugScope(loc,
147+
/*isBindingScope=*/true);
143148

144149
builder.emitBlock(caseBlock);
145150

test/DebugInfo/guard-let-scope.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
func f(c: AnyObject?) {
44
let x = c
55
// CHECK: sil_scope [[S1:[0-9]+]] { {{.*}} parent @{{.*}}1f
6-
// CHECK: sil_scope [[S2:[0-9]+]] { loc "{{.*}}":[[@LINE+3]]:3 parent [[S1]] }
6+
// CHECK: sil_scope [[S2:[0-9]+]] { loc "{{.*}}":[[@LINE+3]]:17 parent [[S1]] }
77
// CHECK: debug_value %{{.*}} : $Optional<AnyObject>, let, name "x"{{.*}} scope [[S1]]
88
// CHECK: debug_value %{{.*}} : $AnyObject, let, name "x", {{.*}} scope [[S2]]
99
guard let x = x else {

test/SILOptimizer/constantprop-wrongscope.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
// Make sure that the destroy_addr instruction has the same scope of the
1313
// instructions surrounding it.
1414

15-
// CHECK: destroy_addr %7 : $*Any, loc {{.*}}:22:19, scope 1
16-
// CHECK: dealloc_stack %12 : $*Optional<Any>, loc {{.*}}:22:23, scope 1
17-
// CHECK: dealloc_stack %7 : $*Any, loc {{.*}}:22:23, scope 1
18-
// CHECK: dealloc_stack %6 : $*A, loc {{.*}}:22:7, scope 1
15+
// CHECK: destroy_addr %7 : $*Any, loc {{.*}}:22:19, scope 2
16+
// CHECK: dealloc_stack %12 : $*Optional<Any>, loc {{.*}}:22:23, scope 2
17+
// CHECK: dealloc_stack %7 : $*Any, loc {{.*}}:22:23, scope 2
18+
// CHECK: dealloc_stack %6 : $*A, loc {{.*}}:22:7, scope 2
1919

2020
import Foundation
2121
func indexedSubscripting(b b: B, idx: Int, a: A) {

test/SILOptimizer/definite-init-wrongscope.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ public class M {
3333

3434
// CHECK-LABEL: sil [ossa] @$s3del1MC4fromAcA12WithDelegate_p_tKcfc : $@convention(method) (@in WithDelegate, @owned M) -> (@owned M, @error Error)
3535

36-
// CHECK: [[I:%.*]] = integer_literal $Builtin.Int2, 1, loc {{.*}}:23:12, scope 2
37-
// CHECK: [[V:%.*]] = load [trivial] %2 : $*Builtin.Int2, loc {{.*}}:23:12, scope 2
38-
// CHECK: [[OR:%.*]] = builtin "or_Int2"([[V]] : $Builtin.Int2, [[I]] : $Builtin.Int2) : $Builtin.Int2, loc {{.*}}:23:12, scope 2
39-
// CHECK: store [[OR]] to [trivial] %2 : $*Builtin.Int2, loc {{.*}}:23:12, scope 2
40-
// CHECK: store %{{.*}} to [init] %{{.*}} : $*C, loc {{.*}}:26:20, scope 2
36+
// CHECK: [[I:%.*]] = integer_literal $Builtin.Int2, 1, loc {{.*}}:23:12, scope 4
37+
// CHECK: [[V:%.*]] = load [trivial] %2 : $*Builtin.Int2, loc {{.*}}:23:12, scope 4
38+
// CHECK: [[OR:%.*]] = builtin "or_Int2"([[V]] : $Builtin.Int2, [[I]] : $Builtin.Int2) : $Builtin.Int2, loc {{.*}}:23:12, scope 4
39+
// CHECK: store [[OR]] to [trivial] %2 : $*Builtin.Int2, loc {{.*}}:23:12, scope 4
40+
// CHECK: store %{{.*}} to [init] %{{.*}} : $*C, loc {{.*}}:26:20, scope 4
4141

4242
// Make sure the dealloc_stack gets the same scope of the instructions surrounding it.
4343

44-
// CHECK: destroy_addr %0 : $*WithDelegate, loc {{.*}}:29:5, scope 2
45-
// CHECK: dealloc_stack %2 : $*Builtin.Int2, loc {{.*}}:23:12, scope 2
44+
// CHECK: destroy_addr %0 : $*WithDelegate, loc {{.*}}:29:5, scope 4
45+
// CHECK: dealloc_stack %2 : $*Builtin.Int2, loc {{.*}}:23:12, scope 4
4646
// CHECK: throw %{{.*}} : $Error, loc {{.*}}:23:12, scope 1

0 commit comments

Comments
 (0)