blob: 68a3b5206152153905a89b29b83b4ae7660a2935 [file] [log] [blame]
// RUN: %target-sil-opt -enable-sil-verify-all %s -devirtualizer -sil-combine | %FileCheck %s
sil_stage canonical
import Builtin
import Swift
import SwiftShims
protocol CanAdd {
static func +(lhs: Self, rhs: Self) -> Self
}
extension Int64 : CanAdd {
}
struct S<T> : CanAdd {
}
func +<T>(lhs: S<T>, rhs: S<T>) -> S<T>
sil hidden [transparent] [thunk] @operator_plus_static_non_generic_witness_for_S : $@convention(witness_method: CanAdd) <T> (@in S<T>, @in S<T>, @thick S<T>.Type) -> @out S<T> {
bb0(%0 : $*S<T>, %1 : $*S<T>, %2 : $*S<T>, %3 : $@thick S<T>.Type) :
%17 = tuple ()
return %17 : $()
}
sil hidden [transparent] [thunk] @operator_plus_static_non_generic_witness : $@convention(witness_method: CanAdd) (@in Int64, @in Int64, @thick Int64.Type) -> @out Int64 {
bb0(%0 : $*Int64, %1 : $*Int64, %2 : $*Int64, %3 : $@thick Int64.Type):
%4 = struct_element_addr %1 : $*Int64, #Int64._value
%5 = load %4 : $*Builtin.Int64
%6 = struct_element_addr %2 : $*Int64, #Int64._value
%7 = load %6 : $*Builtin.Int64
%8 = integer_literal $Builtin.Int1, -1
%9 = builtin "sadd_with_overflow_Int64"(%5 : $Builtin.Int64, %7 : $Builtin.Int64, %8 : $Builtin.Int1) : $(Builtin.Int64, Builtin.Int1)
%10 = tuple_extract %9 : $(Builtin.Int64, Builtin.Int1), 0
%11 = tuple_extract %9 : $(Builtin.Int64, Builtin.Int1), 1
cond_fail %11 : $Builtin.Int1
%15 = struct $Int64 (%10 : $Builtin.Int64)
store %15 to %0 : $*Int64
%17 = tuple ()
return %17 : $()
}
// Test that this partial application of a function reference referring to static non generic witness does not crash the IRGen.
// Such code may be produced e.g. when users refer to global operators defined on builtin types.
// CHECK-LABEL: sil hidden @test_partial_apply_of_static_witness
sil hidden @test_partial_apply_of_static_witness : $@convention(thin) () -> @callee_owned (@in Int64, @in Int64) -> @out Int64 {
bb0:
%1 = metatype $@thick Int64.Type
%2 = function_ref @operator_plus_static_non_generic_witness : $@convention(witness_method: CanAdd) (@in Int64, @in Int64, @thick Int64.Type) -> @out Int64
%3 = partial_apply %2(%1) : $@convention(witness_method: CanAdd) (@in Int64, @in Int64, @thick Int64.Type) -> @out Int64
return %3 : $@callee_owned (@in Int64, @in Int64) -> @out Int64
}
// Test that this partial application of witness_method can be devirtualized.
// CHECK-LABEL: sil hidden @test_devirt_of_partial_apply_of_witness_method
// CHECK-NOT: witness_method $S<Int64>, #CanAdd."+"!1
// CHECK: function_ref @operator_plus_static_non_generic_witness_for_S
// CHECK: partial_apply
// CHECK: return
sil hidden @test_devirt_of_partial_apply_of_witness_method : $@convention(thin) () -> @callee_owned (@in S<Int64>, @in S<Int64>) -> @out S<Int64> {
bb0:
%1 = metatype $@thick S<Int64>.Type
%2 = witness_method $S<Int64>, #CanAdd."+"!1 : $@convention(witness_method: CanAdd) <τ_0_0 where τ_0_0 : CanAdd> (@in τ_0_0, @in τ_0_0, @thick τ_0_0.Type) -> @out τ_0_0
%3 = partial_apply %2<S<Int64>>(%1) : $@convention(witness_method: CanAdd) _0_0 where τ_0_0 : CanAdd> (@in τ_0_0, @in τ_0_0, @thick τ_0_0.Type) -> @out τ_0_0
return %3 : $@callee_owned (@in S<Int64>, @in S<Int64>) -> @out S<Int64>
}
// Test that this application of witness_method can be devirtualized.
// CHECK-LABEL: sil hidden @test_devirt_of_apply_of_witness_method
// CHECK-NOT: witness_method $S<Int64>, #CanAdd."+"!1
// CHECK: function_ref @operator_plus_static_non_generic_witness_for_S
// CHECK: apply
// CHECK: return
sil hidden @test_devirt_of_apply_of_witness_method : $@convention(thin) (@in S<Int64>) -> S<Int64> {
bb0(%0 : $*S<Int64>):
%1 = alloc_stack $S<Int64>
%5 = metatype $@thick S<Int64>.Type
%6 = witness_method $S<Int64>, #CanAdd."+"!1 : $@convention(witness_method: CanAdd) <τ_0_0 where τ_0_0 : CanAdd> (@in τ_0_0, @in τ_0_0, @thick τ_0_0.Type) -> @out τ_0_0
%7 = apply %6<S<Int64>>(%1, %0, %0, %5) : $@convention(witness_method: CanAdd) _0_0 where τ_0_0 : CanAdd> (@in τ_0_0, @in τ_0_0, @thick τ_0_0.Type) -> @out τ_0_0
%8 = load %1: $*S<Int64>
dealloc_stack %1 : $*S<Int64>
return %8 : $S<Int64>
}
sil_witness_table hidden Int64: CanAdd module Test {
method #CanAdd."+"!1: @operator_plus_static_non_generic_witness
}
sil_witness_table hidden <E where E : CanAdd> S<E>: CanAdd module Test {
method #CanAdd."+"!1: @operator_plus_static_non_generic_witness_for_S
}