Skip to content

[eager-specializer] Handle functions with don’t have return basic blocks #8723

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
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
7 changes: 5 additions & 2 deletions lib/SILOptimizer/IPO/EagerSpecializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,10 @@ class EagerDispatch {
/// given specialized function. Converts call arguments. Emits an invocation of
/// the specialized function. Handle the return value.
void EagerDispatch::emitDispatchTo(SILFunction *NewFunc) {
SILBasicBlock *OldReturnBB = &*GenericFunc->findReturnBB();
SILBasicBlock *OldReturnBB = nullptr;
auto ReturnBB = GenericFunc->findReturnBB();
if (ReturnBB != GenericFunc->end())
OldReturnBB = &*ReturnBB;
// 1. Emit a cascading sequence of type checks blocks.

// First split the entry BB, moving all instructions to the FailedTypeCheckBB.
Expand Down Expand Up @@ -395,7 +398,7 @@ void EagerDispatch::emitDispatchTo(SILFunction *NewFunc) {
Result = Builder.createTuple(Loc, VoidTy, { });

// Function marked as @NoReturn must be followed by 'unreachable'.
if (NewFunc->isNoReturnFunction())
if (NewFunc->isNoReturnFunction() || !OldReturnBB)
Builder.createUnreachable(Loc);
else {
auto resultTy = GenericFunc->getConventions().getSILResultType();
Expand Down
51 changes: 51 additions & 0 deletions test/SILOptimizer/eager_specialize.sil
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,57 @@ bb0(%0 : $*T, %1 : $*S):
// CHECK: br bb2
// CHECK: } // end sil function '_T016eager_specialize34checkExplicitPartialSpecializationyx_q_tr0_lF'

/////////////////////////////////////////////////////////////////////////
// Check that functions with unreachable instructions can be specialized.
/////////////////////////////////////////////////////////////////////////

protocol P {
}

struct T : P {
init()
}

extension P {
public static func f(_ x: Self) -> Self
}

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

// CHECK-LABEL: sil @_T016eager_specialize1PPAAE1fxxFZ : $@convention(method) <Self where Self : P> (@in Self, @thick Self.Type) -> @out Self
// CHECK: %3 = metatype $@thick Self.Type
// CHECK: %4 = metatype $@thick T.Type
// CHECK: %5 = unchecked_bitwise_cast %3 : $@thick Self.Type to $Builtin.Word
// CHECK: %6 = unchecked_bitwise_cast %4 : $@thick T.Type to $Builtin.Word
// CHECK: %7 = builtin "cmp_eq_Word"(%5 : $Builtin.Word, %6 : $Builtin.Word) : $Builtin.Int1
// CHECK: cond_br %7, bb2, bb1

// CHECK: bb1:
// CHECK: %9 = function_ref @error : $@convention(thin) () -> Never
// CHECK: %10 = apply %9() : $@convention(thin) () -> Never
// CHECK: unreachable

// CHECK: bb2:
// CHECK: %12 = unchecked_addr_cast %0 : $*Self to $*T
// CHECK: %13 = unchecked_addr_cast %1 : $*Self to $*T
// CHECK: %14 = load %13 : $*T
// CHECK: %15 = unchecked_trivial_bit_cast %2 : $@thick Self.Type to $@thick T.Type
// CHECK: %16 = function_ref @_T016eager_specialize1PPAAE1fxxFZ4main1TV_Tg5 : $@convention(method) (T, @thick T.Type) -> T
// CHECK: %17 = apply %16(%14, %15) : $@convention(method) (T, @thick T.Type) -> T
// CHECK: store %17 to %12 : $*T
// CHECK: %19 = tuple ()
// CHECK: unreachable
// CHECK: } // end sil function '_T016eager_specialize1PPAAE1fxxFZ'

sil [_specialize exported: false, kind: full, where Self == T] @_T016eager_specialize1PPAAE1fxxFZ : $@convention(method) <Self where Self : P> (@in Self, @thick Self.Type) -> @out Self {
bb0(%0 : $*Self, %1 : $*Self, %2 : $@thick Self.Type):
// function_ref error
%5 = function_ref @error : $@convention(thin) () -> Never
%6 = apply %5() : $@convention(thin) () -> Never
unreachable
} // end sil function '_T016eager_specialize1PPAAE1fxxFZ'


////////////////////////////////////////////////////////////////////
// Check that IRGen generates efficient code for fixed-size Trivial
// constraints.
Expand Down