Hi all:
Thank you very much for the feedback during the first round! Special thanks to
Eli, Sanjoy, Hal and Chandler for their detailed public and private comments.
The model is simpler now and the intrinsics have nice symmetry to them:
coro.begin + coro.end, coro.alloc + coro.free. Compared to the previous RFC,
coro.fork is gone, coro.init and coro.start are collapsed into one coro.begin,
coro.suspend is three-way as opposed to two-way. coro.elide and coro.delete
renamed coro.alloc and coro.free.
All the changes implemented and tested. Full document, (nicely formatted by
github is at:
https://github.com/GorNishanov/llvm/blob/coro-rfc/docs/Coroutines.rst
Below is a summary of a proposal to add coroutine support to LLVM.
The proposal is motivated primarily by the desire to support C++ Coroutines [1],
however the llvm representation is language neutral and can be used
to support coroutines in other languages as well.
Clang + llvm coroutines allows you to take this code:
generator<int> range(int from, int to) {
for(int i = from; i < to; ++n)
co_yield i;
}
int main() {
int sum = 0;
for (auto v: range(1,100))
sum += v;
return sum;
}
And translate it down to this:
define i32 @main() #5 {
entry:
ret i32 4950
}
You can also use coroutines in plain C, by calling the builtins mapping to the
intrinsics described by this proposal, so that your coroutine can look like:
#include "Inputs/coro.h"
void print(int v);
void* f(int n) {
CORO_BEGIN(malloc);
while (n-- > 0) {
print(n+1);
CORO_SUSPEND();
}
CORO_END(free);
}
// CHECK-LABEL: @main
int main() {
void* coro = f(4);
CORO_RESUME(coro);
CORO_RESUME(coro);
CORO_DESTROY(coro);
}
I prototyped llvm changes to support this proposal and extended clang coroutine
implementation [2] to emit proposed intrinsics to smoke test the proposed
design. See "More Attention Required" in the doc/Coroutines.rst [4]for details
on what additional work is required beyond cleanup and bug fixes.
I would like to continue the discussion on the overall design, representation
and a roadmap to start upstreaming the changes with the intention to continue
the development in tree incrementally.
Roadmap: