Skip to content

Commit 11053dc

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.
1 parent 094b204 commit 11053dc

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

0 commit comments

Comments
 (0)