Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
; Emulating cps call using LLVM Coroutines
; RUN: opt coro-cps.ll -O2 -enable-coroutines -S
define void @f(i32 %arg) {
%bar.ret.addr = alloca i32
%id = call token 0, i8* null, i8* null, i8* null)
%size = call i32 @llvm.coro.size.i32()
%alloc = call i8* @malloc(i32 %size)
%hdl = call noalias i8* @llvm.coro.begin(token %id, i8* %alloc)
call void @partA(i32 %arg)
%tok = call token* %hdl)
call void @cps_call_bar(i8* %hdl, i32* %bar.ret.addr, i32 41)
%0 = call i8 @llvm.coro.suspend(token %tok, i1 false)
switch i8 %0, label %suspend [i8 0, label %resume
i8 1, label %cleanup]
%n = load i32, i32* %bar.ret.addr
call void @partB(i32 %n)
br label %cleanup
%mem = call i8* %id, i8* %hdl)
call void @free(i8* %mem)
br label %suspend
call i1 @llvm.coro.end(i8* %hdl, i1 false)
ret void
define void @cps_call_bar(i8* %caller, i32* %retval.addr, i32 %n) {
call void @partC(i32 %n)
store i32 42, i32* %retval.addr
call void @llvm.coro.resume(i8* %caller)
ret void
; CHECK-LABEL: @main(
define i32 @main() {
call void @f(i32 40)
ret i32 0
declare i8* @malloc(i32)
declare void @free(i8*)
declare void @partA(i32)
declare void @partB(i32)
declare void @partC(i32)
declare token, i8*, i8*, i8*)
declare i32 @llvm.coro.size.i32()
declare i1 @llvm.coro.alloc(token)
declare i8* @llvm.coro.begin(token, i8*)
declare i8 @llvm.coro.suspend(token, i1)
declare i8*, i8*)
declare i1 @llvm.coro.end(i8*, i1)
declare void @llvm.coro.resume(i8*)
declare void @llvm.coro.destroy(i8*)
declare token*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.