Home
Reading
Searching
Subscribe
Sponsors
Statistics
Posting
Contact
Spam
Lists
Links
About
Hosting
Filtering
Features Download
Marketing
Archives
FAQ
Blog
 
Gmane
From: Tim Northover <t.p.northover-Re5JQEeQqe8AvxtiuMwx3w <at> public.gmane.org>
Subject: RFC: add "cmpxchg weak" to LLVM IR
Newsgroups: gmane.comp.compilers.llvm.cvs
Date: Thursday 12th June 2014 16:22:03 UTC (over 3 years ago)
Hi all,

I've decided the next step in atomics is the weak compare-and-exchange
operation. As with the failure order, I'm going t outline the
direction I'd like to take:

1. All cmpxchg instructions now return { iN, i1 } where the first
value is what we got before (the loaded result), the second == 1 if an
exchange took place.
1. "weak" is an optional modifier to the cmpxchg instructions. If
anyone wants a bikeshed to paint, this would be a good one. I wasn't
sure myself.

Theoretically, we could only modify the return type of the weak
variant, but I think that making it a global change helps keep the IR
uniform. Additionally, this extra bit actually exists on most
platforms even for the strong cmpxchg: on LL/SC ones by virtue of
control flow, and x86 (for example) sets ZF based on it.

Assuming that's OK, onto the DAG level. What I'd like to do is a
little odd, but makes sense I think:

1. Keep ATOMIC_CMP_SWAP with its current semantics: strong cmpxchg,
returning just the loaded value.
2. Add ATOMIC_CMP_SWAP_WITH_SUCCESS (better name suggestions welcome)
that has a second i1 value (initially) as with the IR instruction.
It's still a strong cmpxchg.
3. Expanding ATOMIC_CMP_SWAP_WITH_SUCCESS will yield an
ATOMIC_CMP_SWAP and a SETCC.

The advantage of this scheme is that existing targets can remain
mostly unchanged if they're not interested in the extra work required.

LL/SC architectures will be assumed to move over to something like the
IR level pass already used by ARM if they want weak support (or they
can add weak DAG nodes if they're masochists). x86 can switch its
Custom handling over to the new ATOMIC_CMP_SWAP_WITH_SUCCESS to get
rid of the vast majority of redundant comparisons coming out of C++
code with relative ease.

I've attached two LLVM patches here, in case anyone wants something to
actually play with. They're not quite ready yet, and Clang doesn't
produce the right code obviously, but they give an idea of where I'd
like to go and what the IR might look like:

1. The first patch adds "weak" in a fairly minimal set of places.
2. The second makes the x86 switch, which will get rid of the
redundant comparisons.
3. A hypothetical 3rd patch (very simple) would actually make use of
the "weak" information in the atomic expansion pass.

What do people think of the idea? (I'm sending to llvmdev too; out of
tree front-ends would need to be updated, and probably at least
backend tests for cmpxchg).

Cheers.

Tim.
 
CD: 4ms