[LLVMdev] RFC: add "cmpxchg weak" to LLVM IR

Tim Northover
2014-06-12T16:22:03Z

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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-IR-implement-read-write-of-cmpxchg-weak.patch
Type: application/octet-stream
Size: 103435 bytes
Desc: not available
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20140612/0b1cc835/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0002-Fix-X86-cmpxchg-codegen.patch
Type: application/octet-stream
Size: 5811 bytes
Desc: not available
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20140612/0b1cc835/attachment-0001.obj>