<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 - Suboptimal codegen for CAS loop"
href="https://bugs.llvm.org/show_bug.cgi?id=51104">51104</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Suboptimal codegen for CAS loop
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</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>LLVM Codegen
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>gonzalo.gadeschi@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
</td>
</tr></table>
<p>
<div>
<pre>Given this CAS loop:
#include <atomic>
int fetch_max(std::atomic<int>& mem, int val) {
int read = mem.load(std::memory_order_relaxed);
int write;
do {
write = std::max(read, val);
} while( !mem.compare_exchange_weak(read, write) );
return read;
}
Using -O3 -fno-exceptions -g0 -march=skylake, gcc generates:
fetch_max(std::atomic<int>&, int):
mov eax, DWORD PTR [rdi]
.L2:
cmp eax, esi
mov edx, esi
cmovge edx, eax
lock cmpxchg DWORD PTR [rdi], edx
jne .L2
ret
but clang generates:
fetch_max(std::atomic<int>&, int): # @fetch_max(std::atomic<int>&,
int)
movl (%rdi), %ecx
cmpl %esi, %ecx
movl %ecx, %edx
cmovll %esi, %edx
movl %ecx, %eax
lock cmpxchgl %edx, (%rdi)
je .LBB0_3
.LBB0_1: # =>This Inner Loop Header: Depth=1
movl %eax, %ecx
cmpl %esi, %eax
movl %eax, %edx
cmovll %esi, %edx
lock cmpxchgl %edx, (%rdi)
jne .LBB0_1
.LBB0_3:
movl %ecx, %eax
retq
If instead of return read, I change the example to return write, then the
codegen improves significantly.</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>