https://bugs.llvm.org/show_bug.cgi?id=51104
Bug ID: 51104
Summary: Suboptimal codegen for CAS loop
Product: clang
Version: trunk
Hardware: PC
OS: Linux
Status: NEW
Severity: enhancement
Priority: P
Component: LLVM Codegen
Assignee: unassignedclangb...@nondot.org
Reporter: gonzalo.gades...@gmail.com
CC: llvm-bugs@lists.llvm.org, neeil...@live.com,
richard-l...@metafoo.co.uk
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.
--
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs