Skip to content

Fix begin_apply inlining when there are no yields #39662

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
Oct 9, 2021
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
32 changes: 19 additions & 13 deletions include/swift/SIL/SILCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,16 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {

/// This is called by either of the top-level visitors, cloneReachableBlocks
/// or cloneSILFunction, after all other visitors are have been called.

/// `preFixUp` is called first.
void preFixUp(SILFunction *F) {}
/// After postFixUp, the SIL must be valid and semantically equivalent to the
/// SIL before cloning.
///
/// After fixUp, the SIL must be valid and semantically equivalent to the SIL
/// before cloning.
///
/// Common fix-ups are handled first in `doFixUp` and may not be overridden.
void fixUp(SILFunction *F) {}
/// Common fix-ups are handled first in `commonFixUp` and may not be
/// overridden.
void postFixUp(SILFunction *F) {}

private:
/// MARK: SILCloner implementation details hidden from CRTP extensions.

Expand All @@ -388,8 +392,8 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
void visitBlocksDepthFirst(SILBasicBlock *StartBB);

/// Also perform fundamental cleanup first, then call the CRTP extension,
/// `fixUp`.
void doFixUp(SILFunction *F);
/// `postFixUp`.
void commonFixUp(SILFunction *F);
};

/// A SILBuilder that automatically invokes postprocess on each
Expand Down Expand Up @@ -581,7 +585,7 @@ void SILCloner<ImplClass>::cloneReachableBlocks(
// Discover and map the region to be cloned.
visitBlocksDepthFirst(startBB);

doFixUp(F);
commonFixUp(F);
}

template <typename ImplClass>
Expand All @@ -606,7 +610,7 @@ void SILCloner<ImplClass>::cloneFunctionBody(SILFunction *F,
// This will layout all newly cloned blocks immediate after clonedEntryBB.
visitBlocksDepthFirst(&*F->begin());

doFixUp(F);
commonFixUp(F);
}

template<typename ImplClass>
Expand Down Expand Up @@ -709,9 +713,11 @@ void SILCloner<ImplClass>::visitBlocksDepthFirst(SILBasicBlock *startBB) {
}

