1 ; Second example from Doc/Coroutines.rst (custom alloc and free functions)
2 ; RUN: opt < %s -passes='default<O2>' -S | FileCheck %s
4 define ptr @f(i32 %n) presplitcoroutine {
6 %id = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr null)
7 %need.dyn.alloc = call i1 @llvm.coro.alloc(token %id)
8 br i1 %need.dyn.alloc, label %dyn.alloc, label %coro.begin
10 %size = call i32 @llvm.coro.size.i32()
11 %alloc = call ptr @CustomAlloc(i32 %size)
14 %phi = phi ptr [ null, %entry ], [ %alloc, %dyn.alloc ]
15 %hdl = call noalias ptr @llvm.coro.begin(token %id, ptr %phi)
18 %n.val = phi i32 [ %n, %coro.begin ], [ %inc, %loop ]
19 %inc = add nsw i32 %n.val, 1
20 call void @print(i32 %n.val)
21 %0 = call i8 @llvm.coro.suspend(token none, i1 false)
22 switch i8 %0, label %suspend [i8 0, label %loop
25 %mem = call ptr @llvm.coro.free(token %id, ptr %hdl)
26 %need.dyn.free = icmp ne ptr %mem, null
27 br i1 %need.dyn.free, label %dyn.free, label %suspend
29 call void @CustomFree(ptr %mem)
32 call i1 @llvm.coro.end(ptr %hdl, i1 false, token none)
39 %hdl = call ptr @f(i32 4)
40 call void @llvm.coro.resume(ptr %hdl)
41 call void @llvm.coro.resume(ptr %hdl)
42 %to = icmp eq ptr %hdl, null
43 br i1 %to, label %return, label %destroy
45 call void @llvm.coro.destroy(ptr %hdl)
49 ; CHECK-NOT: call ptr @CustomAlloc
50 ; CHECK: call void @print(i32 4)
51 ; CHECK-NEXT: call void @print(i32 5)
52 ; CHECK-NEXT: call void @print(i32 6)
53 ; CHECK-NEXT: ret i32 0
56 declare ptr @CustomAlloc(i32)
57 declare void @CustomFree(ptr)
58 declare void @print(i32)
60 declare token @llvm.coro.id(i32, ptr, ptr, ptr)
61 declare i1 @llvm.coro.alloc(token)
62 declare i32 @llvm.coro.size.i32()
63 declare ptr @llvm.coro.begin(token, ptr)
64 declare i8 @llvm.coro.suspend(token, i1)
65 declare ptr @llvm.coro.free(token, ptr)
66 declare i1 @llvm.coro.end(ptr, i1, token)
68 declare void @llvm.coro.resume(ptr)
69 declare void @llvm.coro.destroy(ptr)