<div dir="ltr"><div dir="ltr">Hi Daniel,<div><br></div><div>CloneFunctionInto wants to tell you about the new ReturnInstructions it produced, you're expected to pass a vector for this purpose. You're free to ignore these values, though, but you still have to pass that vector:  SmallVector<ReturnInst*, 8> Returns;</div><div><br></div><div>For the argument remapping, you're supposed to pre-populate the VMap you pass with the appropriate argument-to-argument mapping.</div><div><br></div><div>It's perfectly fine to use CloneFunctionInto across modules, btw. Only operations across LLVMContexts are tricky.</div><div><br></div><div>Cheers,</div><div>Philip</div></div></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Sep 3, 2018 at 6:31 PM Daniel Moya via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Thank you Ahmad,</div><div><br></div><div>I figured out that, although the type of both p(oInst) and p(nInst) were the same, I had to:</div><div><br></div>for (unsigned int i = 0; i < callOInst->getNumArgOperands(); i++) { callOInst->getArgOperand(i)->mutateType(callNInst->getArgOperand(i)->getType()); }</div><div dir="ltr"><br></div><div>that solves the issue at the calling instruction in the main function, but now I see that <b>linkModules</b> does not work for me (it's a mess to bring all the other unnecessary functions from the reference module) so what I need is to directly move one function from one module to another one. I'm still facing the issue with the <b>llvm::SmallVectorImpl< llvm::ReturnInst *> &Returns </b>argument for the <b>CloneFunctionInto, </b>can anyone please give an example of use of that function? I guess that's the only way because otherwise if I move the blocks from foo3 in the refModule to the oldModule, the references of the moved block still point to the arguments of foo3 defined in the refModule, and I don't know how to go instruction by instruction in all the moved blocks and correct the reference to point to the arguments in the oldModule's new function, this also sounds very messy and complicated. <br></div><div><br></div><div><br></div><div>Regards,</div><div>Daniel Moya<br></div><div><b></b></div><div dir="ltr"><div><br></div><div><br></div><div><br></div></div></div></div><br><div class="gmail_quote"><div dir="ltr">El dom., 2 de sep. de 2018 a la(s) 23:18, Ahmad Nouralizadeh Khorrami (<a href="mailto:ahmad.llvm@gmail.com" target="_blank">ahmad.llvm@gmail.com</a>) escribió:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr">Hi Daniel,<div>The answer was for your first thread. The benefits are outlined in the repository, but your problem is still there. I'm not sure. But this looks similar to my recent problem. I think that a bitcast will solve the problem. The types after the linking process may have different names but the same contents. The links to the answers are as follows:</div><div><a href="http://lists.llvm.org/pipermail/llvm-dev/2018-August/125413.html" target="_blank">http://lists.llvm.org/pipermail/llvm-dev/2018-August/125413.html</a><br></div><div><a href="https://stackoverflow.com/questions/51894129/convert-function-pointer-call-to-function-call-at-the-ir-level" target="_blank">https://stackoverflow.com/questions/51894129/convert-function-pointer-call-to-function-call-at-the-ir-level</a><br></div><div>Regards.</div></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Sun, 2 Sep 2018 at 23:30, Daniel Moya <<a href="mailto:danielmscr1994@gmail.com" target="_blank">danielmscr1994@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi Ahmad,<div><br></div><div>What does that tool does besides what LLVM linker already does? I don't think my problem is in linking both modules, I think LLVM linker does the job for me, the issue is when changing the called function to call another function (in the example previously provided, to change it from foo2 to foo3, and adjusting the function parameter's references).</div><div><br></div><div>Regards,</div><div>Daniel Moya</div></div><br><div class="gmail_quote"><div dir="ltr">El dom., 2 de sep. de 2018 a la(s) 17:00, Ahmad Nouralizadeh Khorrami (<a href="mailto:ahmad.llvm@gmail.com" target="_blank">ahmad.llvm@gmail.com</a>) escribió:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="ltr">Hi.<div>Besides the LLVM linker, you can also use this tool:</div><div><a href="https://github.com/travitch/whole-program-llvm" target="_blank">https://github.com/travitch/whole-program-llvm</a><br></div><div>It links all the modules and produces a single module containing every function.</div><div>Regards.</div></div></div><br><div class="gmail_quote"><div dir="ltr">On Sun, 2 Sep 2018 at 16:57, Daniel Moya via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Hello and thanks for the answer,<div><br></div><div>I'm still facing issues, I'll do my best to explain my situation, as I explained, I have two modules, each one with its own main and functions, I would like to replace in the <b>oldModule</b> a function call that is calling <b>foo2</b> (defined in <b>oldModule</b>) to instead call <b>foo3</b>, which is defined in the <b>refModule.  </b>So in summary, I have:</div><div><ol><li>The original instruction call, defined in the main function of the oldModule, who is a calling function to foo2, I'll name it <b>oInst </b>(original Instruction)</li><li>The "new" instruction call, defined in the main function of the refModule, who is a calling function to foo3, I'll name it <b>nInst</b> (new Instruction)</li><li>The foo2 function definition, defined in the oldModule, I'll name it <b>oFunc </b>(original Function)</li><li>The foo3 function definition, defined in the refModule, I'll name it <b>nFunc</b> (new Function)</li><li>I have the parameters (or arguments?) of both functions, both in the calling instruction and in the function's definition, which I'll refer to as <b>p(oInst)</b>, <b>p(nInst)</b>, <b>p(oFunc)</b>, <b>p(nFunc) </b>(the parameters of)</li><li>For testing purposes, both foo2 and foo3 and defined identical, same returning type, parameter's type and even the same variable's name in the IR.</li></ol><div>So after calling the <b>llvm::LinkerlinkModules</b> function, I did:</div><div><br></div><div>1. First attempt: </div><div><ol><li>llvm::CallInst *callOInst = static_cast<llvm::CallInst*>(oInst);  // I cast the<b> oInst</b> to a llvm::CallInst<br></li><li>callOInst->setCalledFunction(nFunc); // now <b>oInst</b> should call nFunc<br></li></ol><div>Error: </div><div>Call parameter type does not match function signature!</div></div><div>%0 = load i32, i32* %a, align 4</div><div>i32 %call1 = call i32 @foo3(i32 %0, i32 %1)</div><div><br></div><div>So even though the parameters are the same type, and defined identically in both modules, the <b>p(oInst)</b> apparently does not match the <b>p(nFunc)</b>. </div><div><br></div><div>2. Second attempt:</div><div><div><ol><li>llvm::Instruction *nCloneInst = nInst->clone(); //Clone of the <b>nInst</b>, to avoid remove it from the refModule<br></li><li>nCloneInst->insertAfter(oInst); // I'll bring the nInst because I know <b>p(nInst)</b> and <b>p(nFunc)</b> match<br></li><li>nCloneInst->mutateType(oInst->getType()); //Idk why I have to this, but necessary for next line<br></li><li>oInst->replaceAllUsesWith(nCloneInst);<br></li><li>oInst->dropAllReferences();</li><li>oInst->eraseFromParent();</li></ol></div><div>Error:</div></div><div>Instruction does not dominate all uses!</div><div><div>%0 = load i32, i32* %a, align 4</div><div>%2 = call i32 @foo3(i32 %0, i32 %1)</div></div><div><br></div><div>Great, now the <b>p(nInst)</b> are still referring to their definition in the refModule, so either I bring those instructions too (which sounds really messy) or somehow I change the <b>p(nInst)</b> to refer to the instructions in oldModule, which in my case are actually defined the same (but apparently the references don't change based on the name being the same in both modules). </div><div><br></div><div>3. Third attempt:</div><div><ol><li>The same 1-4 steps as before, from cloning instruction to replaceAllUsesWith</li><li>llvm::CallInst *callNInst = static_cast<llvm::CallInst*>(nCloneInst);<br></li><li>llvm::CallInst *callOInst = static_cast<llvm::CallInst*>(oInst); // cast both <b>oInst</b> and <b>nInst</b><br></li><li>for (unsigned int i = 0; i < callOInst->getNumArgOperands(); i++) { callNInst->setArgOperand(i,callOInst->getArgOperand(i)); } //replace <b>p(nInst)</b> with <b>p(oInst)</b><br></li><li>The same 5-6 steps as before, drop and erase</li></ol><div><div>Error:</div><div>Call parameter type does not match function signature!</div><div>%0 = load i32, i32* %a, align 4</div><div>i32  %2 = call i32 @foo3(i32 %0, i32 %1) <br></div></div></div><div><br></div><div>So back to the first problem, the <b>p(nInst) </b>(now converted to <b>p(oInst)</b>) apparently does not match the <b>p(<span class="m_-5757159750255473956m_-3402637895520460606m_1958153990707407551m_4360065250572099151m_8328214421441790732gmail-gr_ m_-5757159750255473956m_-3402637895520460606m_1958153990707407551m_4360065250572099151m_8328214421441790732gmail-gr_4824 m_-5757159750255473956m_-3402637895520460606m_1958153990707407551m_4360065250572099151m_8328214421441790732gmail-gr-alert m_-5757159750255473956m_-3402637895520460606m_1958153990707407551m_4360065250572099151m_8328214421441790732gmail-gr_spell m_-5757159750255473956m_-3402637895520460606m_1958153990707407551m_4360065250572099151m_8328214421441790732gmail-gr_inline_cards m_-5757159750255473956m_-3402637895520460606m_1958153990707407551m_4360065250572099151m_8328214421441790732gmail-gr_disable_anim_appear m_-5757159750255473956m_-3402637895520460606m_1958153990707407551m_4360065250572099151m_8328214421441790732gmail-ContextualSpelling m_-5757159750255473956m_-3402637895520460606m_1958153990707407551m_4360065250572099151m_8328214421441790732gmail-ins-del m_-5757159750255473956m_-3402637895520460606m_1958153990707407551m_4360065250572099151m_8328214421441790732gmail-multiReplace" id="m_-5757159750255473956m_-3402637895520460606m_1958153990707407551m_4360065250572099151m_8328214421441790732gmail-4824" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit">nFunc</span>)</b>. </div><div><br></div><div>I also looked into the <b>CloneFunctionInto </b>function, but I didn't understand the arguments of it, and there's really no documentation or examples that I could find on the internet. Specifically, I have troubles with <b>llvm::SmallVectorImpl< llvm::ReturnInst *> &Returns </b>argument, I don't know how to initialize it, it doesn't have a 0 argument constructor and if I try:</div><div><br></div><div>llvm::SmallVectorImpl< llvm::ReturnInst *> ReturnsArg = llvm::SmallVectorImpl< llvm::ReturnInst *>(2); // Just as an example<br></div><div><br></div><div>It says that constructor is protected. I didn't want to go further since I'm clueless on how to properly use this function, and I'm even not completely sure if it would fix all the troubles that I've been having with the other three attempts.</div><div><br></div><div>Btw, all these errors happen when I try to run (through JIT) the module, a workaround that I know that I can do for all my attempts is just to dump the module to a file, and then reload it and execute it (I know it works since in both oldModule and refModule I use the same IR variable's names) but I would like to do the work the <b>right</b> way and not having to inefficiently dump a file just to reload it again and get all the references right. </div><div><br></div><div>Thanks for the help in advance, I'll be really grateful for any advice or light in my situation.</div><div><br></div><div>Regards,</div><div>Daniel Moya</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div><br><div class="gmail_quote"><div dir="ltr">El mar., 28 de ago. de 2018 a la(s) 20:26, Friedman, Eli (<a href="mailto:efriedma@codeaurora.org" target="_blank">efriedma@codeaurora.org</a>) escribió:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 8/27/2018 10:37 AM, Daniel Moya via llvm-dev wrote:<br>
> Hello LLVM Developers,<br>
><br>
> I'm trying to replace a function defined in one module into another <br>
> module (different files). The first issue I ran into was that <br>
> llvm::Function does not have a method "moveBefore" or "moveAfter" as <br>
> the llvm::BasicBlock or llvm::Instruction do, so I figured I would <br>
> just move the BasicBlocks of the replacing function into the function <br>
> that was being replaced, and then eliminate the original BasicBlocks.<br>
<br>
Cross-module operations are tricky in general; I'd suggest using the <br>
Linker::linkModules API if possible.<br>
<br>
-Eli<br>
<br>
-- <br>
Employee of Qualcomm Innovation Center, Inc.<br>
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project<br>
<br>
</blockquote></div>
_______________________________________________<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" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>
</blockquote></div>
</blockquote></div>
</blockquote></div>
_______________________________________________<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" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>