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
|
// RUN: %clang_cc1 %s -fblocks -triple x86_64-apple-darwin -emit-llvm -o - | FileCheck %s
namespace test0 {
// CHECK: define void @_ZN5test04testEi(
// CHECK: define internal void @___ZN5test04testEi_block_invoke{{.*}}(
// CHECK: define internal void @___ZN5test04testEi_block_invoke_2{{.*}}(
void test(int x) {
^{ ^{ (void) x; }; };
}
}
extern void (^out)();
namespace test1 {
// Capturing const objects doesn't require a local block.
// CHECK: define void @_ZN5test15test1Ev()
// CHECK: store void ()* bitcast ({{.*}} @__block_literal_global{{.*}} to void ()*), void ()** @out
void test1() {
const int NumHorsemen = 4;
out = ^{ (void) NumHorsemen; };
}
// That applies to structs too...
// CHECK: define void @_ZN5test15test2Ev()
// CHECK: store void ()* bitcast ({{.*}} @__block_literal_global{{.*}} to void ()*), void ()** @out
struct loc { double x, y; };
void test2() {
const loc target = { 5, 6 };
out = ^{ (void) target; };
}
// ...unless they have mutable fields...
// CHECK: define void @_ZN5test15test3Ev()
// CHECK: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]],
// CHECK: [[T0:%.*]] = bitcast [[BLOCK_T]]* [[BLOCK]] to void ()*
// CHECK: store void ()* [[T0]], void ()** @out
struct mut { mutable int x; };
void test3() {
const mut obj = { 5 };
out = ^{ (void) obj; };
}
// ...or non-trivial destructors...
// CHECK: define void @_ZN5test15test4Ev()
// CHECK: [[OBJ:%.*]] = alloca
// CHECK: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]],
// CHECK: [[T0:%.*]] = bitcast [[BLOCK_T]]* [[BLOCK]] to void ()*
// CHECK: store void ()* [[T0]], void ()** @out
struct scope { int x; ~scope(); };
void test4() {
const scope obj = { 5 };
out = ^{ (void) obj; };
}
// ...or non-trivial copy constructors, but it's not clear how to do
// that and still have a constant initializer in '03.
}
namespace test2 {
struct A {
A();
A(const A &);
~A();
};
struct B {
B();
B(const B &);
~B();
};
// CHECK: define void @_ZN5test24testEv()
void test() {
__block A a;
__block B b;
}
// CHECK: define internal void @__Block_byref_object_copy
// CHECK: call void @_ZN5test21AC1ERKS0_(
// CHECK: define internal void @__Block_byref_object_dispose
// CHECK: call void @_ZN5test21AD1Ev(
// CHECK: define internal void @__Block_byref_object_copy
// CHECK: call void @_ZN5test21BC1ERKS0_(
// CHECK: define internal void @__Block_byref_object_dispose
// CHECK: call void @_ZN5test21BD1Ev(
}
// rdar://problem/9334739
// Make sure we mark destructors for parameters captured in blocks.
namespace test3 {
struct A {
A(const A&);
~A();
};
struct B : A {
};
void test(B b) {
extern void consume(void(^)());
consume(^{ (void) b; });
}
}
// rdar://problem/9971485
namespace test4 {
struct A {
A();
~A();
};
void foo(A a);
void test() {
extern void consume(void(^)());
consume(^{ return foo(A()); });
}
// CHECK: define void @_ZN5test44testEv()
// CHECK: define internal void @___ZN5test44testEv_block_invoke
// CHECK: [[TMP:%.*]] = alloca [[A:%.*]], align 1
// CHECK-NEXT: store i8* [[BLOCKDESC:%.*]], i8** {{.*}}, align 8
// CHECK-NEXT: load i8*
// CHECK-NEXT: bitcast i8* [[BLOCKDESC]] to <{ i8*, i32, i32, i8*, %struct.__block_descriptor* }>*
// CHECK: call void @_ZN5test41AC1Ev([[A]]* [[TMP]])
// CHECK-NEXT: call void @_ZN5test43fooENS_1AE([[A]]* [[TMP]])
// CHECK-NEXT: call void @_ZN5test41AD1Ev([[A]]* [[TMP]])
// CHECK-NEXT: ret void
}
namespace test5 {
struct A {
unsigned afield;
A();
A(const A&);
~A();
void foo() const;
};
void doWithBlock(void(^)());
void test(bool cond) {
A x;
void (^b)() = (cond ? ^{ x.foo(); } : (void(^)()) 0);
doWithBlock(b);
}
// CHECK: define void @_ZN5test54testEb(
// CHECK: [[COND:%.*]] = alloca i8
// CHECK-NEXT: [[X:%.*]] = alloca [[A:%.*]], align 4
// CHECK-NEXT: [[B:%.*]] = alloca void ()*, align 8
// CHECK-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:.*]], align 8
// CHECK-NEXT: [[CLEANUP_ACTIVE:%.*]] = alloca i1
// CHECK-NEXT: [[T0:%.*]] = zext i1
// CHECK-NEXT: store i8 [[T0]], i8* [[COND]], align 1
// CHECK-NEXT: call void @_ZN5test51AC1Ev([[A]]* [[X]])
// CHECK-NEXT: [[CLEANUP_ADDR:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
// CHECK-NEXT: [[T0:%.*]] = load i8* [[COND]], align 1
// CHECK-NEXT: [[T1:%.*]] = trunc i8 [[T0]] to i1
// CHECK-NEXT: store i1 false, i1* [[CLEANUP_ACTIVE]]
// CHECK-NEXT: br i1 [[T1]],
// CHECK-NOT: br
// CHECK: [[CAPTURE:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
// CHECK-NEXT: call void @_ZN5test51AC1ERKS0_([[A]]* [[CAPTURE]], [[A]]* [[X]])
// CHECK-NEXT: store i1 true, i1* [[CLEANUP_ACTIVE]]
// CHECK-NEXT: bitcast [[BLOCK_T]]* [[BLOCK]] to void ()*
// CHECK-NEXT: br label
// CHECK: br label
// CHECK: phi
// CHECK-NEXT: store
// CHECK-NEXT: load
// CHECK-NEXT: call void @_ZN5test511doWithBlockEU13block_pointerFvvE(
// CHECK-NEXT: [[T0:%.*]] = load i1* [[CLEANUP_ACTIVE]]
// CHECK-NEXT: br i1 [[T0]]
// CHECK: call void @_ZN5test51AD1Ev([[A]]* [[CLEANUP_ADDR]])
// CHECK-NEXT: br label
// CHECK: call void @_ZN5test51AD1Ev([[A]]* [[X]])
// CHECK-NEXT: ret void
}
namespace test6 {
struct A {
A();
~A();
};
void foo(const A &, void (^)());
void bar();
void test() {
// Make sure that the temporary cleanup isn't somehow captured
// within the block.
foo(A(), ^{ bar(); });
bar();
}
// CHECK: define void @_ZN5test64testEv()
// CHECK: [[TEMP:%.*]] = alloca [[A:%.*]], align 1
// CHECK-NEXT: call void @_ZN5test61AC1Ev([[A]]* [[TEMP]])
// CHECK-NEXT: call void @_ZN5test63fooERKNS_1AEU13block_pointerFvvE(
// CHECK-NEXT: call void @_ZN5test61AD1Ev([[A]]* [[TEMP]])
// CHECK-NEXT: call void @_ZN5test63barEv()
// CHECK-NEXT: ret void
}
namespace test7 {
int f() {
static int n;
int *const p = &n;
return ^{ return *p; }();
}
}
namespace test8 {
// <rdar://problem/10832617>: failure to capture this after skipping rebuild
// of the 'this' pointer.
struct X {
int x;
template<typename T>
int foo() {
return ^ { return x; }();
}
};
template int X::foo<int>();
}
// rdar://13459289
namespace test9 {
struct B {
void *p;
B();
B(const B&);
~B();
};
void use_block(void (^)());
void use_block_2(void (^)(), const B &a);
// Ensuring that creating a non-trivial capture copy expression
// doesn't end up stealing the block registration for the block we
// just parsed. That block must have captures or else it won't
// force registration. Must occur within a block for some reason.
void test() {
B x;
use_block(^{
int y;
use_block_2(^{ (void)y; }, x);
});
}
}
|