Skip to content

Commit 01b92a1

Browse files
authored
Merge pull request #65929 from atrick/fix-splitload-debug
Handle loads in salvageDebugInfo and enable it in splitAggregateLoad
2 parents 8263e15 + 4737090 commit 01b92a1

File tree

6 files changed

+156
-74
lines changed

6 files changed

+156
-74
lines changed

include/swift/SIL/InstructionUtils.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,58 @@ RuntimeEffect getRuntimeEffect(SILInstruction *inst, SILType &impactType);
152152
void findClosuresForFunctionValue(SILValue V,
153153
TinyPtrVector<PartialApplyInst *> &results);
154154

155+
/// An abstraction over LoadInst/LoadBorrowInst so one can handle both types of
156+
/// load using common code.
157+
struct LoadOperation {
158+
llvm::PointerUnion<LoadInst *, LoadBorrowInst *> value;
159+
160+
LoadOperation() : value() {}
161+
LoadOperation(SILInstruction *input) : value(nullptr) {
162+
if (auto *li = dyn_cast<LoadInst>(input)) {
163+
value = li;
164+
return;
165+
}
166+
167+
if (auto *lbi = dyn_cast<LoadBorrowInst>(input)) {
168+
value = lbi;
169+
return;
170+
}
171+
}
172+
173+
explicit operator bool() const { return !value.isNull(); }
174+
175+
SingleValueInstruction *getLoadInst() const {
176+
if (value.is<LoadInst *>())
177+
return value.get<LoadInst *>();
178+
return value.get<LoadBorrowInst *>();
179+
}
180+
181+
SingleValueInstruction *operator*() const { return getLoadInst(); }
182+
183+
const SingleValueInstruction *operator->() const { return getLoadInst(); }
184+
185+
SingleValueInstruction *operator->() { return getLoadInst(); }
186+
187+
SILValue getOperand() const {
188+
if (value.is<LoadInst *>())
189+
return value.get<LoadInst *>()->getOperand();
190+
return value.get<LoadBorrowInst *>()->getOperand();
191+
}
192+
193+
/// Return the ownership qualifier of the underlying load if we have a load or
194+
/// None if we have a load_borrow.
195+
///
196+
/// TODO: Rather than use an optional here, we should include an invalid
197+
/// representation in LoadOwnershipQualifier.
198+
Optional<LoadOwnershipQualifier> getOwnershipQualifier() const {
199+
if (auto *lbi = value.dyn_cast<LoadBorrowInst *>()) {
200+
return None;
201+
}
202+
203+
return value.get<LoadInst *>()->getOwnershipQualifier();
204+
}
205+
};
206+
155207
/// Given a polymorphic builtin \p bi that may be generic and thus have in/out
156208
/// params, stash all of the information needed for either specializing while
157209
/// inlining or propagating the type in constant propagation.

include/swift/SILOptimizer/Utils/DebugOptUtils.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ inline void deleteAllDebugUses(SILInstruction *inst,
4848
/// new `debug_value` instruction before \p I is deleted.
4949
void salvageDebugInfo(SILInstruction *I);
5050

51+
/// Transfer debug information associated with the result of \p load to the
52+
/// load's address operand.
53+
///
54+
/// TODO: combine this with salvageDebugInfo when it is supported by
55+
/// optimizations.
56+
void salvageLoadDebugInfo(LoadOperation load);
57+
5158
/// Create debug_value fragment for a new partial value.
5259
///
5360
/// Precondition: \p oldValue is a struct or class aggregate. \p proj projects a

include/swift/SILOptimizer/Utils/OwnershipOptUtils.h

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -349,58 +349,6 @@ class OwnershipReplaceSingleUseHelper {
349349
}
350350
};
351351

