<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Yep, that doesn't look good and deserves a PR and some more looking into.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Wondering why we haven't seen this before: I guess at higher optimisations levels this problem is hidden by iteration count checks generated by the vectoriser or loop unroller. </div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
It is a bit of a funny test, as also shown by the code produced with a higher opt level, but that shouldn't be an excuse I think.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Cheers,</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Sjoerd. </div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> llvm-dev <llvm-dev-bounces@lists.llvm.org> on behalf of Nagurne, James via llvm-dev <llvm-dev@lists.llvm.org><br>
<b>Sent:</b> 26 March 2021 18:41<br>
<b>To:</b> 'llvm-dev@lists.llvm.org' <llvm-dev@lists.llvm.org><br>
<b>Subject:</b> [llvm-dev] Unsigned integer underflow in HardwareLoops pass (PPC, perhaps ARM)</font>
<div> </div>
</div>
<style>
<!--
@font-face
        {font-family:"Cambria Math"}
@font-face
        {font-family:Calibri}
p.x_MsoNormal, li.x_MsoNormal, div.x_MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif}
a:link, span.x_MsoHyperlink
        {color:blue;
        text-decoration:underline}
a:visited, span.x_MsoHyperlinkFollowed
        {color:purple;
        text-decoration:underline}
p.x_MsoListParagraph, li.x_MsoListParagraph, div.x_MsoListParagraph
        {margin-top:0in;
        margin-right:0in;
        margin-bottom:0in;
        margin-left:.5in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif}
span.x_EmailStyle17
        {font-family:"Calibri",sans-serif;
        color:windowtext}
.x_MsoChpDefault
        {}
@page WordSection1
        {margin:1.0in 1.0in 1.0in 1.0in}
div.x_WordSection1
        {}
ol
        {margin-bottom:0in}
ul
        {margin-bottom:0in}
-->
</style>
<div lang="EN-US" link="blue" vlink="purple">
<div class="x_WordSection1">
<p class="x_MsoNormal">Our team is developing on a downstream target that utilizes the HardwareLoops pass and have found that it generates unexpected code with regards to a regression test that we have. I’ve not 100% vetted the test itself with regards to the
 specifics of the C standard, but logically it makes sense:</p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">I have the test up on Compiler Explorer, and the offending code can be duplicated from a stock trunk clang on PowerPC:
<a href="https://godbolt.org/z/KzW3nYjra">https://godbolt.org/z/KzW3nYjra</a></p>
<p class="x_MsoNormal"></p>
<p class="x_MsoNormal">The test itself intends to ensure that small-width loop counters are not promoted. It does this by constructing a loop with an unsigned 8-bit value and purposefully underflowing line 20 with ‘--count’. What is expected to happen is that
 the 8-bit value underflows to 0xFF, and the loop goes on to execute 256 times, exiting the loop and returning 0. In the failure case where p increments past the end of buffer, the test returns 1. I believe this failure case is optimized out as undefined behavior.</p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">In the PowerPC disassembly of the compiled test:</p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">  mr 30, 3</p>
<p class="x_MsoNormal">…</p>
<p class="x_MsoNormal">  mtctr 30</p>
<p class="x_MsoNormal">.LBB0_1: # =>This Inner Loop Header: Depth=1</p>
<p class="x_MsoNormal">  bdnz .LBB0_1</p>
<p class="x_MsoNormal"> </p>
<ol start="1" type="1" style="margin-top:0in">
<li class="x_MsoListParagraph" style="margin-left:0in">r3 (count) is placed into r30</li><li class="x_MsoListParagraph" style="margin-left:0in">The memset (*p++ = 0) portion of the loop is factored out into an actual call to memset</li><li class="x_MsoListParagraph" style="margin-left:0in">r30 (count) is placed into the CTR</li><li class="x_MsoListParagraph" style="margin-left:0in">The CTR is used in bdnz
<ol start="1" type="a" style="margin-top:0in">
<li class="x_MsoListParagraph" style="margin-left:0in">With a quick glance at the definition of that instruction, the decrement happens before the compare. This means that the CTR may underflow, and will end up as either 0xffffffff or 0xffffffffffffffff</li><li class="x_MsoListParagraph" style="margin-left:0in">The CTR will be compared to 0 and, now being a large positive value, will not be 0</li><li class="x_MsoListParagraph" style="margin-left:0in">The branch will occur, repeating a-c a finite but undesirable number of times</li></ol>
</li></ol>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">Digging slightly deeper into the pass itself, the inserted intrinsics don’t seem to care about the original counter type past the point where the counter is used in the hardware loop count initialization intrinsic:</p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">entry:</p>
<p class="x_MsoNormal">  br label %do.body</p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">do.body:                                          ; preds = %do.body, %entry</p>
<p class="x_MsoNormal">  %count.addr.0 = phi i8 [ %count, %entry ], [ %dec, %do.body ]</p>
<p class="x_MsoNormal">  %dec = add i8 %count.addr.0, -1</p>
<p class="x_MsoNormal">  %cmp1.not = icmp eq i8 %dec, 0</p>
<p class="x_MsoNormal">  br i1 %cmp1.not, label %do.end, label %do.body, !llvm.loop !2</p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">Becomes</p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">entry:</p>
<p class="x_MsoNormal">  %0 = zext i8 %count to i32</p>
<p class="x_MsoNormal">  call void @llvm.set.loop.iterations.i32(i32 %0)</p>
<p class="x_MsoNormal">  br label %do.body</p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">do.body:                                          ; preds = %do.body, %entry</p>
<p class="x_MsoNormal">  %1 = call i1 @llvm.loop.decrement.i32(i32 1)</p>
<p class="x_MsoNormal">  br i1 %1, label %do.body, label %do.end, !llvm.loop !2</p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">This seems like an oversight, albeit a very edge-casey one.</p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">J.B. Nagurne</p>
<p class="x_MsoNormal">Code Generation</p>
<p class="x_MsoNormal">Texas Instruments</p>
</div>
</div>
</body>
</html>