Skip to content

[mandatory-inline] Move the creation of the inliner and bailing upon knowing that inlining will fail, before any side-effect having work occurs. #11830

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
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -6529,6 +6529,16 @@ class ApplySite {
FOREACH_IMPL_RETURN(getSubstitutions());
}

/// Return a begin iterator for the substitution array.
auto subs_begin() const -> decltype(getSubstitutions().begin()) {
return getSubstitutions().begin();
}

/// Return an end iterator for the substitution array.
auto subs_end() const -> decltype(getSubstitutions().end()) {
return getSubstitutions().end();
}

/// The arguments passed to this instruction.
MutableArrayRef<Operand> getArgumentOperands() const {
FOREACH_IMPL_RETURN(getArgumentOperands());
Expand Down
54 changes: 29 additions & 25 deletions lib/SILOptimizer/Mandatory/MandatoryInlining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,35 @@ runOnFunctionRecursively(SILFunction *F, FullApplySite AI,
return false;
}

// Create our initial list of substitutions.
llvm::SmallVector<Substitution, 16> ApplySubs(InnerAI.subs_begin(),
InnerAI.subs_end());

// Then if we have a partial_apply, add any additional subsitutions that
// we may require to the end of the list.
if (PAI) {
copy(PAI->getSubstitutions(), std::back_inserter(ApplySubs));
}

SILOpenedArchetypesTracker OpenedArchetypesTracker(F);
F->getModule().registerDeleteNotificationHandler(
&OpenedArchetypesTracker);
// The callee only needs to know about opened archetypes used in
// the substitution list.
OpenedArchetypesTracker.registerUsedOpenedArchetypes(
InnerAI.getInstruction());
if (PAI) {
OpenedArchetypesTracker.registerUsedOpenedArchetypes(PAI);
}

SILInliner Inliner(*F, *CalleeFunction,
SILInliner::InlineKind::MandatoryInline, ApplySubs,
OpenedArchetypesTracker);
if (!Inliner.canInlineFunction(InnerAI)) {
I = InnerAI.getInstruction()->getIterator();
continue;
}

// Inline function at I, which also changes I to refer to the first
// instruction inlined in the case that it succeeds. We purposely
// process the inlined body after inlining, because the inlining may
Expand Down Expand Up @@ -456,31 +485,6 @@ runOnFunctionRecursively(SILFunction *F, FullApplySite AI,
else
I = ApplyBlock->end();

std::vector<Substitution> ApplySubs(InnerAI.getSubstitutions());

if (PAI) {
auto PAISubs = PAI->getSubstitutions();
ApplySubs.insert(ApplySubs.end(), PAISubs.begin(), PAISubs.end());
}

SILOpenedArchetypesTracker OpenedArchetypesTracker(F);
F->getModule().registerDeleteNotificationHandler(
&OpenedArchetypesTracker);
// The callee only needs to know about opened archetypes used in
// the substitution list.
OpenedArchetypesTracker.registerUsedOpenedArchetypes(InnerAI.getInstruction());
if (PAI) {
OpenedArchetypesTracker.registerUsedOpenedArchetypes(PAI);
}

SILInliner Inliner(*F, *CalleeFunction,
SILInliner::InlineKind::MandatoryInline,
ApplySubs, OpenedArchetypesTracker);
if (!Inliner.canInlineFunction(InnerAI)) {
I = InnerAI.getInstruction()->getIterator();
continue;
}

Inliner.inlineFunction(InnerAI, FullArgs);

// We were able to inline successfully. Remove the apply.
Expand Down