1 ; Second example from Doc/Coroutines.rst (custom alloc and free functions)
2 ; RUN: opt < %s -passes='default<O2>' -enable-coroutines -S | FileCheck %s
4 define i8* @f(i32 %n) {
6 %id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* 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 i8* @CustomAlloc(i32 %size)
14 %phi = phi i8* [ null, %entry ], [ %alloc, %dyn.alloc ]
15 %hdl = call noalias i8* @llvm.coro.begin(token %id, i8* %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 i8* @llvm.coro.free(token %id, i8* %hdl)
26 %need.dyn.free = icmp ne i8* %mem, null
27 br i1 %need.dyn.free, label %dyn.free, label %suspend
29 call void @CustomFree(i8* %mem)
32 call i1 @llvm.coro.end(i8* %hdl, i1 false)
39 %hdl = call i8* @f(i32 4)
40 call void @llvm.coro.resume(i8* %hdl)
41 call void @llvm.coro.resume(i8* %hdl)
42 %to = icmp eq i8* %hdl, null
43 br i1 %to, label %return, label %destroy
45 call void @llvm.coro.destroy(i8* %hdl)
49 ; CHECK-NOT: call i8* @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 i8* @CustomAlloc(i32)
57 declare void @CustomFree(i8*)
58 declare void @print(i32)
60 declare token @llvm.coro.id(i32, i8*, i8*, i8*)
61 declare i1 @llvm.coro.alloc(token)
62 declare i32 @llvm.coro.size.i32()
63 declare i8* @llvm.coro.begin(token, i8*)
64 declare i8 @llvm.coro.suspend(token, i1)
65 declare i8* @llvm.coro.free(token, i8*)
66 declare i1 @llvm.coro.end(i8*, i1)
68 declare void @llvm.coro.resume(i8*)
69 declare void @llvm.coro.destroy(i8*)