[LLVMdev] Proposal: stack/context switching within a thread
kennethuil at gmail.com
Sun Apr 11 09:01:31 PDT 2010
Having read through Stackless Python's web pages a bit:
1. They're doing pretty much what I'd like to do, except that I don't
want to be tied to a particular language and I'd like to be able to
use the stack. (Also, stack use is inescapable with LLVM, as far as I
2. We should be able to support "hard switching" in Stackless Python
by adding a llvm.getcontextstacktop intrinsic. If, as in Kristján's
example, llvm.getcontext is used to create context A, and then
execution continues until context B is created with
llvm.swapcontext(B, A), the region of memory between
llvm.getcontextstacktop(A) and llvm.getcontextstacktop(B) can be saved
and later restored when B is resumed. Of course that usage would
throw a monkey wrench into a segmented stack scheme... it assumes that
context stack areas actually behave like contiguous stacks. Not only
that, it assumes that no pointers to a context's stack exist outside
of the context... when the context is inactive, a pointer into a
context's stack won't be valid!
But in the case of Stackless Python, these caveats can be addressed
with a simple "Don't do that!", since it's all tied into the language.
3. I would need to run some benchmarks, but in some cases it might be
better to use mmap to swap stacks between contexts... that way nothing
would need to be copied.
4. I'm hoping that LLVM ends up growing optimization passes that
minimize the actual physical use of contexts in many use cases. Also,
we might be able to guarantee small stack usage with a pass that
forces recursive calls to spawn a new context and turns large alloca's
into malloc's, making it safer to have a bunch of little stacks
without any needed juggling.
More information about the llvm-dev