File: swift-async-call-conv.c

package info (click to toggle)
llvm-toolchain-13 1%3A13.0.1-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,418,840 kB
  • sloc: cpp: 5,290,826; ansic: 996,570; asm: 544,593; python: 188,212; objc: 72,027; lisp: 30,291; f90: 25,395; sh: 24,898; javascript: 9,780; pascal: 9,398; perl: 7,484; ml: 5,432; awk: 3,523; makefile: 2,913; xml: 953; cs: 573; fortran: 539
file content (184 lines) | stat: -rw-r--r-- 6,783 bytes parent folder | download | duplicates (3)
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
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -target-cpu core2 -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -triple arm64-apple-ios9 -target-cpu cyclone -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -triple armv7-apple-darwin9 -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -triple armv7s-apple-ios9 -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -triple armv7k-apple-ios9 -emit-llvm -o - %s | FileCheck %s

// RUN: %clang_cc1 -x c++ -triple x86_64-apple-darwin10 -target-cpu core2 -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CPPONLY
// RUN: %clang_cc1 -x c++ -triple arm64-apple-ios9 -target-cpu cyclone -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CPPONLY
// RUN: %clang_cc1 -x c++ -triple armv7-apple-darwin9 -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CPPONLY
// RUN: %clang_cc1 -x c++ -triple armv7s-apple-ios9 -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CPPONLY
// RUN: %clang_cc1 -x c++ -triple armv7k-apple-ios9 -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CPPONLY

// Test tail call behavior when a swiftasynccall function is called
// from another swiftasynccall function.

#define SWIFTCALL __attribute__((swiftcall))
#define SWIFTASYNCCALL __attribute__((swiftasynccall))
#define ASYNC_CONTEXT __attribute__((swift_async_context))

// CHECK-LABEL: swifttailcc void {{.*}}async_leaf1{{.*}}(i8* swiftasync
SWIFTASYNCCALL void async_leaf1(char * ASYNC_CONTEXT ctx) {
  *ctx += 1;
}

// CHECK-LABEL: swifttailcc void {{.*}}async_leaf2{{.*}}(i8* swiftasync
SWIFTASYNCCALL void async_leaf2(char * ASYNC_CONTEXT ctx) {
  *ctx += 2;
}

#if __cplusplus
  #define MYBOOL bool
#else
  #define MYBOOL _Bool
#endif

// CHECK-LABEL: swifttailcc void {{.*}}async_branch{{.*}}i8* swiftasync
// CHECK: musttail call swifttailcc void @{{.*}}async_leaf1
// CHECK-NEXT: ret void
// CHECK: musttail call swifttailcc void @{{.*}}async_leaf2
// CHECK-NEXT: ret void
SWIFTASYNCCALL void async_branch(MYBOOL b, char * ASYNC_CONTEXT ctx) {
  if (b) {
    return async_leaf1(ctx);
  } else {
    return async_leaf2(ctx);
  }
}

// CHECK-LABEL: swifttailcc void {{.*}}async_not_all_tail
// CHECK-NOT:  musttail call swifttailcc void @{{.*}}async_leaf1
// CHECK:      call swifttailcc void @{{.*}}async_leaf1
// CHECK-NOT:  ret void
// CHECK:      musttail call swifttailcc void @{{.*}}async_leaf2
// CHECK-NEXT: ret void
SWIFTASYNCCALL void async_not_all_tail(char * ASYNC_CONTEXT ctx) {
  async_leaf1(ctx);
  return async_leaf2(ctx);
}

// CHECK-LABEL: swifttailcc void {{.*}}async_loop
// CHECK: musttail call swifttailcc void @{{.*}}async_leaf1
// CHECK-NEXT: ret void
// CHECK: musttail call swifttailcc void @{{.*}}async_leaf2
// CHECK-NEXT: ret void
// CHECK: musttail call swifttailcc void @{{.*}}async_loop
// CHECK-NEXT: ret void
SWIFTASYNCCALL void async_loop(unsigned u, char * ASYNC_CONTEXT ctx) {
  if (u == 0) {
    return async_leaf1(ctx);
  } else if (u == 1) {
    return async_leaf2(ctx);
  }
  return async_loop(u - 2, ctx);
}

// Forward-declaration + mutual recursion is okay.

SWIFTASYNCCALL void async_mutual_loop2(unsigned u, char * ASYNC_CONTEXT ctx);

