Skip to content

[generic-specializer] Fix for specializations which are no-return functions #8924

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
Apr 21, 2017
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
17 changes: 13 additions & 4 deletions lib/SILOptimizer/Utils/Generics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1670,10 +1670,19 @@ static ApplySite replaceWithSpecializedCallee(ApplySite AI,
Arguments, A->isNonThrowing());
if (StoreResultTo) {
assert(substConv.useLoweredAddresses());
// Store the direct result to the original result address.
fixUsedVoidType(A, Loc, Builder);
Builder.createStore(Loc, NewAI, StoreResultTo,
StoreOwnershipQualifier::Unqualified);
if (!CalleeSILSubstFnTy.isNoReturnFunction()) {
// Store the direct result to the original result address.
fixUsedVoidType(A, Loc, Builder);
Builder.createStore(Loc, NewAI, StoreResultTo,
StoreOwnershipQualifier::Unqualified);
} else {
Builder.createUnreachable(Loc);
// unreachable should be the terminator instruction.
// So, split the current basic block right after the
// inserted unreachable instruction.
Builder.getInsertionPoint()->getParent()->split(
Builder.getInsertionPoint());
}
}
A->replaceAllUsesWith(NewAI);
return NewAI;
Expand Down
55 changes: 55 additions & 0 deletions test/SILOptimizer/specialize.sil
Original file line number Diff line number Diff line change
Expand Up @@ -527,3 +527,58 @@ bb0(%0 : $Builtin.RawPointer):
%5 = tuple ()
return %5 : $()
}

// invokeGenericClosure<A>(todo:)
sil [noinline] @invokeGenericClosure : $@convention(thin) <R> (@owned @callee_owned () -> (@out R, @error Error)) -> (@out R, @error Error) {
bb0(%0 : $*R, %1 : $@callee_owned () -> (@out R, @error Error)):
debug_value %1 : $@callee_owned () -> (@out R, @error Error), let, name "todo", argno 1 // id: %2
debug_value undef : $Error, var, name "$error", argno 2 // id: %3
strong_retain %1 : $@callee_owned () -> (@out R, @error Error) // id: %4
try_apply %1(%0) : $@callee_owned () -> (@out R, @error Error), normal bb1, error bb2 // id: %5

bb1(%6 : $()): // Preds: bb0
strong_release %1 : $@callee_owned () -> (@out R, @error Error) // id: %7
%8 = tuple () // user: %9
return %8 : $() // id: %9

// %10 // user: %12
bb2(%10 : $Error): // Preds: bb0
strong_release %1 : $@callee_owned () -> (@out R, @error Error) // id: %11
throw %10 : $Error // id: %12
} // end sil function 'invokeGenericClosure'

sil public_external @error : $@convention(thin) () -> Never

// action()
sil @action : $@convention(thin) () -> Never

// thunk for @callee_owned () -> (@unowned Never, @error @owned Error)
sil @action_thunk : $@convention(thin) (@owned @callee_owned () -> (Never, @error Error)) -> (@out Never, @error Error)

// Check that in a case where a generic specialization is a non-return function,
// the return value is not stored after the call and an unreachable instruction
// is inserted as a terminator of a basic block.
//
// CHECK-LABEL: sil @testGenericClosureSpecialization
// Call of the generic specialization of invokeGenericClosure<Never>
// CHECK: function_ref @_T020invokeGenericClosures5NeverO_Tg5 : $@convention(thin) (@owned @callee_owned () -> (@out Never, @error Error)) -> (Never, @error Error)
// CHECK: apply [nothrow]
// CHECK: unreachable
// CHECK: end sil function 'testGenericClosureSpecialization'
sil @testGenericClosureSpecialization : $@convention(thin) () -> @error Error {
bb0:
// function_ref invokeGenericClosure<A>(todo:)
%1 = function_ref @invokeGenericClosure : $@convention(thin) <τ_0_0> (@owned @callee_owned () -> (@out τ_0_0, @error Error)) -> (@out τ_0_0, @error Error)
%2 = alloc_stack $Never
// function_ref action()
%3 = function_ref @action : $@convention(thin) () -> Never
%4 = thin_to_thick_function %3 : $@convention(thin) () -> Never to $@callee_owned () -> Never
%5 = convert_function %4 : $@callee_owned () -> Never to $@callee_owned () -> (Never, @error Error)
// function_ref thunk for @callee_owned () -> (@unowned Never, @error @owned Error)
%6 = function_ref @action_thunk : $@convention(thin) (@owned @callee_owned () -> (Never, @error Error)) -> (@out Never, @error Error)
%7 = partial_apply %6(%5) : $@convention(thin) (@owned @callee_owned () -> (Never, @error Error)) -> (@out Never, @error Error)
%8 = apply [nothrow] %1<Never>(%2, %7) : $@convention(thin) <τ_0_0> (@owned @callee_owned () -> (@out τ_0_0, @error Error)) -> (@out τ_0_0, @error Error)
dealloc_stack %2 : $*Never
%12 = tuple ()
return %12 : $()
} // end sil function 'testGenericClosureSpecialization'