<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jun 28, 2016 at 8:11 PM, 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-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><span class=""><blockquote type="cite"><div>On Jun 27, 2016, at 12:25 PM, vivek pandya <<a href="mailto:vivekvpandya@gmail.com" target="_blank">vivekvpandya@gmail.com</a>> wrote:</div><br><div><div dir="ltr">Hello ,<div><br></div><div>To solve this bug locally I have given preference to tail call optimization over local function related optimization in IPRA. I have added following method to achieve this:</div><div><br></div><div><div>bool isEligibleForTailCallOptimization(Function *F) {</div><div> CallingConv::ID CC = F->getCallingConv();</div><div> if (CC == CallingConv::Fast || CC == CallingConv::GHC || CC == CallingConv::HiPE)</div><div> return true;</div><div> return false;</div><div>}</div></div><div><br></div><div>Any other suggestions are always welcomed.</div></div></div></blockquote><div><br></div></span>Why aren’t checking for the presence of a tail call?</div></div></blockquote><div>Are you asking about if tail call optimization is enable or not? If not then above method is inspired from X86ISelLowering::canGuaranteeTCO(). </div><div><br></div><div>-Vivek</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><div><br></div><div>— </div><span class=""><font color="#888888"><div>Mehdi</div></font></span><div><div class="h5"><div><br><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>and I am checking this condition along with hasLocalLinkage() and hasAddressTaken().</div><div><br></div><div>Due to this test-suite now has only 2 runtime failure where as before this there were around 43 due to local function related optimization. But of course by giving preference to tail call many opportunities to optimize IPRA is missed.</div><div><br></div><div>The 2 existing failure are interesting and hard to debug, namely they are MultiSource/Applications/SPASS/SPASS and MultiSource/Applications/sqlite3/sqlite3</div><div><br></div><div>However for test-suite sqlite3 is only complied as CLI program and thus it only contains 2 (big) source files. In sqlite3 source code there are few static methods with variable number of arguments and due to that these static function should not get tail call optimized and thus IPRA optimization can be done but I am digging more into this to know reason of failures.</div><div><br></div><div>Sincerely,</div><div>Vivek</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jun 26, 2016 at 10:05 PM, vivek pandya <span dir="ltr"><<a href="mailto:vivekvpandya@gmail.com" target="_blank">vivekvpandya@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">According to this <a href="http://llvm.org/docs/CodeGenerator.html#tail-call-section" target="_blank">http://llvm.org/docs/CodeGenerator.html#tail-call-section</a>, it seems that adding a new CC for the purpose of local function optimization seems a good idea because tail call optimization only takes place when both caller and callee have fastcc or GHC or HiPE calling convention.<span><font color="#888888"><div><br></div></font></span><div><span><font color="#888888">-Vivek</font></span><div><div><br><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jun 26, 2016 at 1:26 AM, vivek pandya <span dir="ltr"><<a href="mailto:vivekvpandya@gmail.com" target="_blank">vivekvpandya@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote"><div><div>On Sat, Jun 25, 2016 at 11:03 PM, vivek pandya <span dir="ltr"><<a href="mailto:vivekvpandya@gmail.com" target="_blank">vivekvpandya@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div>Hello LLVM Community,<br></div><div><br></div><div>To improve Interprocedural Register Allocation (IPRA) we are trying to force caller</div><div>saved registers for local functions (which has likage type local). To achive it</div><div>I have modified TargetFrameLowering::determineCalleeSaves() to return early for</div><div>function which satisfies if (F->hasLocalLinkage() && !F->hasAddressTaken()) and</div><div>also reflecting the fact that for local function there are no caller saved registers</div><div>I am also changing RegUsageInfoCollector.cpp to not to mark regiseters as callee</div><div>saved in RegMask due to CC with follwoing change in code:</div><div><br></div><div>if (!F->hasLocalLinkage() || F->hasAddressTaken()) {</div><div> const uint32_t *CallPreservedMask =</div><div> TRI->getCallPreservedMask(MF, MF.getFunction()->getCallingConv());</div><div> // Set callee saved register as preserved.</div><div> for (unsigned i = 0; i < RegMaskSize; ++i)</div><div> RegMask[i] = RegMask[i] | CallPreservedMask[i];</div><div> } </div><div><br></div><div>For more details please follow following link.</div><div><a href="https://groups.google.com/d/msg/llvm-dev/XRzGhJ9wtZg/bYFMzppXEwAJ" target="_blank">https://groups.google.com/d/msg/llvm-dev/XRzGhJ9wtZg/bYFMzppXEwAJ</a></div><div><br></div><div>Now consider following bug due to forcing caller saved registers for local function</div><div>when IPRA enable:</div><div><br></div><div>void makewt(int nw, int *ip, double *w) {</div><div> ...</div><div> bitrv2(nw, ip, w);</div><div>}</div><div><br></div><div>here bitrv2 is local fuction and for that when IPRA enable callee saved registers</div><div>are set to none. So for that function following is set of collbered register as</div><div>per regmaks collected by RegUsageInfoCollector pass.</div><div><br></div><div>Function Name : bitrv2</div><div>Clobbered Registers:</div><div>AH AL AX BH BL BP BPL BX CH CL CX DI DIL EAX EBP EBX ECX EDI EFLAGS ESI ESP RAX</div><div>RBP RBX RCX RDI RSI RSP SI SIL SP SPL R8 R9 R10 R11 R12 R13 R14 R15 R8B R9B R10B</div><div>R11B R12B R13B R14B R15B R8D R9D R10D R11D R12D R13D R14D R15D R8W R9W R10W R11W</div><div>R12W R13W R14W R15W</div><div><br></div><div>How ever caller of bitrv2, makewt has callee saved registers as per CC, but this</div><div>code results in segmentation fault when compliled with O1 because makewt has value</div><div>of *ip in R14 register and that is stored and restore by makewt at begining of call</div><div>but due to tail call optimization following code is generated and here bitrv2 does</div><div>not preserve R14 so whwn execution returns to main (which is caller of makewt)</div><div>value of *ip is gone from R14 (which sould not) and when main calls makewt again</div><div>then value of *ip (R14) is wrong and result into segmentation fault.</div><div><br></div><div>Assembly code of makewt:</div><div> _makewt:</div><div> ...</div><div> popq %rbx</div><div> popq %r12</div><div> popq %r13</div><div> popq %r14</div><div> popq %r15</div><div> popq %rbp</div><div> jmp _bitrv2 ## TAILCALL</div></div></blockquote><div><br></div></div></div><div>A very naive solution to this problem come to me is to convert above code to following:</div><div><br></div><div> _makewt:</div><div> ...</div><div> jmp _bitrv2 ## TAILCALL<br></div><span><div> popq %rbx</div><div> popq %r12</div><div> popq %r13</div><div> popq %r14</div><div> popq %r15</div><div> popq %rbp</div><div> </div></span><div>So that when _bitrv2 returns caller will over write callee saved register ( as per CC of that function ) to correct values.</div><div>I wanted to try it out but I am not able to find correct code where I can do that.</div><span><font color="#888888"><div>-Vivek</div></font></span><span><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div><br></div><div>There is one more case of faluire due to local function related optimization.</div><div>I am analysing that (sorry for taking more time but I am not much good at assembly).</div><div><br></div><div>I need some hints for how to solve this. If you feel some problem with my analyses</div><div>please let me know if you want me to send generated .s file and source .c file.</div><div><br></div><div>Sincerely,</div><div>Vivek</div></div>
</blockquote></span></div><br></div></div>
</blockquote></div><br></div></div></div></div></div>
</blockquote></div><br></div>
</div></blockquote></div><br></div></div></div></blockquote></div><br></div></div>