On Mon, Apr 11, 2011 at 7:34 AM, Justin Holewinski <span dir="ltr"><<a href="mailto:justin.holewinski@gmail.com">justin.holewinski@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div class="gmail_quote"><div><div></div><div class="h5">On Mon, Apr 11, 2011 at 9:07 AM, Sanjoy Das <span dir="ltr"><<a href="mailto:sanjoy@playingwithpointers.com" target="_blank">sanjoy@playingwithpointers.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi!<br>
<br>
Thanks for the feedback. For context, my implementation plan is here:<br>
<a href="http://pastebin.com/e9JMZNCE" target="_blank">http://pastebin.com/e9JMZNCE</a><br>
<br>
First, about unwinding:<br>
<br>
In architectures like x86-64, where unwinding based on DWARF info, there<br>
shouldn't be any problems; since the DWARF info will be emitted<br>
correctly. Otherwise, if the unwinding is done by following BP, it<br>
should still be possible to have BP de-reference correctly (ref. "Frame<br>
Pointers" section in the implementation plan). SP will not always have a<br>
correct value - I don't know if this is problem.<br>
<br>
About co-routines:<br>
<br>
Here is a sketch of how I think co-routines can be implemented (I'll<br>
merge this with the main implementation plan after I get some feedback):<br>
<br>
Have a new instruction, called "yield" to return a value from a<br>
co-routine, preserving the state. Thus, we immediately know which<br>
functions are co-routines. Each co-routine will have a new stack.<br>
Associate each co-routine with a thread-local global variable (called<br>
saved_stack here, will have to be mangled with the name of the<br>
co-routine) which points to the start stack block for that co-routine.<br>
This will be the first block in the chain of blocks to follow.<br>
<br>
The structure of the block will be similar to the structure of a regular<br>
stack block, except that it will also have space to store two registers<br>
- this_ip and this_sp.<br>
<br>
The prologue of a co-routine will jump to a function similar to<br>
setup_new_block (setup_new_block_coroutine) which will work like<br>
setup_new_block, except:<br>
<br>
1. It will first check if saved_stack is NULL. If it is NULL, it will<br>
allocate a new block and save it to saved_stack. It if isn't, it'll<br>
simply restore saved_sp, saved_ip.<br>
<br>
2. In case a new block was allocated, it will pretty much do what<br>
setup_block does, after which it will adjust the SP to make space for<br>
the saved registers.<br>
<br>
The destroy_block procedure will also have to be a little different<br>
(mentioned below).<br>
<br>
There are four things (relevant to this discussion) a co-routine can do:<br>
<br>
Yield<br>
<br>
This returns control to the calling function, without forgetting the<br>
current state of the function. To do this, we save saved_ip and<br>
saved_sp. Every yield be padded with instructions pushing and popping<br>
the registers live at that point. Then we set the return value (register<br>
or memory), and restore saved_sp and saved_ip from the current block. We<br>
can't simply return because the actual return value has been hijacked to<br>
provide for block cleanup.<br>
<br>
Call to regular function<br>
<br>
Just a simple call - the caller's prologue will handle setting up a it's<br>
own stack space etc.<br>
<br>
Call to Co-routine<br>
<br>
This too should "just work", since all the heavy-lifting is done in the<br>
co-routine's prologue. However, the above approach will not work for<br>
nested co-routines (i.e. calling the same co-routine body with one call<br>
is still active, recursively). I'm not sure if having support for nested<br>
co-routines will add any value.<br>
<br>
Return<br>
<br>
This will be a regular return. Since the return value has been hijacked<br>
to point to a another block of code (destroy_block_coroutine), control<br>
will jump there instead.<br>
<br>
destroy_block_coroutine will free the linked-list of stack blocks (we<br>
have to free this, since we will won't have a reference to this list<br>
anymore), set saved_stack for this co-routine to NULL, and restore<br>
saved_sp and saved_ip.<br></blockquote><div><br></div></div></div><div>I'm wondering how much of this should be implemented as new LLVM functionality, and how much should be left to the front-end compiler.  With some additional LLVM intrinsics (e.g. llvm.stack.new.block, llvm.stack.delete.block, etc.), a front-end could take care of the details of how co-routines are actually implemented.  This would also give the front-end freedom to implement whatever semantics/conventions are necessary/required for the source language.  I'm just not sure having LLVM dictate <i>how</i> co-routines are implemented is the best way to approach this.</div>


<div></div></div></blockquote><div><br></div><div>I'm in agreement with Justin.</div><div><br></div><div>1) It's much easier to add new intrinsics to LLVM than it is to add new instructions. Library functions are even easier - in general you want to use intrinsics in cases where the presence of the intrinsic will affect the way the code is generated in the calling function.</div>

<div><br></div><div>2) The API that I was envisioning for creating stacks was something fairly simple (maybe too simple):</div><div><br></div><div>   // Create a new stack, where 'stack_handle' is some opaque object, and 'func'</div>

<div>   // is an LLVM function.</div><div>   stack_handle = llvm.createstack(func, data)</div><div><br></div><div>   // Switch to a different stack (i.e. yield)</div><div>   llvm.switchstack(stack_handle)</div><div><br></div>

<div>   // deallocate a stack</div><div>   llvm.destroystack(stack_handle)</div><div><br></div><div>The frontend would be responsible for managing the lifetime of the stack_handle object. For something like Python generators, the stack_handle would be stored in the iterator object that is returned from the generator, and deallocated when the generator gets garbage-collected. For cooperative threads, the handle would be stored as a private member of a Thread or Coroutine class. Different languages would support different ways of using coroutines.</div>

<div><br></div><div>Similarly, the frontend would be responsible for passing data between the different coroutines, using either thread-local variables or some other method.</div><div><br></div><div>Also I'm thinking the frontend would be responsible for propagating exceptions between stacks - so for example, if my coroutine throws an exception that is not caught, but instead propagates all the way to the top of it's stack, then that coroutine gets terminated, and depending on the language semantics, the same exception or another one gets thrown upon return from 'yield' in the other context. I would have no objection to implementing all of that logic in my frontend.</div>

<div><br></div><div>Because the internals of the stack_handle may be different on different platforms, I'm thinking that the frontend should only get a void* pointer - it doesn't need to know what's inside it. I think the frontend only needs a small number of operations: create, yield, destroy, and iterate through call frames (for garbage collection).<br>

<br></div><div>The 'yield' instruction seems somewhat inspired from Python's 'yield' statement, which unfortunately has some substantial drawbacks - such as the fact that the yield must be in the top-most call frame in order for the compiler to know that the function is a generator / coroutine. In other words, you can't in Python have a generator which calls a subroutine that does the yield. I'd like to avoid that limitation. Scheme and Lua, to name just two examples, have much more powerful models for doing continuations and coroutines, and I'd like to be able to support those. I think that can be done if we supply only the most minimal support on the LLVM side, and leave most of the details to the frontend.</div>

<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="gmail_quote"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<font color="#888888"><br>
--<br>
</font><div><div></div><div><div class="im">Sanjoy Das<br>
<a href="http://playingwithpointers.com" target="_blank">http://playingwithpointers.com</a><br></div><div class="im">
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
</div></div></div></blockquote></div><br><br clear="all"><br>-- <br><br><div>Thanks,</div><div><br></div><font color="#888888"><div>Justin Holewinski</div><br>
</font></blockquote></div><br><br clear="all"><br>-- <br>-- Talin<br>