Skip to content

Commit 3dfee33

Browse files
Merge pull request #77655 from felipepiovezan/felipe/fix_code_gen_debug_info
[DebugInfo] Change lowering of SIL instructions to use line 0 when appropriate
2 parents 2affbd8 + f7f869c commit 3dfee33

12 files changed

+59
-49
lines changed

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,15 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
344344
IRGenDebugInfoFormat getDebugInfoFormat() { return Opts.DebugInfoFormat; }
345345

346346
private:
347+
/// Convert a SILLocation into the corresponding LLVM Loc.
348+
FileAndLocation computeLLVMLoc(const SILDebugScope *DS, SILLocation Loc);
349+
350+
/// Compute the LLVM DebugLoc when targeting CodeView. In CodeView, zero is
351+
/// not an artificial line location; attempt to avoid those line locations near
352+
/// user code to reduce the number of breaks in the linetables.
353+
FileAndLocation computeLLVMLocCodeView(const SILDebugScope *DS,
354+
SILLocation Loc);
355+
347356
static StringRef getFilenameFromDC(const DeclContext *DC) {
348357
if (auto *LF = dyn_cast<LoadedFile>(DC))
349358
return LF->getFilename();
@@ -2614,6 +2623,40 @@ bool IRGenDebugInfoImpl::lineEntryIsSane(FileAndLocation DL,
26142623
}
26152624
#endif
26162625

2626+
IRGenDebugInfoImpl::FileAndLocation
2627+
IRGenDebugInfoImpl::computeLLVMLocCodeView(const SILDebugScope *DS,
2628+
SILLocation Loc) {
2629+
// If the scope has not changed and the line number is either zero or
2630+
// artificial, we want to keep the most recent debug location.
2631+
if (DS == LastScope && (Loc.is<ArtificialUnreachableLocation>() ||
2632+
Loc.isLineZero(SM) || Loc.isHiddenFromDebugInfo()))
2633+
return LastFileAndLocation;
2634+
2635+
// Decode the location.
2636+
return decodeFileAndLocation(Loc);
2637+
}
2638+
2639+
IRGenDebugInfoImpl::FileAndLocation
2640+
IRGenDebugInfoImpl::computeLLVMLoc(const SILDebugScope *DS, SILLocation Loc) {
2641+
SILFunction *Fn = DS->getInlinedFunction();
2642+
if (Fn && (Fn->isThunk() || Fn->isTransparent()))
2643+
return {0, 0, CompilerGeneratedFile};
2644+
2645+
if (Opts.DebugInfoFormat == IRGenDebugInfoFormat::CodeView)
2646+
return computeLLVMLocCodeView(DS, Loc);
2647+
2648+
FileAndLocation L =
2649+
Loc.isInPrologue() ? FileAndLocation() : decodeFileAndLocation(Loc);
2650+
2651+
// Otherwise use a line 0 artificial location, but the file from the location.
2652+
if (Loc.isHiddenFromDebugInfo()) {
2653+
L.Line = 0;
2654+
L.Column = 0;
2655+
}
2656+
2657+
return L;
2658+
}
2659+
26172660
void IRGenDebugInfoImpl::setCurrentLoc(IRBuilder &Builder,
26182661
const SILDebugScope *DS,
26192662
SILLocation Loc) {
@@ -2622,38 +2665,7 @@ void IRGenDebugInfoImpl::setCurrentLoc(IRBuilder &Builder,
26222665
if (!Scope)
26232666
return;
26242667

2625-
// NOTE: In CodeView, zero is not an artificial line location. We try to
2626-
// avoid those line locations near user code to reduce the number
2627-
// of breaks in the linetables.
2628-
FileAndLocation L;
2629-
SILFunction *Fn = DS->getInlinedFunction();
2630-
if (Fn && (Fn->isThunk() || Fn->isTransparent())) {
2631-
L = {0, 0, CompilerGeneratedFile};
2632-
} else if (DS == LastScope && Loc.isHiddenFromDebugInfo()) {
2633-
// Reuse the last source location if we are still in the same
2634-
// scope to get a more contiguous line table.
2635-
L = LastFileAndLocation;
2636-
} else if (DS == LastScope &&
2637-
(Loc.is<ArtificialUnreachableLocation>() || Loc.isLineZero(SM)) &&
2638-
Opts.DebugInfoFormat == IRGenDebugInfoFormat::CodeView) {
2639-
// If the scope has not changed and the line number is either zero or
2640-
// artificial, we want to keep the most recent debug location.
2641-
L = LastFileAndLocation;
2642-
} else {
2643-
// Decode the location.
2644-
if (!Loc.isInPrologue() ||
2645-
Opts.DebugInfoFormat == IRGenDebugInfoFormat::CodeView)
2646-
L = decodeFileAndLocation(Loc);
2647-
2648-
// Otherwise use a line 0 artificial location, but the file from the
2649-
// location. If we are emitting CodeView, we do not want to use line zero
2650-
// since it does not represent an artificial line location.
2651-
if (Loc.isHiddenFromDebugInfo() &&
2652-
Opts.DebugInfoFormat != IRGenDebugInfoFormat::CodeView) {
2653-
L.Line = 0;
2654-
L.Column = 0;
2655-
}
2656-
}
2668+
FileAndLocation L = computeLLVMLoc(DS, Loc);
26572669

26582670
if (L.getFilename() != Scope->getFilename()) {
26592671
// We changed files in the middle of a scope. This happens, for

test/Backtracing/Crash.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ struct Crash {
6060
// CHECK-NEXT: 4 [ra] 0x{{[0-9a-f]+}} level1() + {{[0-9]+}} in Crash at {{.*}}/Crash.swift:24:3
6161
// CHECK-NEXT: 5 [ra] 0x{{[0-9a-f]+}} static Crash.main() + {{[0-9]+}} in Crash at {{.*}}/Crash.swift:48:5
6262
// CHECK-NEXT: 6 [ra] [system] 0x{{[0-9a-f]+}} static Crash.$main() + {{[0-9]+}} in Crash at {{.*}}/<compiler-generated>
63-
// CHECK-NEXT: 7 [ra] 0x{{[0-9a-f]+}} main + {{[0-9]+}} in Crash at {{.*}}/Crash.swift
63+
// CHECK-NEXT: 7 [ra] [system] 0x{{[0-9a-f]+}} main + {{[0-9]+}} in Crash at {{.*}}/Crash.swift
6464

6565
// CHECK: Registers:
6666

test/Backtracing/CrashStatic.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct CrashStatic {
5151
// CHECK-NEXT: 4 [ra] 0x{{[0-9a-f]+}} level1() + {{[0-9]+}} in CrashStatic at {{.*}}/CrashStatic.swift:15:3
5252
// CHECK-NEXT: 5 [ra] 0x{{[0-9a-f]+}} static CrashStatic.main() + {{[0-9]+}} in CrashStatic at {{.*}}/CrashStatic.swift:39:5
5353
// CHECK-NEXT: 6 [ra] [system] 0x{{[0-9a-f]+}} static CrashStatic.$main() + {{[0-9]+}} in CrashStatic at {{.*}}/<compiler-generated>
54-
// CHECK-NEXT: 7 [ra] 0x{{[0-9a-f]+}} main + {{[0-9]+}} in CrashStatic at {{.*}}/CrashStatic.swift
54+
// CHECK-NEXT: 7 [ra] [system] 0x{{[0-9a-f]+}} main + {{[0-9]+}} in CrashStatic at {{.*}}/CrashStatic.swift
5555

5656
// CHECK: Registers:
5757

test/Backtracing/CrashWithThunk.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct CrashWithThunk {
3838
// CHECK-NEXT: 1 [ra] [thunk] 0x{{[0-9a-f]+}} thunk for @escaping @callee_guaranteed () -> () + {{[0-9]+}} in CrashWithThunk at {{.*}}/<compiler-generated>
3939
// CHECK-NEXT: 2 [ra] 0x{{[0-9a-f]+}} static CrashWithThunk.main() + {{[0-9]+}} in CrashWithThunk at {{.*}}/CrashWithThunk.swift:29:9
4040
// CHECK-NEXT: 3 [ra] [system] 0x{{[0-9a-f]+}} static CrashWithThunk.$main() + {{[0-9]+}} in CrashWithThunk at {{.*}}/<compiler-generated>
41-
// CHECK-NEXT: 4 [ra] 0x{{[0-9a-f]+}} main + {{[0-9]+}} in CrashWithThunk at {{.*}}/CrashWithThunk.swift
41+
// CHECK-NEXT: 4 [ra] [system] 0x{{[0-9a-f]+}} main + {{[0-9]+}} in CrashWithThunk at {{.*}}/CrashWithThunk.swift
4242

4343
// CHECK: Registers:
4444

test/Backtracing/FatalError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ struct FatalError {
5151
// CHECK-NEXT: 5 [ra] 0x{{[0-9a-f]+}} level1() + {{[0-9]+}} in FatalError at {{.*}}/FatalError.swift:14:3
5252
// CHECK-NEXT: 6 [ra] 0x{{[0-9a-f]+}} static FatalError.main() + {{[0-9]+}} in FatalError at {{.*}}/FatalError.swift:36:5
5353
// CHECK-NEXT: 7 [ra] [system] 0x{{[0-9a-f]+}} static FatalError.$main() + {{[0-9]+}} in FatalError at {{.*}}/<compiler-generated>
54-
// CHECK-NEXT: 8 [ra] 0x{{[0-9a-f]+}} main + {{[0-9]+}} in FatalError at {{.*}}/FatalError.swift
54+
// CHECK-NEXT: 8 [ra] [system] 0x{{[0-9a-f]+}} main + {{[0-9]+}} in FatalError at {{.*}}/FatalError.swift

test/Backtracing/Overflow.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ struct Overflow {
5252
// CHECK-NEXT: 5 [ra] 0x{{[0-9a-f]+}} level1() + {{[0-9]+}} in Overflow at {{.*}}/Overflow.swift:15:3
5353
// CHECK-NEXT: 6 [ra] 0x{{[0-9a-f]+}} static Overflow.main() + {{[0-9]+}} in Overflow at {{.*}}/Overflow.swift:39:5
5454
// CHECK-NEXT: 7 [ra] [system] 0x{{[0-9a-f]+}} static Overflow.$main() + {{[0-9]+}} in Overflow at {{.*}}/<compiler-generated>
55-
// CHECK-NEXT: 8 [ra] 0x{{[0-9a-f]+}} main + {{[0-9]+}} in Overflow at {{.*}}/Overflow.swift
55+
// CHECK-NEXT: 8 [ra] [system] 0x{{[0-9a-f]+}} main + {{[0-9]+}} in Overflow at {{.*}}/Overflow.swift
5656

5757
// CHECK: Registers:
5858

test/Backtracing/StackOverflow.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ struct StackOverflow {
100100

101101
// CHECK: {{[0-9]+}} [ra] 0x{{[0-9a-f]+}} static StackOverflow.main() + {{[0-9]+}} in StackOverflow at {{.*}}/StackOverflow.swift:25:5
102102
// CHECK-NEXT: {{[0-9]+}} [ra] [system] 0x{{[0-9a-f]+}} static StackOverflow.$main() + {{[0-9]+}} in StackOverflow at {{.*}}/<compiler-generated>
103-
// CHECK-NEXT: {{[0-9]+}} [ra] 0x{{[0-9a-f]+}} main + {{[0-9]+}} in StackOverflow at {{.*}}/StackOverflow.swift
103+
// CHECK-NEXT: {{[0-9]+}} [ra] [system] 0x{{[0-9a-f]+}} main + {{[0-9]+}} in StackOverflow at {{.*}}/StackOverflow.swift
104104

105105
// CHECK: Registers:
106106

@@ -129,7 +129,7 @@ struct StackOverflow {
129129

130130
// LIMITED: {{[0-9]+}} [ra] 0x{{[0-9a-f]+}} static StackOverflow.main() + {{[0-9]+}} in StackOverflow at {{.*}}/StackOverflow.swift:25:5
131131
// LIMITED-NEXT: {{[0-9]+}} [ra] [system] 0x{{[0-9a-f]+}} static StackOverflow.$main() + {{[0-9]+}} in StackOverflow at {{.*}}/<compiler-generated>
132-
// LIMITED-NEXT: {{[0-9]+}} [ra] 0x{{[0-9a-f]+}} main + {{[0-9]+}} in StackOverflow at {{.*}}/StackOverflow.swift
132+
// LIMITED-NEXT: {{[0-9]+}} [ra] [system] 0x{{[0-9a-f]+}} main + {{[0-9]+}} in StackOverflow at {{.*}}/StackOverflow.swift
133133

134134
// FRIENDLY: *** Program crashed: Bad pointer dereference at 0x{{[0-9a-f]+}} ***
135135

test/Backtracing/StaticBacktracer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ struct StaticBacktracer {
5252
// CHECK-NEXT: 4 [ra] 0x{{[0-9a-f]+}} level1() + {{[0-9]+}} in StaticBacktracer at {{.*}}/StaticBacktracer.swift:16:3
5353
// CHECK-NEXT: 5 [ra] 0x{{[0-9a-f]+}} static StaticBacktracer.main() + {{[0-9]+}} in StaticBacktracer at {{.*}}/StaticBacktracer.swift:40:5
5454
// CHECK-NEXT: 6 [ra] [system] 0x{{[0-9a-f]+}} static StaticBacktracer.$main() + {{[0-9]+}} in StaticBacktracer at {{.*}}/<compiler-generated>
55-
// CHECK-NEXT: 7 [ra] 0x{{[0-9a-f]+}} main + {{[0-9]+}} in StaticBacktracer at {{.*}}/StaticBacktracer.swift
55+
// CHECK-NEXT: 7 [ra] [system] 0x{{[0-9a-f]+}} main + {{[0-9]+}} in StaticBacktracer at {{.*}}/StaticBacktracer.swift
5656

5757
// CHECK: Registers:
5858

test/DebugInfo/async-let.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public actor Alice {
1717
// CHECK: load ptr, ptr {{.*}} !dbg ![[LET_HOP0:[0-9]+]]
1818

1919
// CHECK: define {{.*}}$s1M5AliceC4callyyYaFSiyYaYbcfu_TY2_{{.*}} !dbg ![[LET_SCOPE1:[0-9]+]]
20-
// CHECK: load ptr, ptr {{.*}} !dbg ![[LET_HOP1:[0-9]+]]
20+
// CHECK: store i64 %{{.*}} !dbg ![[LET_HOP1:[0-9]+]]
2121
public func call() async {
2222
// CHECK: ![[SCOPE0]] = distinct !DISubprogram({{.*}}line: [[@LINE-1]]
2323
// CHECK: ![[HOP0]] = !DILocation(line: [[@LINE+1]], column: 11

test/DebugInfo/autoclosure.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ func &&&&&(lhs: Bool, rhs: @autoclosure () -> Bool) -> Bool {
1919
}
2020

2121
func call_me(_ input: Int64) -> Void {
22-
// rdar://problem/14627460
2322
// An autoclosure should have a line number in the debug info and a scope line of 0.
2423
// CHECK-DAG: !DISubprogram({{.*}}linkageName: "$s11autoclosure7call_meyys5Int64VFSbyXEfu_",{{.*}} spFlags: DISPFlagLocalToUnit | DISPFlagDefinition
25-
// But not in the line table.
26-
// CHECK-DAG: ![[DBG]] = !DILocation(line: [[@LINE+1]],
24+
// Instructions setting up the closure should have a line number of 0.
25+
// CHECK-DAG: ![[DBG]] = !DILocation(line: 0,
2726
if input != 0 &&&&& ( get_truth (input * 2 + 1) > 0 ) {
2827
}
2928

test/DebugInfo/callexpr.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ markUsed(r)
1919
struct MyType {}
2020
func bar(x: MyType = MyType()) {}
2121

22-
// Room for improvement:
23-
// Because the default argument is implicit it inherits the previous source location.
2422
// CHECK2: call {{.*}}MyType{{.*}}, !dbg ![[DEFAULTARG:.*]]
2523
// CHECK2: call {{.*}}bar{{.*}}, !dbg ![[BARCALL:.*]]
26-
bar() // CHECK2: ![[DEFAULTARG]] = !DILocation(line: [[@LINE-9]]
24+
bar() // CHECK2: ![[DEFAULTARG]] = !DILocation(line: 0
2725
// CHECK2: ![[BARCALL]] = !DILocation(line: [[@LINE-1]], column: 1
2826

test/DebugInfo/returnlocation.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,10 @@ public class Class1 {
184184
public required init?() {
185185
print("hello")
186186
// CHECK_INIT: call {{.*}}@"$ss5print_9separator10terminatoryypd_S2StF"{{.*}}, !dbg ![[PRINTLOC:[0-9]+]]
187-
// FIXME: Why doesn't ret have the correct line number?
188-
// CHECK_INIT: ret i{{32|64}} 0, !dbg ![[PRINTLOC]]
187+
// FIXME: ret has an incorrect line number because it is generated with "isHiddenFromDebugInfo"
188+
// CHECK_INIT: ret i{{32|64}} 0, !dbg ![[LINE_0:[0-9]+]]
189189
// CHECK_INIT-DAG: [[PRINTLOC]] = !DILocation(line: [[@LINE-4]]
190+
// CHECK_INIT-DAG: [[LINE_0]] = !DILocation(line: 0
190191
return nil
191192
}
192193
}

0 commit comments

Comments
 (0)