// CHECK-LABEL: swifttailcc void {{.*}}async_mutual_loop1
// CHECK: musttail call swifttailcc void @{{.*}}async_leaf1
// CHECK-NEXT: ret void
// CHECK: musttail call swifttailcc void @{{.*}}async_leaf2
// CHECK-NEXT: ret void
// There is some bugginess around FileCheck's greediness/matching,
// so skipping the check for async_mutual_loop2 here.
SWIFTASYNCCALL void async_mutual_loop1(unsigned u, char * ASYNC_CONTEXT ctx) {
  if (u == 0) {
    return async_leaf1(ctx);
  } else if (u == 1) {
    return async_leaf2(ctx);
  }
  return async_mutual_loop2(u - 2, ctx);
}

// CHECK-LABEL: swifttailcc void {{.*}}async_mutual_loop2
// CHECK: musttail call swifttailcc void @{{.*}}async_leaf1
// CHECK-NEXT: ret void
// CHECK: musttail call swifttailcc void @{{.*}}async_leaf2
// CHECK-NEXT: ret void
// CHECK: musttail call swifttailcc void @{{.*}}async_mutual_loop1
// CHECK-NEXT: ret void
SWIFTASYNCCALL void async_mutual_loop2(unsigned u, char * ASYNC_CONTEXT ctx) {
  if (u == 0) {
    return async_leaf1(ctx);
  } else if (u == 1) {
    return async_leaf2(ctx);
  }
  return async_mutual_loop1(u - 2, ctx);
}

// When swiftasynccall functions are called by non-swiftasynccall functions,
// the call isn't marked as a tail call.

// CHECK-LABEL: swiftcc i8 {{.*}}sync_calling_async
// CHECK-NOT: tail call
// CHECK: call swifttailcc void @{{.*}}async_branch
// CHECK-NOT: tail call
// CHECK: call swifttailcc void @{{.*}}async_loop
SWIFTCALL char sync_calling_async(MYBOOL b, unsigned u) {
  char x = 'a';
  async_branch(b, &x);
  async_loop(u, &x);
  return x;
}

// CHECK-LABEL: i8 {{.*}}c_calling_async
// CHECK-NOT: tail call
// CHECK: call swifttailcc void @{{.*}}async_branch
// CHECK-NOT: tail call
// CHECK: call swifttailcc void @{{.*}}async_loop
char c_calling_async(MYBOOL b, unsigned u) {
  char x = 'a';
  async_branch(b, &x);
  async_loop(u, &x);
  return x;
}

#if __cplusplus
struct S {
  SWIFTASYNCCALL void (*fptr)(char * ASYNC_CONTEXT);

  SWIFTASYNCCALL void async_leaf_method(char * ASYNC_CONTEXT ctx) {
    *ctx += 1;
  }
  SWIFTASYNCCALL void async_nonleaf_method1(char * ASYNC_CONTEXT ctx) {
    return async_leaf_method(ctx);
  }
  SWIFTASYNCCALL void async_nonleaf_method2(char * ASYNC_CONTEXT ctx) {
    return this->async_leaf_method(ctx);
  }
};

SWIFTASYNCCALL void (S::*async_leaf_method_ptr)(char * ASYNC_CONTEXT) = &S::async_leaf_method;

// CPPONLY-LABEL: swifttailcc void {{.*}}async_struct_field_and_methods
// CPPONLY: musttail call swifttailcc void %{{[0-9]+}}
// CPPONLY: musttail call swifttailcc void @{{.*}}async_nonleaf_method1
// CPPONLY: musttail call swifttailcc void %{{[0-9]+}}
// CPPONLY: musttail call swifttailcc void @{{.*}}async_nonleaf_method2
// CPPONLY-NOT: musttail call swifttailcc void @{{.*}}async_leaf_method
// ^ TODO: Member pointers should also work.
SWIFTASYNCCALL void async_struct_field_and_methods(int i, S &sref, S *sptr) {
  char x = 'a';
  if (i == 0) {
    return (*sref.fptr)(&x);
  } else if (i == 1) {
    return sref.async_nonleaf_method1(&x);
  } else if (i == 2) {
    return (*(sptr->fptr))(&x);
  } else if (i == 3) {
    return sptr->async_nonleaf_method2(&x);
  } else if (i == 4) {
    return (sref.*async_leaf_method_ptr)(&x);
  }
  return (sptr->*async_leaf_method_ptr)(&x);
}

// CPPONLY-LABEL: define{{.*}} swifttailcc void @{{.*}}async_nonleaf_method1
// CPPONLY: musttail call swifttailcc void @{{.*}}async_leaf_method

// CPPONLY-LABEL: define{{.*}} swifttailcc void @{{.*}}async_nonleaf_method2
// CPPONLY: musttail call swifttailcc void @{{.*}}async_leaf_method
#endif