<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jul 21, 2017 at 5:00 PM, Sean Silva <span dir="ltr"><<a href="mailto:chisophugis@gmail.com" target="_blank">chisophugis@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div><div class="h5"><div><br><div class="gmail_extra"><br><div class="gmail_quote">On Jul 21, 2017 3:24 PM, "Peter Lawrence" <<a href="mailto:peterl95124@sbcglobal.net" target="_blank">peterl95124@sbcglobal.net</a>> wrote:<br type="attribution"><blockquote class="m_-791613647157136467quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><div class="m_-791613647157136467quoted-text"><blockquote type="cite"><div>On Jul 20, 2017, at 11:22 AM, David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>> wrote:</div><br class="m_-791613647157136467m_-996355277070279085Apple-interchange-newline"><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br><br><div class="gmail_quote"><div dir="ltr">On Wed, Jul 19, 2017 at 10:17 AM Peter Lawrence 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: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">Chandler,<div>               The only thing David made clear that wasn’t already clear</div><div>is that he believes UB to be “comparatively rare”, which is in agreement</div><div>with what Hal already said which is that he does not expect deleting</div><div>UB will be of benefit to for example SPEC benchmarks.</div><div><br></div><div>Given that it is “comparatively rare”, why all the effort to delete it ? </div></div></blockquote><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>And why make deleting it the default rather than warning about it ? </div></div></blockquote><div><br>There seems to be some confusion/misunderstanding here. My best understanding is that when David said this:<br><br>"<span style="color:rgb(33,33,33);font-size:13px">The cases where the compiler can statically prove that undefined behaviour is present are comparatively rare."<br></span><br>What he was referring to/describing was a contrast with the optimizations described prior to that.<br><br>It's something like this:<br><br>UB-based optimizations don't prove UB is present - they optimize on the assumption that it is not present due to some unproven (by the compiler, but assumed to be known by the developer) invariants in the program.<br><br>Think about a simple case like array bounds - the compiler emits an unconditional load to the memory because it assumes the developer correctly validated the bounds or otherwise constructed so that out of bounds indexes never reach that piece of code. This is quite common - that UB is assumed to not happen, and the compiler optimizes on this fact.<br><br>What is less common, is for the compiler to be able to (in reasonable time) prove that UB /does/ happen (in many cases this would require complex interprocedural analysis - the array is defined in one function, maybe with a complex dynamic bound, then passed to another function and indexed using a non-trivial dynamic expression... statically proving that to be true or false is complex/expensive and so basically not done by any compiler - so any cases that are caught by the compiler are relatively trivial ("oh, you declared a const null pointer value, then dereferenced it within the same function", etc) & so don't happen very often (because they're also fairly obvious to developers too))<br><br>Does that help explain the difference/distinction being drawn here?<br></div></div></div></div></blockquote><div><br></div><div><br></div><div><br></div></div><div>Dave,</div><div>          perhaps you missed these parts of the discussion</div><div><br></div><div><div>Here is the definition, acknowledged by Hal, of what we’re doing</div><div class="m_-791613647157136467quoted-text"><div><br></div><div><blockquote type="cite">1. Sometimes there are abstraction penalties in C++ code<br>2. That can be optimized away after template instantiation, function inlining, etc<br>3. When they for example exhibit this pattern<br><span class="m_-791613647157136467m_-996355277070279085Apple-tab-span" style="white-space:pre-wrap"> </span>if (A) {<br><span class="m_-791613647157136467m_-996355277070279085Apple-tab-span" style="white-space:pre-wrap">   </span><span class="m_-791613647157136467m_-996355277070279085Apple-tab-span" style="white-space:pre-wrap">     </span>stuff;<br><span class="m_-791613647157136467m_-996355277070279085Apple-tab-span" style="white-space:pre-wrap">     </span>} else {<br><span class="m_-791613647157136467m_-996355277070279085Apple-tab-span" style="white-space:pre-wrap">   </span><span class="m_-791613647157136467m_-996355277070279085Apple-tab-span" style="white-space:pre-wrap">     </span>other stuff including “undefined behavior”;<br><span class="m_-791613647157136467m_-996355277070279085Apple-tab-span" style="white-space:pre-wrap">    </span>}<br>4. Where the compiler assumes “undefined behavior” doesn’t actually happen because<br>  In the C language standard it is the users responsibility to avoid it<br>5. Therefore in this example the compiler can a) delete the else-clause<br>   b) delete the if-cond, c) assume A is true and propagate that information<br></blockquote><div><br></div></div></div></div><div><br></div><div>And, here’s the optimization that according to Sean we’re using to delete UB</div><div><br></div><div><div><div><blockquote type="cite">[ … ]</blockquote><blockquote type="cite"><br></blockquote></div></div><blockquote type="cite"><span style="float:none;display:inline!important">In other words, if we can prove  "when program statement A executes then</span><br><span style="float:none;display:inline!important">program statement B is guaranteed to execute and have undefined behavior"</span><br><span style="float:none;display:inline!important">then we can assume that program statement A is never executed.</span><br><br><span style="float:none;display:inline!important">In particular, deleting program statement A is a correct transformation.</span><br><span style="float:none;display:inline!important">Deleting program statement B itself is a special case of this (when A = B).</span><br><br><span style="float:none;display:inline!important">And yes, this does include everything up to and including `main`,</span><br><span style="float:none;display:inline!important">intraprocedurally and interprocedurally.</span><br><br><br><span style="float:none;display:inline!important">[ … ]</span></blockquote><blockquote type="cite"><br><span style="float:none;display:inline!important">-- Sean Silva</span></blockquote><div><span style="float:none;display:inline!important"><br></span></div><div><span style="float:none;display:inline!important"><br></span></div></div><div>This is entirely a separate issue from what Dan Gohman did to optimize sign extension</div><div>of i32 induction variables out of loops for LP64 target machines, where the optimization</div><div>is justified based on “the assumption that UB does not happen”, and no actual UB</div><div>exists either statically or dynamically.</div><div></div></div></div></blockquote></div></div></div><div dir="auto"><br></div></div></div><div dir="auto">Sorry, the way I phrased this in terms of program statements may have made this unclear, but this is precisely the particular case A=B that I mentioned. In this case, A=B="the induction variable increment" </div></div></blockquote><div><br></div><div>This should have said A=B="the induction variable increments and overflows". Describing it in terms of general program properties instead of "program statements" is more mathematically precise in this case (talking about coarse-granularity things like statements is restrictive for the induction variable widening case where you want to talk about a statement and something dynamically happening in that statement rather than just its liveness).</div><div><br></div><div>-- Sean Silva</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div dir="auto">and we use that to deduce that the statement will not execute and overflow, which is what justifies the widening.</div><div dir="auto"><br></div><div dir="auto">Notice that I mentioned that deleting code is only a particular case. In the general case we deduce that dynamically something simply does not happen, which is what we do in order to prove that induction variable widening is safe (overflow cannot happen).</div><div dir="auto"><br></div><div dir="auto">There is nothing special about induction variable widening with respect to UB. It is justified by applying the same principles as all other UB-related transformations.</div><div dir="auto"><br></div><div dir="auto"><br><span style="font-family:sans-serif">Briefly, there is only one axiom in the compiler writer's toolbox w.r.t. UB and that is "the input program does not execute UB". Everything else is derived from that by pure logical reasoning. Does that make sense? Can you see how all the descriptions I gave above are derivable from that axiom?</span><br></div><div dir="auto"><br></div><div dir="auto">The common application of this is that we can assume any program property P whatsoever (not just liveness, but variable ranges, etc.) if we can prove that the program would execute UB should that property P fail to hold.</div><span class="HOEnZb"><font color="#888888"><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto">-- Sean Silva</div></font></span><div><div class="h5"><div dir="auto"><br></div><div dir="auto"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="m_-791613647157136467quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div><br></div><div>But when it comes to actual provable UB the plan is to delete it.</div><div>On that there is no confusion, and there is no mis-understanding.</div><font color="#888888"><div><br></div><div><br></div><div>Peter Lawrence.</div></font><div class="m_-791613647157136467elided-text"><div><br></div><div><br></div><div><br></div><br><blockquote type="cite"><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div class="gmail_quote"><div><br>- Dave</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>Peter</div></div><div style="word-wrap:break-word"><div><br></div><div><br><div><div><blockquote type="cite"><div>On Jul 13, 2017, at 2:15 PM, Chandler Carruth <<a href="mailto:chandlerc@gmail.com" target="_blank">chandlerc@gmail.com</a>> wrote:</div><br class="m_-791613647157136467m_-996355277070279085m_5407134396719276379Apple-interchange-newline"><div><div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Thu, Jul 13, 2017 at 5:13 PM Peter Lawrence 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:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">David,<br>         <span class="m_-791613647157136467m_-996355277070279085Apple-converted-space"> </span>Here is the definition accepted by Hal of what we’re doing<br><br>> 1. Sometimes there are abstraction penalties in C++ code<br>> 2. That can be optimized away after template instantiation, function inlining, etc<br>> 3. When they for example exhibit this pattern<br>>       if (A) {<br>>               stuff;<br>>       } else {<br>>               other stuff including “undefined behavior”;<br>>       }<br>> 4. Where the compiler assumes “undefined behavior” doesn’t actually happen because<br>>    In the C language standard it is the users responsibility to avoid it<br>> 5. Therefore in this example the compiler can a) delete the else-clause<br>>     b) delete the if-cond, c) assume A is true and propagate that information<br><br><br><br>We are actively deleting undefined behavior, and the question is why<br>given that doing so potentially masks a real source code bug.<br>At the very least deleting undefined behavior should not be the default.<br></blockquote><div><br></div><div>You are asserting this (again), but others have clearly stated that they disagree. David gave detailed and clear reasons why. Continuing to re-state positions is not productive.</div><div><br></div><div>-Chandler</div></div></div></div></blockquote></div><br></div></div></div>______________________________<wbr>_________________<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/<wbr>mailman/listinfo/llvm-dev</a></blockquote></div></div></div></blockquote></div></div><br></div></blockquote></div><br></div></div></div></div></div>
</blockquote></div><br></div></div>