<div dir="ltr"><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Sep 14, 2021 at 1:49 PM Mahesha S 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:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);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 style="font-family:"trebuchet ms",sans-serif;font-size:small">Hi-</div><div style="font-family:"trebuchet ms",sans-serif;font-size:small"><br></div><div style="font-family:"trebuchet ms",sans-serif;font-size:small">Theoretically, alloca instructions can appear anywhere within the function body, but practically, it is better to keep them as a one cluster at the beginning of the entry basic block for better optimization opportunities. </div><div style="font-family:"trebuchet ms",sans-serif;font-size:small"><br></div><div style="font-family:"trebuchet ms",sans-serif;font-size:small">And, I think the front-end code gen phase mostly succeeds in clustering all alloca instructions at the beginning of the entry basic block. Nevertheless, it is not guaranteed always. One more case is inlining - which may leave alloca instructions other than in entry block.</div></div></div></div></div></div></div></div></blockquote><div><br></div><div>The inliner explicitly scans for allocas in the callee entry block and moves them to the caller's entry block.</div><div><br></div><div>In other words: if you're seeing surprising alloca's in the middle of a function (i.e., _not_ VLAs like Roman mentioned), please first do a root-cause into why those allocas end up where they do. If you're unfamiliar with LLVM, such root-causing can be done with tools like the -print-before-all command-line flags, which give you the IR between every pass. You can go through that log to figure out where allocas first appear in surprising places.</div><div><br></div><div>Cheers,</div><div>Nicolai<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 dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div style="font-family:"trebuchet ms",sans-serif;font-size:small"><br></div><div style="font-family:"trebuchet ms",sans-serif;font-size:small">So, I am thinking of adding a new late IR pass (after inlining) which deliberately moves all allocas which appear elsewhere to the beginning of the entry block.</div><div style="font-family:"trebuchet ms",sans-serif;font-size:small"><br></div><div style="font-family:"trebuchet ms",sans-serif;font-size:small">Now my questions are:</div><div style="font-family:"trebuchet ms",sans-serif;font-size:small"><br></div><div style="font-family:"trebuchet ms",sans-serif;font-size:small">(1) Can we neatly implement this pass? For example, what would happen if an alloca which appears within a loop is moved to the entry block?</div><div style="font-family:"trebuchet ms",sans-serif;font-size:small">(2) Is there a possibility where some other later pass(es) other than inlining also can result in inserting alloca elsewhere, so the new proposed pass needs to be invoked multiple times?</div><div style="font-family:"trebuchet ms",sans-serif;font-size:small"><br></div><div style="font-family:"trebuchet ms",sans-serif;font-size:small">The main reason for the above ask is - I have my own doubt that if moving alloca to the entry block is such an easy task why not anyone from the community did not attempt it till date considering the importance of keeping the allocas at the beginning of the entry block for better optimization opportunities. If there are genuine reason for that, it is better to know it instead of blindly attempting to implement the pass.</div><div style="font-family:"trebuchet ms",sans-serif;font-size:small"><br></div><div><div dir="ltr"><div dir="ltr"><font face="trebuchet ms, sans-serif">Thanks,</font><div><font face="trebuchet ms, sans-serif">Mahesha</font></div></div></div></div></div></div></div></div></div></div></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="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div><br clear="all"><br>-- <br><div dir="ltr" class="gmail_signature">Lerne, wie die Welt wirklich ist,<br>aber vergiss niemals, wie sie sein sollte.</div></div>