[LLVMdev] Cloning Functions

David Greene dag at cray.com
Wed Jul 9 10:43:45 PDT 2008


On Wednesday 09 July 2008 12:16, Eli Friedman wrote:
> On Wed, Jul 9, 2008 at 9:56 AM, David Greene <dag at cray.com> wrote:
> > I seem to recall havbing asked this before but I can't find it by
> > searching.
> >
> > What's the right way to clone a Function?  I need to save off the text at
> > a certain point and spit it out later, after other optimizations have
> > run.  But I need to spit out the original text, not the optimized
> > version.
>
> You can use CloneFunction (in llvm/Transforms/Utils/Cloning.h) to
> duplicate a function within a module.
>
> That said, I'm not completely sure what you're trying to do, so I
> don't know if that's what you're looking for.

Ok, I've looked at the cloning stuff and it's on the right track.  Here's
what I need to do:

I need to implement functionality similar to llvm-gcc's -emit-llvm in
our compiler.  I need to emit the llvm before any optimizations on the
IR have been performed.

But here's the catch.  Our compiler processes one function at a time,
so the LLVM IR is generated and fully optimized before the next function
appears.  This also means the global symbol table changes as we
process functions.

So what I need to do is somehow clone the final module (so I capture all
of the needed global symbols) and then replace the functions in the cloned
module (which have been optimized) with the unoptimized LLVM IR.

So it seems I need to do something like this:

1. Call CloneFunction for each function as it comes across to LLVM.  This
   captures the unoptimized LLVM IR.

2. Call CloneModule at the end of all processing to capture all globals.

3. ??!?

4. Profit!

The ??!? is something like, replace the cloned optimized Functions in the 
cloned Module with the cloned unoptimized Functions that were saved
off in step 1.  But how do I correctly remap the values?  I would have a
ValueMap for each function from step 1 but I somehow have to map those
Values onto the Values in the cloned module.  And map the arguments as
well (what CloneFunctionInto does).

Can anyone with more familiarity that I have about the cloning mechanism
provide guidance?

Thanks!

                                             -Dave

                                                   -Dave



More information about the llvm-dev mailing list