Need help about LLVM build coroutine
Dakota
dakota at gmail.com
Sat Mar 2 12:56:27 UTC 2024
On Saturday, 2 March 2024 at 12:32:32 UTC, Richard (Rikki) Andrew
Cattermole wrote:
> From what I'm reading of the documentation, it looks like you
> have to work at the IR level to declare a coroutine.
>
> This isn't something you can do with only intrinsics in library
> code.
>
> https://llvm.org/docs/Coroutines.html#introduction
>
> Take note of the function attribute ``presplitcoroutine`` under
> coroutine representation.
>
> https://llvm.org/docs/Coroutines.html#coroutine-representation
Thanks for the tips. I dont understand LLVM IR. I want to create
a LLIR function which accept 2 parameters, first one is a
pointer of coroutine function entry, the other is a pointer to a
struct look like this:
```d
struct coroutine_object {
void* coro_handler;
void* core_argv;
size_t core_argc;
}
```
this llvm IR should create one new coroutine, and save the
handler into arg2.coro_handler, then start enter the coroutine to
execute.
I use chatGPT get this code. any one can help me fix it and
export to `extern(C)`
```ir
define void @start_coroutine(i8* %func_ptr, i8**
%coro_handle_ptr) {
entry:
%func = bitcast i8* %func_ptr to void (i8*)*
%coro_begin = call i8* @llvm.coro.begin(token none, i8* null,
i8* null)
store i8* %coro_begin, i8** %coro_handle_ptr
call void @llvm.coro.alloc(i8* %coro_begin, i8* bitcast (void
(i8*)* @coro_func to i8*))
call void @llvm.coro.init(token none, i8* %coro_begin, i8*
%coro_begin)
call void @llvm.coro.save(i8* %coro_begin, i8** null)
call void @llvm.coro.resume(i8* %coro_begin)
ret void
}
declare token @llvm.coro.begin(token, i8*, i8*)
declare void @llvm.coro.alloc(i8*, i8*)
declare void @llvm.coro.init(token, i8*, i8*)
declare void @llvm.coro.save(i8*, i8**)
declare void @llvm.coro.resume(i8*)
```
to build it use :
```sh
llvm-as -o coro.bc coro.ll
llvm-ar rcs libcoro.a coro.bc
```
More information about the digitalmars-d-ldc
mailing list