Skip to content

Commit e5d0a48

Browse files
committed
[sil-optimizer] Add comments to InstModCallback that explains why we have both notifyWillBeDeletedFunc and deleteInstFunc.
1 parent af789d5 commit e5d0a48

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

include/swift/SILOptimizer/Utils/InstOptUtils.h

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ template <class T> class NullablePtr;
4848
/// that in a loop this property should predict well since you have a single
4949
/// branch that is going to go the same way everytime.
5050
class InstModCallbacks {
51-
/// A function that takes in an instruction and deletes the inst.
52-
///
53-
/// Default implementation is instToDelete->eraseFromParent();
54-
std::function<void(SILInstruction *instToDelete)> deleteInstFunc;
55-
5651
/// A function that is called to notify that a new function was created.
5752
///
5853
/// Default implementation is a no-op, but we still mark madeChange.
@@ -66,8 +61,37 @@ class InstModCallbacks {
6661
/// iterators.
6762
std::function<void(Operand *use, SILValue newValue)> setUseValueFunc;
6863

64+
/// A function that takes in an instruction and deletes the inst.
65+
///
66+
/// Default implementation is instToDelete->eraseFromParent();
67+
///
68+
/// NOTE: The reason why we have deleteInstFunc and notifyWillBeDeletedFunc is
69+
/// InstModCallback supports 2 stage deletion where a callee passed
70+
/// InstModCallback is allowed to drop all references to the instruction
71+
/// before calling deleteInstFunc. In contrast, notifyWillBeDeletedFunc
72+
/// assumes that the IR is in a good form before being called so that the
73+
/// caller can via the callback gather state about the instruction that will
74+
/// be deleted. As an example, see InstructionDeleter::deleteInstruction() in
75+
/// InstOptUtils.cpp.
76+
std::function<void(SILInstruction *instToDelete)> deleteInstFunc;
77+
6978
/// If non-null, called before an instruction is deleted or has its references
70-
/// dropped.
79+
/// dropped. If null, no-op.
80+
///
81+
/// NOTE: The reason why we have deleteInstFunc and notifyWillBeDeletedFunc is
82+
/// InstModCallback supports 2 stage deletion where a callee passed
83+
/// InstModCallback is allowed to drop all references to the instruction
84+
/// before calling deleteInstFunc. In contrast, notifyWillBeDeletedFunc
85+
/// assumes that the IR is in a good form before being called so that the
86+
/// caller can via the callback gather state about the instruction that will
87+
/// be deleted. As an example, see InstructionDeleter::deleteInstruction() in
88+
/// InstOptUtils.cpp.
89+
///
90+
/// NOTE: This is called in InstModCallback::deleteInst() if one does not use
91+
/// a default bool argument to disable the notification. In general that
92+
/// should only be done when one is writing custom handling and is performing
93+
/// the notification ones self. It is assumed that the notification will be
94+
/// called with a valid instruction.
7195
std::function<void(SILInstruction *instThatWillBeDeleted)>
7296
notifyWillBeDeletedFunc;
7397

@@ -80,18 +104,18 @@ class InstModCallbacks {
80104

81105
InstModCallbacks(decltype(deleteInstFunc) deleteInstFunc,
82106
decltype(createdNewInstFunc) createdNewInstFunc)
83-
: deleteInstFunc(deleteInstFunc), createdNewInstFunc(createdNewInstFunc) {
107+
: createdNewInstFunc(createdNewInstFunc), deleteInstFunc(deleteInstFunc) {
84108
}
85109

86110
InstModCallbacks(decltype(deleteInstFunc) deleteInstFunc,
87111
decltype(setUseValueFunc) setUseValueFunc)
88-
: deleteInstFunc(deleteInstFunc), setUseValueFunc(setUseValueFunc) {}
112+
: setUseValueFunc(setUseValueFunc), deleteInstFunc(deleteInstFunc) {}
89113

90114
InstModCallbacks(decltype(deleteInstFunc) deleteInstFunc,
91115
decltype(createdNewInstFunc) createdNewInstFunc,
92116
decltype(setUseValueFunc) setUseValueFunc)
93-
: deleteInstFunc(deleteInstFunc), createdNewInstFunc(createdNewInstFunc),
94-
setUseValueFunc(setUseValueFunc) {}
117+
: createdNewInstFunc(createdNewInstFunc),
118+
setUseValueFunc(setUseValueFunc), deleteInstFunc(deleteInstFunc) {}
95119

96120
InstModCallbacks() = default;
97121
~InstModCallbacks() = default;

0 commit comments

Comments
 (0)