[llvm-dev] clone function

Mehdi Amini via llvm-dev llvm-dev at lists.llvm.org
Thu Jul 14 12:07:14 PDT 2016


> On Jul 14, 2016, at 7:58 AM, Pierre Gagelin via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> 
> Hi,
> 
> I am trying to use the CloneFunction from llvm/Transforms/Utils/Cloning.h and I don't understand how the ValueToValueMapTy argument should be initialized. For instance, let say I want to clone this function (to add an argument):
> 
> define void @function(i32 %i) #0 {
> entry:
>   %i.addr = alloca i32, align 4
>   store i32 %i, i32* %i.addr, align 4
>   ret void
> }
> 
> to another function which should, after the CloneFunction, be like:
> 
> define void @cloned_function(i32 %i, i8* %ptr) #0 {
> entry:
>   %i.addr = alloca i32, align 4
>   store i32 %i, i32* %i.addr, align 4
>   ret void
> }
> 
> To do so, I first declared the clone_function with its arguments into the module then I should call CloneFunction like:
> 
> Function *function = M.getFunction("function");
> Function *cloned_function = M.getFunction("cloned_function”);

This last statement is dead code, you overwrite the cloned_function on the next line.

> cloned_function = CloneFunction(
>     function,
>     VM,
>     false /* or true? didn't get the difference */
>   );

Current LLVM has this prototype:

Function *CloneFunction(Function *F, ValueToValueMapTy &VMap,
                        ClonedCodeInfo *CodeInfo = nullptr);

Notice that the last argument is not a boolean (You may be using a different version of LLVM?).
Also, the API takes a function and returns another one. I’m not sure what you expect by first declaring another function with its arguments: it won’t be magically picked as a “destination” for the clone.

You have to design this as a two-part process:

1) Clone the function (if you need to keep the original around)
2) Change the function signature. For this there is no helper (that I know of), you may look at how it is done in the DeadArgElimination pass.

Apparently you can do part of the transformation during the clone, first creating your new function and mapping the old argument to the new ones, but you’ll still have to splice the function body from the created clone to your new function. I’m not sure if it really buys you anything over RAUW it after the fact though.

— 
Mehdi




> 
> The question is how should I setup VM so that the job is correctly done? I assumed I had to link arguments from function to their equivalent in cloned_function but I don't see why VM should contain pair<Value*, WeakVH> instead of pair<Value*, Value*>...
> 
> Thank you for the help,
> Pierre
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

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


More information about the llvm-dev mailing list