<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - Possible bad optimisation of unsigned atomic fetch_sub(1) > 1 on x64"
href="https://bugs.llvm.org/show_bug.cgi?id=35068">35068</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Possible bad optimisation of unsigned atomic fetch_sub(1) > 1 on x64
</td>
</tr>
<tr>
<th>Product</th>
<td>libraries
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Windows NT
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>Backend: X86
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>lewissbaker@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>I have some C++ coroutine code that appears to be generating incorrect assembly
for some atomic operations used to perform reference counting.
The issue arose when testing the 'generic_ops' branch of cppcoro
(<a href="https://github.com/lewissbaker/cppcoro/tree/generic_ops">https://github.com/lewissbaker/cppcoro/tree/generic_ops</a>) and seems to be
causing many of the 'shared_task' tests to fail under clang optimised builds.
I have reduced the issue down to what I believe is the root cause:
I have an await_suspend() method that looks like:
bool await_suspend(std::experimental::coroutine_handle<promise_type> h)
{
// Decrement reference count and return true to suspend if there are
// still outstanding references.
// Note that m_refCount has type std::atomic<std::uint32_t>.
return h.promise().m_refCount.fetch_sub(1, std::memory_order_acq_rel) > 1;
}
When targeting clang x64 -O3 builds this generates assembly which looks like:
lock
add dword ptr [rdi + 16], -1
jbe .cleanup
ret
In this instance, the code generated should only be taking the branch if
await_suspend() returns false (ie. the initial value was 0 or 1).
The jbe instruction takes the branch if either ZF=1 or CF=1.
ZF=1 would only occur if the result of the add is zero (ie. initial value was
1).
However, since we're adding -1 (0xFFFFFFFF), I'm assuming this would cause
unsigned integer overflow and thus set CF=1 for all values except an initial
value of zero.
Thus the branch would seem to be taken if initial value > 0 rather than if the
initial value was 0 or 1, as required.
Should it be using a 'lock sub' instruction instead of 'lock add'?
eg.
lock
sub dword ptr [rdi + 16], 1
jbe .cleanup
ret
The sub instruction would only trigger unsigned overflow (ie. set CF=1) if the
original value is 0, and would only set ZF=1 if result is zero (ie. original
value is 1), which would give the desired result when used with subsequent jbe
instruction.
Example source code that reproduces the issue is available here:
<a href="https://godbolt.org/g/GoGk3o">https://godbolt.org/g/GoGk3o</a></pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>