<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jul 15, 2016, at 1:15 AM, Pierre Gagelin <<a href="mailto:Pierre.Gagelin@myport.ac.uk" class="">Pierre.Gagelin@myport.ac.uk</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><br class=""><div class="gmail_extra"><br class=""><div class="gmail_quote">On 14 July 2016 at 20:07, Mehdi Amini <span dir="ltr" class=""><<a href="mailto:mehdi.amini@apple.com" target="_blank" class="">mehdi.amini@apple.com</a>></span> wrote:<br class=""><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" class=""><br class=""><div class=""><span class=""><blockquote type="cite" class=""><div class="">On Jul 14, 2016, at 7:58 AM, Pierre Gagelin via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a>> wrote:</div><br class=""><div class=""><div dir="ltr" class=""><div class=""><div class=""><div class=""><div class=""><div class="">Hi,<br class=""><br class=""></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 class=""><br class="">define void @function(i32 %i) #0 {<br class="">entry:<br class="">  %i.addr = alloca i32, align 4<br class="">  store i32 %i, i32* %i.addr, align 4<br class="">  ret void<br class="">}<br class=""><br class=""></div>to another function which should, after the CloneFunction, be like:<br class=""><br class="">define void @cloned_function(i32 %i, i8* %ptr) #0 {<br class="">entry:<br class="">  %i.addr = alloca i32, align 4<br class="">  store i32 %i, i32* %i.addr, align 4<br class="">  ret void<br class="">}<br class=""><br class=""></div>To do so, I first declared the clone_function with its arguments into the module then I should call CloneFunction like:<br class=""><br class=""></div>Function *function = M.getFunction("function");<br class=""></div>Function *cloned_function = M.getFunction("cloned_function”);<br class=""></div></div></blockquote><div class=""><br class=""></div></span><div class="">This last statement is dead code, you overwrite the cloned_function on the next line. <br class=""></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" class=""><div class=""><span class=""><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class="">cloned_function = CloneFunction(<br class="">    function,<br class="">    VM,<br class="">    false /* or true? didn't get the difference */<br class="">  );<br class=""></div></div></blockquote><div class=""><br class=""></div></span><div class="">Current LLVM has this prototype:</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class=""><span style="color:rgb(79,129,135)" class="">Function</span><span class=""> *</span>CloneFunction(</span><span style="color:rgb(79,129,135)" class="">Function</span><span class=""> *F, </span><span style="color:rgb(79,129,135)" class="">ValueToValueMapTy</span><span class=""> &VMap,</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">                        </span><span style="color:rgb(79,129,135)" class="">ClonedCodeInfo</span><span class=""> *CodeInfo = </span><span style="color:rgb(186,45,162)" class="">nullptr</span><span class="">);</span></div><div class=""><span class=""><br class=""></span></div><div class=""><span class="">Notice that the last argument is not a boolean (You may be using a different version of LLVM?).</span> <br class=""></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" class=""><div class=""><div class=""><div class="">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 class=""><br class=""></div><div class="">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 class=""><br class="">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 class=""><br class="">For CloneFunction I followed the signature here (<a href="http://llvm.org/docs/doxygen/html/namespacellvm.html#a5596246c2350cb74f31ffa7695d5923c" class="">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 class=""></div><div class=""> </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" class=""><div class=""><div class=""><div class=""><span class=""><br class=""></span></div><div class="">You have to design this as a two-part process:</div><div class=""><br class=""></div><div class="">1) Clone the function (if you need to keep the original around)</div><div class="">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 class=""><br class=""></div><div class="">Yes that's the hard part. And you can't clone the function if there is arguments. </div></div></div></div></div></blockquote><div><br class=""></div><div>Not sure what you mean by "you can't clone the function if there is arguments”, CloneFunction is supposed to handle this.</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class="">The instructions using these arguments will have Values refering to another function body which triggers errors like:<br class=""><br class="">Referring to an argument in another function!<br class="">  store i8* %ptr, i8** %ptr.addr, align 8<br class=""><br class=""></div><div class="">for a function taking i8* %ptr as argument. </div></div></div></div></div></blockquote><div><br class=""></div><div>If you end up with this by *only* calling CloneFunction, that’s a bug. It is supposed to handle this perfectly.</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class="">But I think CloneFunctionInto allows you to do this kind of argument expansion</div></div></div></div></div></blockquote><div><br class=""></div><div>Yes, CloneFunction is setting up the call for CloneFunctionInto, if you look at the implementation you should be able to copy it, and with some adaptation call CallFunctionInto.</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class=""> (because that's the only thing after all: adding an argument to the function).<br class=""></div></div></div></div></div></blockquote><div><br class=""></div><div>Note: If you just want to *add* an argument and don’t need to preserve the old function, cloning it is an expensive solution…</div><div><br class=""></div><div><br class=""></div>— </div><div>Mehdi</div><div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class=""> </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" class=""><div class=""><div class=""><div class=""><br class=""></div><div class="">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 class=""></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" class=""><div class=""><div class=""><div class=""><br class=""></div><div class="">— </div><div class="">Mehdi</div><div class=""><br class=""></div><div class=""><br class=""></div></div><div class=""><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><span class=""><div dir="ltr" class=""><div class=""><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">Thank you for the help,<br class=""></div><div class="">Pierre<br class=""></div></div></div></span>
_______________________________________________<br class="">LLVM Developers mailing list<br class=""><a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a><br class=""><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br class=""></div></blockquote></div><br class=""></div></blockquote></div><br class=""></div></div>
</div></blockquote></div><br class=""></body></html>