<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Mon, Jun 19, 2017 at 7:36 AM Peter Lawrence 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:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Sanjoy,<br>
            You have changed the subject.  We still need real world examples<br>
showing how taking advantage of “undef” results in beneficial optimization.<br>
<br>
My belief is that they don’t exist, my reasoning is this: real world programmers<br>
are likely to run UBSan before compiling (or if they don’t they should), therefore<br>
it is highly unlikely that any “undef” will actually exist during compilation of their<br>
source code.<br></blockquote><div><br>Wait - that ^ doesn't seem to follow. The point of undefined behavior optimizations isn't to only break programs that would trigger UBSan. The point is to optimize on the assumption that they won't break UBSan/exhibit undefined behavior.<br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Yes, “Undef” can be created during SSA construction, but we already discussed<br>
That in a previous email (its a register allocation problem).<br>
<br>
<br>
The only other way I can see of “undef” occurring in IR is if we go out of our<br>
way to optimize for example<br>
<br>
        if (   ((i64)X + (i64)Y)) < INT_MIN   ||    ((i64)X) + (i64)Y) > INT_MAX   ) {<br>
                . . .<br>
                Z = X  "+nsw”  Y;<br>
                . . .<br>
        }<br>
<br>
Here “Z” could in theory be replaced with “undef”, but there is no point in doing so.<br>
<br>
Similarly with provably out-of-bounds GEP, similarly with provably invalid pointers,<br>
etc, but again there is no point in doing so.<br></blockquote><div><br></div><div>Why isn't there any point in doing so? One common situation where exploiting UB (aka: "assuming UB can't happen") may be beneficial is when the code in isolation is fine, but when inlined it's possible to prove certain paths create contradictions. That way even if the conditions for those paths can't be analyzed to prove they are unreachable (or values are not needed, etc), the compiler can skip all that and just collapse the code (remove the unreachable code, delete the value computation, etc) anyway.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
So is there any other way of having “undef” appear in the IR ?<br>
<br>
<br>
Peter Lawrence.<br>
<br>
<br>
<br>
> On Jun 16, 2017, at 7:45 PM, Sanjoy Das <<a href="mailto:sanjoy@playingwithpointers.com" target="_blank">sanjoy@playingwithpointers.com</a>> wrote:<br>
><br>
> Hi Peter,<br>
><br>
> Why we need an undef value is covered here:<br>
> <a href="http://sunfishcode.github.io/blog/2014/07/14/undef-introduction.html" rel="noreferrer" target="_blank">http://sunfishcode.github.io/blog/2014/07/14/undef-introduction.html</a><br>
> (in short, to do SSA construction well).<br>
><br>
> For a while we did not have a literal representation for poison.<br>
> However, in practice having both undef and poison was problematic (see<br>
> the paper), so we decided to ditch undef and keep poison.<br>
><br>
> However for the new poison to provide the same functionality as the<br>
> old undef (which is now going away), we need a literal representation<br>
> for the new poison.<br>
><br>
> -- Sanjoy<br>
><br>
> On Fri, Jun 16, 2017 at 3:03 PM, Peter Lawrence via llvm-dev<br>
> <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
>> All,<br>
>>     These discussions seem to be based on the premise that there is a<br>
>> need for the compiler to exploit undefined behavior for performance<br>
>> optimization reasons.<br>
>><br>
>> So far the only beneficial optimization I am aware of that relies on some<br>
>> form of “undefined” is Dan Gohman’s original project for LP64 targets of<br>
>> promoting i32 induction variables to i64 and hoisting sign-extension out<br>
>> of the loop.<br>
>><br>
>> But “undef” / “poison” never appears in either the original or the transformed<br>
>> IR for these types of loops, instead properties of “+nsw” are used to<br>
>> justify the transformation.  The transformation does not just fall out because<br>
>> we’ve done a good job at defining “undef” / “poison” IR nodes.<br>
>><br>
>> So I’d like to see some concrete examples of where the compiler can<br>
>> do useful optimization based on “undef” / “poison” appearing explicitly<br>
>> In the IR,  finding some would surely advance this discussion.<br>
>><br>
>><br>
>><br>
>> Peter Lawrence.<br>
>><br>
>><br>
>> _______________________________________________<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/mailman/listinfo/llvm-dev</a><br>
<br>
_______________________________________________<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/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div>