Skip to content

Commit 31dd1f8

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 f11154a commit 31dd1f8

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
@@ -1075,7 +1075,7 @@ class SILBuilder {
10751075
SILLocation Loc, SILValue src, SILDebugVariable Var,
10761076
PoisonRefs_t poisonRefs = DontPoisonRefs,
10771077
UsesMoveableValueDebugInfo_t wasMoved = DoesNotUseMoveableValueDebugInfo,
1078-
bool trace = false);
1078+
bool trace = false, bool overrideLoc = true);
10791079
DebugValueInst *createDebugValueAddr(
10801080
SILLocation Loc, SILValue src, SILDebugVariable Var,
10811081
UsesMoveableValueDebugInfo_t wasMoved = DoesNotUseMoveableValueDebugInfo,

lib/SIL/IR/SILBuilder.cpp

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

645645
llvm::SmallString<4> Name;
646646

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

654661
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)