1; Fourth example from Doc/Coroutines.rst (coroutine promise) 2; RUN: opt < %s -O2 -enable-coroutines -S | FileCheck %s 3; RUN: opt < %s -passes='default<O2>' -enable-coroutines -S | FileCheck %s 4 5define i8* @f(i32 %n) { 6entry: 7 %promise = alloca i32 8 %pv = bitcast i32* %promise to i8* 9 %id = call token @llvm.coro.id(i32 0, i8* %pv, i8* null, i8* null) 10 %need.dyn.alloc = call i1 @llvm.coro.alloc(token %id) 11 br i1 %need.dyn.alloc, label %dyn.alloc, label %coro.begin 12dyn.alloc: 13 %size = call i32 @llvm.coro.size.i32() 14 %alloc = call i8* @malloc(i32 %size) 15 br label %coro.begin 16coro.begin: 17 %phi = phi i8* [ null, %entry ], [ %alloc, %dyn.alloc ] 18 %hdl = call noalias i8* @llvm.coro.begin(token %id, i8* %phi) 19 br label %loop 20loop: 21 %n.val = phi i32 [ %n, %coro.begin ], [ %inc, %loop ] 22 %inc = add nsw i32 %n.val, 1 23 store i32 %n.val, i32* %promise 24 %0 = call i8 @llvm.coro.suspend(token none, i1 false) 25 switch i8 %0, label %suspend [i8 0, label %loop 26 i8 1, label %cleanup] 27cleanup: 28 %mem = call i8* @llvm.coro.free(token %id, i8* %hdl) 29 call void @free(i8* %mem) 30 br label %suspend 31suspend: 32 call i1 @llvm.coro.end(i8* %hdl, i1 false) 33 ret i8* %hdl 34} 35 36; CHECK-LABEL: @main 37define i32 @main() { 38entry: 39 %hdl = call i8* @f(i32 4) 40 %promise.addr.raw = call i8* @llvm.coro.promise(i8* %hdl, i32 4, i1 false) 41 %promise.addr = bitcast i8* %promise.addr.raw to i32* 42 %val0 = load i32, i32* %promise.addr 43 call void @print(i32 %val0) 44 call void @llvm.coro.resume(i8* %hdl) 45 %val1 = load i32, i32* %promise.addr 46 call void @print(i32 %val1) 47 call void @llvm.coro.resume(i8* %hdl) 48 %val2 = load i32, i32* %promise.addr 49 call void @print(i32 %val2) 50 call void @llvm.coro.destroy(i8* %hdl) 51 ret i32 0 52; CHECK: call void @print(i32 4) 53; CHECK-NEXT: call void @print(i32 5) 54; CHECK-NEXT: call void @print(i32 6) 55; CHECK: ret i32 0 56} 57 58declare i8* @llvm.coro.promise(i8*, i32, i1) 59declare i8* @malloc(i32) 60declare void @free(i8*) 61declare void @print(i32) 62 63declare token @llvm.coro.id(i32, i8*, i8*, i8*) 64declare i1 @llvm.coro.alloc(token) 65declare i32 @llvm.coro.size.i32() 66declare i8* @llvm.coro.begin(token, i8*) 67declare i8 @llvm.coro.suspend(token, i1) 68declare i8* @llvm.coro.free(token, i8*) 69declare i1 @llvm.coro.end(i8*, i1) 70 71declare void @llvm.coro.resume(i8*) 72declare void @llvm.coro.destroy(i8*) 73