Skip to content

Commit 85a49dc

Browse files
committed
Optimizer: make salvageDebugInfo optional when deleting instructions
Add a boolean parameter `salvageDebugInfo` to `Context.erase(instruction:)`. Sometimes it needs to be turned off because the caller might require that after erasing the original instruction the operands no users anymore.
1 parent b5a31e3 commit 85a49dc

File tree

7 files changed

+35
-19
lines changed

7 files changed

+35
-19
lines changed

SwiftCompilerSources/Sources/Optimizer/PassManager/Context.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ extension MutatingContext {
140140
return _bridged.createBlockAfter(block.bridged).block
141141
}
142142

143-
func erase(instruction: Instruction) {
143+
/// Removes and deletes `instruction`.
144+
/// If `salvageDebugInfo` is true, compensating `debug_value` instructions are inserted for certain
145+
/// kind of instructions.
146+
func erase(instruction: Instruction, salvageDebugInfo: Bool = true) {
144147
if !instruction.isInStaticInitializer {
145148
verifyIsTransforming(function: instruction.parentFunction)
146149
}
@@ -152,7 +155,7 @@ extension MutatingContext {
152155
}
153156
notifyInstructionsChanged()
154157

155-
_bridged.eraseInstruction(instruction.bridged)
158+
_bridged.eraseInstruction(instruction.bridged, salvageDebugInfo)
156159
}
157160

158161
func erase(instructionIncludingAllUsers inst: Instruction) {
@@ -164,7 +167,9 @@ extension MutatingContext {
164167
erase(instructionIncludingAllUsers: use.instruction)
165168
}
166169
}
167-
erase(instruction: inst)
170+
// We rely that after deleting the instruction its operands have no users.
171+
// Therefore `salvageDebugInfo` must be turned off because we cannot insert debug_value instructions.
172+
erase(instruction: inst, salvageDebugInfo: false)
168173
}
169174

