<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On 14 July 2016 at 20:07, Mehdi Amini <span dir="ltr"><<a href="mailto:mehdi.amini@apple.com" target="_blank">mehdi.amini@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><br><div><span class=""><blockquote type="cite"><div>On Jul 14, 2016, at 7:58 AM, Pierre Gagelin via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:</div><br><div><div dir="ltr"><div><div><div><div><div>Hi,<br><br></div>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):<br><br>define void @function(i32 %i) #0 {<br>entry:<br> %i.addr = alloca i32, align 4<br> store i32 %i, i32* %i.addr, align 4<br> ret void<br>}<br><br></div>to another function which should, after the CloneFunction, be like:<br><br>define void @cloned_function(i32 %i, i8* %ptr) #0 {<br>entry:<br> %i.addr = alloca i32, align 4<br> store i32 %i, i32* %i.addr, align 4<br> ret void<br>}<br><br></div>To do so, I first declared the clone_function with its arguments into the module then I should call CloneFunction like:<br><br></div>Function *function = M.getFunction("function");<br></div>Function *cloned_function = M.getFunction("cloned_function”);<br></div></div></blockquote><div><br></div></span><div>This last statement is dead code, you overwrite the cloned_function on the next line. <br></div></div></div></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div><span class=""><br><blockquote type="cite"><div><div dir="ltr">cloned_function = CloneFunction(<br> function,<br> VM,<br> false /* or true? didn't get the difference */<br> );<br></div></div></blockquote><div><br></div></span><div>Current LLVM has this prototype:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span><span style="color:rgb(79,129,135)">Function</span><span> *</span>CloneFunction(</span><span style="color:rgb(79,129,135)">Function</span><span> *F, </span><span style="color:rgb(79,129,135)">ValueToValueMapTy</span><span> &VMap,</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(79,129,135)">ClonedCodeInfo</span><span> *CodeInfo = </span><span style="color:rgb(186,45,162)">nullptr</span><span>);</span></div><div><span><br></span></div><div><span>Notice that the last argument is not a boolean (You may be using a different version of LLVM?).</span> <br></div></div></div></div></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div><div><div>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.</div></div></div></div></blockquote><div><br></div><div>Yup sorry, just that I tried to use CloneFunctionInto first which takes
both the function to clone and the other. I just read the code this
morning and CloneFunction is not doing what I want, it deletes arguments
passed though the VMap. I just thought quickly that as there where only 3 arguments it would be easier for someone to explain...<br><br>The one I want to use is the CloneFunctionInto. It seems it does remap the argument passed to the
function so that instructions in the new body doesn't point to Values in other function body. That's why it is usefull to declare the function with its arguments: it generates independant arguments with their own address (which differs with the one to clone). So it is definitely not CloneFunction that I should have put the question on but CloneFunctionInto, my bad.<br><br>For CloneFunction I followed the signature here (<a href="http://llvm.org/docs/doxygen/html/namespacellvm.html#a5596246c2350cb74f31ffa7695d5923c">http://llvm.org/docs/doxygen/html/namespacellvm.html#a5596246c2350cb74f31ffa7695d5923c</a>)
but it seems not to be the version I have in my source files actually...<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div><div><div><span><br></span></div><div>You have to design this as a two-part process:</div><div><br></div><div>1) Clone the function (if you need to keep the original around)</div><div>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.</div></div></div></div></blockquote><div><br></div><div>Yes that's the hard part. And you can't clone the function if there is arguments. The instructions using these arguments will have Values refering to another function body which triggers errors like:<br><br>Referring to an argument in another function!<br> store i8* %ptr, i8** %ptr.addr, align 8<br><br></div><div>for a function taking i8* %ptr as argument. But I think CloneFunctionInto allows you to do this kind of argument expansion (because that's the only thing after all: adding an argument to the function).<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div><div><div><br></div><div>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 <br></div></div></div></div></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div><div><div><br></div><div>— </div><div>Mehdi</div><div><br></div><div><br></div></div><div><br></div><br><blockquote type="cite"><div><span class=""><div dir="ltr"><div><div><br></div><div>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*>...<br><br></div><div>Thank you for the help,<br></div><div>Pierre<br></div></div></div></span>
_______________________________________________<br>LLVM Developers mailing list<br><a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br></div></blockquote></div><br></div></blockquote></div><br></div></div>