Skip to content

Commit b6bfea1

Browse files
committed
[sil-optimizer] Get rid of the InstModCallback constructors in favor of onDelete/onCreatedNewInst/etc.
Without this when constructing an InstModCallback it is hard to distinguish which closure is meant for which operation when passed to the constructor of InstModCallback (if this was in Swift, we could use argument labels, but we do not have such things in c++). This new value type sort of formulation makes it unambiguous which callback is used for what when constructing one of these.
1 parent e5d0a48 commit b6bfea1

File tree

7 files changed

+47
-54
lines changed

7 files changed

+47
-54
lines changed

include/swift/SILOptimizer/Utils/CanonicalizeInstruction.h

+12-7
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,22 @@ struct CanonicalizeInstruction {
4646
CanonicalizeInstruction(const char *passDebugType,
4747
DeadEndBlocks &deadEndBlocks)
4848
: deadEndBlocks(deadEndBlocks),
49-
callbacks(
50-
[&](SILInstruction *toDelete) { killInstruction(toDelete); },
51-
[&](SILInstruction *newInst) { notifyNewInstruction(newInst); },
52-
[&](Operand *use, SILValue newValue) {
53-
use->set(newValue);
54-
notifyHasNewUsers(newValue);
55-
}) {
49+
callbacks() {
5650
#ifndef NDEBUG
5751
if (llvm::DebugFlag && !llvm::isCurrentDebugType(debugType))
5852
debugType = passDebugType;
5953
#endif
54+
callbacks = InstModCallbacks()
55+
.onDelete([&](SILInstruction *toDelete) {
56+
killInstruction(toDelete);
57+
})
58+
.onCreateNewInst([&](SILInstruction *newInst) {
59+
notifyNewInstruction(newInst);
60+
})
61+
.onSetUseValue([&](Operand *use, SILValue newValue) {
62+
use->set(newValue);
63+
notifyHasNewUsers(newValue);
64+
});
6065
}
6166

6267
virtual ~CanonicalizeInstruction();

include/swift/SILOptimizer/Utils/InstOptUtils.h

-19
Original file line numberDiff line numberDiff line change
@@ -99,28 +99,9 @@ class InstModCallbacks {
9999
bool wereAnyCallbacksInvoked = false;
100100

101101
public:
102-
InstModCallbacks(decltype(deleteInstFunc) deleteInstFunc)
103-
: deleteInstFunc(deleteInstFunc) {}
104-
105-
InstModCallbacks(decltype(deleteInstFunc) deleteInstFunc,
106-
decltype(createdNewInstFunc) createdNewInstFunc)
107-
: createdNewInstFunc(createdNewInstFunc), deleteInstFunc(deleteInstFunc) {
108-
}
109-
110-
InstModCallbacks(decltype(deleteInstFunc) deleteInstFunc,
111-
decltype(setUseValueFunc) setUseValueFunc)
112-
: setUseValueFunc(setUseValueFunc), deleteInstFunc(deleteInstFunc) {}
113-
114-
InstModCallbacks(decltype(deleteInstFunc) deleteInstFunc,
115-
decltype(createdNewInstFunc) createdNewInstFunc,
116-
decltype(setUseValueFunc) setUseValueFunc)
117-
: createdNewInstFunc(createdNewInstFunc),
118-
setUseValueFunc(setUseValueFunc), deleteInstFunc(deleteInstFunc) {}
119-
120102
InstModCallbacks() = default;
121103
~InstModCallbacks() = default;
122104
InstModCallbacks(const InstModCallbacks &) = default;
123-
InstModCallbacks(InstModCallbacks &&) = default;
124105

125106
/// Return a copy of self with deleteInstFunc set to \p newDeleteInstFunc.
126107
InstModCallbacks onDelete(decltype(deleteInstFunc) newDeleteInstFunc) const {

lib/SILOptimizer/Mandatory/MandatoryCombine.cpp

+15-11
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,22 @@ class MandatoryCombiner final
143143
DeadEndBlocks &deadEndBlocks)
144144
: compilingWithOptimization(optimized), worklist("MC"), madeChange(false),
145145
iteration(0),
146-
instModCallbacks(
147-
[&](SILInstruction *instruction) {
148-
worklist.erase(instruction);
149-
instructionsPendingDeletion.push_back(instruction);
150-
},
151-
[&](SILInstruction *instruction) { worklist.add(instruction); },
152-
[this](Operand *use, SILValue newValue) {
153-
use->set(newValue);
154-
worklist.add(use->getUser());
155-
}),
146+
instModCallbacks(),
156147
createdInstructions(createdInstructions),
157-
deadEndBlocks(deadEndBlocks){};
148+
deadEndBlocks(deadEndBlocks) {
149+
instModCallbacks = InstModCallbacks()
150+
.onDelete([&](SILInstruction *instruction) {
151+
worklist.erase(instruction);
152+
instructionsPendingDeletion.push_back(instruction);
153+
})
154+
.onCreateNewInst([&](SILInstruction *instruction) {
155+
worklist.add(instruction);
156+
})
157+
.onSetUseValue([this](Operand *use, SILValue newValue) {
158+
use->set(newValue);
159+
worklist.add(use->getUser());
160+
});
161+
};
158162

159163
void addReachableCodeToWorklist(SILFunction &function);
160164

lib/SILOptimizer/Mandatory/OwnershipModelEliminator.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ static bool stripOwnership(SILFunction &func) {
527527
auto value = visitor.instructionsToSimplify.pop_back_val();
528528
if (!value.hasValue())
529529
continue;
530-
InstModCallbacks callbacks([&](SILInstruction *instToErase) {
530+
auto callbacks = InstModCallbacks().onDelete([&](SILInstruction *instToErase) {
531531
visitor.eraseInstruction(instToErase);
532532
});
533533
// We are no longer in OSSA, so we don't need to pass in a deBlocks.

lib/SILOptimizer/SILCombiner/SILCombiner.h

+14-12
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,21 @@ class SILCombiner :
116116
},
117117
/* EraseAction */
118118
[&](SILInstruction *I) { eraseInstFromFunction(*I); }),
119-
instModCallbacks(
120-
[&](SILInstruction *instToDelete) {
121-
eraseInstFromFunction(*instToDelete);
122-
},
123-
[&](SILInstruction *newlyCreatedInst) {
124-
Worklist.add(newlyCreatedInst);
125-
},
126-
[&](Operand *use, SILValue newValue) {
127-
use->set(newValue);
128-
Worklist.add(use->getUser());
129-
}),
119+
instModCallbacks(),
130120
deBlocks(&B.getFunction()),
131-
ownershipFixupContext(instModCallbacks, deBlocks) {}
121+
ownershipFixupContext(instModCallbacks, deBlocks) {
122+
instModCallbacks = InstModCallbacks()
123+
.onDelete([&](SILInstruction *instToDelete) {
124+
eraseInstFromFunction(*instToDelete);
125+
})
126+
.onCreateNewInst([&](SILInstruction *newlyCreatedInst) {
127+
Worklist.add(newlyCreatedInst);
128+
})
129+
.onSetUseValue([&](Operand *use, SILValue newValue) {
130+
use->set(newValue);
131+
Worklist.add(use->getUser());
132+
});
133+
}
132134

133135
bool runOnFunction(SILFunction &F);
134136

lib/SILOptimizer/SemanticARC/SemanticARCOptVisitor.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ struct LLVM_LIBRARY_VISIBILITY SemanticARCOptVisitor
5252
explicit SemanticARCOptVisitor(SILFunction &fn, DeadEndBlocks &deBlocks,
5353
bool onlyMandatoryOpts)
5454
: ctx(fn, deBlocks, onlyMandatoryOpts,
55-
InstModCallbacks(
56-
[this](SILInstruction *inst) { eraseInstruction(inst); },
57-
[this](Operand *use, SILValue newValue) {
55+
InstModCallbacks()
56+
.onDelete(
57+
[this](SILInstruction *inst) { eraseInstruction(inst); })
58+
.onSetUseValue([this](Operand *use, SILValue newValue) {
5859
use->set(newValue);
5960
worklist.insert(newValue);
6061
})) {}

lib/SILOptimizer/Transforms/TempRValueElimination.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ void TempRValueOptPass::run() {
783783
}
784784
}
785785

786-
InstModCallbacks callbacks(
786+
auto callbacks = InstModCallbacks().onDelete(
787787
[](SILInstruction *instToKill) {
788788
// SimplifyInstruction is not in the business of removing
789789
// copy_addr. If it were, then we would need to update deadCopies.

0 commit comments

Comments
 (0)