<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="gmail-gr_ gmail-gr_4824 gmail-gr-alert gmail-gr_spell gmail-gr_inline_cards gmail-gr_disable_anim_appear gmail-ContextualSpelling gmail-ins-del gmail-multiReplace" id="gmail-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">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>