|
| 1 | +// RUN: %target-sil-opt -wmo -enable-sil-verify-all %s -enable-sil-existential-specializer -existential-specializer -inline -sil-combine -generic-specializer -devirtualizer |
| 2 | + |
| 3 | +// This file tests existential specializer transformation followed by concrete type propagation and generic specialization leading to a devirtualization of a witness method call. |
| 4 | + |
| 5 | +sil_stage canonical |
| 6 | + |
| 7 | +import Builtin |
| 8 | +import Swift |
| 9 | +import SwiftShims |
| 10 | + |
| 11 | +protocol RP { |
| 12 | + func getThres() -> Int32 |
| 13 | +} |
| 14 | + |
| 15 | +class RC : RP { |
| 16 | + @inline(never) func getThres() -> Int32 |
| 17 | +} |
| 18 | + |
| 19 | +sil hidden [noinline] @$s6simple2RCC8getThress5Int32VyF : $@convention(method) (@guaranteed RC) -> Int32 { |
| 20 | +bb0(%0 : $RC): |
| 21 | + %1 = integer_literal $Builtin.Int32, 10 |
| 22 | + %2 = struct $Int32 (%1 : $Builtin.Int32) |
| 23 | + return %2 : $Int32 |
| 24 | +} |
| 25 | + |
| 26 | +// Note: The function_ref of "function_ref @$s6simple4find5count4Obj1Sbs5Int32V_AA2RP_ptFTf4ne_n4main2RCC_Tg5 : $@convention(thin) (Int32, @guaranteed RC) -> Bool" in the transformed code was originally a "function_ref @$s6simple4find5count4Obj1Sbs5Int32V_AA2RP_ptF : $@convention(thin) (Int32, @in_guaranteed RP) -> Bool" taking an existential parameter and has been generic specialized after existential specialization. |
| 27 | +// CHECK-LABEL: sil hidden [noinline] @$s6simple12find_wrapperSbyF : $@convention(thin) () -> Bool { |
| 28 | +// CHECK: bb0: |
| 29 | +// CHECK: alloc_ref |
| 30 | +// CHECK: integer_literal |
| 31 | +// CHECK: struct |
| 32 | +// CHECK: alloc_stack |
| 33 | +// CHECK: store |
| 34 | +// CHECK: strong_retain |
| 35 | +// CHECK: function_ref @$s6simple4find5count4Obj1Sbs5Int32V_AA2RP_ptFTf4ne_n4main2RCC_Tg5 : $@convention(thin) (Int32, @guaranteed RC) -> Bool |
| 36 | +// CHECK: load |
| 37 | +// CHECK: apply |
| 38 | +// CHECK: destroy_addr |
| 39 | +// CHECK: dealloc_stack |
| 40 | +// CHECK: strong_release |
| 41 | +// CHECK: return |
| 42 | +// CHECK-LABEL: } |
| 43 | +sil hidden [noinline] @$s6simple12find_wrapperSbyF : $@convention(thin) () -> Bool { |
| 44 | +bb0: |
| 45 | + %0 = alloc_ref $RC |
| 46 | + %3 = integer_literal $Builtin.Int32, 0 |
| 47 | + %4 = struct $Int32 (%3 : $Builtin.Int32) |
| 48 | + %5 = alloc_stack $RP |
| 49 | + %6 = init_existential_addr %5 : $*RP, $RC |
| 50 | + store %0 to %6 : $*RC |
| 51 | + %8 = function_ref @$s6simple4find5count4Obj1Sbs5Int32V_AA2RP_ptF : $@convention(thin) (Int32, @in_guaranteed RP) -> Bool |
| 52 | + strong_retain %0 : $RC |
| 53 | + %10 = apply %8(%4, %5) : $@convention(thin) (Int32, @in_guaranteed RP) -> Bool |
| 54 | + destroy_addr %5 : $*RP |
| 55 | + dealloc_stack %5 : $*RP |
| 56 | + strong_release %0 : $RC |
| 57 | + return %10 : $Bool |
| 58 | +} |
| 59 | + |
| 60 | +sil private [transparent] [thunk] @$s6simple2RCCAA2RPA2aDP8getThress5Int32VyFTW : $@convention(witness_method: RP) (@in_guaranteed RC) -> Int32 { |
| 61 | +bb0(%0 : $*RC): |
| 62 | + %1 = load %0 : $*RC |
| 63 | + %2 = class_method %1 : $RC, #RC.getThres!1 : (RC) -> () -> Int32, $@convention(method) (@guaranteed RC) -> Int32 |
| 64 | + %3 = apply %2(%1) : $@convention(method) (@guaranteed RC) -> Int32 |
| 65 | + return %3 : $Int32 |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +// Note 1: The function_ref of "function_ref @$s6simple2RCC8getThress5Int32VyF : $@convention(method) (@guaranteed RC) -> Int32" in the transformed code was originally a witness method "witness_method $@opened("7AE0C0CA-28D3-11E9-B1D4-F21898973DF0") RP, #RP.getThres!1 : <Self where Self : RP> (Self) -> () -> Int32, %4 : $*@opened("7AE0C0CA-28D3-11E9-B1D4-F21898973DF0") RP : $@convention(witness_method: RP) <τ_0_0 where τ_0_0 : RP> (@in_guaranteed τ_0_0) -> Int32" and has been devirtualized after existential specialization. |
| 70 | +// Note 2: The function_ref of "function_ref @$s6simple4find5count4Obj1Sbs5Int32V_AA2RP_ptFTf4ne_n4main2RCC_Tg5 : $@convention(thin) (Int32, @guaranteed RC) -> Bool" in the transformed code was originally a "function_ref @$s6simple4find5count4Obj1Sbs5Int32V_AA2RP_ptF : $@convention(thin) (Int32, @in_guaranteed RP) -> Bool" taking an existential parameter and has been generic specialized after existential specialization. |
| 71 | +// CHECK-LABEL: sil shared [noinline] @$s6simple4find5count4Obj1Sbs5Int32V_AA2RP_ptFTf4ne_n4main2RCC_Tg5 : $@convention(thin) (Int32, @guaranteed RC) -> Bool { |
| 72 | +// CHECK: bb0 |
| 73 | +// CHECK: alloc_stack |
| 74 | +// CHECK: store |
| 75 | +// CHECK: alloc_stack |
| 76 | +// CHECK: init_existential_addr |
| 77 | +// CHECK: copy_addr |
| 78 | +// CHECK: open_existential_addr |
| 79 | +// CHECK: unchecked_addr_cast |
| 80 | +// CHECK: load |
| 81 | +// CHECK: function_ref @$s6simple2RCC8getThress5Int32VyF : $@convention(method) (@guaranteed RC) -> Int32 |
| 82 | +// CHECK: apply |
| 83 | +// CHECK: struct_extract |
| 84 | +// CHECK: struct_extract |
| 85 | +// CHECK: builtin |
| 86 | +// CHECK: cond_br |
| 87 | +// CHECK: bb1: |
| 88 | +// CHECK: integer_literal |
| 89 | +// CHECK: integer_literal |
| 90 | +// CHECK: builtin |
| 91 | +// CHECK: tuple_extract |
| 92 | +// CHECK: struct |
| 93 | +// CHECK: function_ref @$s6simple4find5count4Obj1Sbs5Int32V_AA2RP_ptFTf4ne_n4main2RCC_Tg5 : $@convention(thin) (Int32, @guaranteed RC) -> Bool |
| 94 | +// CHECK: load |
| 95 | +// CHECK: apply |
| 96 | +// CHECK: br |
| 97 | +// CHECK: bb2: |
| 98 | +// CHECK: integer_literal |
| 99 | +// CHECK: struct |
| 100 | +// CHECK: br |
| 101 | +// CHECK: bb3 |
| 102 | +// CHECK: dealloc_stack |
| 103 | +// CHECK: dealloc_stack |
| 104 | +// CHECK: return |
| 105 | +// CHECK-LABEL: } |
| 106 | +sil hidden [noinline] @$s6simple4find5count4Obj1Sbs5Int32V_AA2RP_ptF : $@convention(thin) (Int32, @in_guaranteed RP) -> Bool { |
| 107 | +bb0(%0 : $Int32, %1 : $*RP): |
| 108 | + %4 = open_existential_addr immutable_access %1 : $*RP to $*@opened("7AE0C0CA-28D3-11E9-B1D4-F21898973DF0") RP |
| 109 | + %5 = witness_method $@opened("7AE0C0CA-28D3-11E9-B1D4-F21898973DF0") RP, #RP.getThres!1 : <Self where Self : RP> (Self) -> () -> Int32, %4 : $*@opened("7AE0C0CA-28D3-11E9-B1D4-F21898973DF0") RP : $@convention(witness_method: RP) <τ_0_0 where τ_0_0 : RP> (@in_guaranteed τ_0_0) -> Int32 |
| 110 | + %6 = apply %5<@opened("7AE0C0CA-28D3-11E9-B1D4-F21898973DF0") RP>(%4) : $@convention(witness_method: RP) <τ_0_0 where τ_0_0 : RP> (@in_guaranteed τ_0_0) -> Int32 |
| 111 | + %7 = struct_extract %0 : $Int32, #Int32._value |
| 112 | + %8 = struct_extract %6 : $Int32, #Int32._value |
| 113 | + %9 = builtin "cmp_slt_Int32"(%7 : $Builtin.Int32, %8 : $Builtin.Int32) : $Builtin.Int1 |
| 114 | + cond_br %9, bb2, bb1 |
| 115 | + |
| 116 | +bb1: |
| 117 | + %11 = integer_literal $Builtin.Int1, -1 |
| 118 | + %12 = struct $Bool (%11 : $Builtin.Int1) |
| 119 | + br bb3(%12 : $Bool) |
| 120 | + |
| 121 | +bb2: |
| 122 | + %14 = integer_literal $Builtin.Int32, 1 |
| 123 | + %15 = integer_literal $Builtin.Int1, -1 |
| 124 | + %16 = builtin "sadd_with_overflow_Int32"(%7 : $Builtin.Int32, %14 : $Builtin.Int32, %15 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1) |
| 125 | + %17 = tuple_extract %16 : $(Builtin.Int32, Builtin.Int1), 0 |
| 126 | + %18 = struct $Int32 (%17 : $Builtin.Int32) |
| 127 | + %19 = function_ref @$s6simple4find5count4Obj1Sbs5Int32V_AA2RP_ptF : $@convention(thin) (Int32, @in_guaranteed RP) -> Bool |
| 128 | + %20 = apply %19(%18, %1) : $@convention(thin) (Int32, @in_guaranteed RP) -> Bool |
| 129 | + br bb3(%20 : $Bool) |
| 130 | + |
| 131 | +bb3(%22 : $Bool): |
| 132 | + return %22 : $Bool |
| 133 | +} |
| 134 | + |
| 135 | +sil_vtable RC { |
| 136 | + #RC.getThres!1: (RC) -> () -> Int32 : @$s6simple2RCC8getThress5Int32VyF |
| 137 | +} |
| 138 | + |
| 139 | +sil_witness_table hidden RC: RP module simple { |
| 140 | + method #RP.getThres!1: <Self where Self : RP> (Self) -> () -> Int32 : @$s6simple2RCCAA2RPA2aDP8getThress5Int32VyFTW |
| 141 | +} |
0 commit comments