Skip to content

SIL optimizer: fix a compile time performance problem in UpdatingInstructionIteratorRegistry #41239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 32 additions & 21 deletions include/swift/SILOptimizer/Utils/UpdatingInstructionIterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,31 +169,33 @@ class UpdatingInstructionIteratorRegistry {
SmallVector<UpdatingInstructionIterator *, 4> forwardIterators;
SmallVector<UpdatingReverseInstructionIterator *, 4> reverseIterators;

std::function<void(SILInstruction *)> chainedDelete;
std::function<void(SILInstruction *)> chainedNew;

/// Callbacks used when adding/deleting instructions.
InstModCallbacks callbacks;

public:
UpdatingInstructionIteratorRegistry(
InstModCallbacks chainedCallbacks = InstModCallbacks()) {
rechainCallbacks(chainedCallbacks);
}

// The callbacks capture 'this'. So copying is invalid.
UpdatingInstructionIteratorRegistry(
const UpdatingInstructionIteratorRegistry &) = delete;

UpdatingInstructionIteratorRegistry &
operator=(const UpdatingInstructionIteratorRegistry &) = delete;

InstModCallbacks &getCallbacks() { return callbacks; }
public:
UpdatingInstructionIteratorRegistry() :
callbacks(InstModCallbacks()
.onDelete([this](SILInstruction *toDelete) {
notifyDelete(toDelete);
toDelete->eraseFromParent();
})
.onCreateNewInst(
[this](SILInstruction *newlyCreatedInst) {
notifyNew(newlyCreatedInst);
}))
{}

void rechainCallbacks(InstModCallbacks chainedCallbacks) {
UpdatingInstructionIteratorRegistry(InstModCallbacks &&chainedCallbacks) :
// Copy the two std::functions that we need. The rest of the callbacks are
// copied implicitly by assignment.
auto chainedDelete = chainedCallbacks.deleteInstFunc;
auto chainedNew = chainedCallbacks.createdNewInstFunc;
callbacks = chainedCallbacks
.onDelete([this, chainedDelete](SILInstruction *toDelete) {
chainedDelete(std::move(chainedCallbacks.deleteInstFunc)),
chainedNew(std::move(chainedCallbacks.createdNewInstFunc)),
callbacks(std::move(chainedCallbacks
.onDelete([this](SILInstruction *toDelete) {
notifyDelete(toDelete);
if (chainedDelete) {
chainedDelete(toDelete);
Expand All @@ -202,13 +204,22 @@ class UpdatingInstructionIteratorRegistry {
toDelete->eraseFromParent();
})
.onCreateNewInst(
[this, chainedNew](SILInstruction *newlyCreatedInst) {
[this](SILInstruction *newlyCreatedInst) {
notifyNew(newlyCreatedInst);
if (chainedNew) {
chainedNew(newlyCreatedInst);
}
});
}
})))
{}

// The callbacks capture 'this'. So copying is invalid.
UpdatingInstructionIteratorRegistry(
const UpdatingInstructionIteratorRegistry &) = delete;

UpdatingInstructionIteratorRegistry &
operator=(const UpdatingInstructionIteratorRegistry &) = delete;

InstModCallbacks &getCallbacks() { return callbacks; }

void registerIterator(UpdatingInstructionIterator *i) {
forwardIterators.push_back(i);
Expand Down