[llvm-bugs] [Bug 51104] New: Suboptimal codegen for CAS loop
via llvm-bugs
llvm-bugs at lists.llvm.org
Thu Jul 15 03:12:25 PDT 2021
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: unassignedclangbugs at nondot.org
Reporter: gonzalo.gadeschi at gmail.com
CC: llvm-bugs at lists.llvm.org, neeilans at live.com,
richard-llvm at 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210715/86610eea/attachment-0001.html>
More information about the llvm-bugs
mailing list