<div dir="ltr"><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Jan 14, 2019 at 5:44 PM Chandler Carruth <<a href="mailto:chandlerc@gmail.com">chandlerc@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr">On Mon, Jan 14, 2019 at 5:03 PM Mehdi AMINI <<a href="mailto:joker.eph@gmail.com" target="_blank">joker.eph@gmail.com</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Jan 14, 2019 at 4:51 PM Chandler Carruth <<a href="mailto:chandlerc@gmail.com" target="_blank">chandlerc@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="auto"><div><br><div class="gmail_quote"><div dir="ltr">On Mon, Jan 14, 2019, 15:59 Mehdi AMINI <<a href="mailto:joker.eph@gmail.com" target="_blank">joker.eph@gmail.com</a> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Jan 14, 2019 at 9:36 AM Chandler Carruth via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" rel="noreferrer" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr">While I'm very interested in the end result here, I have some questions that don't seem well answered yet around pointer subtraction...<div><br></div><div>First and foremost - how do you address correctness issues here? Because the subtraction `A - B` can escape/capture more things. Specifically, if one of `A` or `B` is escaped/captured, the subtraction can be used to escape or capture the other pointer. </div></div></blockquote><div><br></div><div>Isn't escaping supposed to work at the "address ranges" level and not at the pointer value?<br></div><div>I mean that if `A` or `B` is escaped/captured, then any pointer that is associated to the same memory range should be considered as "escaped", and thus the subtraction does not seem to leak anything more to me.</div></div></div></div></blockquote></div></div><div dir="auto"><br></div><div dir="auto">I believe this is true for subtracting "inbounds" (to borrow the gep terminology), but just as we support non inbounds GEP, we support non imbounds subtracting. There it seems like this does escape the other global. I know that in the past I've discussed this exact case with nlewycky and he believed that to be the case, so I suspect quite a bit of LLVM is written under this model. No idea what would be the impact of changing it beyond the ability to represent code like the example I gave earlier on the thread. </div></div></blockquote><div><br></div><div>That does not match my current reading of LangRef (admittedly shallow/imperfect): not having inbounds allows you to form pointers that are outside of the memory range, however the "result value may not necessarily be used to access memory though, even if it happens to point into allocated storage".</div><div>Also the "pointer aliasing rules" section mentions: "Any memory access must be done through a pointer value associated with an address range of the memory access, otherwise the behavior is undefined".</div></div></div></div></div></blockquote><div><br></div><div>I think there are still two issues here:</div><div><br></div><div>1) Escape of the address doesn't necessarily imply you can access the memory. It can still be an escaped address. It may not *alias* the memory, but that's a different question (confusingly). This means, for example, that a global may still be address-taken even though it cannot be accessed.</div></div></div></blockquote><div><br></div><div>I suspect we are out of the LangRef domain, but I'm surprised by this definition. Why is it useful to consider such address as "escaping"?</div><div>I'm not an expert on the literature, but I have always encountered the notion of escape analysis in relation with aliasing: it isn't the value of the pointer that is interesting to consider "escaped" but the memory it is pointing to.</div><div><br></div><div>-- </div><div>Mehdi</div><div><br></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div>2) This model does not play well with the way we manage things like CSE. If we prove bit equivalence of pointers we will (AFAIK) fold from one to the other even if one of them would make memory accesses well defined and the other would make them undefined.</div><div><br></div><div>Maybe #2 has been fixed? I would honestly be surprised -- it's been a long standing tension in compiler optimization that I'm aware of... Adding <a class="gmail_plusreply" id="gmail-m_1196558202554924771plusReplyChip-8" href="mailto:sanjoy@playingwithpointers.com" target="_blank">+Sanjoy Das</a> who I think has a lot of experience with these semantic parts of LLVM. Also <a class="gmail_plusreply" id="gmail-m_1196558202554924771plusReplyChip-9" href="mailto:gbiv@google.com" target="_blank">+George Burgess</a>.</div><div><br></div><div>But what I really want to see is a careful and thorough examination of this before we shift the representation of the IR to embed a fundamental reliance on this property.</div><div><br></div><div>For example: do we have a sanitizer that verifies these properties? Has anyone used that on significant application code?</div><div><br></div><div>-Chandler</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="auto"><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>So *some* of the conservative treatment is necessary. What is the plan to update all the analyses to remain correct? What correctness testing have you done?</div><div><br></div><div>Second - an intrinsic seems a poor fit here given the significance of this operation. We have an instruction that covers most pointer arithmetic (`getelementptr`), and I can imagine growing pointer subtraction, but it seems like it should be an instruction if we're going to have it. Based on the above, we will need to use it very often in analysis.</div><div><br></div><div><br></div><div>Regarding the instcombine, it should be very easy to keep loads and stores of pointers as pointer typed in instcombine. Likely just a missing case in the code I added/touched there.</div></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Jan 14, 2019 at 3:23 AM Juneyoung Lee via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" rel="noreferrer" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Hello all,</div><div><br></div><div>This is a proposal for reducing # of ptrtoint/inttoptr casts which are not</div><div>written by programmers but rather generated by LLVM passes.</div><div>Currently the majority of ptrtoint/inttoptr casts are generated by LLVM;</div><div>when compiling SPEC 2017 with LLVM r348082 (Dec 2 2018) with -O3,</div><div>the output IR contains 22,771 inttoptr instructions. However, when</div><div>compiling it with -O0, there are only 1048 inttoptrs, meaning that 95.4%</div><div>of them are generated by LLVM passes.</div><div><br></div><div>This trend is similar in ptrtoint instruction as well. When compiling SPEC 2017</div><div>with -O0, there are 23,208 ptrtoint instructions, but among them 22,016 (94.8%)</div><div>are generated by Clang frontend to represent pointer subtraction.</div><div>They aren't effectively optimized out because there are even more ptrtoints (31,721) after -O3.</div><div>This is bad for performance because existence of ptrtoint makes analysis return conservative</div><div>result as a pointer can be escaped through the cast.</div><div>Memory accesses to a pointer came from inttoptr is assumed</div><div>to possibly access anywhere, therefore it may block</div><div>store-to-load forwarding, merging two same loads, etc.</div><div><br></div><div>I believe this can be addressed by applying two patches - first one is representing pointer subtraction with a dedicated intrinsic function, llvm.psub, and second one is disabling InstCombine transformation</div><div><br></div><div>    %q = load i8*, i8** %p1</div><div>    store i8* %q, i8** %p2</div><div>=></div><div>  %1 = bitcast i8** %p1 to i64*</div><div>  %q1 = load i64, i64* %1, align 8</div><div>  %2 = bitcast i8** %p2 to i64*</div><div>  store i64 %q1, i64* %2, align 8</div><div><br></div><div>This transformation can introduce inttoptrs later if loads are followed (<a href="https://godbolt.org/z/wsZ3II" rel="noreferrer" target="_blank">https://godbolt.org/z/wsZ3II</a> ). Both are discussed in <a href="https://bugs.llvm.org/show_bug.cgi?id=39846" rel="noreferrer" target="_blank">https://bugs.llvm.org/show_bug.cgi?id=39846</a> as well.</div><div>After llvm.psub is used & this transformation is disabled, # of inttoptrs decreases from 22,771 to 1,565 (6.9%), and # of ptrtoints decreases from 31,721 to 7,772 (24.5%).</div><div><br></div><div>I'll introduce llvm.psub patch first.</div><div><br></div><div><br></div><div>--- Adding llvm.psub ---</div><div><br></div><div>By defining pointer subtraction intrinsic, we can get performance gain because it gives more undefined behavior than just subtracting two ptrtoints.</div><div><br></div><div>Patch <a href="https://reviews.llvm.org/D56598" rel="noreferrer" target="_blank">https://reviews.llvm.org/D56598</a> adds llvm.psub(p1,p2) intrinsic function, which subtracts two pointers and returns the difference. Its semantic is as follows.</div><div>If p1 and p2 point to different objects, and neither of them is based on a pointer casted from an integer, `llvm.psub(p1, p2)` returns poison. For example,</div><div><br></div><div>%p = alloca</div><div>%q = alloca</div><div>%i = llvm.psub(p, q) ; %i is poison</div><div><br></div><div>This allows aggressive escape analysis on pointers. Given i = llvm.psub(p1, p2), if neither of p1 and p2 is based on a pointer casted from an integer, the llvm.psub call does not make p1 or p2 escape. (<a href="https://reviews.llvm.org/D56601" rel="noreferrer" target="_blank">https://reviews.llvm.org/D56601</a> )</div><div><br></div><div>If either p1 or p2 is based on a pointer casted from integer, or p1 and p2 point to a same object, it returns the result of subtraction (in bytes); for example,</div><div><br></div><div>%p = alloca</div><div>%q = inttoptr %x</div><div>%i = llvm.psub(p, q) ; %i is equivalent to (ptrtoint %p) - %x</div><div><br></div><div>`null` is regarded as a pointer casted from an integer because</div><div>it is equivalent to `inttoptr 0`.</div><div><br></div><div>Adding llvm.psub allows LLVM to utilize significant portion of ptrtoints & reduce a portion of inttoptrs. After llvm.psub is used, when SPECrate 2017 is compiled with -O3, # of inttoptr decreases to ~13,500 (59%) and # of ptrtoint decreases to ~14,300 (45%).</div><div><br></div><div>To see the performance change, I ran SPECrate 2017 (thread # = 1) with three versions of LLVM, which are r313797 (Sep 21, 2017), LLVM 6.0 official, and r348082 (Dec 2, 2018). </div><div>Running r313797 shows that 505.mcf_r has consistent 2.0% speedup over 3 different machines (which are i3-6100, i5-6600, i7-7700). For LLVM 6.0 and r348082, there's neither consistent speedup nor slowdown, but the average speedup is near 0. I believe there's still a room of improvement because there are passes which are not aware of llvm.psub.</div><div><br></div><div>Thank you for reading this, and any comment is welcome.</div><div><br></div><div>Best Regards,</div><div>Juneyoung Lee</div></div></div></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" rel="noreferrer" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" rel="noreferrer" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div></div>
</blockquote></div></div></div>
</blockquote></div></div></div></div>
</blockquote></div></div>
</blockquote></div></div></div>