/// Clean-up after cloning.
template<typename ImplClass>
void
SILCloner<ImplClass>::doFixUp(SILFunction *F) {
template <typename ImplClass>
void SILCloner<ImplClass>::commonFixUp(SILFunction *F) {
// Call any cleanup specific to the CRTP extensions.
asImpl().preFixUp(F);

// If our source function is in ossa form, but the function into which we are
// cloning is not in ossa, after we clone, eliminate default arguments.
if (!getBuilder().hasOwnership() && F->hasOwnership()) {
Expand Down Expand Up @@ -745,7 +751,7 @@ SILCloner<ImplClass>::doFixUp(SILFunction *F) {
}

// Call any cleanup specific to the CRTP extensions.
asImpl().fixUp(F);
asImpl().postFixUp(F);
}

template<typename ImplClass>
Expand Down
2 changes: 1 addition & 1 deletion include/swift/SILOptimizer/Utils/GenericCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class GenericCloner
return SC.getCloned();
}

void fixUp(SILFunction *calleeFunction);
void postFixUp(SILFunction *calleeFunction);

static SILFunction *createDeclaration(SILOptFunctionBuilder &FuncBuilder,
SILFunction *Orig,
Expand Down
2 changes: 1 addition & 1 deletion lib/SILOptimizer/Utils/GenericCloner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ const SILDebugScope *GenericCloner::remapScope(const SILDebugScope *DS) {
return RemappedScope;
}

void GenericCloner::fixUp(SILFunction *f) {
void GenericCloner::postFixUp(SILFunction *f) {
for (auto *apply : noReturnApplies) {
auto applyBlock = apply->getParent();
applyBlock->split(std::next(SILBasicBlock::iterator(apply)));
Expand Down
13 changes: 9 additions & 4 deletions lib/SILOptimizer/Utils/SILInliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,12 @@ class SILInlineCloner
/// This hook is called after either of the top-level visitors:
/// cloneReachableBlocks or cloneSILFunction.
///
/// After fixUp, the SIL must be valid and semantically equivalent to the SIL
/// before cloning.
void fixUp(SILFunction *calleeFunction);
/// After `preFixUp` is called `commonFixUp` will be called.
void preFixUp(SILFunction *calleeFunction);

/// After postFixUp, the SIL must be valid and semantically equivalent to the
/// SIL before cloning.
void postFixUp(SILFunction *calleeFunction);

const SILDebugScope *getOrCreateInlineScope(const SILDebugScope *DS);

Expand Down Expand Up @@ -602,12 +605,14 @@ void SILInlineCloner::visitTerminator(SILBasicBlock *BB) {
visit(BB->getTerminator());
}

void SILInlineCloner::fixUp(SILFunction *calleeFunction) {
void SILInlineCloner::preFixUp(SILFunction *calleeFunction) {
// "Completing" the BeginApply only fixes the end of the apply scope. The
// begin_apply itself lingers.
if (BeginApply)
BeginApply->complete();
}

void SILInlineCloner::postFixUp(SILFunction *calleeFunction) {
NextIter = std::next(Apply.getInstruction()->getIterator());

assert(!Apply.getInstruction()->hasUsesOfAnyResult());
Expand Down
39 changes: 39 additions & 0 deletions test/SILOptimizer/inline_begin_apply.sil
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ unwind:
unwind
}

sil [transparent] [ossa] @test_unreachable : $@yield_once <C: SomeClass> () -> (@yields @in Indirect<C>) {
entry:
unreachable
}

// CHECK-LABEL: sil @test_simple_call
// CHECK: bb0(%0 : $Builtin.Int1):
// CHECK: [[MARKER:%.*]] = function_ref @marker
Expand Down Expand Up @@ -109,6 +114,40 @@ cont:
return %ret : $()
}

// CHECK-LABEL: sil @test_unreachable_call :
// CHECK: bb0(%0 : $Builtin.Int1):
// CHECK: [[MARKER:%.*]] = function_ref @marker
// CHECK: unreachable
// CHECK: } // end sil function 'test_unreachable_call'
sil @test_unreachable_call : $(Builtin.Int1) -> () {
entry(%flag : $Builtin.Int1):
%marker = function_ref @marker : $@convention(thin) (Builtin.Int32) -> ()
%0 = function_ref @test_unreachable : $@convention(thin) @yield_once <T: SomeClass> () -> (@yields @in Indirect<T>)
(%value, %token) = begin_apply %0<SomeSubclass>() : $@convention(thin) @yield_once <T: SomeClass> () -> (@yields @in Indirect<T>)
destroy_addr %value : $*Indirect<SomeSubclass>
cond_br %flag, yes, no

yes:
%10 = integer_literal $Builtin.Int32, 10
apply %marker(%10) : $@convention(thin) (Builtin.Int32) -> ()
end_apply %token
%20 = integer_literal $Builtin.Int32, 20
apply %marker(%20) : $@convention(thin) (Builtin.Int32) -> ()
br cont

no:
%11 = integer_literal $Builtin.Int32, 11
apply %marker(%11) : $@convention(thin) (Builtin.Int32) -> ()
abort_apply %token
%21 = integer_literal $Builtin.Int32, 21
apply %marker(%21) : $@convention(thin) (Builtin.Int32) -> ()
br cont

cont:
%ret = tuple ()
return %ret : $()
}

sil [transparent] [ossa] @test_two_yield : $@yield_once <C: SomeClass> (Builtin.Int1) -> (@yields @in Indirect<C>, @yields Builtin.Int64) {
entry(%0 : $Builtin.Int1):
%marker = function_ref @marker : $@convention(thin) (Builtin.Int32) -> ()
Expand Down