Skip to content

Commit 10057fe

Browse files
committed
SILBuilder by default does not apply loc overrides for debug values, as
in the absence of a debug variable source loc, the instruction's loc gets applied as the debug variable's loc which is incorrect. This patch appropriately serializes the debug variable loc when possible and reverts to the default behavior otherwise. Also adds a test for debug_value serialization/deserialization
1 parent d5f4f7a commit 10057fe

File tree

5 files changed

+60
-10
lines changed

5 files changed

+60
-10
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ class SILBuilder {
10821082
SILLocation Loc, SILValue src, SILDebugVariable Var,
10831083
PoisonRefs_t poisonRefs = DontPoisonRefs,
10841084
UsesMoveableValueDebugInfo_t wasMoved = DoesNotUseMoveableValueDebugInfo,
1085-
bool trace = false);
1085+
bool trace = false, bool overrideLoc = true);
10861086
DebugValueInst *createDebugValueAddr(
10871087
SILLocation Loc, SILValue src, SILDebugVariable Var,
10881088
UsesMoveableValueDebugInfo_t wasMoved = DoesNotUseMoveableValueDebugInfo,

lib/SIL/IR/SILBuilder.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -637,17 +637,24 @@ DebugValueInst *SILBuilder::createDebugValue(SILLocation Loc, SILValue src,
637637
SILDebugVariable Var,
638638
PoisonRefs_t poisonRefs,
639639
UsesMoveableValueDebugInfo_t moved,
640-
bool trace) {
640+
bool trace, bool overrideLoc) {
641641
if (shouldDropVariable(Var, Loc))
642642
return nullptr;
643643

644644
llvm::SmallString<4> Name;
645645

646-
// Debug location overrides cannot apply to debug value instructions.
647-
DebugLocOverrideRAII LocOverride{*this, std::nullopt};
648-
return insert(DebugValueInst::create(
649-
getSILDebugLocation(Loc, true), src, getModule(),
650-
*substituteAnonymousArgs(Name, Var, Loc), poisonRefs, moved, trace));
646+
SILDebugLocation DebugLoc;
647+
if (overrideLoc) {
648+
// Debug location overrides cannot apply to debug value instructions.
649+
DebugLocOverrideRAII LocOverride{*this, std::nullopt};
650+
DebugLoc = getSILDebugLocation(Loc, true);
651+
} else {
652+
DebugLoc = getSILDebugLocation(Loc, true);
653+
}
654+
655+
return insert(DebugValueInst::create(DebugLoc, src, getModule(),
656+
*substituteAnonymousArgs(Name, Var, Loc),
657+
poisonRefs, moved, trace));
651658
}
652659

653660
DebugValueInst *SILBuilder::createDebugValueAddr(

lib/Serialization/DeserializeSIL.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,6 +1625,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
16251625
auto HasTrace = (Attr >> 2) & 0x1;
16261626

16271627
bool HaveDebugVar = (Attr >> 3) & 0x1;
1628+
bool HasLoc = false;
16281629

16291630
SILDebugVariable DebugVar;
16301631
if (HaveDebugVar) {
@@ -1633,7 +1634,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
16331634
unsigned IsDenseMapSingleton = (Attr >> 5) & 0x3;
16341635
bool HasType = (Attr >> 7) & 0x1;
16351636
bool HasScope = (Attr >> 8) & 0x1;
1636-
bool HasLoc = (Attr >> 9) & 0x1;
1637+
HasLoc = (Attr >> 9) & 0x1;
16371638

16381639
auto VarName = MF->getIdentifierText(ListOfValues[2]);
16391640
auto ArgNo = ListOfValues[3];
@@ -1700,8 +1701,9 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
17001701
DebugVar.isDenseMapSingleton = IsDenseMapSingleton;
17011702
}
17021703

1703-
ResultInst = Builder.createDebugValue(Loc, Value, DebugVar, PoisonRefs,
1704-
UsesMoveableValDebugInfo, HasTrace);
1704+
ResultInst =
1705+
Builder.createDebugValue(Loc, Value, DebugVar, PoisonRefs,
1706+
UsesMoveableValDebugInfo, HasTrace, !HasLoc);
17051707

17061708
break;
17071709
}

lib/Serialization/SerializeSIL.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,14 @@ void SILSerializer::writeSILInstruction(const SILInstruction &SI) {
10301030
ListOfValues.push_back(LC.first);
10311031
ListOfValues.push_back(LC.second);
10321032
ListOfValues.push_back(FNameID);
1033+
} else if (RawLoc.isFilenameAndLocation()) {
1034+
// TODO: this is a workaround until rdar://problem/25225083 is
1035+
// implemented.
1036+
attrs |= 1 << 9;
1037+
auto FNameLoc = RawLoc.getFilenameAndLocation();
1038+
ListOfValues.push_back(FNameLoc->line);
1039+
ListOfValues.push_back(FNameLoc->column);
1040+
ListOfValues.push_back(S.addUniquedStringRef(FNameLoc->filename));
10331041
}
10341042
}
10351043

