[PATCH] D106344: [PowerPC] Correct behavior of __compare_and_swap

Kai Luo via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 19 23:02:15 PDT 2021


lkail created this revision.
lkail added reviewers: jsji, nemanjai, w2yehia, shchenz, PowerPC.
Herald added subscribers: jfb, kbarton.
lkail requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

XL's `__compare_and_swap` has a weird behavior that

> In either case, the contents of the memory location specified by addr are copied into the memory location specified by old_val_addr.

This patch let clang's implementation follow this behavior.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D106344

Files:
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/test/CodeGen/builtins-ppc-xlcompat-cas.c


Index: clang/test/CodeGen/builtins-ppc-xlcompat-cas.c
===================================================================
--- clang/test/CodeGen/builtins-ppc-xlcompat-cas.c
+++ clang/test/CodeGen/builtins-ppc-xlcompat-cas.c
@@ -19,6 +19,7 @@
 // CHECK-NEXT:    [[TMP2:%.*]] = cmpxchg weak volatile i32* [[A_ADDR]], i32 [[TMP1]], i32 [[TMP0]] monotonic monotonic, align 4
 // CHECK-NEXT:    [[TMP3:%.*]] = extractvalue { i32, i1 } [[TMP2]], 0
 // CHECK-NEXT:    [[TMP4:%.*]] = extractvalue { i32, i1 } [[TMP2]], 1
+// CHECK-NEXT:    store i32 [[TMP3]], i32* [[B_ADDR]], align 4
 // CHECK-NEXT:    ret void
 //
 void test_builtin_ppc_compare_and_swap(int a, int b, int c) {
@@ -39,6 +40,7 @@
 // CHECK-NEXT:    [[TMP2:%.*]] = cmpxchg weak volatile i64* [[A_ADDR]], i64 [[TMP1]], i64 [[TMP0]] monotonic monotonic, align 8
 // CHECK-NEXT:    [[TMP3:%.*]] = extractvalue { i64, i1 } [[TMP2]], 0
 // CHECK-NEXT:    [[TMP4:%.*]] = extractvalue { i64, i1 } [[TMP2]], 1
+// CHECK-NEXT:    store i64 [[TMP3]], i64* [[B_ADDR]], align 8
 // CHECK-NEXT:    ret void
 //
 void test_builtin_ppc_compare_and_swaplp(long a, long b, long c) {
Index: clang/lib/CodeGen/CGBuiltin.cpp
===================================================================
--- clang/lib/CodeGen/CGBuiltin.cpp
+++ clang/lib/CodeGen/CGBuiltin.cpp
@@ -15590,6 +15590,15 @@
     auto Pair = EmitAtomicCompareExchange(
         LV, RValue::get(OldVal), RValue::get(Ops[2]), E->getExprLoc(),
         llvm::AtomicOrdering::Monotonic, llvm::AtomicOrdering::Monotonic, true);
+    // FIXME:
+    // Unlike c11's atomic_compare_exchange, accroding to
+    // https://www.ibm.com/docs/en/xl-c-and-cpp-aix/16.1?topic=functions-compare-swap-compare-swaplp
+    // > In either case, the contents of the memory location specified by addr
+    // > are copied into the memory location specified by old_val_addr.
+    // But it hasn't specified storing to OldValAddr is atomic or not and
+    // which order to use.
+    Value *LoadedVal = Pair.first.getScalarVal();
+    Builder.CreateStore(LoadedVal, OldValAddr);
     return Pair.second;
   }
   case PPC::BI__builtin_ppc_fetch_and_add:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D106344.360021.patch
Type: text/x-patch
Size: 2133 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210720/6d4aaf76/attachment.bin>


More information about the cfe-commits mailing list