Skip to content

Commit 4174e21

Browse files
committed
Remove the redundant lexical scope for function-body brace statements.
Before this patch every Swift function would contain a top-level DW_TAG_lexical_scope that didn't provide any useful information, used extra space in the debug info and prevented local variables from showing up in virtual async backtraces.
1 parent d2a9b68 commit 4174e21

18 files changed

+66
-81
lines changed

lib/SILGen/SILGenFunction.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,11 +600,12 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
600600
auto *Parent =
601601
DebugScopeStack.size() ? DebugScopeStack.back().getPointer() : F.getDebugScope();
602602
auto *DS = Parent;
603-
// Don't nest a scope for Loc under Parent unless it's actually different.
604-
if (RegularLocation(DS->getLoc()) != RegularLocation(Loc)) {
605-
DS = new (SGM.M)
603+
// Don't create a pointless scope for the function body's BraceStmt.
604+
if (!DebugScopeStack.empty())
605+
// Don't nest a scope for Loc under Parent unless it's actually different.
606+
if (RegularLocation(DS->getLoc()) != RegularLocation(Loc))
607+
DS = new (SGM.M)
606608
SILDebugScope(RegularLocation(Loc), &getFunction(), DS);
607-
}
608609
DebugScopeStack.emplace_back(DS, isGuardScope);
609610
B.setCurrentDebugScope(DS);
610611
}

test/DebugInfo/any.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ func markUsed<T>(_ t: T) {}
44

