<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">On 06/28/2017 05:33 PM, Peter Lawrence
wrote:<br>
</div>
<blockquote
cite="mid:37027DE0-B3CC-47C2-907C-C5A7A5A97BD1@sbcglobal.net"
type="cite">Chandler,
<div class=""> where we disagree is in whether the
current project is moving the issue</div>
<div class="">forward. It is not. It is making the compiler more
complex for no additional value.</div>
<div class=""><br class="">
</div>
<div class="">The current project is not based in evidence, I have
asked for any SPEC benchmark</div>
<div class="">that shows performance gain by the compiler taking
advantage of “undefined behavior”</div>
<div class="">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>
if (mask & 2)<br>
return 1;<br>
<br>
if (cond) {<br>
T high_mask = mask >> 48;<br>
if (high_mask > 5)<br>
do_something_1(high_mask);<br>
else if (high_mask > 3)<br>
do_something_2();<br>
}<br>
<br>
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). Once
we have a self-consistent model for undef, we should be able to fix
that. The user was confused, however, why seemingly innocuous
changes to the code changed the performance characteristics of their
application. The proposed semantics by John, et al. should fix this
uniformly.<br>
<br>
In any case, to your point about:<br>
<br>
<blockquote type="cite">
<div style="margin: 0px; font-size: 14px; line-height: normal;
font-family: Menlo; background-color: rgb(255, 255, 255);"
class=""><span style="font-variant-ligatures:
no-common-ligatures" class=""> if (a == a)</span></div>
<div style="margin: 0px; font-size: 14px; line-height: normal;
font-family: Menlo; background-color: rgb(255, 255, 255);"
class=""><span style="font-variant-ligatures:
no-common-ligatures" class=""> S;</span></div>
</blockquote>
<br>
I have the same thought. If a == undef here, the code should be
dead. Dead code must be aggressively dropped to enable inlining and
further optimization. This is an important way we eliminate
abstraction penalties. Dead code also has costs in terms of register
allocation, speculative execution, inlining, etc.<br>
<br>
I've also seen cases where templated types are used with fixed-sized
arrays where the compiler to leveraged knowledge of UB on
uninitialized values and out-of-bounds accesses to eliminate
unnecessary part of the code. In short, "optimizing on undefined
behavior" can end up being an important tool.<br>
<br>
-Hal<br>
<pre class="moz-signature" cols="72">--
Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory</pre>
</body>
</html>