<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">2017-06-29 10:43 GMT-07:00 Peter Lawrence via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span>:<br><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 style="word-wrap:break-word"><div><div class="gmail-h5"><br><div><blockquote type="cite"><div>On Jun 29, 2017, at 9:32 AM, Hal Finkel <<a href="mailto:hfinkel@anl.gov" target="_blank">hfinkel@anl.gov</a>> wrote:</div><br class="gmail-m_-3269729547621288564Apple-interchange-newline"><div><div class="gmail-m_-3269729547621288564moz-cite-prefix" 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;background-color:rgb(255,255,255)"><br class="gmail-m_-3269729547621288564Apple-interchange-newline">On 06/29/2017 10:41 AM, Peter Lawrence wrote:<br></div><blockquote type="cite" 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;background-color:rgb(255,255,255)"><br><div><blockquote type="cite"><div>On Jun 29, 2017, at 4:39 AM, Hal Finkel <<a href="mailto:hfinkel@anl.gov" target="_blank">hfinkel@anl.gov</a>> wrote:</div><br class="gmail-m_-3269729547621288564Apple-interchange-newline"><div><div bgcolor="#FFFFFF"><div class="gmail-m_-3269729547621288564moz-cite-prefix">On 06/28/2017 05:33 PM, Peter Lawrence wrote:<br></div><blockquote type="cite">Chandler,<div>               where we disagree is in whether the current project is moving the issue</div><div>forward.  It is not.  It is making the compiler more complex for no additional value.</div><div><br></div><div>The current project is not based in evidence, I have asked for any SPEC benchmark</div><div>that shows performance gain by the compiler taking advantage of “undefined behavior”</div><div>and no one can show that.</div></blockquote><br>I can't comment on SPEC, but this does remind me of code I was working on recently. To abstract the relevant parts, it looked something like this:<br><br>template <typename T><br>int do_something(T mask, bool cond) {<br> <span class="gmail-m_-3269729547621288564Apple-converted-space"> </span>if (mask & 2)<br>   <span class="gmail-m_-3269729547621288564Apple-converted-space"> </span>return 1;<br><br> <span class="gmail-m_-3269729547621288564Apple-converted-space"> </span>if (cond) {<br>   <span class="gmail-m_-3269729547621288564Apple-converted-space"> </span>T high_mask = mask >> 48;<br>   <span class="gmail-m_-3269729547621288564Apple-converted-space"> </span>if (high_mask > 5)<br>     <span class="gmail-m_-3269729547621288564Apple-converted-space"> </span>do_something_1(high_<wbr>mask);<br>   <span class="gmail-m_-3269729547621288564Apple-converted-space"> </span>else if (high_mask > 3)<br>     <span class="gmail-m_-3269729547621288564Apple-converted-space"> </span>do_something_2();<br> <span class="gmail-m_-3269729547621288564Apple-converted-space"> </span>}<br><br> <span class="gmail-m_-3269729547621288564Apple-converted-space"> </span>return 0;<br>}<br><br>This function ended up being instantiated on different types T (e.g. unsigned char, unsigned int, unsigned long, etc.) and, dynamically, cond was always false when T was char. The question is: Can the compiler eliminate all of the code predicated on cond for the smaller types? In this case, this code was hot, and moreover, performance depended on the fact that, for T = unsigned char, the function was inlined and the branch on cond was eliminated. In the relevant translation unit, however, the compiler would never see how cond was set.<br><br>Luckily, we do the right thing here currently. In the case where T = unsigned char, we end up folding both of the high_mask tests as though they were false. That entire part of the code is eliminated, the function is inlined, and everyone is happy.<br><br>Why was I looking at this? As it turns out, if the 'else if' in this example is just 'else', we don't actually eliminate both sides of the branch. The same is true for many other variants of the conditionals (i.e. we don't recognize all of the code as dead).</div></div></blockquote><div><div><br></div><div><br></div><div>I apologize in advance if I have missed something here and am misreading your example...</div><div><br></div></div><div>This doesn’t make sense to me, a shift amount of 48 is “undefined” for unsigned char,</div><div>How do we know this isn’t a source code bug,</div><div>What makes us think the the user intended the result to be “0”.</div></div></blockquote><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;background-color:rgb(255,255,255)"><span 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;background-color:rgb(255,255,255);float:none;display:inline">As I said, this is representation of what the real code did, and looked like, after other inlining had taken place, etc. In the original form, the user's intent was clear. That code is never executed when T is a small integer type.</span><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;background-color:rgb(255,255,255)"></div></blockquote><br></div><div><br></div></div></div><div>I will still have a hard time believing this until I see a real example, can you fill in the details ?</div></div></blockquote><div><br></div><div><br></div><div>Hal gave you a real example, have you tried? I feel like you're asking more effort from others than you are ready to put in: it took me less than 5 minutes to reproduce what Hal was describing using his snippet:</div><div><br></div><div>See the difference between <a href="https://godbolt.org/g/YYtsxB">https://godbolt.org/g/YYtsxB</a> and <a href="https://godbolt.org/g/dTBBDq">https://godbolt.org/g/dTBBDq</a></div><div><br></div><div>-- </div><div>Mehdi</div><div><br></div><div><br></div><div><br></div></div></div></div>