Skip to content

Instantly share code, notes, and snippets.

@bollu bollu/coro-cps.ll forked from GorNishanov/coro-cps.ll
Created Jun 15, 2019

Embed
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) {
entry:
%bar.ret.addr = alloca i32
%id = call token @llvm.coro.id(i32 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 @llvm.coro.save(i8* %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]
resume:
%n = load i32, i32* %bar.ret.addr
call void @partB(i32 %n)
br label %cleanup
cleanup:
%mem = call i8* @llvm.coro.free(token %id, i8* %hdl)
call void @free(i8* %mem)
br label %suspend
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() {
entry:
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 @llvm.coro.id(i32, 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* @llvm.coro.free(token, i8*)
declare i1 @llvm.coro.end(i8*, i1)
declare void @llvm.coro.resume(i8*)
declare void @llvm.coro.destroy(i8*)
declare token @llvm.coro.save(i8*)
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.