Deutsch English Français Italiano |
<At6cnfsh1ZzpB_v6nZ2dnZfqn_adnZ2d@supernews.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!Xl.tags.giganews.com!local-3.nntp.ord.giganews.com!nntp.supernews.com!news.supernews.com.POSTED!not-for-mail NNTP-Posting-Date: Sat, 21 Dec 2024 10:37:40 +0000 Sender: Andrew Haley <aph@zarquon.pink> From: aph@littlepinkcloud.invalid Subject: Re: Strange asm generated by GCC... Newsgroups: comp.arch References: <vk2ek2$33ckv$3@dont-email.me> <vk4ahk$3i1bs$1@dont-email.me> User-Agent: tin/1.9.2-20070201 ("Dalaruan") (UNIX) (Linux/4.18.0-553.27.1.el8_10.x86_64 (x86_64)) Message-ID: <At6cnfsh1ZzpB_v6nZ2dnZfqn_adnZ2d@supernews.com> Date: Sat, 21 Dec 2024 10:37:40 +0000 Lines: 51 X-Trace: sv3-rbbUX/ALR1xjiGkEiqn45Sg0EZ8qiT/W0Jn02KFSoOdk+e7M8Q1qhLVWVUfJ3NCJPsD53WoTXJquUSV!YEChicm3i2rohrdOQe47g5n7XlzKfqKA7f0ZJoPaHn3pYInZl9fiRQvnRUuisPPCSfB1Mt/RASkQ!MeL3ICWJ X-Complaints-To: www.supernews.com/docs/abuse.html X-DMCA-Complaints-To: www.supernews.com/docs/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 Bytes: 2599 jseigh <jseigh_es00@xemaps.com> wrote: > On 12/19/24 19:43, Chris M. Thomasson wrote: >> Why in the world would GCC use an XCHG instruction for the following >> code. The damn XCHG has an implied LOCK prefix! Yikes! >> > > Speaking of strange code > > #include <atomic> > > bool test1(std::atomic<int> var, int addend) > { > int expected = var.load(std::memory_order_relaxed); > int update = expected + addend; > return var.compare_exchange_weak(expected, update, > std::memory_order_acq_rel, std::memory_order_seq_cst); > } > > This is asm for armv8-a clang 9.0.0 > > test1(std::atomic<int>, int): > ldr w8, [x0] > ldaxr w9, [x0] > cmp w9, w8 > b.ne .LBB0_3 > add w8, w8, w1 > stlxr w9, w8, [x0] > cbz w9, .LBB0_4 > mov w0, wzr > ret > .LBB0_3: > clrex > mov w0, wzr > ret > .LBB0_4: > mov w0, #1 > ret > > I picked a version that just did ll/sc to avoid > the question of whether a failed CASAL did a store or not. > > I don't see anything that forces a store memory barrier > on all the fail paths. I could be missing something. Why would there be one? If the store does not take place, there's no need for a memory barrier because there's no store for anyone to synchronize with. The only effect of a failed weak CAS is a load. If you really need a store on failure because of its side effect you can always add one. Andrew.