Skip to content

Commit 6136183

Browse files
committed
[DebugInfo] Fix loss of VarDecl in debug values for salvaged stores
When a store is salvaged, its debug_value will have two locations: the location of the store, attached to the debug_value instruction, and the location of the variable, attached to the SILDebugVariable. The getDecl function was using the location of the store, instead of the location of the variable, and so was returning nullptr.
1 parent 4bc672e commit 6136183

File tree

5 files changed

+38
-38
lines changed

5 files changed

+38
-38
lines changed

include/swift/SIL/SILInstruction.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,6 +2083,13 @@ class AllocStackInst final
20832083
/// VarDecl.
20842084
void setIsFromVarDecl() { sharedUInt8().AllocStackInst.fromVarDecl = true; }
20852085

2086+
/// Return the SILLocation for the debug variable.
2087+
SILLocation getVarLoc() const {
2088+
if (hasAuxDebugLocation())
2089+
return *getTrailingObjects<SILLocation>();
2090+
return getLoc();
2091+
}
2092+
20862093
/// Return the debug variable information attached to this instruction.
20872094
std::optional<SILDebugVariable> getVarInfo() const {
20882095
// If we used to have debug info attached but our debug info is now
@@ -5374,6 +5381,13 @@ class DebugValueInst final
53745381
/// or null if we don't have one.
53755382
VarDecl *getDecl() const;
53765383

5384+
/// Return the SILLocation for the debug variable.
5385+
SILLocation getVarLoc() const {
5386+
if (hasAuxDebugLocation())
5387+
return *getTrailingObjects<SILLocation>();
5388+
return getLoc();
5389+
}
5390+
53775391
/// Return the debug variable information attached to this instruction.
53785392
std::optional<SILDebugVariable> getVarInfo() const {
53795393
std::optional<SILType> AuxVarType;

lib/SIL/IR/SILInstructions.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ AllocStackInst *AllocStackInst::create(SILDebugLocation Loc,
286286
}
287287

288288
VarDecl *AllocationInst::getDecl() const {
289+
if (auto ASI = dyn_cast<AllocStackInst>(this)) {
290+
return ASI->getVarLoc().getAsASTNode<VarDecl>();
291+
}
289292
return getLoc().getAsASTNode<VarDecl>();
290293
}
291294

@@ -505,7 +508,7 @@ bool DebugValueInst::exprStartsWithDeref() const {
505508
}
506509

507510
VarDecl *DebugValueInst::getDecl() const {
508-
return getLoc().getAsASTNode<VarDecl>();
511+
return getVarLoc().getAsASTNode<VarDecl>();
509512
}
510513

511514
VarDecl *SILDebugVariable::getDecl() const {

test/SILOptimizer/assemblyvision_remark/cast_remarks.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,11 @@ public func forcedCast3<NS, T>(_ ns: NS) -> T {
3535

3636
public func forcedCast4<NS, T>(_ ns: NS, _ ns2: NS) -> T {
3737
// Make sure the colon info is right so that the arrow is under the a.
38-
//
39-
// Today, we lose that x was assigned ns2. This is flow sensitive information
40-
// that we might be able to recover. We still emit that a runtime cast
41-
// occurred here, just don't say what the underlying value was.
4238
var x = ns
4339
x = ns2
4440
return x as! T // expected-remark @:12 {{unconditional runtime cast of value with type 'NS' to 'T'}}
45-
// expected-note @-9:44 {{of 'ns2'}}
41+
// expected-note @-5:44 {{of 'ns2'}}
42+
// expected-note @-4:7 {{of 'x'}}
4643
}
4744

4845
public func condCast<NS, T>(_ ns: NS) -> T? {
@@ -73,14 +70,11 @@ public func condCast3<NS, T>(_ ns: NS) -> T? {
7370

7471
public func condCast4<NS, T>(_ ns: NS, _ ns2: NS) -> T? {
7572
// Make sure the colon info is right so that the arrow is under the a.
76-
//
77-
// Today, we lose that x was assigned ns2. This is flow sensitive information
78-
// that we might be able to recover. We still emit that a runtime cast
79-
// occurred here, just don't say what the underlying value was.
8073
var x = ns
8174
x = ns2
8275
return x as? T // expected-remark @:12 {{conditional runtime cast of value with type 'NS' to 'T'}}
83-
// expected-note @-9:42 {{of 'ns2'}}
76+
// expected-note @-5:42 {{of 'ns2'}}
77+
// expected-note @-4:7 {{of 'x'}}
8478
}
8579

8680
public func condCast5<NS, T>(_ ns: NS) -> T? {
@@ -252,6 +246,7 @@ public func forcedCast4(_ ns: Existential1, _ ns2: Existential1) -> Existential2
252246
x = ns2
253247
return x as! Existential2 // expected-remark @:12 {{unconditional runtime cast of value with type 'any Existential1' to 'any Existential2'}}
254248
// expected-note @-5:47 {{of 'ns2'}}
249+
// expected-note @-4:7 {{of 'x'}}
255250
}
256251

257252
public func condCast(_ ns: Existential1) -> Existential2? {
@@ -287,6 +282,7 @@ public func condCast4(_ ns: Existential1, _ ns2: Existential1) -> Existential2?
287282
x = ns2
288283
return x as? Existential2 // expected-remark @:12 {{conditional runtime cast of value with type 'any Existential1' to 'any Existential2'}}
289284
// expected-note @-5:45 {{of 'ns2'}}
285+
// expected-note @-4:7 {{of 'x'}}
290286
}
291287

292288
public func condCast5(_ ns: Existential1) -> Existential2? {

test/SILOptimizer/assemblyvision_remark/cast_remarks_objc.swift

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,27 @@ public func forcedCast<NS, T>(_ ns: NS) -> T {
1818

1919
public func forcedCast2<NS, T>(_ ns: NS) -> T {
2020
// Make sure the colon info is right so that the arrow is under the a.
21-
//
22-
// Today, we seem to completely eliminate 'x' here in the debug info. TODO:
23-
// Maybe we can recover this info somehow.
2421
let x = ns
2522
return x as! T // expected-remark @:12 {{unconditional runtime cast of value with type 'NS' to 'T'}}
26-
// expected-note @-7:34 {{of 'ns'}}
23+
// expected-note @-4:34 {{of 'ns'}}
24+
// expected-note @-3:7 {{of 'x'}}
2725
}
2826

2927
public func forcedCast3<NS, T>(_ ns: NS) -> T {
3028
// Make sure the colon info is right so that the arrow is under the a.
31-
//
32-
// Today, we seem to completely eliminate 'x' here in the debug info. TODO:
33-
// Maybe we can recover this info somehow.
3429
var x = ns // expected-warning {{variable 'x' was never mutated}}
3530
return x as! T // expected-remark @:12 {{unconditional runtime cast of value with type 'NS' to 'T'}}
36-
// expected-note @-7:34 {{of 'ns'}}
31+
// expected-note @-4:34 {{of 'ns'}}
32+
// expected-note @-3:7 {{of 'x'}}
3733
}
3834

3935
public func forcedCast4<NS, T>(_ ns: NS, _ ns2: NS) -> T {
4036
// Make sure the colon info is right so that the arrow is under the a.
41-
//
42-
// Today, we lose that x was assigned ns2. This is flow sensitive information
43-
// that we might be able to recover. We still emit that a runtime cast
44-
// occurred here, just don't say what the underlying value was.
4537
var x = ns
4638
x = ns2
4739
return x as! T // expected-remark @:12 {{unconditional runtime cast of value with type 'NS' to 'T'}}
48-
// expected-note @-9:44 {{of 'ns2'}}
40+
// expected-note @-5:44 {{of 'ns2'}}
41+
// expected-note @-4:7 {{of 'x'}}
4942
}
5043

5144
public func condCast<NS, T>(_ ns: NS) -> T? {
@@ -56,34 +49,27 @@ public func condCast<NS, T>(_ ns: NS) -> T? {
5649

5750
public func condCast2<NS, T>(_ ns: NS) -> T? {
5851
// Make sure the colon info is right so that the arrow is under the a.
59-
//
60-
// Today, we seem to completely eliminate 'x' here in the debug info. TODO:
61-
// Maybe we can recover this info somehow.
6252
let x = ns
6353
return x as? T // expected-remark @:12 {{conditional runtime cast of value with type 'NS' to 'T'}}
64-
// expected-note @-7:32 {{of 'ns'}}
54+
// expected-note @-4:32 {{of 'ns'}}
55+
// expected-note @-3:7 {{of 'x'}}
6556
}
6657

6758
public func condCast3<NS, T>(_ ns: NS) -> T? {
6859
// Make sure the colon info is right so that the arrow is under the a.
69-
//
70-
// Today, we seem to completely eliminate 'x' here in the debug info. TODO:
71-
// Maybe we can recover this info somehow.
7260
var x = ns // expected-warning {{variable 'x' was never mutated}}
7361
return x as? T // expected-remark @:12 {{conditional runtime cast of value with type 'NS' to 'T'}}
74-
// expected-note @-7:32 {{of 'ns'}}
62+
// expected-note @-4:32 {{of 'ns'}}
63+
// expected-note @-3:7 {{of 'x'}}
7564
}
7665

7766
public func condCast4<NS, T>(_ ns: NS, _ ns2: NS) -> T? {
7867
// Make sure the colon info is right so that the arrow is under the a.
79-
//
80-
// Today, we lose that x was assigned ns2. This is flow sensitive information
81-
// that we might be able to recover. We still emit that a runtime cast
82-
// occurred here, just don't say what the underlying value was.
8368
var x = ns
8469
x = ns2
8570
return x as? T // expected-remark @:12 {{conditional runtime cast of value with type 'NS' to 'T'}}
86-
// expected-note @-9:42 {{of 'ns2'}}
71+
// expected-note @-5:42 {{of 'ns2'}}
72+
// expected-note @-4:7 {{of 'x'}}
8773
}
8874

8975
public func condCast5<NS, T>(_ ns: NS) -> T? {

test/SILOptimizer/assemblyvision_remark/chacha.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ public func run_ChaCha(_ N: Int) {
4343
// expected-remark @-1:27 {{release of type '}}
4444
}
4545
} // expected-remark {{release of type '}}
46-
46+
// expected-note @-7 {{of 'plaintext}}
4747
// expected-remark @-2 {{release of type '}}
4848
// expected-note @-16 {{of 'nonce}}
4949
// expected-remark @-4 {{release of type '}}
5050
// expected-note @-19 {{of 'key}}
5151
// expected-remark @-6 {{release of type '}}
52+
// expected-note @-18 {{of 'checkedtext}}

0 commit comments

Comments
 (0)