55
func main() {
66
// CHECK: call void @llvm.dbg.declare(metadata %Any* {{.*}}, metadata ![[S:.*]], metadata !DIExpression()), !dbg ![[DBG:.*]]
7-
// CHECK: ![[S]] = !DILocalVariable(name: "s", {{.*}}line: [[@LINE+3]]
8-
// CHECK: ![[SCOPE:.*]] = distinct !DILexicalBlock({{.*}}line: 5, column: 13)
9-
// CHECK: ![[DBG]] = !DILocation(line: [[@LINE+1]], column: 7, scope: ![[SCOPE:.*]])
7+
// CHECK: ![[S]] = !DILocalVariable(name: "s", {{.*}}line: [[@LINE+2]]
8+
// CHECK: ![[DBG]] = !DILocation(line: [[@LINE+1]], column: 7,
109
var s: Any = "hello world"
1110
var n: Any = 12
1211
var t: Any = (1,2)

test/DebugInfo/basic.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@ public
4242
func foo(_ a: Int64, _ b: Int64) -> Int64 {
4343
var a = a
4444
var b = b
45-
// CHECK-DAG: !DILexicalBlock(scope: ![[FOO]],{{.*}} line: [[@LINE-3]]
46-
// CHECK-DAG: ![[ASCOPE:.*]] = !DILocation(line: [[@LINE-4]],{{.*}} scope: ![[FOO]])
45+
// CHECK-DAG: ![[ALOC:.*]] = !DILocation(line: [[@LINE-3]],{{.*}} scope: ![[FOO]])
4746
// Check that a is the first and b is the second argument.
4847
// CHECK-DAG: store i64 %0, i64* [[AADDR:.*]], align
4948
// CHECK-DAG: store i64 %1, i64* [[BADDR:.*]], align
5049
// CHECK-DAG: [[AVAL:%.*]] = getelementptr inbounds {{.*}}, [[AMEM:.*]], i32 0, i32 0
5150
// CHECK-DAG: [[BVAL:%.*]] = getelementptr inbounds {{.*}}, [[BMEM:.*]], i32 0, i32 0
52-
// CHECK-DAG: call void @llvm.dbg.declare(metadata i64* [[AADDR]], metadata ![[AARG:.*]], metadata !DIExpression()), !dbg ![[ASCOPE]]
51+
// CHECK-DAG: call void @llvm.dbg.declare(metadata i64* [[AADDR]], metadata ![[AARG:.*]], metadata !DIExpression()), !dbg ![[ALOC]]
5352
// CHECK-DAG: call void @llvm.dbg.declare(metadata i64* [[BADDR]], metadata ![[BARG:.*]], metadata !DIExpression())
5453
// CHECK-DAG: ![[AARG]] = !DILocalVariable(name: "a", arg: 1
5554
// CHECK-DAG: ![[BARG]] = !DILocalVariable(name: "b", arg: 2

test/DebugInfo/columns.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ public func foo(_ a: Int64, _ b: Int64) -> Int64 { // line 5
1515
}
1616

1717
// CHECK-DAG: !DISubprogram(name: "foo",{{.*}} line: 5,
18-
// DWARF-CHECK-DAG: !DILexicalBlock({{.*}}, line: 5, column: 50)
1918
// DWARF-CHECK-DAG: ![[DIV]] = !DILocation(line: 8, column: 17,
2019
// DWARF-CHECK-DAG: ![[ADD]] = !DILocation(line: 8, column: 13,
2120
// DWARF-CHECK-DAG: ![[SLT]] = !DILocation(line: 10, column: 9,
2221
// DWARF-CHECK-DAG: !DILexicalBlock({{.*}}, line: 10, column: 14)
2322
// DWARF-CHECK-DAG: ![[SUB]] = !DILocation(line: 12, column: 14,
2423

25-
// CV-CHECK-DAG: !DILexicalBlock({{.*}}, line: 5)
2624
// CV-CHECK-DAG: ![[DIV]] = !DILocation(line: 8, scope:
2725
// CV-CHECK-DAG: ![[SLT]] = !DILocation(line: 10, scope:
2826
// CV-CHECK-DAG: !DILexicalBlock({{.*}}, line: 10)

test/DebugInfo/doubleinlines.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ func callCondFail(arg: Builtin.Int1, msg: Builtin.RawPointer) {
1313
// CHECK: define hidden swiftcc void @"$s13DoubleInlines12callCondFail3arg3msgyBi1__BptF"{{.*}} !dbg ![[FUNCSCOPE:.*]] {
1414
// CHECK: tail call void asm sideeffect "", "n"(i32 0) #3, !dbg ![[SCOPEONE:.*]]
1515
// CHECK: ![[FUNCSCOPEOTHER:.*]] = distinct !DISubprogram(name: "condFail",{{.*}}
16-
// CHECK: ![[SCOPEFIVE:.*]] = distinct !DILexicalBlock(scope: ![[FUNCSCOPE]], file: ![[FILE:.*]], line: 9)
17-
// CHECK: ![[SCOPESIX:.*]] = distinct !DILexicalBlock(scope: ![[FUNCSCOPEOTHER]], file: ![[FILE]], line: 5)
1816
// CHECK: ![[SCOPEONE]] = !DILocation(line: 0, scope: ![[SCOPETWO:.*]], inlinedAt: ![[SCOPETHREE:.*]])
1917
// CHECK: ![[SCOPETHREE]] = !DILocation(line: 6, scope: ![[SCOPEFOUR:.*]])
20-
// CHECK: ![[SCOPEFOUR]] = distinct !DILexicalBlock(scope: ![[SCOPEFIVE]], file: ![[FILE]], line: 10)
18+
// CHECK: ![[SCOPEFOUR]] = distinct !DILexicalBlock(scope: ![[FUNCSCOPE]], file: !{{.*}}, line: 10)

test/DebugInfo/guard-let-scope.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// RUN: | %FileCheck %s
33
func f(c: AnyObject?) {
44
let x = c
5-
// CHECK: sil_scope [[S1:[0-9]+]] { loc "{{.*}}":[[@LINE-2]]:23 parent
5+
// CHECK: sil_scope [[S1:[0-9]+]] { {{.*}} parent @{{.*}}1f
66
// CHECK: sil_scope [[S2:[0-9]+]] { loc "{{.*}}":[[@LINE+3]]:3 parent [[S1]] }
7-
// CHECK: debug_value %0 : $Optional<AnyObject>, let, name "x"{{.*}} scope [[S1]]
8-
// CHECK: debug_value %6 : $AnyObject, let, name "x", {{.*}} scope [[S2]]
7+
// CHECK: debug_value %{{.*}} : $Optional<AnyObject>, let, name "x"{{.*}} scope [[S1]]
8+
// CHECK: debug_value %{{.*}} : $AnyObject, let, name "x", {{.*}} scope [[S2]]
99
guard let x = x else {
1010
fatalError(".")
1111
}

test/DebugInfo/inlined-generics-basic.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,11 @@ func yes() -> Bool { return true }
2929
}
3030

3131
// SIL: sil_scope [[F:.*]] { {{.*}}parent @$s1A1CC1fyyqd__lF
32-
// SIL: sil_scope [[F1:.*]] { loc "f.swift":1:28 parent [[F]] }
33-
// SIL: sil_scope [[F1G:.*]] { loc "f.swift":2:5 parent [[F1]] }
32+
// SIL: sil_scope [[F1G:.*]] { loc "f.swift":2:5 parent [[F]] }
3433
// SIL: sil_scope [[F1G1:.*]] { loc "g.swift":2:3 {{.*}}inlined_at [[F1G]] }
3534
// SIL: sil_scope [[F1G3:.*]] { loc "g.swift":3:5 {{.*}}inlined_at [[F1G]] }
3635
// SIL: sil_scope [[F1G3H:.*]] { loc "h.swift":1:24
3736
// SIL-SAME: parent @{{.*}}1h{{.*}} inlined_at [[F1G3]] }
38-
// SIL: sil_scope [[F1G3H1:.*]] { loc "h.swift":1:37
39-
// SIL-SAME: parent [[F1G3H]] inlined_at [[F1G3]] }
4037

4138
#sourceLocation(file: "C.swift", line: 1)
4239
public class C<R> {
@@ -49,7 +46,7 @@ public class C<R> {
4946
public func f<S>(_ s: S) {
5047
// SIL: debug_value_addr %0 : $*S, let, name "s", argno 1,{{.*}} scope [[F]]
5148
// SIL: function_ref {{.*}}yes{{.*}} scope [[F1G1]]
52-
// SIL: function_ref {{.*}}use{{.*}} scope [[F1G3H1]]
49+
// SIL: function_ref {{.*}}use{{.*}} scope [[F1G3H]]
5350
// IR: dbg.value(metadata %swift.type* %S, metadata ![[MD_1_0:[0-9]+]]
5451
// IR: dbg.value(metadata %swift.opaque* %0, metadata ![[S:[0-9]+]]
5552
// IR: dbg.value(metadata %swift.opaque* %0, metadata ![[GS_T:[0-9]+]]

test/DebugInfo/inlinedAt.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,10 @@ public func f(_ i : Int) -> Int { // 301
4141
// CHECK: ![[G:.*]] = distinct !DISubprogram(name: "g",
4242

4343
// CHECK: ![[L3:.*]] = !DILocation(line: 302, column: 10,
44-
// CHECK-SAME: scope: ![[F_SCOPE:.*]])
45-
// CHECK: ![[F_SCOPE]] = distinct !DILexicalBlock(scope: ![[F]],
46-
// CHECK-SAME: line: 301, column: 33)
47-
// CHECK: ![[G_SCOPE:.*]] = distinct !DILexicalBlock(scope: ![[G]],
48-
// CHECK-SAME: line: 201, column: 26)
44+
// CHECK-SAME: scope: ![[F:.*]])
4945
// CHECK: ![[H:.*]] = distinct !DISubprogram(name: "h",
5046
// CHECK: ![[L1]] = !DILocation(line: 101, column: 8, scope: ![[H]],
5147
// CHECK-SAME: inlinedAt: ![[L2:.*]])
52-
// CHECK: ![[L2]] = !DILocation(line: 203, column: 10, scope: ![[G_SCOPE]],
48+
// CHECK: ![[L2]] = !DILocation(line: 203, column: 10, scope: ![[G]],
5349
// CHECK-SAME: inlinedAt: ![[L3]])
5450

test/DebugInfo/inlinescopes.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ func transparent(_ x: Int64) -> Int64 { return noinline(x) }
2424

2525
@inline(__always)
2626
func inlined(_ x: Int64) -> Int64 {
27-
// CHECK-DAG: ![[CALL]] = !DILocation(line: [[@LINE+2]], column: {{.*}}, scope: ![[SCOPE:.*]], inlinedAt: ![[INLINED:.*]])
28-
// CHECK-DAG: ![[SCOPE:.*]] = distinct !DILexicalBlock(
27+
// CHECK-DAG: ![[CALL]] = !DILocation(line: [[@LINE+3]], column: {{.*}}, scope: ![[SCOPE:.*]], inlinedAt: ![[INLINED:.*]])
28+
// Check if the inlined and removed function still has the correct linkage name.
29+
// CHECK-DAG: ![[SCOPE]] = distinct !DISubprogram(name: "inlined", linkageName: "$s4main7inlinedys5Int64VADF"
2930
let result = transparent(x)
3031
// TRANSPARENT-CHECK-NOT: !DISubprogram(name: "transparent"
3132
return result
3233
}
3334
// CHECK-DAG: !DIGlobalVariable(name: "y",{{.*}} file: ![[TOPLEVEL]],{{.*}} line: [[@LINE+1]]
3435
public let y = inlined(x)
35-
36-
// Check if the inlined and removed function still has the correct linkage name.
37-
// CHECK-DAG: !DISubprogram(name: "inlined", linkageName: "$s4main7inlinedys5Int64VADF"

test/DebugInfo/mandatory-inlining-ownership.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
// RUN: -sil-print-after=mandatory-inlining \
55
// RUN: -Xllvm -sil-print-debuginfo -o /dev/null 2>&1 | %FileCheck %s
66

7-
// CHECK: begin_borrow {{.*}} : $OSLog, loc {{.*}}, scope 5
8-
// CHECK: tuple (), loc {{.*}}, scope 5
9-
// CHECK: end_borrow %9 : $OSLog, loc {{.*}}, scope 5
7+
// CHECK: begin_borrow {{.*}} : $OSLog, loc {{.*}}, scope 4
8+
// CHECK: tuple (), loc {{.*}}, scope 4
9+
// CHECK: end_borrow %9 : $OSLog, loc {{.*}}, scope 4
1010

1111
import os
1212

0 commit comments

Comments
 (0)