Skip to content

Commit 62ac21d

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 9d7f735 commit 62ac21d

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
@@ -1065,7 +1065,7 @@ class SILBuilder {
10651065
SILLocation Loc, SILValue src, SILDebugVariable Var,
10661066
PoisonRefs_t poisonRefs = DontPoisonRefs,
10671067
UsesMoveableValueDebugInfo_t wasMoved = DoesNotUseMoveableValueDebugInfo,
1068-
bool trace = false);
1068+
bool trace = false, bool overrideLoc = true);
10691069
DebugValueInst *createDebugValueAddr(
10701070
SILLocation Loc, SILValue src, SILDebugVariable Var,
10711071
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
@@ -1628,6 +1628,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
16281628
auto HasTrace = (Attr >> 2) & 0x1;
16291629

16301630
bool HaveDebugVar = (Attr >> 3) & 0x1;
1631+
bool HasLoc = false;
16311632

16321633
SILDebugVariable DebugVar;
16331634
if (HaveDebugVar) {
@@ -1636,7 +1637,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
16361637
unsigned IsDenseMapSingleton = (Attr >> 5) & 0x3;
16371638
bool HasType = (Attr >> 7) & 0x1;
16381639
bool HasScope = (Attr >> 8) & 0x1;
1639-
bool HasLoc = (Attr >> 9) & 0x1;
1640+
HasLoc = (Attr >> 9) & 0x1;
16401641

16411642
auto VarName = MF->getIdentifierText(ListOfValues[2]);
16421643
auto ArgNo = ListOfValues[3];
@@ -1703,8 +1704,9 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
17031704
DebugVar.isDenseMapSingleton = IsDenseMapSingleton;
17041705
}
17051706

1706-
ResultInst = Builder.createDebugValue(Loc, Value, DebugVar, PoisonRefs,
1707-
UsesMoveableValDebugInfo, HasTrace);
1707+
ResultInst =
1708+
Builder.createDebugValue(Loc, Value, DebugVar, PoisonRefs,
1709+
UsesMoveableValDebugInfo, HasTrace, !HasLoc);
17081710

17091711
break;
17101712
}

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)