<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Mon, Jul 31, 2017 at 7:40 AM Peter Lawrence <<a href="mailto:peterl95124@sbcglobal.net">peterl95124@sbcglobal.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Dave,<div> Dead code elimination is generally done in a pass called dead code elimination,</div><div>Can you give concrete examples why the same would not be true for UB code elimination ?</div></div></blockquote><div><br>I haven't actually looked at how optimizations on the basis of the code being UB-free cause code to be eliminated - I'd hazard a guess that many optimizations would replace certain code with undef or unreachable not only a specific pass intended for that purpose. At least I'm pretty sure that's how it is today (I know we don't have a "UB code elimination" pass - it comes out as a result of many other passes making steps like mutation to undef/unreachable)<br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Yes, speculatively hoisting code requires it to be UB-free, but that has nothing to do with<br></div><div>UBCE deleting entire blocks of code because of the existence of UB. </div></div></blockquote><div><br>There's more similarity there than might first appear - if we assume the code is UB-free, then when an unconditional store to a null pointer is found we assume that must be dynamically unreachable (& the only reason it came up is due to inlining, etc - not that the user wrote/intended to store to null - maybe this particular call site got inlined and now what was a dynamic property (the user always called that function with a parameter that ensured the program didn't go down that code path and load from the pointer - but other callers do, so that's fine and good and correct) is a static property of this particular version/post-inlining state so the compiler can trivially observe it and replace it with unreachable)<br><br>For example:<br><br><div><font face="monospace">inline __attribute__((always_inline)) void f1(bool b, int *i) {</font></div><div><font face="monospace"> if (b)</font></div><div><font face="monospace"> *i = 3;</font></div><div><font face="monospace">}<br><br>bool f2(int*);</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">void f3() {</font></div><div><font face="monospace"> int *x = nullptr;</font></div><div><font face="monospace"> f1(f2(x), x);</font></div><div><font face="monospace">}</font></div> <br>For the sake of argument, you could imagine that this code arose from perfectly reasonable user code - the user knows that 'f2' always returns false for a null pointer because it says so in the name (in the user code, with real names, etc).<br><br>& it's actually Jump Threading that makes the code in the 'if' block (once inlined into f3) unreachable:<br><br><div><font face="monospace">*** IR Dump Before Jump Threading ***</font></div><div><font face="monospace">; Function Attrs: uwtable</font></div><div><font face="monospace">define void @_Z2f3v() local_unnamed_addr #1 {</font></div><div><font face="monospace">entry:</font></div><div><font face="monospace"> %call = call zeroext i1 @_Z2f2Pi(i32* null)</font></div><div><font face="monospace"> br i1 %call, label %if.then.i, label %_Z2f1bPi.exit</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">if.then.i: ; preds = %entry</font></div><div><font face="monospace"> store i32 3, i32* null, align 4, !tbaa !2</font></div><div><font face="monospace"> br label %_Z2f1bPi.exit</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">_Z2f1bPi.exit: ; preds = %entry, %if.then.i</font></div><div><font face="monospace"> ret void</font></div><div><font face="monospace">}</font></div><div><font face="monospace">*** IR Dump After Jump Threading ***</font></div><div><font face="monospace">; Function Attrs: uwtable</font></div><div><font face="monospace">define void @_Z2f3v() local_unnamed_addr #1 {</font></div><div><font face="monospace">entry:</font></div><div><font face="monospace"> %call = call zeroext i1 @_Z2f2Pi(i32* null)</font></div><div><font face="monospace"> br i1 %call, label %if.then.i, label %_Z2f1bPi.exit</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">if.then.i: ; preds = %entry</font></div><div><font face="monospace"> call void @llvm.trap()</font></div><div><font face="monospace"> unreachable</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">_Z2f1bPi.exit: ; preds = %entry</font></div><div><font face="monospace"> ret void</font></div><div><font face="monospace">}</font></div><div><br></div>Sure, it didn't remove the block, but it certainly removed the code. Nice enough to replace it with a trap, though. (so, yeah, not a perfect example - but you get the idea, all sorts of optimizations might decide to simplify code on the basis that UB can't actually happen)<br><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>The former requires</div><div>an analysis proving UB-absense, the later requires an analysis proving UB-presence. But it</div><div>isn’t logical to say you must delete UB-presence to enable proving UB-absense, because</div><div>in the real world after programs have passed static-analysis there won’t be any UB-presence</div><div>to delete.</div></div></blockquote><div><br></div><div>I don't understand this last bit, sorry. Without knowledge of the whole program I can't prove that the example above will never execute UB - but the C++ language lets me, the compiler, assume that that won't happen & optimize on that basis. No local static analysis could prove it - but the assumption helps improve the code. (& even if we had the whole program, some invariants would be difficult/impossible to practically prove - programmer indexes into an array based on the number of elementsn in a hash table - pretty darn difficult to statically analyze the whole probing hash table system, and the dynamic logic that populates it, to prove that only N elements will ever end up in the hash table, and thus using its size as an index into an array of N elements is safe - but we can assume, so there's no need to synthesize bounds checks around the array access, etc)</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><br></div><div><br></div><div>Peter Lawrence.</div></div><div style="word-wrap:break-word"><div><br></div><div><br><div><blockquote type="cite"><div>On Jul 27, 2017, at 7:45 PM, David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>> wrote:</div><br class="m_9100368324655226475Apple-interchange-newline"><div><div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Thu, Jul 27, 2017 at 7:30 PM Peter Lawrence <<a href="mailto:peterl95124@sbcglobal.net" target="_blank">peterl95124@sbcglobal.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Dave,<div> The way I see it there should be just one pass that implements</div><div>deleting UB (maybe it would come to be called UBCE), and that one pass </div><div>should have a command line option simply for the reason than all passes</div><div>should have one.</div></div></blockquote><div><br>I would hazard a guess that would be very difficult to implement the same functionality (as the compiler has today) efficiently with that approach - many optimizations would be inhibited from making progress because they couldn't prove the code didn't have UB, etc.<br><br>Generally, even deleting UB code isn't approached directly, but may come about as a consequence of various other steps the compiler is taking.<br><br>- Dave<br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><br></div><div><br></div><div>Peter Lawrence.</div></div><div style="word-wrap:break-word"><div><br></div><div><br><div><blockquote type="cite"><div>On Jul 26, 2017, at 10:02 PM, David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>> wrote:</div><br class="m_9100368324655226475m_2141608741726357655Apple-interchange-newline"><div><div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Wed, Jul 26, 2017 at 9:23 PM Peter Lawrence <<a href="mailto:peterl95124@sbcglobal.net" target="_blank">peterl95124@sbcglobal.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">David,<div> -fsanitize=undefined sounds great, but is not quite what I want.</div><div><br></div><div><br></div><div>I recently ran into a problem with <span style="background-color:rgb(255,255,255)">"CodeGen/MachineSink.cpp” [*], for a target</span></div><div><span style="background-color:rgb(255,255,255)">that has to expand Select into control flow.</span></div><div><span style="background-color:rgb(255,255,255)">The original IR had two select in a row that were based on the same condition,</span></div><div><span style="background-color:rgb(255,255,255)">so the CMP that sets the FLAGS reg in the second select was MCSE’ed to the</span></div><div><span style="background-color:rgb(255,255,255)">earlier CMP in the first select, so here we see the second Select without a CMP:</span></div><div><span style="background-color:rgb(255,255,255)"><br></span></div><div><div style="margin:0px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">BB#10: derived from LLVM BB %for.body.5</span></div><div style="margin:0px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures"> Predecessors according to CFG: BB#3 BB#9</span></div><div style="margin:0px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures"> %vreg49<def> = PHI %vreg47, <BB#9>, %vreg48, <BB#3>; DataRegs:%vreg49,%vreg47,%vreg48</span></div><div style="margin:0px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures"> //// <=== this SLLI clobbers FLAGS <============</span></div><div style="margin:0px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures"> %vreg46<def> = SLLI %vreg5, 1, %FLAGS<imp-def,dead>; DataRegs:%vreg46,%vreg5</span></div><div style="margin:0px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures"> BCC 2, <BB#12>, %FLAGS<imp-use></span></div><div style="margin:0px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures"> Successors according to CFG: BB#11 BB#12</span></div></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div><span style="background-color:rgb(255,255,255)"><br></span></div><div><span style="background-color:rgb(255,255,255)">The problem is that Machine Code Sinking put an “SLLI"</span><span style="background-color:rgb(255,255,255)"> instruction, that</span></div><div><span style="background-color:rgb(255,255,255)">modifies the FLAGS registers, in between the CMP and the BCC.</span></div><div><span style="background-color:rgb(255,255,255)"><br></span></div><div><span style="background-color:rgb(255,255,255)">The way I was able to work around this problem was to add</span></div><div><span style="background-color:rgb(255,255,255)">a command line option to “MachineSink.cpp” that defaults to false,</span></div><div><span style="background-color:rgb(255,255,255)">and add a check in </span>its runOnMachineFunction() to omit this pass.</div><div><br></div><div><br></div><div>But I should not have had to, every FunctionPass and MachineFunctionPass</div><div>should have a name and a command line option to disable it by name.</div><div><br></div><div>Other compilers I’ve worked on have had such options, and I use them to</div><div>track down compiler bugs. In this case I instead had to "—debug-after-all"</div><div>and very tediously search through thousands of lines of output to locate</div><div>this bug.</div><div><br></div><div><br></div><div>So I hope you can see where I’m coming from, the pass that deletes UB</div><div>should be no different, I should be able to disable it from the command line</div><div>as a matter of course.</div><div><br></div><div>Thoughts ?</div></div></blockquote><div><br>These things seem like distinct goals to me - debuggability of the compiler and dealing with UB. I think -fsanitize=undefined is a pretty good way to help users deal with, diagnose, and act on UB. It isolates the issue - rather than having all optimizations have ways of switching themselves off when part of their analysis relies on UB - instead the optimizations rely on the IR definitions to make valid transformations, and it's a separate pass in the frontend (clang) that can add extra checks to avoid UB (& diagnose it as such, if that's the best thing to do).<br><br>One could make an IR version of UBSan, that could take some IR and add null checks around every load/store, all the other things UBSan does. But I wouldn't expect that would be a priority for anyone to build (as it's more a compiler developer tool at that point - smaller audience, etc).<br><br>For your debugging situation - bugpoint can slice & dice the pass list to try to reduce both the set of code being optimized, and the particular optimization that seems to be causing the problem. That might be worth a shot for you in cases like this?<br><br>Otherwise I find print-after-all or the like to be pretty handy. Usually I reduce the input code first (with something like creduce or delta) so the IR isn't massive/hard to read. (bugpoint can help with that reduction as well)<br><br>- Dave<br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><br></div><div><br></div><div>Peter.</div><div><br></div><div><br></div><div>[* I haven’t reported this as a bug yet because I’m on 3.7.1, and haven’t had time</div><div> to replicate it in 4.0.1, but should be able to within a month. My target resembles</div><div> MSP430, so I’ll try to replicate it for that target in 4.0.1 ]</div></div><div style="word-wrap:break-word"><div><br></div><div><br></div><div><br><div><blockquote type="cite"><div>On Jul 24, 2017, at 9:08 AM, David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>> wrote:</div><br class="m_9100368324655226475m_2141608741726357655m_-1088913725511589707Apple-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 Mon, Jul 24, 2017 at 9:02 AM Peter Lawrence <<a href="mailto:peterl95124@sbcglobal.net" target="_blank">peterl95124@sbcglobal.net</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"><div><blockquote type="cite"><div>On Jul 21, 2017, at 10:55 PM, Mehdi AMINI <<a href="mailto:joker.eph@gmail.com" target="_blank">joker.eph@gmail.com</a>> wrote:</div><br class="m_9100368324655226475m_2141608741726357655m_-1088913725511589707m_-3161789555371559420Apple-interchange-newline"><div><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">2017-07-21 22:44 GMT-07:00 Peter Lawrence<span class="m_9100368324655226475m_2141608741726357655m_-1088913725511589707Apple-converted-space"> </span><span dir="ltr"><<a href="mailto:peterl95124@sbcglobal.net" target="_blank">peterl95124@sbcglobal.net</a>></span>:<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"><span style="font-size:14px">Mehdi,</span><div><span style="font-size:14px"> Hal’s transformation only kicks in in the *presence* of UB</span></div></div></blockquote><div><br></div><div>No, sorry I entirely disagree with this assertion: I believe we optimize program where there is no UB. We delete dead code, code that never runs, so it is code that does not exercise UB.</div></div></div></div></div></blockquote><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br></div></div></div></div></blockquote><div><br></div></div></div><div style="word-wrap:break-word"><div><div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">Mehdi,</span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures"> I had to read that sentence several times to figure out what the problem</span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">is, which is sloppy terminology on my part</span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:16px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">Strictly speaking the C standard uses “undefined behavior” to describe what</span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">happens at runtime when an “illegal” construct is executed. I have been using</span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">“undefined behavior” and UB to describe the “illegal” construct whether it is</span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">executed or not.</span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:16px"><span style="font-variant-ligatures:no-common-ligatures"> </span><br class="m_9100368324655226475m_2141608741726357655m_-1088913725511589707m_-3161789555371559420webkit-block-placeholder"></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">Hence I say “Hal’s transform is triggered by UB”, when I should be saying</span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">“Hal’s transformation is triggered by illegal IR”.</span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:16px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">All I can say is I’m not the only one being sloppy, what started this entire </span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">conversation is the paper titled “Taming Undefined Behavior in LLVM”, while</span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">the correct title would be “Taming Illegal IR in LLVM”. (I think we are all</span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">pretty confident that LLVM itself is UB-free, or at least we all hope so :-).</span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:16px"><span style="font-variant-ligatures:no-common-ligatures"></span></div></div><div>I believe you are being sloppy when you say "we optimize program </div><div>where there is no UB”, because I believe you mean "we optimize program </div><div>under the assumption that there is no UB”. In other words we recognize</div><div>“Illegal” constructs and then assume they are unreachable, and delete </div><div>them, even when we can’t prove by any other means that they are</div><div>unreachable. We don’t know that there is no (runtime) UB, we just assume it.</div></div></div><div style="word-wrap:break-word"><div><div><br></div><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>The example Hal showed does not exhibit UB, it is perfectly valid according to the standard.</div><div><br></div></div></div></div></div></blockquote><div><br></div></div></div><div style="word-wrap:break-word"><div><div>Whether it exhibits UB at runtime or not is not the issue, the issue is what </div><div>a static analyzer or compiler can tell before runtime, see below</div></div></div><div style="word-wrap:break-word"><div><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> <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"><div><span style="font-size:14px">, and</span></div><div><span style="font-size:14px">it does not matter how that UB got there, whether by function inlining</span></div><div><span style="font-size:14px">or without function inlining.</span></div><div><span style="font-size:14px"><br></span></div><div><span style="font-size:14px">The problem with Hal’s argument is that the compiler does not have</span></div><div><span style="font-size:14px">a built in ouija board with which it can conjure up the spirit of the</span></div><div><span style="font-size:14px">author of the source code and find out if the UB was intentional</span></div><div><span style="font-size:14px">with the expectation of it being deleted, or is simply a bug.</span></div><div><span style="font-size:14px">Function inlining does not magically turn a bug into not-a-bug, nor</span></div><div><span style="font-size:14px">does post-inlining simplification magically turn a bug into not-a-bug.</span></div><div><span style="font-size:14px"><br></span></div><div><span style="font-size:14px">Let me say it again: if the compiler can find this UB (after whatever</span></div><div><span style="font-size:14px">optimizations it takes to get there) then the static analyzer must</span></div><div><span style="font-size:14px">be able to do the same thing, forcing the programmer to fix it</span></div><div><span style="font-size:14px">rather than have the compiler optimize it.</span></div></div></blockquote><div><br></div><div>This is again incorrect: there is no UB in the program, there is nothing the static analyzer should report.</div></div></div></div></div></blockquote><div><br></div><div><br></div></div></div><div style="word-wrap:break-word"><div><div>Hal’s example starts with this template</div><div><br></div><div><div><blockquote type="cite" style="background-color:rgb(255,255,255)"><div><div>template <typename T></div><div>int do_something(T mask, bool cond) {</div><div> if (mask & 2)</div><div> return 42;</div><div><br></div><div> if (cond) {</div><div> T high_mask = mask >> 48; // UB if sizeof(T) < 8, and cond true</div><div> if (high_mask > 5)</div><div> do_something_1(high_mask);</div><div> else</div><div> do_something_2();</div><div> }</div><div><br></div><div> return 0;</div><div>}</div></div></blockquote></div><div><div></div></div></div><div><br></div><div>Which is then instantiated with T = char,</div><div>and where it is impossible for either a static analyzer or a </div><div>compiler to figure out and prove that ‘cond’ is always false.</div><div><br></div><div>Hence a static analyzer issues a warning about the shift,</div><div>while llvm gives no warning and instead optimizes the entire</div><div>if-statement away on the assumption that it is unreachable.</div><div><br></div><div>Yes a static analyzer does issue a warning in this case.</div><div><br></div><div><br></div><div>This is not the only optimization to be based on assumption</div><div>rather than fact, for example type-based-alias-analysis is</div><div>based on the assumption that the program is free of this sort</div><div>of aliasing. The difference is that a user can disable TBAA</div><div>and only TBAA if a program seems to be running incorrectly </div><div>when optimized and thereby possibly track down a bug, but</div><div>so far there is no command line option to disable UB-based-</div><div>analysis (or ‘illegal-IR-based” :-), but there really needs to be.</div><div><br></div><div>Do we at least agree on that last paragraph ?</div></div></div></blockquote><div><br>We likely agree it's good to have tools to help developers identify/diagnose UB in their programs. And we have that: -fsanitize=undefined (not only does it effectively disable many UB-based optimizations (because it makes them not undefined - by conditionalizing the code to check that UB isn't reached, as such) - it even provides pretty diagnostics (of course you can't actually continue running the program - if the line after the diagnostic will dereference a null pointer - there's no non-null pointer we can magic-up, so execution must stop))<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"><div><div><br></div><div><br></div><div>Peter Lawrence.</div><div><br></div><div><br></div><div><br></div></div></div><div style="word-wrap:break-word"><div><br></div><div><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br></div><div>The compile is still able to delete some code, because of breaking the abstraction through inlining or template instantiation for example (cf Hal example).</div><div><br></div><div>-- </div><div>Mehdi</div><div><br></div><div> </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><span style="font-size:14px"><br></span></div><div><span style="font-size:14px">Or, to put it another way: there is no difference between a compiler</span></div><div><span style="font-size:14px">and a static analyzer [*]. So regardless of whether it is the compiler or</span></div><div><span style="font-size:14px">the static analyzer that finds any UB, the only rational thing to do with</span></div><div><span style="font-size:14px">it is report it as a bug.</span></div><div><span style="font-size:14px"><br></span></div><div><span style="font-size:14px"><br></span></div><div><span style="font-size:14px">Peter Lawrence.</span></div><div><span style="font-size:14px"><br></span></div><div><span style="font-size:14px"><br></span></div><div><span style="font-size:14px">[* in fact that’s one of the primary reasons Apple adopted llvm, to use</span></div><div><span style="font-size:14px"> <span class="m_9100368324655226475m_2141608741726357655m_-1088913725511589707Apple-converted-space"> </span>It as a base for static analysis]</span></div><div><div class="m_9100368324655226475m_2141608741726357655m_-1088913725511589707m_-3161789555371559420h5"><div><span style="font-size:14px"><br></span></div><div><br></div><div><br><div><blockquote type="cite"><div>On Jul 21, 2017, at 10:03 PM, Mehdi AMINI <<a href="mailto:joker.eph@gmail.com" target="_blank">joker.eph@gmail.com</a>> wrote:</div><br class="m_9100368324655226475m_2141608741726357655m_-1088913725511589707m_-3161789555371559420m_794137231564833888Apple-interchange-newline"><div><br class="m_9100368324655226475m_2141608741726357655m_-1088913725511589707m_-3161789555371559420m_794137231564833888Apple-interchange-newline"><br 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" 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">2017-07-21 21:27 GMT-07:00 Peter Lawrence<span class="m_9100368324655226475m_2141608741726357655m_-1088913725511589707m_-3161789555371559420m_794137231564833888Apple-converted-space"> </span><span dir="ltr"><<a href="mailto:peterl95124@sbcglobal.net" target="_blank">peterl95124@sbcglobal.net</a>></span>:<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"><font face="Menlo">Sean,</font><div><font face="Menlo"> Let me re-phrase a couple words to make it perfectly clear</font></div><div><br><div><span><blockquote type="cite"><div><font face="Menlo">On Jul 21, 2017, at 6:29 PM, Peter Lawrence <<a href="mailto:peterl95124@sbcglobal.net" target="_blank">peterl95124@sbcglobal.net</a>> wrote:</font></div><font face="Menlo"><br class="m_9100368324655226475m_2141608741726357655m_-1088913725511589707m_-3161789555371559420m_794137231564833888m_2150444843056015504Apple-interchange-newline"></font><div><div style="word-wrap:break-word"><font face="Menlo">Sean,</font><div><font face="Menlo"><br></font></div><div><font face="Menlo">Dan Gohman’s “transform” changes a loop induction variable, but does not change the CFG,</font></div><div><font face="Menlo"><br></font></div><div><font face="Menlo">Hal’s “transform” deletes blocks out of the CFG, fundamentally altering it.</font></div><div><font face="Menlo"><br></font></div><div><span style="font-family:Menlo">These are two totally different transforms.</span></div></div></div></blockquote><blockquote type="cite"><div style="word-wrap:break-word"><div><br></div><div><span style="font-family:Menlo"><br></span></div></div></blockquote><blockquote type="cite"><div><div style="word-wrap:break-word"><div><font face="Menlo">And even the analysis is different,</font></div><div><font face="Menlo"><br></font></div><div><font face="Menlo">The first is based on an *assumption* of non-UB (actually there is no analysis to perform)</font></div></div></div></blockquote></span><font face="Menlo"> the *absence* of UB<br></font><span><blockquote type="cite"><div style="word-wrap:break-word"><div><font face="Menlo"><br></font></div><div><font face="Menlo">the second Is based on a *proof* of existence of UB (here typically some non-trivial analysis is required)</font></div></div></blockquote></span><font face="Menlo"> <span class="m_9100368324655226475m_2141608741726357655m_-1088913725511589707m_-3161789555371559420m_794137231564833888Apple-converted-space"> </span>the *presence* of UB<br></font><span><br><blockquote type="cite"><div style="word-wrap:break-word"><div><font face="Menlo">These have, practically speaking, nothing in common.</font></div><div><font face="Menlo"><br></font></div></div></blockquote><div><br></div><div><br></div></span><div><font face="Menlo">In particular, the first is an optimization, while the second is a transformation that</font></div><div><font face="Menlo">fails to be an optimization because the opportunity for it happening in real world</font></div><div><font face="Menlo">code that is expected to pass compilation without warnings, static analysis without</font></div><div><font face="Menlo">warnings, and dynamic sanitizers without warnings, is zero.</font></div><div><font face="Menlo"><br></font></div><div><font face="Menlo">Or to put it another way, if llvm manages to find some UB that no analyzer or</font></div><div><font face="Menlo">sanitizer does, and then deletes the UB, then the author of that part of llvm</font></div><div><font face="Menlo">is in the wrong group, and belongs over in the analyzer and/or sanitizer group.</font></div></div></div></div></blockquote><div><br></div><div>I don't understand your claim, it does not match at all my understand of what we managed to get on agreement on in the past.</div><div><br></div><div>The second transformation (dead code elimination to simplify) is based on the assumption that there is no UB.</div><div><br></div><div>I.e. after inlining for example, the extra context of the calling function allows us to deduce the value of some conditional branching in the inline body based on the impossibility of one of the path *in the context of this particular caller*.</div><div><br></div><div>This does not mean that the program written by the programmer has any UB inside.</div><div><br></div><div>This is exactly the example that Hal gave.</div><div><br></div><div>This can't be used to expose any meaningful information to the programmer, because it would be full of false positive. Basically a program could be clean of any static analyzer error, of any UBSAN error, and totally UB-free, and still exhibit tons and tons of such issues.</div><div><br></div><div>-- </div><div>Mehdi</div></div></div></blockquote></div></div></div></div></div></blockquote></div></div></div></div></blockquote></div></div></blockquote></div></div></div></blockquote></div><br></div></div></blockquote></div></div>
</div></blockquote></div><br></div></div></blockquote></div></div>
</div></blockquote></div><br></div></div></blockquote></div></div>