Page MenuHomePhabricator

[CodeGen][X86] Implement _InterlockedCompareExchange128 intrinsic
ClosedPublic

Authored by colden on Dec 8 2017, 1:55 PM.

Details

Summary

InterlockedCompareExchange128 is a bit more complicated than the other InterlockedCompareExchange functions, so it requires a bit more work. It doesn't directly refer to 128bit ints, instead it takes pointers to 64bit ints for Destination and ComparandResult, and exchange is taken as two 64bit ints (high & low). The previous value is written to ComparandResult, and success is returned. This implementation does the following in order to produce a cmpxchg instruction:

  1. Cast everything to 128bit ints or int pointers, and glues together the Exchange values
  2. Reads from CompareandResult to get the comparand
  3. Calls cmpxchg volatile (on X86 this will produce a lock cmpxchg16b instruction)
    1. Result 0 (previous value) is written back to ComparandResult
    2. Result 1 (success bool) is zext'ed to a uchar and returned

Resolves bug 35251

Diff Detail

Repository
rC Clang

Event Timeline

colden created this revision.Dec 8 2017, 1:55 PM
colden updated this revision to Diff 126634.Dec 12 2017, 2:38 PM
colden retitled this revision from [CodeGen] Implement _InterlockedCompareExchange128 intrinsic to [CodeGen][X86] Implement _InterlockedCompareExchange128 intrinsic.

Moved implementation to X86_64 specific code, as according to Microsoft docs this function isn't supported on X86 or ARM.

majnemer added inline comments.
llvm/tools/clang/lib/CodeGen/CGBuiltin.cpp
8439 ↗(On Diff #126634)

Builder.getInt128Ty()

colden updated this revision to Diff 126641.Dec 12 2017, 4:01 PM

llvm::IntegerType::get(getLLVMContext(), 128) -> Builder.getInt128Ty()

colden marked an inline comment as done.Dec 12 2017, 4:02 PM
colden added inline comments.
llvm/tools/clang/lib/CodeGen/CGBuiltin.cpp
8439 ↗(On Diff #126634)

Oh nice, that's way cleaner.

colden marked an inline comment as done.Dec 12 2017, 4:02 PM
rnk accepted this revision.Dec 13 2017, 1:53 PM

lgtm

This revision is now accepted and ready to land.Dec 13 2017, 1:53 PM

Thanks Reid! Would you mind submitting this for me?

This revision was automatically updated to reflect the committed changes.