352-
/// An abstraction over LoadInst/LoadBorrowInst so one can handle both types of
353-
/// load using common code.
354-
struct LoadOperation {
355-
llvm::PointerUnion<LoadInst *, LoadBorrowInst *> value;
356-
357-
LoadOperation() : value() {}
358-
LoadOperation(SILInstruction *input) : value(nullptr) {
359-
if (auto *li = dyn_cast<LoadInst>(input)) {
360-
value = li;
361-
return;
362-
}
363-
364-
if (auto *lbi = dyn_cast<LoadBorrowInst>(input)) {
365-
value = lbi;
366-
return;
367-
}
368-
}
369-
370-
explicit operator bool() const { return !value.isNull(); }
371-
372-
SingleValueInstruction *getLoadInst() const {
373-
if (value.is<LoadInst *>())
374-
return value.get<LoadInst *>();
375-
return value.get<LoadBorrowInst *>();
376-
}
377-
378-
SingleValueInstruction *operator*() const { return getLoadInst(); }
379-
380-
const SingleValueInstruction *operator->() const { return getLoadInst(); }
381-
382-
SingleValueInstruction *operator->() { return getLoadInst(); }
383-
384-
SILValue getOperand() const {
385-
if (value.is<LoadInst *>())
386-
return value.get<LoadInst *>()->getOperand();
387-
return value.get<LoadBorrowInst *>()->getOperand();
388-
}
389-
390-
/// Return the ownership qualifier of the underlying load if we have a load or
391-
/// None if we have a load_borrow.
392-
///
393-
/// TODO: Rather than use an optional here, we should include an invalid
394-
/// representation in LoadOwnershipQualifier.
395-
Optional<LoadOwnershipQualifier> getOwnershipQualifier() const {
396-
if (auto *lbi = value.dyn_cast<LoadBorrowInst *>()) {
397-
return None;
398-
}
399-
400-
return value.get<LoadInst *>()->getOwnershipQualifier();
401-
}
402-
};
403-
404352
/// Extend the store_borrow \p sbi's scope such that it encloses \p newUsers.
405353
bool extendStoreBorrow(StoreBorrowInst *sbi,
406354
SmallVectorImpl<Operand *> &newUses,

lib/SILOptimizer/Utils/CanonicalizeInstruction.cpp

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
#include "swift/SILOptimizer/Utils/CanonicalizeInstruction.h"
2222
#include "swift/SIL/DebugUtils.h"
2323
#include "swift/SIL/InstructionUtils.h"
24+
#include "swift/SIL/OwnershipUtils.h"
2425
#include "swift/SIL/Projection.h"
2526
#include "swift/SIL/SILBuilder.h"
2627
#include "swift/SIL/SILFunction.h"
2728
#include "swift/SIL/SILInstruction.h"
2829
#include "swift/SILOptimizer/Analysis/SimplifyInstruction.h"
2930
#include "swift/SILOptimizer/Utils/DebugOptUtils.h"
30-
#include "swift/SILOptimizer/Utils/OwnershipOptUtils.h"
3131
#include "llvm/ADT/Statistic.h"
3232
#include "llvm/Support/Debug.h"
3333

@@ -36,10 +36,6 @@ using namespace swift;
3636
// Tracing within the implementation can also be activated by the pass.
3737
#define DEBUG_TYPE pass.debugType
3838

39-
llvm::cl::opt<bool> EnableLoadSplittingDebugInfo(
40-
"sil-load-splitting-debug-info", llvm::cl::init(false),
41-
llvm::cl::desc("Create debug fragments at -O for partial loads"));
42-
4339
// Vtable anchor.
4440
CanonicalizeInstruction::~CanonicalizeInstruction() {}
4541

@@ -301,19 +297,6 @@ splitAggregateLoad(LoadOperation loadInst, CanonicalizeInstruction &pass) {
301297
}
302298
pass.notifyNewInstruction(**lastNewLoad);
303299

304-
// FIXME: This drops debug info at -Onone load-splitting is required at
305-
// -Onone for exclusivity diagnostics. Fix this by
306-
//
307-
// 1. At -Onone, preserve the original load when pass.preserveDebugInfo is
308-
// true, but moving it out of its current access scope and into an "unknown"
309-
// access scope, which won't be enforced as an exclusivity violation.
310-
//
311-
// 2. At -O, create "debug fragments" recover as much debug info as possible
312-
// by creating debug_value fragments for each new partial load. Currently
313-
// disabled because of LLVM back-end crashes.
314-
if (!pass.preserveDebugInfo && EnableLoadSplittingDebugInfo) {
315-
createDebugFragments(*loadInst, proj, lastNewLoad->getLoadInst());
316-
}
317300
if (loadOwnership) {
318301
if (*loadOwnership == LoadOwnershipQualifier::Copy) {
319302
// Destroy the loaded value wherever the aggregate load was destroyed.
@@ -336,6 +319,10 @@ splitAggregateLoad(LoadOperation loadInst, CanonicalizeInstruction &pass) {
336319
nextII = killInstruction(extract, nextII, pass);
337320
}
338321

322+
// Preserve the original load's debug information.
323+
if (pass.preserveDebugInfo) {
324+
swift::salvageLoadDebugInfo(loadInst);
325+
}
339326
// Remove the now unused borrows.
340327
for (auto *borrow : borrows)
341328
nextII = killInstAndIncidentalUses(borrow, nextII, pass);
@@ -344,9 +331,8 @@ splitAggregateLoad(LoadOperation loadInst, CanonicalizeInstruction &pass) {
344331
for (auto *destroy : lifetimeEndingInsts)
345332
nextII = killInstruction(destroy, nextII, pass);
346333

347-
// FIXME: remove this temporary hack to advance the iterator beyond
348-
// debug_value. A soon-to-be merged commit migrates CanonicalizeInstruction to
349-
// use InstructionDeleter.
334+
// TODO: remove this hack to advance the iterator beyond debug_value and check
335+
// SILInstruction::isDeleted() in the caller instead.
350336
while (nextII != loadInst->getParent()->end()
351337
&& nextII->isDebugInstruction()) {
352338
++nextII;

lib/SILOptimizer/Utils/InstOptUtils.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1837,6 +1837,10 @@ void swift::endLifetimeAtLeakingBlocks(SILValue value,
18371837
}
18381838

18391839
// TODO: this currently fails to notify the pass with notifyNewInstruction.
1840+
//
1841+
// TODO: whenever a debug_value is inserted at a new location, check that no
1842+
// other debug_value instructions exist between the old and new location for
1843+
// the same variable.
18401844
void swift::salvageDebugInfo(SILInstruction *I) {
18411845
if (!I)
18421846
return;
@@ -1856,7 +1860,6 @@ void swift::salvageDebugInfo(SILInstruction *I) {
18561860
}
18571861
}
18581862
}
1859-
18601863
// If a `struct` SIL instruction is "unwrapped" and removed,
18611864
// for instance, in favor of using its enclosed value directly,
18621865
// we need to make sure any of its related `debug_value` instruction
@@ -1921,6 +1924,23 @@ void swift::salvageDebugInfo(SILInstruction *I) {
19211924
}
19221925
}
19231926

1927+
void swift::salvageLoadDebugInfo(LoadOperation load) {
1928+
for (Operand *debugUse : getDebugUses(load.getLoadInst())) {
1929+
// Create a new debug_value rather than reusing the old one so the
1930+
// SILBuilder adds 'expr(deref)' to account for the indirection.
1931+
auto *debugInst = cast<DebugValueInst>(debugUse->getUser());
1932+
auto varInfo = debugInst->getVarInfo();
1933+
if (!varInfo)
1934+
continue;
1935+
1936+
// The new debug_value must be "hoisted" to the load to ensure that the
1937+
// address is still valid.
1938+
SILBuilder(load.getLoadInst(), debugInst->getDebugScope())
1939+
.createDebugValueAddr(debugInst->getLoc(), load.getOperand(),
1940+
varInfo.value());
1941+
}
1942+
}
1943+
19241944
// TODO: this currently fails to notify the pass with notifyNewInstruction.
19251945
void swift::createDebugFragments(SILValue oldValue, Projection proj,
19261946
SILValue newValue) {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// RUN: %target-sil-opt -opt-mode=none -silgen-cleanup -sil-print-debuginfo -sil-verify-all %s | %FileCheck %s --check-prefix=CHECK --check-prefix=CHECKDEB
2+
3+
// TODO: add -opt-mode=speed after calling salvageLoadDebugInfo() from salvageDebugInfo().
4+
5+
import Builtin
6+
7+
sil_stage raw
8+
9+
struct Int {
10+
var _value : Builtin.Int32
11+
}
12+
13+
// rdar://104700920 (At -Onone preserve debug info after splitting loads)
14+
//
15+
// loadFromArg checks that a new debug_value is inserted with "expr op_deref".
16+
//
17+
// loadFromStack checks that a new debug_value is insert without op_deref.
18+
19+
sil_scope 10 { loc "./loadFromArg.swift":1:1 parent @loadFromArg : $@convention(thin) (@inout Int) -> Builtin.Int32 }
20+
sil_scope 11 { loc "./loadFromArg.swift":1:1 parent 10 }
21+
// CHECK: sil_scope [[ARGSCOPE1:[0-9]+]] { loc "./loadFromArg.swift":1:1 parent @loadFromArg : $@convention(thin) (@inout Int) -> Builtin.Int32 }
22+
// CHECK: sil_scope [[ARGSCOPE2:[0-9]+]] { loc "./loadFromArg.swift":1:1 parent [[ARGSCOPE1]] }
23+
24+
// CHECK-LABEL: sil [ossa] @loadFromArg : $@convention(thin) (@inout Int) -> Builtin.Int32 {
25+
// CHECK: bb0(%0 : $*Int):
26+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unknown] %0 : $*Int
27+
// CHECK: struct_element_addr [[ACCESS]] : $*Int, #Int._value
28+
// CHECK: load [trivial] %{{.*}} : $*Builtin.Int32, loc "./loadFromArg.swift":2:1, scope [[ARGSCOPE1]]
29+
// CHECK: debug_value [[ACCESS]] : $*Int, let, name "sVar", expr op_deref, loc "./loadFromArg.swift":3:1, scope [[ARGSCOPE2]]
30+
// CHECK-LABEL: } // end sil function 'loadFromArg'
31+
sil [ossa] @loadFromArg : $@convention(thin) (@inout Int) -> Builtin.Int32 {
32+
bb0(%0 : $*Int):
33+
%2 = begin_access [read] [unknown] %0 : $*Int
34+
%3 = load [trivial] %2 : $*Int, loc "./loadFromArg.swift":2:1, scope 10
35+
end_access %2 : $*Int
36+
debug_value %3 : $Int, let, name "sVar", loc "./loadFromArg.swift":3:1, scope 11
37+
%6 = struct_extract %3 : $Int, #Int._value
38+
return %6 : $Builtin.Int32
39+
}
40+
41+
sil @storeToStack : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> @out τ_0_0
42+
43+
sil_scope 20 { loc "./loadFromStack.swift":1:1 parent @loadFromStack : $@convention(thin) (Int) -> Builtin.Int32 }
44+
sil_scope 21 { loc "./loadFromStack.swift":1:1 parent 20 }
45+
// CHECK: sil_scope [[STACKSCOPE1:[0-9]+]] { loc "./loadFromStack.swift":1:1 parent @loadFromStack : $@convention(thin) (Int) -> Builtin.Int32 }
46+
// CHECK: sil_scope [[STACKSCOPE2:[0-9]+]] { loc "./loadFromStack.swift":1:1 parent [[STACKSCOPE1]] }
47+
48+
// CHECK-LABEL: sil [ossa] @loadFromStack : $@convention(thin) (Int) -> Builtin.Int32 {
49+
// CHECK: bb0(%0 : $Int):
50+
// CHECK: [[STACK:%.*]] = alloc_stack $Int
51+
// CHECK: struct_element_addr [[STACK]] : $*Int, #Int._value
52+
// CHECK: load [trivial] %{{.*}} : $*Builtin.Int32, loc "./loadFromStack.swift":2:1, scope [[STACKSCOPE1]]
53+
// CHECK: debug_value [[STACK]] : $*Int, let, name "sVar", loc "./loadFromStack.swift":3:1, scope [[STACKSCOPE2]]
54+
// CHECK-LABEL: } // end sil function 'loadFromStack'
55+
sil [ossa] @loadFromStack : $@convention(thin) (Int) -> Builtin.Int32 {
56+
bb0(%0 : $Int):
57+
debug_value %0 : $Int, let, name "s", argno 1
58+
%2 = alloc_stack $Int
59+
%3 = alloc_stack $Int
60+
store %0 to [trivial] %3 : $*Int
61+
%5 = function_ref @storeToStack : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> @out τ_0_0
62+
%6 = apply %5<Int>(%2, %3) : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> @out τ_0_0
63+
dealloc_stack %3 : $*Int
64+
%8 = load [trivial] %2 : $*Int, loc "./loadFromStack.swift":2:1, scope 20
65+
debug_value %8 : $Int, let, name "sVar", loc "./loadFromStack.swift":3:1, scope 21
66+
dealloc_stack %2 : $*Int
67+
%11 = struct_extract %8 : $Int, #Int._value
68+
return %11 : $Builtin.Int32
69+
}

0 commit comments

Comments
 (0)