Skip to content

[sil-mandatory-inliner] Decrease the compile time #4635

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
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
46 changes: 43 additions & 3 deletions lib/SILOptimizer/Mandatory/MandatoryInlining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,44 @@ static void diagnose(ASTContext &Context, SourceLoc loc, Diag<T...> diag,
Context.Diags.diagnose(loc, diag, std::forward<U>(args)...);
}

namespace {

/// A helper class to update an instruction iterator if
/// removal of instructions would invalidate it.
class DeleteInstructionsHandler : public DeleteNotificationHandler {
SILBasicBlock::iterator &CurrentI;
SILModule &Module;

public:
DeleteInstructionsHandler(SILBasicBlock::iterator &I)
: CurrentI(I), Module(I->getModule()) {
Module.registerDeleteNotificationHandler(this);
}

~DeleteInstructionsHandler() {
// Unregister the handler.
Module.removeDeleteNotificationHandler(this);
}

// Handling of instruction removal notifications.
bool needsNotifications() { return true; }

// Handle notifications about removals of instructions.
void handleDeleteNotification(swift::ValueBase *Value) {
if (auto DeletedI = dyn_cast<SILInstruction>(Value)) {
if (CurrentI == SILBasicBlock::iterator(DeletedI)) {
if (CurrentI != CurrentI->getParent()->begin()) {
--CurrentI;
} else {
++CurrentI;
}
}
}
}
};

} // end of namespace

/// \brief Fixup reference counts after inlining a function call (which is a
/// no-op unless the function is a thick function). Note that this function
/// makes assumptions about the release/retain convention of thick function
Expand Down Expand Up @@ -419,8 +457,9 @@ runOnFunctionRecursively(SILFunction *F, FullApplySite AI,
// Reestablish our iterator if it wrapped.
if (I == ApplyBlock->end())
I = ApplyBlock->begin();
else
++I;

// Update the iterator when instructions are removed.
DeleteInstructionsHandler DeletionHandler(I);

// If the inlined apply was a thick function, then we need to balance the
// reference counts for correctness.
Expand All @@ -433,8 +472,9 @@ runOnFunctionRecursively(SILFunction *F, FullApplySite AI,

// Reposition iterators possibly invalidated by mutation.
FI = SILFunction::iterator(ApplyBlock);
I = ApplyBlock->begin();
E = ApplyBlock->end();
assert(FI == SILFunction::iterator(I->getParent()) &&
"Mismatch between the instruction and basic block");
++NumMandatoryInlines;
}
}
Expand Down