170175
func erase<S: Sequence>(instructions: S) where S.Element: Instruction {

include/swift/SIL/SILInstructionWorklist.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,12 @@ class SILInstructionWorklist : SILInstructionWorklistBase {
302302
// method to delete the given instruction.
303303
void eraseInstFromFunction(SILInstruction &instruction,
304304
SILBasicBlock::iterator &iterator,
305-
bool addOperandsToWorklist = true) {
306-
// Try to salvage debug info first.
307-
swift::salvageDebugInfo(&instruction);
305+
bool addOperandsToWorklist = true,
306+
bool salvageDebugInfo = true) {
307+
if (salvageDebugInfo) {
308+
// Try to salvage debug info first.
309+
swift::salvageDebugInfo(&instruction);
310+
}
308311
// Then delete old debug users.
309312
for (auto result : instruction.getResults()) {
310313
while (!result->use_empty()) {
@@ -323,9 +326,11 @@ class SILInstructionWorklist : SILInstructionWorklistBase {
323326
}
324327

325328
void eraseInstFromFunction(SILInstruction &instruction,
326-
bool addOperandsToWorklist = true) {
329+
bool addOperandsToWorklist = true,
330+
bool salvageDebugInfo = true) {
327331
SILBasicBlock::iterator nullIter;
328-
return eraseInstFromFunction(instruction, nullIter, addOperandsToWorklist);
332+
return eraseInstFromFunction(instruction, nullIter, addOperandsToWorklist,
333+
salvageDebugInfo);
329334
}
330335

331336
void eraseSingleInstFromFunction(SILInstruction &instruction,

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ struct BridgedPassContext {
234234
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedBasicBlock splitBlockAfter(BridgedInstruction bridgedInst) const;
235235
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedBasicBlock createBlockAfter(BridgedBasicBlock bridgedBlock) const;
236236
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedBasicBlock appendBlock(BridgedFunction bridgedFunction) const;
237-
BRIDGED_INLINE void eraseInstruction(BridgedInstruction inst) const;
237+
BRIDGED_INLINE void eraseInstruction(BridgedInstruction inst, bool salvageDebugInfo) const;
238238
BRIDGED_INLINE void eraseBlock(BridgedBasicBlock block) const;
239239
static BRIDGED_INLINE void moveInstructionBefore(BridgedInstruction inst, BridgedInstruction beforeInst);
240240
bool tryOptimizeApplyOfPartialApply(BridgedInstruction closure) const;

include/swift/SILOptimizer/OptimizerBridgingImpl.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "swift/SILOptimizer/OptimizerBridging.h"
2828
#include "swift/SILOptimizer/PassManager/PassManager.h"
2929
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
30+
#include "swift/SILOptimizer/Utils/DebugOptUtils.h"
3031

3132
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
3233

@@ -253,8 +254,8 @@ BridgedBasicBlock BridgedPassContext::appendBlock(BridgedFunction bridgedFunctio
253254
return {bridgedFunction.getFunction()->createBasicBlock()};
254255
}
255256

256-
void BridgedPassContext::eraseInstruction(BridgedInstruction inst) const {
257-
invocation->eraseInstruction(inst.unbridged());
257+
void BridgedPassContext::eraseInstruction(BridgedInstruction inst, bool salvageDebugInfo) const {
258+
invocation->eraseInstruction(inst.unbridged(), salvageDebugInfo);
258259
}
259260

260261
void BridgedPassContext::eraseBlock(BridgedBasicBlock block) const {

include/swift/SILOptimizer/PassManager/PassManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class SwiftPassInvocation {
138138
void freeOperandSet(OperandSet *set);
139139

140140
/// The top-level API to erase an instruction, called from the Swift pass.
141-
void eraseInstruction(SILInstruction *inst);
141+
void eraseInstruction(SILInstruction *inst, bool salvageDebugInfo);
142142

143143
/// Called by the pass when changes are made to the SIL.
144144
void notifyChanges(SILAnalysis::InvalidationKind invalidationKind);

lib/SILOptimizer/SILCombiner/SILCombine.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -694,11 +694,13 @@ SILTransform *swift::createSILCombine() {
694694
// SwiftFunctionPassContext
695695
//===----------------------------------------------------------------------===//
696696

697-
void SwiftPassInvocation::eraseInstruction(SILInstruction *inst) {
697+
void SwiftPassInvocation::eraseInstruction(SILInstruction *inst, bool salvageDebugInfo) {
698698
if (silCombiner) {
699-
silCombiner->eraseInstFromFunction(*inst);
699+
silCombiner->eraseInstFromFunction(*inst, /*addOperandsToWorklist=*/ true, salvageDebugInfo);
700700
} else {
701-
swift::salvageDebugInfo(inst);
701+
if (salvageDebugInfo) {
702+
swift::salvageDebugInfo(inst);
703+
}
702704
if (inst->isStaticInitializerInst()) {
703705
inst->getParent()->erase(inst, *getPassManager()->getModule());
704706
} else {

lib/SILOptimizer/SILCombiner/SILCombiner.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,10 @@ class SILCombiner :
220220
// by this method.
221221
SILInstruction *eraseInstFromFunction(SILInstruction &I,
222222
SILBasicBlock::iterator &InstIter,
223-
bool AddOperandsToWorklist = true) {
224-
Worklist.eraseInstFromFunction(I, InstIter, AddOperandsToWorklist);
223+
bool AddOperandsToWorklist = true,
224+
bool salvageDebugInfo = true) {
225+
Worklist.eraseInstFromFunction(I, InstIter, AddOperandsToWorklist,
226+
salvageDebugInfo);
225227
MadeChange = true;
226228
// Dummy return, so the caller doesn't need to explicitly return nullptr.
227229
return nullptr;
@@ -232,9 +234,10 @@ class SILCombiner :
232234
void eraseInstIncludingUsers(SILInstruction *inst);
233235

234236
SILInstruction *eraseInstFromFunction(SILInstruction &I,
235-
bool AddOperandsToWorklist = true) {
237+
bool AddOperandsToWorklist = true,
238+
bool salvageDebugInfo = true) {
236239
SILBasicBlock::iterator nullIter;
237-
return eraseInstFromFunction(I, nullIter, AddOperandsToWorklist);
240+
return eraseInstFromFunction(I, nullIter, AddOperandsToWorklist, salvageDebugInfo);
238241
}
239242

240243
void addInitialGroup(ArrayRef<SILInstruction *> List) {

0 commit comments

Comments
 (0)