Skip to content

Commit aba8d13

Browse files
authored
Merge pull request #65966 from atrick/5.9-fix-splitload-debug
[5.9] Add salvageLoadDebugInfo and call it in splitAggregateLoad
2 parents 8397fbd + 8685234 commit aba8d13

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
@@ -356,58 +356,6 @@ class OwnershipReplaceSingleUseHelper {
356356
}
357357
};
358358

359-
/// An abstraction over LoadInst/LoadBorrowInst so one can handle both types of
360-
/// load using common code.
361-
struct LoadOperation {
362-
llvm::PointerUnion<LoadInst *, LoadBorrowInst *> value;
363-
364-
LoadOperation() : value() {}
365-
LoadOperation(SILInstruction *input) : value(nullptr) {
366-
if (auto *li = dyn_cast<LoadInst>(input)) {
367-
value = li;
368-
return;
369-
}
370-
371-
if (auto *lbi = dyn_cast<LoadBorrowInst>(input)) {
372-
value = lbi;
373-
return;
374-
}
375-
}
376-
377-
explicit operator bool() const { return !value.isNull(); }
378-
379-
SingleValueInstruction *getLoadInst() const {
380-
if (value.is<LoadInst *>())
381-
return value.get<LoadInst *>();
382-
return value.get<LoadBorrowInst *>();
383-
}
384-
385-
SingleValueInstruction *operator*() const { return getLoadInst(); }
386-
387-
const SingleValueInstruction *operator->() const { return getLoadInst(); }
388-
389-
SingleValueInstruction *operator->() { return getLoadInst(); }
390-
391-
SILValue getOperand() const {
392-
if (value.is<LoadInst *>())
393-
return value.get<LoadInst *>()->getOperand();
394-
return value.get<LoadBorrowInst *>()->getOperand();
395-
}
396-
397-
/// Return the ownership qualifier of the underlying load if we have a load or
398-
/// None if we have a load_borrow.
399-
///
400-
/// TODO: Rather than use an optional here, we should include an invalid
401-
/// representation in LoadOwnershipQualifier.
402-
Optional<LoadOwnershipQualifier> getOwnershipQualifier() const {
403-
if (auto *lbi = value.dyn_cast<LoadBorrowInst *>()) {
404-
return None;
405-
}
406-
407-
return value.get<LoadInst *>()->getOwnershipQualifier();
408-
}
409-
};
410-
411359
/// Extend the store_borrow \p sbi's scope such that it encloses \p newUsers.
412360
bool extendStoreBorrow(StoreBorrowInst *sbi,
413361
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
@@ -1826,6 +1826,10 @@ void swift::endLifetimeAtLeakingBlocks(SILValue value,
18261826
}
18271827

18281828
// TODO: this currently fails to notify the pass with notifyNewInstruction.
1829+
//
1830+
// TODO: whenever a debug_value is inserted at a new location, check that no
1831+
// other debug_value instructions exist between the old and new location for
1832+
// the same variable.
18291833
void swift::salvageDebugInfo(SILInstruction *I) {
18301834
if (!I)
18311835
return;
@@ -1845,7 +1849,6 @@ void swift::salvageDebugInfo(SILInstruction *I) {
18451849
}
18461850
}
18471851
}
1848-
18491852
// If a `struct` SIL instruction is "unwrapped" and removed,
18501853
// for instance, in favor of using its enclosed value directly,
18511854
// we need to make sure any of its related `debug_value` instruction
@@ -1910,6 +1913,23 @@ void swift::salvageDebugInfo(SILInstruction *I) {
19101913
}
19111914
}
19121915

1916+
void swift::salvageLoadDebugInfo(LoadOperation load) {
1917+
for (Operand *debugUse : getDebugUses(load.getLoadInst())) {
1918+
// Create a new debug_value rather than reusing the old one so the
1919+
// SILBuilder adds 'expr(deref)' to account for the indirection.
1920+
auto *debugInst = cast<DebugValueInst>(debugUse->getUser());
1921+
auto varInfo = debugInst->getVarInfo();
1922+
if (!varInfo)
1923+
continue;
1924+
1925+
// The new debug_value must be "hoisted" to the load to ensure that the
1926+
// address is still valid.
1927+
SILBuilder(load.getLoadInst(), debugInst->getDebugScope())
1928+
.createDebugValueAddr(debugInst->getLoc(), load.getOperand(),
1929+
varInfo.value());
1930+
}
1931+
}
1932+
19131933
// TODO: this currently fails to notify the pass with notifyNewInstruction.
19141934
void swift::createDebugFragments(SILValue oldValue, Projection proj,
19151935
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)