1// RUN: mlir-opt %s -split-input-file -async-to-async-runtime                  \
2// RUN:   | FileCheck %s --dump-input=always
3
4// CHECK-LABEL: @execute_no_async_args
5func.func @execute_no_async_args(%arg0: f32, %arg1: memref<1xf32>) {
6  %token = async.execute {
7    %c0 = arith.constant 0 : index
8    memref.store %arg0, %arg1[%c0] : memref<1xf32>
9    async.yield
10  }
11  async.await %token : !async.token
12  return
13}
14
15// Function outlined from the async.execute operation.
16// CHECK-LABEL: func private @async_execute_fn
17// CHECK-SAME: -> !async.token
18
19// Create token for return op, and mark a function as a coroutine.
20// CHECK: %[[TOKEN:.*]] = async.runtime.create : !async.token
21// CHECK: %[[ID:.*]] = async.coro.id
22// CHECK: %[[HDL:.*]] = async.coro.begin
23
24// Pass a suspended coroutine to the async runtime.
25// CHECK: %[[SAVED:.*]] = async.coro.save %[[HDL]]
26// CHECK: async.runtime.resume %[[HDL]]
27// CHECK: async.coro.suspend %[[SAVED]]
28// CHECK-SAME: ^[[SUSPEND:.*]], ^[[RESUME:.*]], ^[[CLEANUP:.*]]
29
30// Resume coroutine after suspension.
31// CHECK: ^[[RESUME]]:
32// CHECK:   memref.store
33// CHECK:   async.runtime.set_available %[[TOKEN]]
34
35// Delete coroutine.
36// CHECK: ^[[CLEANUP]]:
37// CHECK:   async.coro.free %[[ID]], %[[HDL]]
38
39// Suspend coroutine, and also a return statement for ramp function.
40// CHECK: ^[[SUSPEND]]:
41// CHECK:   async.coro.end %[[HDL]]
42// CHECK:   return %[[TOKEN]]
43
44// -----
45
46// CHECK-LABEL: @nested_async_execute
47func.func @nested_async_execute(%arg0: f32, %arg1: f32, %arg2: memref<1xf32>) {
48  // CHECK: %[[TOKEN:.*]] = call @async_execute_fn_0(%arg0, %arg2, %arg1)
49  %token0 = async.execute {
50    %c0 = arith.constant 0 : index
51
52    %token1 = async.execute {
53      %c1 = arith.constant 1: index
54      memref.store %arg0, %arg2[%c0] : memref<1xf32>
55      async.yield
56    }
57    async.await %token1 : !async.token
58
59    memref.store %arg1, %arg2[%c0] : memref<1xf32>
60    async.yield
61  }
62  // CHECK: async.runtime.await %[[TOKEN]]
63  // CHECK: %[[IS_ERROR:.*]] = async.runtime.is_error %[[TOKEN]]
64  // CHECK: %[[TRUE:.*]] = arith.constant true
65  // CHECK: %[[NOT_ERROR:.*]] = arith.xori %[[IS_ERROR]], %[[TRUE]] : i1
66  // CHECK: cf.assert %[[NOT_ERROR]]
67  // CHECK-NEXT: return
68  async.await %token0 : !async.token
69  return
70}
71
72// Function outlined from the inner async.execute operation.
73// CHECK-LABEL: func private @async_execute_fn
74// CHECK-SAME: -> !async.token
75
76// CHECK: %[[TOKEN:.*]] = async.runtime.create : !async.token
77// CHECK: %[[ID:.*]] = async.coro.id
78// CHECK: %[[HDL:.*]] = async.coro.begin
79
80// CHECK: async.runtime.resume %[[HDL]]
81// CHECK: async.coro.suspend
82// CHECK-SAME: ^[[SUSPEND:.*]], ^[[RESUME:.*]], ^[[CLEANUP:.*]]
83
84// CHECK: ^[[RESUME]]:
85// CHECK:   memref.store
86// CHECK:   async.runtime.set_available %[[TOKEN]]
87
88// Function outlined from the outer async.execute operation.
89// CHECK-LABEL: func private @async_execute_fn_0
90// CHECK-SAME: -> !async.token
91
92// CHECK: %[[TOKEN:.*]] = async.runtime.create : !async.token
93// CHECK: %[[ID:.*]] = async.coro.id
94// CHECK: %[[HDL:.*]] = async.coro.begin
95
96// Suspend coroutine in the beginning.
97// CHECK: async.runtime.resume %[[HDL]]
98// CHECK: async.coro.suspend
99// CHECK-SAME: ^[[SUSPEND:.*]], ^[[RESUME_0:.*]], ^[[CLEANUP:.*]]
100
101// Suspend coroutine second time waiting for the completion of inner execute op.
102// CHECK: ^[[RESUME_0]]:
103// CHECK:   %[[INNER_TOKEN:.*]] = call @async_execute_fn
104// CHECK:   %[[SAVED:.*]] = async.coro.save %[[HDL]]
105// CHECK:   async.runtime.await_and_resume %[[INNER_TOKEN]], %[[HDL]]
106// CHECK:   async.coro.suspend %[[SAVED]]
107// CHECK-SAME: ^[[SUSPEND]], ^[[RESUME_1:.*]], ^[[CLEANUP]]
108
109// Check the error of the awaited token after resumption.
110// CHECK: ^[[RESUME_1]]:
111// CHECK:   %[[ERR:.*]] = async.runtime.is_error %[[INNER_TOKEN]]
112// CHECK:   cf.cond_br %[[ERR]], ^[[SET_ERROR:.*]], ^[[CONTINUATION:.*]]
113
114// Set token available if the token is not in the error state.
115// CHECK: ^[[CONTINUATION:.*]]:
116// CHECK:   memref.store
117// CHECK:   async.runtime.set_available %[[TOKEN]]
118
119// CHECK: ^[[SET_ERROR]]:
120// CHECK: ^[[CLEANUP]]:
121// CHECK: ^[[SUSPEND]]:
122
123// -----
124
125// CHECK-LABEL: @async_execute_token_dependency
126func.func @async_execute_token_dependency(%arg0: f32, %arg1: memref<1xf32>) {
127  // CHECK: %[[TOKEN:.*]] = call @async_execute_fn
128  %token = async.execute {
129    %c0 = arith.constant 0 : index
130    memref.store %arg0, %arg1[%c0] : memref<1xf32>
131    async.yield
132  }
133  // CHECK: call @async_execute_fn_0(%[[TOKEN]], %arg0, %arg1)
134  %token_0 = async.execute [%token] {
135    %c0 = arith.constant 0 : index
136    memref.store %arg0, %arg1[%c0] : memref<1xf32>
137    async.yield
138  }
139  return
140}
141
142// Function outlined from the first async.execute operation.
143// CHECK-LABEL: func private @async_execute_fn
144// CHECK-SAME: -> !async.token
145// CHECK: %[[TOKEN:.*]] = async.runtime.create : !async.token
146// CHECK: return %[[TOKEN]] : !async.token
147
148// Function outlined from the second async.execute operation with dependency.
149// CHECK-LABEL: func private @async_execute_fn_0
150// CHECK-SAME:    %[[ARG0:.*]]: !async.token
151// CHECK-SAME:    %[[ARG1:.*]]: f32
152// CHECK-SAME:    %[[ARG2:.*]]: memref<1xf32>
153// CHECK-SAME: -> !async.token
154// CHECK: %[[TOKEN:.*]] = async.runtime.create : !async.token
155// CHECK: %[[HDL:.*]] = async.coro.begin
156
157// Suspend coroutine in the beginning.
158// CHECK: async.runtime.resume %[[HDL]]
159// CHECK: async.coro.suspend
160// CHECK-SAME: ^[[SUSPEND:.*]], ^[[RESUME_0:.*]], ^[[CLEANUP:.*]]
161
162// Suspend coroutine second time waiting for the completion of token dependency.
163// CHECK: ^[[RESUME_0]]:
164// CHECK:   %[[SAVED:.*]] = async.coro.save %[[HDL]]
165// CHECK:   async.runtime.await_and_resume %[[ARG0]], %[[HDL]]
166// CHECK:   async.coro.suspend %[[SAVED]]
167// CHECK-SAME: ^[[SUSPEND]], ^[[RESUME_1:.*]], ^[[CLEANUP]]
168
169// Check the error of the awaited token after resumption.
170// CHECK: ^[[RESUME_1]]:
171// CHECK:   %[[ERR:.*]] = async.runtime.is_error %[[ARG0]]
172// CHECK:   cf.cond_br %[[ERR]], ^[[SET_ERROR:.*]], ^[[CONTINUATION:.*]]
173
174// Emplace result token after second resumption and error checking.
175// CHECK: ^[[CONTINUATION:.*]]:
176// CHECK:   memref.store
177// CHECK:   async.runtime.set_available %[[TOKEN]]
178
179// CHECK: ^[[CLEANUP]]:
180// CHECK: ^[[SUSPEND]]:
181
182// -----
183
184// CHECK-LABEL: @async_group_await_all
185func.func @async_group_await_all(%arg0: f32, %arg1: memref<1xf32>) {
186  // CHECK: %[[C:.*]] = arith.constant 1 : index
187  %c = arith.constant 1 : index
188  // CHECK: %[[GROUP:.*]] = async.runtime.create_group %[[C]] : !async.group
189  %0 = async.create_group %c : !async.group
190
191  // CHECK: %[[TOKEN:.*]] = call @async_execute_fn
192  %token = async.execute { async.yield }
193  // CHECK: async.runtime.add_to_group %[[TOKEN]], %[[GROUP]]
194  async.add_to_group %token, %0 : !async.token
195
196  // CHECK: call @async_execute_fn_0
197  async.execute {
198    async.await_all %0
199    async.yield
200  }
201
202  // CHECK: async.runtime.await %[[GROUP]] : !async.group
203  async.await_all %0
204  return
205}
206
207// Function outlined from the second async.execute operation.
208// CHECK-LABEL: func private @async_execute_fn_0
209// CHECK-SAME: (%[[ARG:.*]]: !async.group) -> !async.token
210
211// CHECK: %[[TOKEN:.*]] = async.runtime.create : !async.token
212// CHECK: %[[HDL:.*]] = async.coro.begin
213
214// Suspend coroutine in the beginning.
215// CHECK: async.runtime.resume %[[HDL]]
216// CHECK: async.coro.suspend
217// CHECK-SAME: ^[[SUSPEND:.*]], ^[[RESUME_0:.*]], ^[[CLEANUP:.*]]
218
219// Suspend coroutine second time waiting for the group.
220// CHECK: ^[[RESUME_0]]:
221// CHECK:   async.runtime.await_and_resume %[[ARG]], %[[HDL]]
222// CHECK:   async.coro.suspend
223// CHECK-SAME: ^[[SUSPEND]], ^[[RESUME_1:.*]], ^[[CLEANUP]]
224
225// Check the error of the awaited token after resumption.
226// CHECK: ^[[RESUME_1]]:
227// CHECK:   %[[ERR:.*]] = async.runtime.is_error %[[ARG]]
228// CHECK:   cf.cond_br %[[ERR]], ^[[SET_ERROR:.*]], ^[[CONTINUATION:.*]]
229
230// Emplace result token after error checking.
231// CHECK: ^[[CONTINUATION:.*]]:
232// CHECK:   async.runtime.set_available %[[TOKEN]]
233
234// CHECK: ^[[CLEANUP]]:
235// CHECK: ^[[SUSPEND]]:
236
237// -----
238
239// CHECK-LABEL: @execute_and_return_f32
240func.func @execute_and_return_f32() -> f32 {
241 // CHECK: %[[RET:.*]]:2 = call @async_execute_fn
242  %token, %result = async.execute -> !async.value<f32> {
243    %c0 = arith.constant 123.0 : f32
244    async.yield %c0 : f32
245  }
246
247  // CHECK: async.runtime.await %[[RET]]#1 : !async.value<f32>
248  // CHECK: %[[VALUE:.*]] = async.runtime.load %[[RET]]#1 : <f32>
249  %0 = async.await %result : !async.value<f32>
250
251  // CHECK: return %[[VALUE]]
252  return %0 : f32
253}
254
255// Function outlined from the async.execute operation.
256// CHECK-LABEL: func private @async_execute_fn()
257// CHECK: %[[TOKEN:.*]] = async.runtime.create : !async.token
258// CHECK: %[[VALUE:.*]] = async.runtime.create : !async.value<f32>
259// CHECK: %[[HDL:.*]] = async.coro.begin
260
261// Suspend coroutine in the beginning.
262// CHECK: async.runtime.resume %[[HDL]]
263// CHECK: async.coro.suspend
264// CHECK-SAME: ^[[SUSPEND:.*]], ^[[RESUME:.*]], ^[[CLEANUP:.*]]
265
266// Emplace result value.
267// CHECK: ^[[RESUME]]:
268// CHECK:   %[[CST:.*]] = arith.constant 1.230000e+02 : f32
269// CHECK:   async.runtime.store %cst, %[[VALUE]]
270// CHECK:   async.runtime.set_available %[[VALUE]]
271// CHECK:   async.runtime.set_available %[[TOKEN]]
272
273// CHECK: ^[[CLEANUP]]:
274// CHECK: ^[[SUSPEND]]:
275
276// -----
277
278// CHECK-LABEL: @async_value_operands
279func.func @async_value_operands() {
280  // CHECK: %[[RET:.*]]:2 = call @async_execute_fn
281  %token, %result = async.execute -> !async.value<f32> {
282    %c0 = arith.constant 123.0 : f32
283    async.yield %c0 : f32
284  }
285
286  // CHECK: %[[TOKEN:.*]] = call @async_execute_fn_0(%[[RET]]#1)
287  %token0 = async.execute(%result as %value: !async.value<f32>) {
288    %0 = arith.addf %value, %value : f32
289    async.yield
290  }
291
292  // CHECK: async.runtime.await %[[TOKEN]] : !async.token
293  async.await %token0 : !async.token
294
295  return
296}
297
298// Function outlined from the first async.execute operation.
299// CHECK-LABEL: func private @async_execute_fn()
300
301// Function outlined from the second async.execute operation.
302// CHECK-LABEL: func private @async_execute_fn_0
303// CHECK-SAME: (%[[ARG:.*]]: !async.value<f32>) -> !async.token
304
305// CHECK: %[[TOKEN:.*]] = async.runtime.create : !async.token
306// CHECK: %[[HDL:.*]] = async.coro.begin
307
308// Suspend coroutine in the beginning.
309// CHECK: async.runtime.resume %[[HDL]]
310// CHECK: async.coro.suspend
311// CHECK-SAME: ^[[SUSPEND:.*]], ^[[RESUME_0:.*]], ^[[CLEANUP:.*]]
312
313// Suspend coroutine second time waiting for the async operand.
314// CHECK: ^[[RESUME_0]]:
315// CHECK:   async.runtime.await_and_resume %[[ARG]], %[[HDL]]
316// CHECK:   async.coro.suspend
317// CHECK-SAME: ^[[SUSPEND]], ^[[RESUME_1:.*]], ^[[CLEANUP]]
318
319// Check the error of the awaited token after resumption.
320// CHECK: ^[[RESUME_1]]:
321// CHECK:   %[[ERR:.*]] = async.runtime.is_error %[[ARG]]
322// CHECK:   cf.cond_br %[[ERR]], ^[[SET_ERROR:.*]], ^[[CONTINUATION:.*]]
323
324// // Load from the async.value argument after error checking.
325// CHECK: ^[[CONTINUATION:.*]]:
326// CHECK:   %[[LOADED:.*]] = async.runtime.load %[[ARG]] : <f32
327// CHECK:   arith.addf %[[LOADED]], %[[LOADED]] : f32
328// CHECK:   async.runtime.set_available %[[TOKEN]]
329
330// CHECK: ^[[CLEANUP]]:
331// CHECK: ^[[SUSPEND]]:
332
333// -----
334
335// CHECK-LABEL: @execute_assertion
336func.func @execute_assertion(%arg0: i1) {
337  %token = async.execute {
338    cf.assert %arg0, "error"
339    async.yield
340  }
341  async.await %token : !async.token
342  return
343}
344
345// Function outlined from the async.execute operation.
346// CHECK-LABEL: func private @async_execute_fn(
347// CHECK-SAME:  %[[ARG0:.*]]: i1
348// CHECK-SAME:  -> !async.token
349
350// Create token for return op, and mark a function as a coroutine.
351// CHECK: %[[TOKEN:.*]] = async.runtime.create : !async.token
352// CHECK: %[[ID:.*]] = async.coro.id
353// CHECK: %[[HDL:.*]] = async.coro.begin
354
355// Initial coroutine suspension.
356// CHECK:      async.coro.suspend
357// CHECK-SAME: ^[[SUSPEND:.*]], ^[[RESUME:.*]], ^[[CLEANUP:.*]]
358
359// Resume coroutine after suspension.
360// CHECK: ^[[RESUME]]:
361// CHECK:   cf.cond_br %[[ARG0]], ^[[SET_AVAILABLE:.*]], ^[[SET_ERROR:.*]]
362
363// Set coroutine completion token to available state.
364// CHECK: ^[[SET_AVAILABLE]]:
365// CHECK:   async.runtime.set_available %[[TOKEN]]
366// CHECK:   cf.br ^[[CLEANUP]]
367
368// Set coroutine completion token to error state.
369// CHECK: ^[[SET_ERROR]]:
370// CHECK:   async.runtime.set_error %[[TOKEN]]
371// CHECK:   cf.br ^[[CLEANUP]]
372
373// Delete coroutine.
374// CHECK: ^[[CLEANUP]]:
375// CHECK:   async.coro.free %[[ID]], %[[HDL]]
376
377// Suspend coroutine, and also a return statement for ramp function.
378// CHECK: ^[[SUSPEND]]:
379// CHECK:   async.coro.end %[[HDL]]
380// CHECK:   return %[[TOKEN]]
381
382// -----
383// Structured control flow operations with async operations in the body must be
384// lowered to branch-based control flow to enable coroutine CFG rewrite.
385
386// CHECK-LABEL: @lower_scf_to_cfg
387func.func @lower_scf_to_cfg(%arg0: f32, %arg1: memref<1xf32>, %arg2: i1) {
388  %token0 = async.execute { async.yield }
389  %token1 = async.execute {
390    scf.if %arg2 {
391      async.await %token0 : !async.token
392    } else {
393      async.await %token0 : !async.token
394    }
395    async.yield
396  }
397  return
398}
399
400// Function outlined from the first async.execute operation.
401// CHECK-LABEL: func private @async_execute_fn(
402// CHECK-SAME: -> !async.token
403
404// Function outlined from the second async.execute operation.
405// CHECK-LABEL: func private @async_execute_fn_0(
406// CHECK:         %[[TOKEN:.*]]: !async.token
407// CHECK:         %[[FLAG:.*]]: i1
408// CHECK-SAME: -> !async.token
409
410// Check that structured control flow lowered to CFG.
411// CHECK-NOT: scf.if
412// CHECK: cf.cond_br %[[FLAG]]
413
414// -----
415// Constants captured by the async.execute region should be cloned into the
416// outline async execute function.
417
418// CHECK-LABEL: @clone_constants
419func.func @clone_constants(%arg0: f32, %arg1: memref<1xf32>) {
420  %c0 = arith.constant 0 : index
421  %token = async.execute {
422    memref.store %arg0, %arg1[%c0] : memref<1xf32>
423    async.yield
424  }
425  async.await %token : !async.token
426  return
427}
428
429// Function outlined from the async.execute operation.
430// CHECK-LABEL: func private @async_execute_fn(
431// CHECK-SAME:    %[[VALUE:arg[0-9]+]]: f32,
432// CHECK-SAME:    %[[MEMREF:arg[0-9]+]]: memref<1xf32>
433// CHECK-SAME:  ) -> !async.token
434// CHECK:         %[[CST:.*]] = arith.constant 0 : index
435// CHECK:         memref.store %[[VALUE]], %[[MEMREF]][%[[CST]]]
436