[LLVMdev] getting started with IR needing GC

Gordon Henriksen gordonhenriksen at mac.com
Sun Apr 20 18:52:09 PDT 2008


On 2008-04-20, at 21:05, Terence Parr wrote:

> On Apr 20, 2008, at 5:36 PM, Gordon Henriksen wrote:
>
>> Since the semispace heap doesn't actually work (it's an example, at  
>> best), I suggest you simply copy the stack visitor into your  
>> project; it's only a dozen lines of code or so.
>
>
> Ok, copying; can't find ShadowStackEntry though. Even make in that  
> dir doesn't work:

Please use the version from subversion; this is broken in 2.2 release,  
unfortunately.

> how does the gc "shadow-stack" gcroot intrinsic work exactly?  I  
> couldn't read the assembly very well.  Seems my example above  
> wouldn't work would it unless i create/fill in a shadow stack record?
>

'gc "shadow-stack"' in the LLVM IR instructs the code generator to  
automatically maintain the linked list of stack frames. You don't have  
to do anything to maintain these shadow stack frames except to keep  
your variables in the llvm.gcroot'd allocas. Essentially, it does this:

     struct ShadowStackEntry {
         ShadowStackLink *next;
         const ShadowStackMetadata *metadata;
         void *roots[0];
     };

     template <size_t count>
     struct Roots {
         ShadowStackLink *next;
         const ShadowStackMetadata *metadata;
         void *roots[0];
     };

     ShadowStackEntry *shadowStackHead;

     // Defined by the code generator.
     const ShadowStackMetadata f_metadata = ...;

     void f() {
         Roots<3> roots;
         roots.next = shadowStackHead;
         roots.metadata = f_metadata;
         roots.roots[0] = NULL;
         roots.roots[1] = NULL;
         roots.roots[2] = NULL;
         shadowStackHead = (ShadowStackEntry *) &roots;

         ... user code ...

         shadowStackHead = entry.next; // before any exit
         return;
     }

> Taking a giant step back, I can build something similar to  
> semispace.c myself so I'm in control of my world, right?  i would  
> set up the shadow stack using IR instructions and could avoid gcroot  
> by notifying my collector as I see fit...

That's true; the shadow stack design is explicitly for uncooperative  
environments, after all.

When you want to eliminate the shadow stack overhead, you will need to  
(a.) use a conservative GC or (b.) emit stack frame metadata using the  
LLVM GC support.

> Sorry I'm so lost...just trying to figure out what llvm does for me  
> and what I have to do.

No problem!

Generally speaking, LLVM is going to help you find roots on the stack,  
which is the part that the compiler backend must help with; the rest  
is your playground. The infrastructure is more suited toward  
interfacing with an existing GC rather than necessarily making writing  
a new runtime trivial. (See exception handling for precedent…)

— Gordon

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20080420/ce58cb7b/attachment.html>


More information about the llvm-dev mailing list