test/Serialization/debug-value.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %{python} %utils/split_file.py -o %t %s
3+
4+
// RUN: %target-swift-frontend -g -experimental-serialize-debug-info -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift -O
5+
// RUN: %target-swift-frontend -g -I %t %t/Main.swift -experimental-serialize-debug-info -O -emit-sil -o - | %FileCheck %s
6+
7+
// BEGIN MyModule.swift
8+
@_alwaysEmitIntoClient @inline(never)
9+
public func bar(_ x: [Int64], sum: Int64) -> Int64 {
10+
var temp = sum
11+
for i in x {
12+
temp += i
13+
}
14+
return temp
15+
}
16+
17+
// BEGIN Main.swift
18+
import MyModule
19+
20+
func test() {
21+
let _ = bar([10], sum: 0)
22+
}
23+
// CHECK: sil {{.*}} @$s8MyModule3bar_3sums5Int64VSayAEG_AEtF : $@convention(thin) (@guaranteed Array<Int64>, Int64) -> Int64 {
24+
25+
// CHECK: debug_value %0 : $Array<Int64>, let, name "x", argno 1, loc "{{.*}}MyModule.swift":2:19, scope
26+
// CHECK: debug_value %1 : $Int64, let, name "sum", argno 2, loc "{{.*}}MyModule.swift":2:31, scope
27+
// CHECK: [[OPERAND_ONE:%[0-9]+]] = struct_extract %1 : $Int64, #Int64._value, loc "{{.*}}MyModule.swift":3:16,
28+
// CHECK: debug_value [[OPERAND_ONE]] : $Builtin.Int64, var, (name "temp", loc "{{.*}}MyModule.swift":3:9, scope {{.*}}), type $Int64, expr op_fragment:#Int64._value, loc "{{.*}}MyModule.swift":3:16, scope
29+
// CHECK: debug_value %0 : $Array<Int64>, var, (name "$i$generator", loc "{{.*}}MyModule.swift":4:14), type $IndexingIterator<Array<Int64>>, expr op_fragment:#IndexingIterator._elements, loc "{{.*}}MyModule.swift":4:14, scope
30+
// CHECK: debug_value {{.*}} : $Builtin.Int64, var, (name "$i$generator", loc "{{.*}}MyModule.swift":4:14), type $IndexingIterator<Array<Int64>>, expr op_fragment:#IndexingIterator._position:op_fragment:#Int._value, loc "{{.*}}MyModule.swift":4:14, scope
31+
// CHECK: debug_value {{.*}} : $Builtin.Int64, var, (name "temp", loc "{{.*}}MyModule.swift":3:9, scope {{.*}}), type $Int64, expr op_fragment:#Int64._value, loc "{{.*}}MyModule.swift":5:14, scope
32+
33+
test()

0 commit comments

Comments
 (0)