[PATCH] Fix atomic libcall.

Logan Chien tzuhsiang.chien at gmail.com
Fri Mar 7 08:40:17 PST 2014


This CL fixes an cast instruction assertion failure
due to the incompatible type cast.  This will only happen when
the target requires atomic libcalls.

http://llvm-reviews.chandlerc.com/D3006

Files:
  lib/CodeGen/CGAtomic.cpp
  test/CodeGen/atomic-ops-libcall.c

Index: lib/CodeGen/CGAtomic.cpp
===================================================================
--- lib/CodeGen/CGAtomic.cpp
+++ lib/CodeGen/CGAtomic.cpp
@@ -476,6 +476,8 @@
     Args.add(RValue::get(EmitCastToVoidPtr(Ptr)), getContext().VoidPtrTy);
 
     std::string LibCallName;
+    QualType LoweredMemTy =
+      MemTy->isPointerType() ? getContext().getIntPtrType() : MemTy;
     QualType RetTy;
     bool HaveRetTy = false;
     switch (E->getOp()) {
@@ -531,7 +533,7 @@
     case AtomicExpr::AO__c11_atomic_fetch_add:
     case AtomicExpr::AO__atomic_fetch_add:
       LibCallName = "__atomic_fetch_add";
-      AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, MemTy,
+      AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, LoweredMemTy,
                         E->getExprLoc());
       break;
     // T __atomic_fetch_and_N(T *mem, T val, int order)
@@ -552,7 +554,7 @@
     case AtomicExpr::AO__c11_atomic_fetch_sub:
     case AtomicExpr::AO__atomic_fetch_sub:
       LibCallName = "__atomic_fetch_sub";
-      AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, MemTy,
+      AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, LoweredMemTy,
                         E->getExprLoc());
       break;
     // T __atomic_fetch_xor_N(T *mem, T val, int order)
Index: test/CodeGen/atomic-ops-libcall.c
===================================================================
--- /dev/null
+++ test/CodeGen/atomic-ops-libcall.c
@@ -0,0 +1,47 @@
+// RUN: %clang_cc1 < %s -triple armv5e-none-linux-gnueabi -emit-llvm -O0 | FileCheck %s
+
+enum memory_order {
+  memory_order_relaxed, memory_order_consume, memory_order_acquire,
+  memory_order_release, memory_order_acq_rel, memory_order_seq_cst
+};
+
+int *test_c11_atomic_fetch_add_int_ptr(_Atomic(int *) *p) {
+  // CHECK: test_c11_atomic_fetch_add_int_ptr
+  // CHECK: store i32 12, i32* %.atomictmp
+  // CHECK: %2 = load i32* %.atomictmp, align 4
+  // CHECK: %call = call i32* @__atomic_fetch_add_4(i8* %1, i32 %2, i32 5)
+  return __c11_atomic_fetch_add(p, 3, memory_order_seq_cst);
+}
+
+int *test_c11_atomic_fetch_sub_int_ptr(_Atomic(int *) *p) {
+  // CHECK: test_c11_atomic_fetch_sub_int_ptr
+  // CHECK: store i32 20, i32* %.atomictmp
+  // CHECK: %2 = load i32* %.atomictmp, align 4
+  // CHECK: %call = call i32* @__atomic_fetch_sub_4(i8* %1, i32 %2, i32 5)
+  return __c11_atomic_fetch_sub(p, 5, memory_order_seq_cst);
+}
+
+int test_c11_atomic_fetch_add_int(_Atomic(int) *p) {
+  // CHECK: test_c11_atomic_fetch_add_int
+  // CHECK: store i32 3, i32* %.atomictmp
+  // CHECK: %2 = load i32* %.atomictmp, align 4
+  // CHECK: %call = call i32 bitcast (i32* (i8*, i32, i32)* @__atomic_fetch_add_4 to i32 (i8*, i32, i32)*)(i8* %1, i32 %2, i32 5)
+  return __c11_atomic_fetch_add(p, 3, memory_order_seq_cst);
+}
+
+int test_c11_atomic_fetch_sub_int(_Atomic(int) *p) {
+  // CHECK: test_c11_atomic_fetch_sub_int
+  // CHECK: store i32 5, i32* %.atomictmp
+  // CHECK: %2 = load i32* %.atomictmp, align 4
+  // CHECK: %call = call i32 bitcast (i32* (i8*, i32, i32)* @__atomic_fetch_sub_4 to i32 (i8*, i32, i32)*)(i8* %1, i32 %2, i32 5)
+  return __c11_atomic_fetch_sub(p, 5, memory_order_seq_cst);
+}
+
+int *fp2a(int **p) {
+  // CHECK: @fp2a
+  // CHECK: store i32 4, i32* %.atomictmp
+  // CHECK: %2 = load i32* %.atomictmp, align 4
+  // CHECK: %call = call i32* @__atomic_fetch_sub_4(i8* %1, i32 %2, i32 0)
+  // Note, the GNU builtins do not multiply by sizeof(T)!
+  return __atomic_fetch_sub(p, 4, memory_order_relaxed);
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D3006.1.patch
Type: text/x-patch
Size: 3526 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140307/21224ffc/attachment.bin>


More information about the cfe-commits mailing list