File: partial_apply.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 (636 lines) | stat: -rw-r--r-- 32,619 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -g -emit-module -enable-library-evolution -emit-module-path=%t/resilient_struct.swiftmodule -module-name=resilient_struct %S/../../Inputs/resilient_struct.swift
// RUN: %target-swift-frontend -g -I %t -emit-ir %s | %FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-%target-ptrsize

// REQUIRES: concurrency

import Builtin
import Swift
import resilient_struct

class SwiftClass {}
sil_vtable SwiftClass {}
sil @$s13partial_apply10SwiftClassCfD : $@async @convention(method) (SwiftClass) -> ()

sil @partially_applyable_to_class : $@async @convention(thin) (@guaranteed SwiftClass) -> () {
entry(%c : $SwiftClass):
  %0 = builtin "int_trap"() : $Never
  unreachable
}
sil @partially_applyable_to_two_classes : $@async @convention(thin) (@guaranteed SwiftClass, @guaranteed SwiftClass) -> () {
entry(%c : $SwiftClass, %d : $SwiftClass):
  %0 = builtin "int_trap"() : $Never
  unreachable
}


sil @use_closure : $@async @convention(thin) (@noescape @async @callee_guaranteed () -> ()) -> ()

// CHECK: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @partial_apply_class(
sil @partial_apply_class : $@async @convention(thin) (SwiftClass) -> @async @callee_guaranteed () -> () {
entry(%c : $SwiftClass):
  %f = function_ref @partially_applyable_to_class : $@async @convention(thin) (@guaranteed SwiftClass) -> ()
  %g = partial_apply [callee_guaranteed] %f(%c) : $@async @convention(thin) (@guaranteed SwiftClass) -> ()
  return %g : $@async @callee_guaranteed () -> ()
}

// CHECK: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @partial_apply_class_on_stack(
sil @partial_apply_class_on_stack : $@async @convention(thin) (@owned SwiftClass) -> () {
entry(%a : $SwiftClass):
  %f = function_ref @partially_applyable_to_class : $@async @convention(thin) (@guaranteed SwiftClass) -> ()
  %c = partial_apply [callee_guaranteed] [on_stack] %f(%a) : $@async @convention(thin) (@guaranteed SwiftClass) -> ()
  %use = function_ref @use_closure : $@async @convention(thin) (@noescape @async @callee_guaranteed () -> ()) -> ()
  apply %use(%c) : $@async @convention(thin) (@noescape @async @callee_guaranteed () -> ()) -> ()
  dealloc_stack %c : $@noescape @async @callee_guaranteed () ->()
  strong_release %a : $SwiftClass
  %t = tuple()
  return %t : $()
}

// CHECK: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @partial_apply_two_classes_on_stack(

sil @partial_apply_two_classes_on_stack : $@async @convention(thin) (@owned SwiftClass, @owned SwiftClass) -> () {
entry(%a : $SwiftClass, %b: $SwiftClass):
  %f = function_ref @partially_applyable_to_two_classes : $@async @convention(thin) (@guaranteed SwiftClass, @guaranteed SwiftClass) -> ()
  %c = partial_apply [callee_guaranteed] [on_stack] %f(%a, %b) : $@async @convention(thin) (@guaranteed SwiftClass, @guaranteed SwiftClass) -> ()
  %use = function_ref @use_closure : $@async @convention(thin) (@noescape @async @callee_guaranteed () -> ()) -> ()
  apply %use(%c) : $@async @convention(thin) (@noescape @async @callee_guaranteed () -> ()) -> ()
  dealloc_stack %c : $@noescape @async @callee_guaranteed () ->()
  strong_release %a : $SwiftClass
  strong_release %b : $SwiftClass
  %t = tuple()
  return %t : $()
}
// CHECK-LABEL: define internal swift{{(tail)?}}cc void @"$s22generic_captured_paramTA"(
sil @generic_captured_param : $@async @convention(thin) <T> (Int, @inout T) -> Int {
entry(%i : $Int, %t : $*T):
return %i : $Int
}

sil @partial_apply_generic_capture : $@async @convention(thin) (Int) -> @async @callee_guaranteed (Int) -> Int {
entry(%x : $Int):
  %a = alloc_stack $Int
  store %x to %a : $*Int
  %f = function_ref @generic_captured_param : $@async @convention(thin) <T> (Int, @inout T) -> Int
  %p = partial_apply [callee_guaranteed] %f<Int>(%a) : $@async @convention(thin) <T> (Int, @inout T) -> Int
  dealloc_stack %a : $*Int
  return %p : $@async @callee_guaranteed (Int) -> Int
}

sil public @generic_captured_and_open_param : $@async @convention(thin) <T> (@in T, @inout T) -> @out T {
entry(%o : $*T, %i : $*T, %io : $*T):
  %0 = builtin "int_trap"() : $Never
  unreachable
}


// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @partial_apply_open_generic_capture(
sil @partial_apply_open_generic_capture : $@async @convention(thin) <T> (@inout T) -> @async @callee_guaranteed (@in T) -> @out T {
entry(%a : $*T):
  %f = function_ref @generic_captured_and_open_param : $@async @convention(thin) <U> (@in U, @inout U) -> @out U
  %p = partial_apply [callee_guaranteed] %f<T>(%a) : $@async @convention(thin) <U> (@in U, @inout U) -> @out U
  return %p : $@async @callee_guaranteed (@in T) -> @out T
}

/*****************************************************************************/
/* Swift-refcounted class captures.  Optimizable by using the reference      */
/* as the partial apply context.                                             */
/*****************************************************************************/

sil public @guaranteed_captured_class_param : $@async @convention(thin) (Int, @guaranteed SwiftClass) -> Int {
entry(%i : $Int, %c : $SwiftClass):
  %0 = builtin "int_trap"() : $Never
  unreachable
}

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @partial_apply_guaranteed_class_param(

sil @partial_apply_guaranteed_class_param : $@async @convention(thin) (@owned SwiftClass) -> @async @callee_guaranteed (Int) -> Int {
bb0(%x : $SwiftClass):
  %f = function_ref @guaranteed_captured_class_param : $@async @convention(thin) (Int, @guaranteed SwiftClass) -> Int
  %p = partial_apply [callee_guaranteed] %f(%x) : $@async @convention(thin) (Int, @guaranteed SwiftClass) -> Int
  return %p : $@async @callee_guaranteed (Int) -> Int
}

sil public @indirect_guaranteed_captured_class_param : $@async @convention(thin) (Int, @in_guaranteed SwiftClass) -> Int {
entry(%i : $Int, %c : $*SwiftClass):
  %0 = builtin "int_trap"() : $Never
  unreachable
}

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @partial_apply_indirect_guaranteed_class_param(
// CHECK-LABEL: define internal swift{{(tail)?}}cc void @"$s40indirect_guaranteed_captured_class_paramTA"(

sil @partial_apply_indirect_guaranteed_class_param : $@async @convention(thin) (@in SwiftClass) -> @async @callee_guaranteed (Int) -> Int {
bb0(%x : $*SwiftClass):
  %f = function_ref @indirect_guaranteed_captured_class_param : $@async @convention(thin) (Int, @in_guaranteed SwiftClass) -> Int
  %p = partial_apply [callee_guaranteed] %f(%x) : $@async @convention(thin) (Int, @in_guaranteed SwiftClass) -> Int
  return %p : $@async @callee_guaranteed (Int) -> Int
}

sil public @indirect_consumed_captured_class_param : $@async @convention(thin) (Int, @in SwiftClass) -> Int {
entry(%i : $Int, %c : $*SwiftClass):
  %0 = builtin "int_trap"() : $Never
  unreachable
}

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @partial_apply_indirect_consumed_class_param(
// CHECK-LABEL: define internal swift{{(tail)?}}cc void @"$s38indirect_consumed_captured_class_paramTA"(

sil @partial_apply_indirect_consumed_class_param : $@async @convention(thin) (@in SwiftClass) -> @async @callee_guaranteed (Int) -> Int {
bb0(%x : $*SwiftClass):
  %f = function_ref @indirect_consumed_captured_class_param : $@async @convention(thin) (Int, @in SwiftClass) -> Int
  %p = partial_apply [callee_guaranteed] %f(%x) : $@async @convention(thin) (Int, @in SwiftClass) -> Int
  return %p : $@async @callee_guaranteed (Int) -> Int
}

/*****************************************************************************/
/* A non-trivial capture.  Indirect applications can directly reference the  */
/* field from the partial apply context.                                     */
/*****************************************************************************/

struct SwiftClassPair { var x: SwiftClass, y: SwiftClass }

sil public @guaranteed_captured_class_pair_param : $@async @convention(thin) (Int, @guaranteed SwiftClassPair) -> Int {
entry(%i : $Int, %c : $SwiftClassPair):
  %0 = builtin "int_trap"() : $Never
  unreachable
}


// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @partial_apply_guaranteed_class_pair_param(
// CHECK-LABEL: define internal swift{{(tail)?}}cc void @"$s36guaranteed_captured_class_pair_paramTA"(

sil @partial_apply_guaranteed_class_pair_param : $@async @convention(thin) (@owned SwiftClassPair) -> @async @callee_guaranteed (Int) -> Int {
bb0(%x : $SwiftClassPair):
  %f = function_ref @guaranteed_captured_class_pair_param : $@async @convention(thin) (Int, @guaranteed SwiftClassPair) -> Int
  %p = partial_apply [callee_guaranteed] %f(%x) : $@async @convention(thin) (Int, @guaranteed SwiftClassPair) -> Int
  return %p : $@async @callee_guaranteed (Int) -> Int
}

sil public @indirect_guaranteed_captured_class_pair_param : $@async @convention(thin) (Int, @in_guaranteed SwiftClassPair) -> Int {
entry(%i : $Int, %c : $*SwiftClassPair):
  %0 = builtin "int_trap"() : $Never
  unreachable
}

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @partial_apply_indirect_guaranteed_class_pair_param(
// CHECK-LABEL: define internal swift{{(tail)?}}cc void @"$s45indirect_guaranteed_captured_class_pair_paramTA"(

sil @partial_apply_indirect_guaranteed_class_pair_param : $@async @convention(thin) (@in SwiftClassPair) -> @async @callee_guaranteed (Int) -> Int {
bb0(%x : $*SwiftClassPair):
  %f = function_ref @indirect_guaranteed_captured_class_pair_param : $@async @convention(thin) (Int, @in_guaranteed SwiftClassPair) -> Int
  %p = partial_apply [callee_guaranteed] %f(%x) : $@async @convention(thin) (Int, @in_guaranteed SwiftClassPair) -> Int
  return %p : $@async @callee_guaranteed (Int) -> Int
}

sil public @indirect_consumed_captured_class_pair_param : $@async @convention(thin) (Int, @in SwiftClassPair) -> Int {
entry(%i : $Int, %c : $*SwiftClassPair):
  %0 = builtin "int_trap"() : $Never
  unreachable
}


// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @partial_apply_indirect_consumed_class_pair_param(
// CHECK-LABEL: define internal swift{{(tail)?}}cc void @"$s43indirect_consumed_captured_class_pair_paramTA"(

sil @partial_apply_indirect_consumed_class_pair_param : $@async @convention(thin) (@in SwiftClassPair) -> @async @callee_guaranteed (Int) -> Int {
bb0(%x : $*SwiftClassPair):
  %f = function_ref @indirect_consumed_captured_class_pair_param : $@async @convention(thin) (Int, @in SwiftClassPair) -> Int
  %p = partial_apply [callee_guaranteed] %f(%x) : $@async @convention(thin) (Int, @in SwiftClassPair) -> Int
  return %p : $@async @callee_guaranteed (Int) -> Int
}

sil public @captured_fixed_and_dependent_params : $@async @convention(thin) <A> (@guaranteed SwiftClass, @in_guaranteed A, Int) -> () {
entry(%c : $SwiftClass, %a : $*A, %i : $Int):
  %0 = builtin "int_trap"() : $Never
  unreachable
}

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @partial_apply_indirect_non_fixed_layout
// CHECK-LABEL: define internal swift{{(tail)?}}cc void @"$s35captured_fixed_and_dependent_paramsTA"(
sil @partial_apply_indirect_non_fixed_layout : $@async @convention(thin) <T> (@owned SwiftClass, @in T, Int) -> @async @callee_guaranteed () -> () {
bb0(%a : $SwiftClass, %b : $*T, %c : $Int):
  %f = function_ref @captured_fixed_and_dependent_params : $@async @convention(thin) <B> (@guaranteed SwiftClass, @in_guaranteed B, Int) -> ()
  %p = partial_apply [callee_guaranteed] %f<T>(%a, %b, %c) : $@async @convention(thin) <C> (@guaranteed SwiftClass, @in_guaranteed C, Int) -> ()
  return %p : $@async @callee_guaranteed () -> ()
}

sil public @captured_dependent_out_param : $@async @convention(thin) <A> (@in A) -> @out A {
entry(%o : $*A, %i : $*A):
  %0 = builtin "int_trap"() : $Never
  unreachable
}

sil @partial_apply_with_out_param : $@async @convention(thin) <T> (@in T) -> @async @callee_guaranteed () -> @out T {
bb0(%x : $*T):
  %f = function_ref @captured_dependent_out_param : $@async @convention(thin) <B> (@in B) -> @out B
  %p = partial_apply [callee_guaranteed] %f<T>(%x) : $@async @convention(thin) <C> (@in C) -> @out C
  return %p : $@async @callee_guaranteed () -> @out T
}

// CHECK-LABEL: define internal swift{{(tail)?}}cc void @"$s28captured_dependent_out_paramTA"(

sil @partial_apply_dynamic_with_out_param : $@async @convention(thin) <T> (Int32, @owned @async @callee_guaranteed (Int32) -> @out T) -> @async @callee_guaranteed () -> @out T {
bb0(%x : $Int32, %f : $@async @callee_guaranteed (Int32) -> @out T):
  %p = partial_apply [callee_guaranteed] %f(%x) : $@async @callee_guaranteed (Int32) -> @out T
  return %p : $@async @callee_guaranteed () -> @out T
}

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @partial_apply_dynamic_with_out_param(

class Base {
}
sil_vtable Base {}

class Sub : Base {
}

sil_vtable Sub {}

sil @parametric_casting_closure : $@async @convention(thin) <C where C : Base> (@guaranteed Base) -> @owned C {
bb0(%0 : $Base):
  %1 = unconditional_checked_cast %0 : $Base to C
  return %1 : $C
}

sil public_external @receive_closure : $@async @convention(thin) <C where C : Base> (@owned @async @callee_guaranteed () -> (@owned C)) -> ()
sil public_external @receive_closure2 : $@async @convention(thin) <C where C : Base> (@owned @async @callee_guaranteed (@guaranteed Base) -> (@owned C)) -> ()

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @test_partial_apply(
// CHECK-LABEL: define internal swift{{(tail)?}}cc void @"$s26parametric_casting_closureTA"(
// CHECK-LABEL: define internal swift{{(tail)?}}cc void @"$s26parametric_casting_closureTA.{{[0-9]+}}"(
sil @test_partial_apply : $@async @convention(thin) (@owned Base) -> () {
bb0(%0 : $Base):
 %1 = function_ref @parametric_casting_closure : $@async @convention(thin) <C where C : Base> (@guaranteed Base) -> @owned C
 %6 = partial_apply [callee_guaranteed] %1<Sub>() : $@async @convention(thin) <C where C : Base> (@guaranteed Base) -> @owned C
 %2 = partial_apply [callee_guaranteed] %1<Sub>(%0) : $@async @convention(thin) <C where C : Base> (@guaranteed Base) -> @owned C
 %3 = function_ref @receive_closure : $@async @convention(thin) <C where C : Base> (@owned @async @callee_guaranteed () -> (@owned C)) -> ()
 %4 = apply %3<Sub>(%2) : $@async @convention(thin) <C where C : Base> (@owned @async @callee_guaranteed () -> (@owned C)) -> ()
 %5 = function_ref @receive_closure2 : $@async @convention(thin) <C where C : Base> (@owned @async @callee_guaranteed (@guaranteed Base) -> (@owned C)) -> ()
 %8 = apply %5<Sub>(%6) : $@async @convention(thin) <C where C : Base> (@owned @async @callee_guaranteed (@guaranteed Base) -> (@owned C)) -> ()
 %7 = tuple ()
 return %7 : $()
}


protocol P0 {}
protocol P1 { associatedtype X : P0 }
protocol P2 { associatedtype Y : P1 }

sil hidden @complex_generic_function : $@async @convention(thin) <T where T : P2, T.Y : P2> (Int) -> () {
entry(%i : $Int):
  %0 = builtin "int_trap"() : $Never
  unreachable
}

sil public_external @receive_closure3 : $@async @convention(thin) (@owned @async @callee_guaranteed () -> ()) -> ()

sil @partial_apply_complex_generic_function : $@async @convention(thin) <T where T : P2, T.Y : P2> (Int) -> () {
bb0(%0 : $Int):
  %fn = function_ref @complex_generic_function : $@async @convention(thin) <T where T : P2, T.Y : P2> (Int) -> ()
  %pa = partial_apply [callee_guaranteed] %fn <T>(%0) : $@async @convention(thin) <T where T : P2, T.Y : P1, T.Y : P2> (Int) -> ()
  %fn2 = function_ref @receive_closure3 : $@async @convention(thin)  (@owned @async @callee_guaranteed () -> ()) -> ()
  apply %fn2(%pa) : $@async @convention(thin) (@owned @async @callee_guaranteed () -> ()) -> ()
  %result = tuple ()
  return %result : $()
}
// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @partial_apply_complex_generic_function(
// CHECK-LABEL: define internal swift{{(tail)?}}cc void @"$s24complex_generic_functionTA"(
struct ComplexBoundedType<T: P2> {}

// https://github.com/apple/swift/issues/43513
// Ensure that a 'partial_apply' which captures bound generic type metadata
// doesn't crash when restoring the generic context.

sil hidden @generic_function : $@async @convention(thin) <T> () -> () {
  %0 = builtin "int_trap"() : $Never
  unreachable
}
sil @partial_apply_with_generic_type : $@async @convention(thin) <U: P2> () -> () {
bb0:
  %fn = function_ref @generic_function : $@async @convention(thin) <T> () -> ()
  %pa = partial_apply [callee_guaranteed] %fn <ComplexBoundedType<U>>() : $@async @convention(thin) <T> () -> ()
  %result = tuple ()
  return %result : $()
}

// Crash on partial apply of witness_method without generic signature

extension Int: P0 {}

sil hidden_external @concrete_witness_method : $@async @convention(witness_method: P0) (Int, Int) -> ()

sil hidden @partial_apply_witness_method : $@async @convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %fn = function_ref @concrete_witness_method : $@async @convention(witness_method: P0) (Int, Int) -> ()
  %pa = partial_apply [callee_guaranteed] %fn (%0) : $@async @convention(witness_method: P0) (Int, Int) -> ()
  %result = tuple ()
  return %result : $()
}


// Crash on partial apply of a generic enum.
enum GenericEnum<T> {
  case X(String)
  case Y(T, T, T, T, T)
}
sil public @generic_indirect_return : $@async @convention(thin) <T> (Int) -> @owned GenericEnum<T> {
entry(%i : $Int):
  %0 = builtin "int_trap"() : $Never
  unreachable
}

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @partial_apply_generic_indirect_return(
// CHECK-LABEL: define internal swift{{(tail)?}}cc void @"$s23generic_indirect_returnTA"(
sil @partial_apply_generic_indirect_return : $@async @convention(thin) (Int) -> @async @callee_guaranteed () -> @owned GenericEnum<Int> {
  bb0(%0 : $Int):
  %fn = function_ref @generic_indirect_return :$@async @convention(thin) <T> (Int) -> @owned GenericEnum<T>
  %pa = partial_apply [callee_guaranteed] %fn<Int> (%0) : $@async @convention(thin) <T> (Int) -> @owned GenericEnum<T>
  return %pa : $@async @callee_guaranteed () -> @owned GenericEnum<Int>
}

// Crash on partial apply of a generic enum.
enum GenericEnum2<T> {
  case X(String)
  case Y(T)
}
sil public @generic_indirect_return2 : $@async @convention(thin) <T> (Int) -> @owned GenericEnum2<T> {
entry(%i : $Int):
  %0 = builtin "int_trap"() : $Never
  unreachable
}

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @partial_apply_generic_indirect_return2(
// CHECK-LABEL: define internal swift{{(tail)?}}cc void @"$s24generic_indirect_return2TA"(
sil @partial_apply_generic_indirect_return2 : $@async @convention(thin) (Int) -> @async @callee_guaranteed () -> @owned GenericEnum2<Int> {
  bb0(%0 : $Int):
  %fn = function_ref @generic_indirect_return2 :$@async @convention(thin) <T> (Int) -> @owned GenericEnum2<T>
  %pa = partial_apply [callee_guaranteed] %fn<Int> (%0) : $@async @convention(thin) <T> (Int) -> @owned GenericEnum2<T>
  return %pa : $@async @callee_guaranteed () -> @owned GenericEnum2<Int>
}

struct SwiftStruct {}

sil @fun : $@async @convention(thin) (@thin SwiftStruct.Type, @guaranteed SwiftClass) -> () {
entry(%t : $@thin SwiftStruct.Type, %c : $SwiftClass):
  %0 = builtin "int_trap"() : $Never
  unreachable
}

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @partial_apply_thin_type(

sil @partial_apply_thin_type : $@async @convention(thin) (@thin SwiftStruct.Type, @owned SwiftClass) -> @async @callee_guaranteed () -> () {
entry(%0: $@thin SwiftStruct.Type, %1: $SwiftClass):
  %fun = function_ref @fun : $@async @convention(thin) (@thin SwiftStruct.Type, @guaranteed SwiftClass) -> ()
  %closure = partial_apply [callee_guaranteed] %fun (%0, %1) : $@async @convention(thin) (@thin SwiftStruct.Type, @guaranteed SwiftClass) -> ()
  return %closure : $@async @callee_guaranteed () -> ()
}

sil @afun : $@async @convention(thin) (Int) -> @error Error

// Check that we don't assert on a thin noescape function.
// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @convert_thin_test(
sil @convert_thin_test : $@async @convention(thin) (Int) -> () {
bb(%0 : $Int):
  %f = function_ref @afun : $@async @convention(thin) (Int) -> @error Error
  %c = convert_function %f : $@async @convention(thin) (Int) -> @error Error to $@async @convention(thin) @noescape (Int) -> @error Error
  try_apply %c(%0) : $@async @convention(thin) @noescape (Int) -> @error Error, normal bb2, error bb1

bb1(%err: $Error):
  %t = tuple ()
  br bb3(%t: $())

bb2(%r : $()):
  br bb3(%r : $())

bb3(%v : $()):
  return %v : $()
}

struct A1 {
    let b: () -> ()
}

struct A2<T>  {
    let a: T
}

class  A3 {}

sil @amethod : $@async @convention(method) (@in_guaranteed A2<A3>) -> (@owned A1, @error Error) {
entry(%a : $*A2<A3>):
  %0 = builtin "int_trap"() : $Never
  unreachable
}

sil @repo : $@async @convention(thin) (@in_guaranteed A2<A3>) -> @owned @async @callee_guaranteed () -> (@owned A1, @error Error) {
bb0(%0 : $*A2<A3>):
  %1 = load %0 : $*A2<A3>
  %2 = alloc_stack $A2<A3>
  store %1 to %2 : $*A2<A3>
  %4 = function_ref @amethod : $@async @convention(method) (@in_guaranteed A2<A3>) -> (@owned A1, @error Error)
  %5 = partial_apply [callee_guaranteed] %4(%2) : $@async @convention(method) (@in_guaranteed A2<A3>) -> (@owned A1, @error Error)
  dealloc_stack %2 : $*A2<A3>
  return %5 : $@async @callee_guaranteed () -> (@owned A1, @error Error)
}

//sil @capture_class : $@async @convention(thin) (@guaranteed A3) -> ()
//
//// CHECK LABEL: define{{.*}} swift{{(tail)?}}cc ptr @partial_apply_stack_in_coroutine(ptr {{.*}} %0, ptr %1)
//sil @partial_apply_stack_in_coroutine : $@async @yield_once (@owned A3) -> () {
//entry(%0: $A3):
//  %f = function_ref @capture_class : $@async @convention(thin) (@guaranteed A3) -> ()
//  %p = partial_apply [callee_guaranteed] [on_stack] %f(%0) : $@async @convention(thin) (@guaranteed A3) -> ()
//  apply %p() : $@noescape @async @callee_guaranteed () -> ()
//  dealloc_stack %p : $@noescape @async @callee_guaranteed () -> ()
//  %1000 = integer_literal $Builtin.Int32, 1000
//  yield (), resume resume, unwind unwind
//
//resume:
//  %ret = tuple ()
//  return %ret : $()
//
//unwind:
//  unwind
//}
sil_vtable A3 {}


// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @partial_apply_callee_guaranteed_indirect_guaranteed_class_pair_param(
// CHECK-LABEL: define internal swift{{(tail)?}}cc void @"$s45indirect_guaranteed_captured_class_pair_paramTA.{{[0-9]+}}"(
sil @partial_apply_callee_guaranteed_indirect_guaranteed_class_pair_param : $@async @convention(thin) (@in SwiftClassPair) -> @owned @async @callee_guaranteed (Int) -> Int {
bb0(%x : $*SwiftClassPair):
  %f = function_ref @indirect_guaranteed_captured_class_pair_param : $@async @convention(thin) (Int, @in_guaranteed SwiftClassPair) -> Int
  %p = partial_apply [callee_guaranteed] %f(%x) : $@async @convention(thin) (Int, @in_guaranteed SwiftClassPair) -> Int
  return %p : $@async @callee_guaranteed(Int) -> (Int)
}

sil public_external @use_closure2 : $@async @convention(thin) (@noescape @async @callee_guaranteed (Int) -> Int) -> ()

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @partial_apply_stack_callee_guaranteed_indirect_guaranteed_class_pair_param(

sil @partial_apply_stack_callee_guaranteed_indirect_guaranteed_class_pair_param : $@async @convention(thin) (@in_guaranteed SwiftClassPair) -> () {
bb0(%x : $*SwiftClassPair):
  %f = function_ref @indirect_guaranteed_captured_class_pair_param : $@async @convention(thin) (Int, @in_guaranteed SwiftClassPair) -> Int
  %p = partial_apply [callee_guaranteed] [on_stack] %f(%x) : $@async @convention(thin) (Int, @in_guaranteed SwiftClassPair) -> Int
  %u = function_ref @use_closure2 : $@async @convention(thin) (@noescape @async @callee_guaranteed (Int) -> Int) -> ()
  %r = apply %u(%p) : $@async @convention(thin) (@noescape @async @callee_guaranteed (Int) -> Int) -> ()
  dealloc_stack %p : $@noescape @async @callee_guaranteed (Int) ->(Int)
  %t = tuple()
  return %t : $()
}

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @partial_apply_stack_callee_guaranteed_indirect_in_class_pair_param(
// CHECK-LABEL: define internal swift{{(tail)?}}cc void @"$s37indirect_in_captured_class_pair_paramTA"(
sil public @indirect_in_captured_class_pair_param : $@async @convention(thin) (Int, @in_guaranteed SwiftClassPair) -> Int {
entry(%i : $Int, %p : $*SwiftClassPair):
  %0 = builtin "int_trap"() : $Never
  unreachable
}

sil @partial_apply_stack_callee_guaranteed_indirect_in_class_pair_param : $@async @convention(thin) (@in SwiftClassPair) -> () {
bb0(%x : $*SwiftClassPair):
  %f = function_ref @indirect_in_captured_class_pair_param : $@async @convention(thin) (Int, @in_guaranteed SwiftClassPair) -> Int
  %p = partial_apply [callee_guaranteed] [on_stack] %f(%x) : $@async @convention(thin) (Int, @in_guaranteed SwiftClassPair) -> Int
  %u = function_ref @use_closure2 : $@async @convention(thin) (@noescape @async @callee_guaranteed (Int) -> Int) -> ()
  %r = apply %u(%p) : $@async @convention(thin) (@noescape @async @callee_guaranteed (Int) -> Int) -> ()
  dealloc_stack %p : $@noescape @async @callee_guaranteed (Int) ->(Int)
  destroy_addr %x: $*SwiftClassPair
  %t = tuple()
  return %t : $()
}


// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @partial_apply_stack_callee_guaranteed_indirect_in_constant_class_pair_param(
// CHECK-LABEL: define internal swift{{(tail)?}}cc void @"$s46indirect_in_constant_captured_class_pair_paramTA"(
sil public @indirect_in_constant_captured_class_pair_param : $@async @convention(thin) (Int, @in_guaranteed SwiftClassPair) -> Int {
entry(%i : $Int, %ic : $*SwiftClassPair):
  %0 = builtin "int_trap"() : $Never
  unreachable
}

sil @partial_apply_stack_callee_guaranteed_indirect_in_constant_class_pair_param : $@async @convention(thin) (@in SwiftClassPair) -> () {
bb0(%x : $*SwiftClassPair):
  %f = function_ref @indirect_in_constant_captured_class_pair_param : $@async @convention(thin) (Int, @in_guaranteed SwiftClassPair) -> Int
  %p = partial_apply [callee_guaranteed] [on_stack] %f(%x) : $@async @convention(thin) (Int, @in_guaranteed SwiftClassPair) -> Int
  %u = function_ref @use_closure2 : $@async @convention(thin) (@noescape @async @callee_guaranteed (Int) -> Int) -> ()
  %r = apply %u(%p) : $@async @convention(thin) (@noescape @async @callee_guaranteed (Int) -> Int) -> ()
  dealloc_stack %p : $@noescape @async @callee_guaranteed (Int) ->(Int)
  destroy_addr %x: $*SwiftClassPair
  %t = tuple()
  return %t : $()
}

sil public @closure : $@async @convention(thin) (@in_guaranteed ResilientInt, @guaranteed SwiftClass) -> () {
entry(%i : $*ResilientInt, %c : $SwiftClass):
  %0 = builtin "int_trap"() : $Never
  unreachable
}

// Make sure that we use the heap header size (16) for the initial offset.
// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @test_initial_offset(

sil @test_initial_offset : $@async @convention(thin) (@in ResilientInt, @guaranteed SwiftClass) -> () {
bb0(%x : $*ResilientInt, %y : $SwiftClass):
  %f = function_ref @closure : $@async @convention(thin) (@in_guaranteed ResilientInt, @guaranteed SwiftClass) -> ()
  %p = partial_apply [callee_guaranteed] %f(%x, %y) : $@async @convention(thin) (@in_guaranteed ResilientInt, @guaranteed SwiftClass) -> ()
  release_value %p : $@async @callee_guaranteed () ->()
  %t = tuple()
  return %t : $()
}

protocol Proto1 {}
protocol Proto2 {}
struct EmptyType : Proto1 { }

struct SomeType : Proto2 {
  var d : ResilientInt // some resilient type
  var x : Int
}

sil @foo : $@async @convention(thin) <τ_0_0, τ_0_1 where τ_0_0 : Proto1, τ_0_1 : Proto2> (@in_guaranteed τ_0_0, @in_guaranteed τ_0_1) -> () {
entry(%z : $*τ_0_0, %o : $*τ_0_1):
  %0 = builtin "int_trap"() : $Never
  unreachable
}

// CHECK-64-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @empty_followed_by_non_fixed(

sil @empty_followed_by_non_fixed : $@async @convention(thin)  (EmptyType, @in_guaranteed SomeType) -> () {
entry(%0 : $EmptyType, %1: $*SomeType):
  %5 = alloc_stack $EmptyType
  store %0 to %5 : $*EmptyType
  %31 = function_ref @foo : $@async @convention(thin) <τ_0_0, τ_0_1 where τ_0_0 : Proto1, τ_0_1 : Proto2> (@in_guaranteed τ_0_0, @in_guaranteed τ_0_1) -> ()
  %32 = alloc_stack $EmptyType
  copy_addr %5 to [init] %32 : $*EmptyType
  %34 = alloc_stack $SomeType
  copy_addr %1 to [init] %34 : $*SomeType // id: %35
  %36 = partial_apply [callee_guaranteed] %31<EmptyType, SomeType>(%32, %34) : $@async @convention(thin) <τ_0_0, τ_0_1 where τ_0_0 : Proto1, τ_0_1 : Proto2> (@in_guaranteed τ_0_0, @in_guaranteed τ_0_1) -> ()
  release_value %36: $@async @callee_guaranteed () ->()
  dealloc_stack %34 : $*SomeType
  dealloc_stack %32 : $*EmptyType
  dealloc_stack %5 : $*EmptyType
  %40 = tuple()
  return %40 : $()
}

struct FixedType {
  var f: Int32
}
// CHECK-64-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @fixed_followed_by_empty_followed_by_non_fixed

sil @foo2 : $@async @convention(thin) <τ_0_0, τ_0_1, τ_0_2> (@in_guaranteed τ_0_0, @in_guaranteed τ_0_1, @in_guaranteed τ_0_2) -> () {
entry(%z : $*τ_0_0, %o : $*τ_0_1, %t : $*τ_0_2):
  %0 = builtin "int_trap"() : $Never
  unreachable
}
sil @fixed_followed_by_empty_followed_by_non_fixed : $@async @convention(thin)  (EmptyType, @in_guaranteed SomeType, FixedType) -> () {
entry(%0 : $EmptyType, %1: $*SomeType, %3: $FixedType):
  %5 = alloc_stack $EmptyType
  store %0 to %5 : $*EmptyType
  %7 = alloc_stack $FixedType
  store %3 to %7 : $*FixedType
  %31 = function_ref @foo2 : $@async @convention(thin) <τ_0_0, τ_0_1, τ_0_2> (@in_guaranteed τ_0_0, @in_guaranteed τ_0_1, @in_guaranteed τ_0_2) -> ()
  %32 = alloc_stack $EmptyType
  copy_addr %5 to [init] %32 : $*EmptyType
  %34 = alloc_stack $SomeType
  copy_addr %1 to [init] %34 : $*SomeType // id: %35
  %36 = partial_apply [callee_guaranteed] %31<FixedType, EmptyType, SomeType>(%7, %32, %34) : $@async @convention(thin) <τ_0_0, τ_0_1, τ_0_2> (@in_guaranteed τ_0_0, @in_guaranteed τ_0_1, @in_guaranteed τ_0_2) -> ()
  release_value %36: $@async @callee_guaranteed () ->()
  dealloc_stack %34 : $*SomeType
  dealloc_stack %32 : $*EmptyType
  dealloc_stack %7 : $*FixedType
  dealloc_stack %5 : $*EmptyType
  %40 = tuple()
  return %40 : $()
}

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc { ptr, ptr } @create_pa_f2(
sil @create_pa_f2 : $@convention(thin) (@callee_guaranteed @async @convention(thick) (Int64, Int32) -> Int64, Int32) -> @owned @async @callee_guaranteed (Int64) -> Int64 {
bb0(%thick : $@callee_guaranteed @async @convention(thick) (Int64, Int32) -> Int64 , %captured : $Int32):
  %pa_f = partial_apply [callee_guaranteed] %thick(%captured) : $@callee_guaranteed @async @convention(thick) (Int64, Int32) -> Int64
  %pa_f2 = partial_apply [callee_guaranteed] %thick(%captured) : $@callee_guaranteed @async @convention(thick) (Int64, Int32) -> Int64
  return %pa_f : $@async @callee_guaranteed (Int64) -> Int64
}

// CHECK-LABEL: define internal swift{{(tail)?}}cc void @"$sTA.{{[0-9]+}}"(

sil @external_closure : $@convention(thin) @async (Int, Int) -> (Int, @error Error)

sil @dont_crash : $@convention(thin) @async (Int) -> @owned @async @callee_guaranteed (Int) -> (Int, @error Error) {
bb0(%0 : $Int):
  %2 = function_ref @external_closure : $@convention(thin) @async (Int, Int) -> (Int, @error Error)
  %3 = partial_apply [callee_guaranteed] %2(%0) : $@convention(thin) @async (Int, Int) -> (Int, @error Error)
  return %3 : $@async @callee_guaranteed (Int) -> (Int, @error Error)
}

sil @method_with_out_return : $@convention(method) @async (SwiftClass) -> @out Int

sil @partially_apply_method_with_out_return : $@convention(thin) (SwiftClass) -> @async @callee_guaranteed () -> @out Int {
bb(%class : $SwiftClass):
  %method_with_out_param = function_ref @method_with_out_return : $@convention(method) @async (SwiftClass) -> @out Int
  %result = partial_apply [callee_guaranteed] %method_with_out_param(%class) : $@convention(method) @async (SwiftClass) -> @out Int
  return %result : $@callee_guaranteed @async () -> @out Int
}