Skip to content

Commit 9a4935c

Browse files
committed
Add salvageLoadDebugInfo to handle LoadInst
Before deleting a load, simply rewrite its debug info to refer to the loaded address: %l = load %a debug_value %l, loc0, scope0, var0 ---> debug_value %a, loc0, scope0, var0, expr op_deref %l = load %a Note that alloc_stack addresses do not require the addition of op_deref because they are already special-cased in IRGen. This will be called from salvageDebugInfo when it is supported by optmizations. Until then, it is only called selectively at -Onone. (cherry picked from commit 11053dc)
1 parent 91814ca commit 9a4935c

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

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

lib/SILOptimizer/Utils/CanonicalizeInstruction.cpp

Lines changed: 1 addition & 1 deletion
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

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

0 commit comments

Comments
 (0)