File: devirt_static_witness_method.sil

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (93 lines) | stat: -rw-r--r-- 4,396 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// 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."+"
// 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."+" : $@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."+"
// 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."+" : $@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."+": @operator_plus_static_non_generic_witness
}

sil_witness_table hidden <E where E : CanAdd> S<E>: CanAdd module Test {
  method #CanAdd."+": @operator_plus_static_non_generic_witness_for_S
}