[LLVMdev] Cloning Functions

Devang Patel dpatel at apple.com
Wed Jul 9 11:24:22 PDT 2008


On Jul 9, 2008, at 10:43 AM, David Greene wrote:

> 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.

Is it possible to explain intended use of original unoptimized 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.

You do not need to optimize LLVM IR before processing next function.
If your compiler works like

	for each function
		generate_ir
		optimize

then it seems you're doing

	for each function
		generate_ir
		convert_to_llvm_ir
		optimize_llvm_ir

If you're doing this then you may try

	for each function
		generate_ir
		convert_to_llvm_ir
	optimize_llvm_ir_module

Now, you have access to un-optimized functions before you do  
optimize_llvm_ir_module. Would that work ?
			

>  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
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

-
Devang






More information about the llvm-dev mailing list