// RUN: %target-sil-opt -enable-sil-verify-all -generic-specializer -sil-partial-specialization -sil-partial-specialization-with-generic-substitutions %s | %FileCheck %s // Test different cases of partial specialization. // In particular, test the correctness of partial specializations where substitutions may be generic. sil_stage canonical import Builtin import Swift import SwiftShims @_silgen_name("use") public func use(_ t: T) public protocol P { } public struct S { @inline(never) public init(_ t: T) } @inline(never) public func simple_generic_callee(_ u: U, _ v: V) public func simple_generic_caller1(_ u: U) public func simple_generic_caller2(_ u: U) // use sil @use : $@convention(thin) <τ_0_0> (@in τ_0_0) -> () // S.init(_:) sil [noinline] @$s22partial_specialization1SVyACyxGxcfC : $@convention(method) (@in T, @thin S.Type) -> S // simple_generic_callee(_:_:) sil [noinline] @simple_generic_callee : $@convention(thin) (@in U, @in V) -> () { bb0(%0 : $*U, %1 : $*V): // function_ref use %4 = function_ref @use : $@convention(thin) <τ_0_0> (@in τ_0_0) -> () %5 = alloc_stack $U copy_addr %0 to [init] %5 : $*U %7 = apply %4(%5) : $@convention(thin) <τ_0_0> (@in τ_0_0) -> () dealloc_stack %5 : $*U // function_ref use %9 = function_ref @use : $@convention(thin) <τ_0_0> (@in τ_0_0) -> () %10 = alloc_stack $V copy_addr %1 to [init] %10 : $*V %12 = apply %9(%10) : $@convention(thin) <τ_0_0> (@in τ_0_0) -> () dealloc_stack %10 : $*V destroy_addr %1 : $*V destroy_addr %0 : $*U %16 = tuple () return %16 : $() } // end sil function 'simple_generic_callee' // simple_generic_caller1(_:) sil @simple_generic_caller1 : $@convention(thin) (@in U) -> () { bb0(%0 : $*U): // function_ref simple_generic_callee(_:_:) %2 = function_ref @simple_generic_callee : $@convention(thin) <τ_0_0, τ_0_1> (@in τ_0_0, @in τ_0_1) -> () %3 = alloc_stack $U copy_addr %0 to [init] %3 : $*U %5 = integer_literal $Builtin.Int32, 1 %6 = struct $Int32 (%5 : $Builtin.Int32) %7 = alloc_stack $Int32 store %6 to %7 : $*Int32 %9 = apply %2(%3, %7) : $@convention(thin) <τ_0_0, τ_0_1> (@in τ_0_0, @in τ_0_1) -> () dealloc_stack %7 : $*Int32 dealloc_stack %3 : $*U destroy_addr %0 : $*U %13 = tuple () return %13 : $() } // end sil function 'simple_generic_caller1' // simple_generic_caller2(_:) sil @simple_generic_caller2 : $@convention(thin) (@in U) -> () { bb0(%0 : $*U): // function_ref simple_generic_callee(_:_:) %2 = function_ref @simple_generic_callee : $@convention(thin) <τ_0_0, τ_0_1> (@in τ_0_0, @in τ_0_1) -> () // function_ref S.init(_:) %3 = function_ref @$s22partial_specialization1SVyACyxGxcfC : $@convention(method) <τ_0_0> (@in τ_0_0, @thin S<τ_0_0>.Type) -> S<τ_0_0> %4 = metatype $@thin S.Type %5 = alloc_stack $U copy_addr %0 to [init] %5 : $*U %7 = apply %3(%5, %4) : $@convention(method) <τ_0_0> (@in τ_0_0, @thin S<τ_0_0>.Type) -> S<τ_0_0> dealloc_stack %5 : $*U %9 = alloc_stack $S store %7 to %9 : $*S %11 = integer_literal $Builtin.Int32, 1 %12 = struct $Int32 (%11 : $Builtin.Int32) %13 = alloc_stack $Int32 store %12 to %13 : $*Int32 %15 = apply %2, Int32>(%9, %13) : $@convention(thin) <τ_0_0, τ_0_1> (@in τ_0_0, @in τ_0_1) -> () dealloc_stack %13 : $*Int32 dealloc_stack %9 : $*S destroy_addr %0 : $*U %19 = tuple () return %19 : $() } // end sil function 'simple_generic_caller2' // Check that a partial specialization for simple_generic_callee was created. // CHECK-LABEL: sil shared [noinline] @$s21simple_generic_calleexs5Int32Vq_RszACRs0_r1_lIetiy_Tp5 : $@convention(thin) <τ_0_0, τ_0_1, τ_0_2 where τ_0_0 == τ_0_1, τ_0_2 == Int32> (@in τ_0_0, Int32) -> () // Check that a partial specialization for simple_generic_callee, Int32> was created. // CHECK-LABEL: sil shared [noinline] @$s21simple_generic_callee4main1SVyxGs5Int32VAERs_AGRs0_r1_lIetyy_Tp5 : $@convention(thin) <τ_0_0, τ_0_1, τ_0_2 where τ_0_1 == S<τ_0_0>, τ_0_2 == Int32> (S<τ_0_0>, Int32) -> () // Check that no partial specializations are produced if all substitutions in the substitution list // are archetypes. // CHECK-LABEL: sil @simple_generic_caller3 : $@convention(thin) (@in U) -> () // CHECK-NOT: specialized // CHECK: function_ref @simple_generic_callee : $@convention(thin) <τ_0_0, τ_0_1> (@in τ_0_0, @in τ_0_1) -> () // CHECK-NOT: specialized // CHECK: // end sil function 'simple_generic_caller3' // simple_generic_caller3(_:) sil @simple_generic_caller3 : $@convention(thin) (@in U) -> () { bb0(%0 : $*U): // function_ref simple_generic_callee(_:_:) %2 = function_ref @simple_generic_callee : $@convention(thin) <τ_0_0, τ_0_1> (@in τ_0_0, @in τ_0_1) -> () %3 = alloc_stack $U copy_addr %0 to [init] %3 : $*U %5 = apply %2(%3, %0) : $@convention(thin) <τ_0_0, τ_0_1> (@in τ_0_0, @in τ_0_1) -> () dealloc_stack %3 : $*U %7 = tuple () return %7 : $() } // end sil function 'simple_generic_caller3' // Check that partial specialization is not performed for generic type parameters where // the substitution contains an open existential. // CHECK-LABEL: sil @simple_generic_caller4 : $@convention(thin) (@in any P, Builtin.Int1) -> () // CHECK: function_ref @$s21simple_generic_calleexBi1_Bi1_Rs_r0_lIetiy_Tp5 : $@convention(thin) <τ_0_0, τ_0_1 where τ_0_1 == Builtin.Int1> (@in τ_0_0, Builtin.Int1) -> () // CHECK: // end sil function 'simple_generic_caller4' sil @simple_generic_caller4 : $@convention(thin) (@in P, Builtin.Int1) -> () { bb0(%0 : $*P, %1: $Builtin.Int1): cond_br %1, bb1, bb2 bb1: %2 = open_existential_addr mutable_access %0 : $*P to $*@opened("1B6851A6-4796-11E6-B7DF-B8E856428C60", P) Self %3 = alloc_stack $@opened("1B6851A6-4796-11E6-B7DF-B8E856428C60", P) Self copy_addr [take] %2 to [init] %3 : $*@opened("1B6851A6-4796-11E6-B7DF-B8E856428C60", P) Self %5 = alloc_stack $Builtin.Int1 store %1 to %5 : $*Builtin.Int1 // function_ref simple_generic_callee(_:_:) %7 = function_ref @simple_generic_callee : $@convention(thin) <τ_0_0, τ_0_1> (@in τ_0_0, @in τ_0_1) -> () %8 = apply %7<@opened("1B6851A6-4796-11E6-B7DF-B8E856428C60", P) Self, Builtin.Int1>(%3, %5) : $@convention(thin) <τ_0_0, τ_0_1> (@in τ_0_0, @in τ_0_1) -> () dealloc_stack %5 : $*Builtin.Int1 destroy_addr %3 : $*@opened("1B6851A6-4796-11E6-B7DF-B8E856428C60", P) Self dealloc_stack %3 : $*@opened("1B6851A6-4796-11E6-B7DF-B8E856428C60", P) Self br bb3 bb2: destroy_addr %0 : $*P br bb3 bb3: %13 = tuple () return %13 : $() } // end sil function 'simple_generic_caller4'