<div dir="ltr">I am just curious, was a PR ever opened for this? I can certainly confirm that this causes 2^64 iterations of the loop on PPC64 so this is probably something we should fix.<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Mar 29, 2021 at 1:45 PM Sjoerd Meijer 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:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div 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="gmail-m_7139551247050508317appendonsend"></div>
<hr style="display:inline-block;width:98%">
<div id="gmail-m_7139551247050508317divRplyFwdMsg" dir="ltr"><font style="font-size:11pt" face="Calibri, sans-serif" color="#000000"><b>From:</b> llvm-dev <<a href="mailto:llvm-dev-bounces@lists.llvm.org" target="_blank">llvm-dev-bounces@lists.llvm.org</a>> on behalf of Nagurne, James via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>><br>
<b>Sent:</b> 26 March 2021 18:41<br>
<b>To:</b> '<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>' <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>><br>
<b>Subject:</b> [llvm-dev] Unsigned integer underflow in HardwareLoops pass (PPC, perhaps ARM)</font>
<div> </div>
</div>
<div lang="EN-US">
<div>
<p>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> </p>
<p>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" target="_blank">https://godbolt.org/z/KzW3nYjra</a></p>
<p></p>
<p>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> </p>
<p>In the PowerPC disassembly of the compiled test:</p>
<p> </p>
<p> mr 30, 3</p>
<p>…</p>
<p> mtctr 30</p>
<p>.LBB0_1: # =>This Inner Loop Header: Depth=1</p>
<p> bdnz .LBB0_1</p>
<p> </p>
<ol style="margin-top:0in" type="1" start="1">
<li style="margin-left:0in">r3 (count) is placed into r30</li><li style="margin-left:0in">The memset (*p++ = 0) portion of the loop is factored out into an actual call to memset</li><li style="margin-left:0in">r30 (count) is placed into the CTR</li><li style="margin-left:0in">The CTR is used in bdnz
<ol style="margin-top:0in" type="a" start="1">
<li 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 style="margin-left:0in">The CTR will be compared to 0 and, now being a large positive value, will not be 0</li><li style="margin-left:0in">The branch will occur, repeating a-c a finite but undesirable number of times</li></ol>
</li></ol>
<p> </p>
<p>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> </p>
<p>entry:</p>
<p> br label %do.body</p>
<p> </p>
<p>do.body: ; preds = %do.body, %entry</p>
<p> %count.addr.0 = phi i8 [ %count, %entry ], [ %dec, %do.body ]</p>
<p> %dec = add i8 %count.addr.0, -1</p>
<p> %cmp1.not = icmp eq i8 %dec, 0</p>
<p> br i1 %cmp1.not, label %do.end, label %do.body, !llvm.loop !2</p>
<p> </p>
<p>Becomes</p>
<p> </p>
<p>entry:</p>
<p> %0 = zext i8 %count to i32</p>
<p> call void @llvm.set.loop.iterations.i32(i32 %0)</p>
<p> br label %do.body</p>
<p> </p>
<p>do.body: ; preds = %do.body, %entry</p>
<p> %1 = call i1 @llvm.loop.decrement.i32(i32 1)</p>
<p> br i1 %1, label %do.body, label %do.end, !llvm.loop !2</p>
<p> </p>
<p>This seems like an oversight, albeit a very edge-casey one.</p>
<p> </p>
<p>J.B. Nagurne</p>
<p>Code Generation</p>
<p>Texas Instruments</p>
</div>
</div>
</div>
_______________________________________________<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="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>