1 ; Second example from Doc/Coroutines.rst (custom alloc and free functions)
2 ; RUN: opt < %s -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 call void @llvm.coro.destroy(i8* %hdl)
44 ; CHECK: call void @print(i32 4)
45 ; CHECK-NEXT: call void @print(i32 5)
46 ; CHECK-NEXT: call void @print(i32 6)
47 ; CHECK-NEXT: ret i32 0
50 declare i8* @CustomAlloc(i32)
51 declare void @CustomFree(i8*)
52 declare void @print(i32)
54 declare token @llvm.coro.id(i32, i8*, i8*, i8*)
55 declare i1 @llvm.coro.alloc(token)
56 declare i32 @llvm.coro.size.i32()
57 declare i8* @llvm.coro.begin(token, i8*)
58 declare i8 @llvm.coro.suspend(token, i1)
59 declare i8* @llvm.coro.free(token, i8*)
60 declare i1 @llvm.coro.end(i8*, i1)
62 declare void @llvm.coro.resume(i8*)
63 declare void @llvm.coro.destroy(i8*)