<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:Helvetica;
        panose-1:0 0 0 0 0 0 0 0 0 0;}
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
span.apple-converted-space
        {mso-style-name:apple-converted-space;}
span.EmailStyle19
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style>
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">The GCC loop will execute a shorter loop 5 out of 6 iterations. When “state” is 0 through 5 the “jle 10” will be the only loop that’s executed. Branches in both cases are completely predictable so they effectively don’t count as instructions
 so looking at the other instructions its 9 for Clang and 7 for GCC in the short iteration and 10 in the long, average of 7.5 so at a “educated guess” level the difference makes sense.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">That being said, with such a small micro-benchmark all the minutiae of micro-architecture design will hit you in the face. The difference could arise from function alignment, CPU front-end design, what branch predictor is being used etc.
 If you can certainly, test this out on some different architectures to see if the difference is persistent.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">As Florian mentioned the codegen is also different in trunk, so try that out and file a bug if the issue persists.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Modi<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On 8/23/20, 7:47 PM, "llvm-dev on behalf of Min-Yih Hsu via llvm-dev" <<a href="mailto:llvm-dev-bounces@lists.llvm.org">llvm-dev-bounces@lists.llvm.org</a> on behalf of
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal">Hi,<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">While machine assembly might be a way to diagnose problems, another way would be leveraging the Optimization Remark framework following the instructions here: <a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__llvm.org_docs_Remarks.html&d=DwMFaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=HlATw2CSJtDwZKMxZp741A&m=huFg8C2cR4Db0Ph-V6gIH98b-ICNpHYnpqy1ZEUtk3I&s=Z-pyVCDSv8XRLuPvCqqFGLRpj9IYZLosUsvoMwxp6Aw&e=">https://llvm.org/docs/Remarks.html</a><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Basically it will print out a bunch of message regarding whether an optimization missed certain expectations. And telling you which part of the code it happened as well.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Best,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">Min<o:p></o:p></p>
<div>
<p class="MsoNormal"><br>
<br>
<o:p></o:p></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<p class="MsoNormal">On Aug 23, 2020, at 10:53 AM, Riyaz Puthiyapurayil via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">I am analyzing a clang 10.0.0 vs gcc 7.3 performance difference that I can reproduce in the following test.</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><b><span style="font-size:14.0pt;font-family:Consolas">unsigned</span></b><span class="apple-converted-space"><span style="font-size:14.0pt;font-family:Consolas"> </span></span><span style="font-size:14.0pt;font-family:Consolas">foo(<b>unsigned</b><span class="apple-converted-space"> </span>t1,<span class="apple-converted-space"> </span><b>unsigned</b><span class="apple-converted-space"> </span>t2,<span class="apple-converted-space"> </span><b>int</b><span class="apple-converted-space"> </span>count,<span class="apple-converted-space"> </span><b>int</b><span class="apple-converted-space"> </span>step)
 {</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">   <span class="apple-converted-space"> </span><b>unsigned</b><span class="apple-converted-space"> </span>tmp = 0;</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">   <span class="apple-converted-space"> </span><b>int</b><span class="apple-converted-space"> </span>state = 0;</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">   <b>for</b><span class="apple-converted-space"> </span>(<b>int</b><span class="apple-converted-space"> </span>i = 0 ; i < count ; i += step) {</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">        state++;</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">       <span class="apple-converted-space"> </span><b>if</b><span class="apple-converted-space"> </span>(state > 5)</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">            state = 0;</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">       <span class="apple-converted-space"> </span><b>if</b><span class="apple-converted-space"> </span>(state == 3)</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">            tmp += t2;</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">    }</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">   <span class="apple-converted-space"> </span><b>return</b>  tmp;</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">}</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">Clang output is about 40% slower when the function is called with t2=7, count=2000000000, step=3 (t1 is unimportant in this case as it is unused here). The attached screenshot shows the
 `perf report` annotated assembly code from clang and gcc (clang is on the left). Gcc generated code takes 0.512 sec vs clang’s 0.731 sec. The machine I am running is a Broadwell… Intel(R) Xeon(R) CPU E5-2640 v4 @ 2.40GHz.</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">The code generated by gcc runs consistently faster for all values for `step` I tried; in some cases, the performance difference is worse than 40% seen with the aforementioned parameter
 values to `foo`. The code generated by clang is a direct result of simplifycfg that eliminates the inner branches and replaces them with `select` which is then lowered to the two `cmov` instructions.</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">The code generated by clang takes far fewer branches but executes more instructions. `perf` reports 32.76% front-end cycles idle with the clang code compared to 24.20% for gcc generated
 code. Clang generated code seems to perform worse in branch-miss and icache events (as reported by `perf`). But it is not clear why. Are the two back-to-back cmove instructions the reason? Any comments on this?</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><image002.png><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas"> </span><o:p></o:p></p>
</div>
<p class="MsoNormal"><span style="font-size:12.0pt;font-family:Helvetica">_______________________________________________<br>
LLVM Developers mailing list<br>
</span><a href="mailto:llvm-dev@lists.llvm.org"><span style="font-size:12.0pt;font-family:Helvetica;color:#954F72">llvm-dev@lists.llvm.org</span></a><span style="font-size:12.0pt;font-family:Helvetica"><br>
</span><a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.llvm.org_cgi-2Dbin_mailman_listinfo_llvm-2Ddev&d=DwMFaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=HlATw2CSJtDwZKMxZp741A&m=huFg8C2cR4Db0Ph-V6gIH98b-ICNpHYnpqy1ZEUtk3I&s=WUceMHt3YvXZSLPvRRdDXPC3P6qUwFYGIUZ_AsyPTyo&e="><span style="font-size:12.0pt;font-family:Helvetica;color:#954F72">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</span></a><o:p></o:p></p>
</div>
</blockquote>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</body>
</html>