Skip to content

Commit 81167fb

Browse files
committed
Add missing atomic libcall support.
Support for emitting libcalls for __atomic_fetch_nand and __atomic_{add,sub,and,or,xor,nand}_fetch was missing; add it, and some test cases. Differential Revision: http://reviews.llvm.org/D10847 llvm-svn: 244063
1 parent 95f0606 commit 81167fb

File tree

3 files changed

+146
-3
lines changed

3 files changed

+146
-3
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,9 @@ The macros ``__ATOMIC_RELAXED``, ``__ATOMIC_CONSUME``, ``__ATOMIC_ACQUIRE``,
17151715
provided, with values corresponding to the enumerators of C11's
17161716
``memory_order`` enumeration.
17171717
1718+
(Note that Clang additionally provides GCC-compatible ``__atomic_*``
1719+
builtins)
1720+
17181721
Low-level ARM exclusive memory builtins
17191722
---------------------------------------
17201723

clang/lib/CodeGen/CGAtomic.cpp

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E, llvm::Value *Dest) {
699699

700700
switch (E->getOp()) {
701701
case AtomicExpr::AO__c11_atomic_init:
702-
llvm_unreachable("Already handled!");
702+
llvm_unreachable("Already handled above with EmitAtomicInit!");
703703

704704
case AtomicExpr::AO__c11_atomic_load:
705705
case AtomicExpr::AO__atomic_load_n:
@@ -785,20 +785,43 @@ RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E, llvm::Value *Dest) {
785785
if (UseLibcall) {
786786
bool UseOptimizedLibcall = false;
787787
switch (E->getOp()) {
788+
case AtomicExpr::AO__c11_atomic_init:
789+
llvm_unreachable("Already handled above with EmitAtomicInit!");
790+
788791
case AtomicExpr::AO__c11_atomic_fetch_add:
789792
case AtomicExpr::AO__atomic_fetch_add:
790793
case AtomicExpr::AO__c11_atomic_fetch_and:
791794
case AtomicExpr::AO__atomic_fetch_and:
792795
case AtomicExpr::AO__c11_atomic_fetch_or:
793796
case AtomicExpr::AO__atomic_fetch_or:
797+
case AtomicExpr::AO__atomic_fetch_nand:
794798
case AtomicExpr::AO__c11_atomic_fetch_sub:
795799
case AtomicExpr::AO__atomic_fetch_sub:
796800
case AtomicExpr::AO__c11_atomic_fetch_xor:
797801
case AtomicExpr::AO__atomic_fetch_xor:
802+
case AtomicExpr::AO__atomic_add_fetch:
803+
case AtomicExpr::AO__atomic_and_fetch:
804+
case AtomicExpr::AO__atomic_nand_fetch:
805+
case AtomicExpr::AO__atomic_or_fetch:
806+
case AtomicExpr::AO__atomic_sub_fetch:
807+
case AtomicExpr::AO__atomic_xor_fetch:
798808
// For these, only library calls for certain sizes exist.
799809
UseOptimizedLibcall = true;
800810
break;
801-
default:
811+
812+
case AtomicExpr::AO__c11_atomic_load:
813+
case AtomicExpr::AO__c11_atomic_store:
814+
case AtomicExpr::AO__c11_atomic_exchange:
815+
case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
816+
case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
817+
case AtomicExpr::AO__atomic_load_n:
818+
case AtomicExpr::AO__atomic_load:
819+
case AtomicExpr::AO__atomic_store_n:
820+
case AtomicExpr::AO__atomic_store:
821+
case AtomicExpr::AO__atomic_exchange_n:
822+
case AtomicExpr::AO__atomic_exchange:
823+
case AtomicExpr::AO__atomic_compare_exchange_n:
824+
case AtomicExpr::AO__atomic_compare_exchange:
802825
// Only use optimized library calls for sizes for which they exist.
803826
if (Size == 1 || Size == 2 || Size == 4 || Size == 8)
804827
UseOptimizedLibcall = true;
@@ -820,6 +843,9 @@ RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E, llvm::Value *Dest) {
820843
QualType RetTy;
821844
bool HaveRetTy = false;
822845
switch (E->getOp()) {
846+
case AtomicExpr::AO__c11_atomic_init:
847+
llvm_unreachable("Already handled!");
848+
823849
// There is only one libcall for compare an exchange, because there is no
824850
// optimisation benefit possible from a libcall version of a weak compare
825851
// and exchange.
@@ -903,7 +929,49 @@ RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E, llvm::Value *Dest) {
903929
AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, MemTy,
904930
E->getExprLoc(), sizeChars);
905931
break;
906-
default: return EmitUnsupportedRValue(E, "atomic library call");
932+
// T __atomic_fetch_nand_N(T *mem, T val, int order)
933+
case AtomicExpr::AO__atomic_fetch_nand:
934+
LibCallName = "__atomic_fetch_nand";
935+
AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, MemTy,
936+
E->getExprLoc(), sizeChars);
937+
break;
938+
939+
// T __atomic_add_fetch_N(T *mem, T val, int order)
940+
case AtomicExpr::AO__atomic_add_fetch:
941+
LibCallName = "__atomic_add_fetch";
942+
AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, LoweredMemTy,
943+
E->getExprLoc(), sizeChars);
944+
break;
945+
// T __atomic_and_fetch_N(T *mem, T val, int order)
946+
case AtomicExpr::AO__atomic_and_fetch:
947+
LibCallName = "__atomic_and_fetch";
948+
AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, MemTy,
949+
E->getExprLoc(), sizeChars);
950+
break;
951+
// T __atomic_or_fetch_N(T *mem, T val, int order)
952+
case AtomicExpr::AO__atomic_or_fetch:
953+
LibCallName = "__atomic_or_fetch";
954+
AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, MemTy,
955+
E->getExprLoc(), sizeChars);
956+
break;
957+
// T __atomic_sub_fetch_N(T *mem, T val, int order)
958+
case AtomicExpr::AO__atomic_sub_fetch:
959+
LibCallName = "__atomic_sub_fetch";
960+
AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, LoweredMemTy,
961+
E->getExprLoc(), sizeChars);
962+
break;
963+
// T __atomic_xor_fetch_N(T *mem, T val, int order)
964+
case AtomicExpr::AO__atomic_xor_fetch:
965+
LibCallName = "__atomic_xor_fetch";
966+
AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, MemTy,
967+
E->getExprLoc(), sizeChars);
968+
break;
969+
// T __atomic_nand_fetch_N(T *mem, T val, int order)
970+
case AtomicExpr::AO__atomic_nand_fetch:
971+
LibCallName = "__atomic_nand_fetch";
972+
AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, MemTy,
973+
E->getExprLoc(), sizeChars);
974+
break;
907975
}
908976

909977
// Optimized functions have the size in their name.

clang/test/CodeGen/atomic-ops-libcall.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,75 @@ int *fp2a(int **p) {
3535
// Note, the GNU builtins do not multiply by sizeof(T)!
3636
return __atomic_fetch_sub(p, 4, memory_order_relaxed);
3737
}
38+
39+
int test_atomic_fetch_add(int *p) {
40+
// CHECK: test_atomic_fetch_add
41+
// CHECK: {{%[^ ]*}} = tail call i32 @__atomic_fetch_add_4(i8* {{%[0-9]+}}, i32 55, i32 5)
42+
return __atomic_fetch_add(p, 55, memory_order_seq_cst);
43+
}
44+
45+
int test_atomic_fetch_sub(int *p) {
46+
// CHECK: test_atomic_fetch_sub
47+
// CHECK: {{%[^ ]*}} = tail call i32 @__atomic_fetch_sub_4(i8* {{%[0-9]+}}, i32 55, i32 5)
48+
return __atomic_fetch_sub(p, 55, memory_order_seq_cst);
49+
}
50+
51+
int test_atomic_fetch_and(int *p) {
52+
// CHECK: test_atomic_fetch_and
53+
// CHECK: {{%[^ ]*}} = tail call i32 @__atomic_fetch_and_4(i8* {{%[0-9]+}}, i32 55, i32 5)
54+
return __atomic_fetch_and(p, 55, memory_order_seq_cst);
55+
}
56+
57+
int test_atomic_fetch_or(int *p) {
58+
// CHECK: test_atomic_fetch_or
59+
// CHECK: {{%[^ ]*}} = tail call i32 @__atomic_fetch_or_4(i8* {{%[0-9]+}}, i32 55, i32 5)
60+
return __atomic_fetch_or(p, 55, memory_order_seq_cst);
61+
}
62+
63+
int test_atomic_fetch_xor(int *p) {
64+
// CHECK: test_atomic_fetch_xor
65+
// CHECK: {{%[^ ]*}} = tail call i32 @__atomic_fetch_xor_4(i8* {{%[0-9]+}}, i32 55, i32 5)
66+
return __atomic_fetch_xor(p, 55, memory_order_seq_cst);
67+
}
68+
69+
int test_atomic_fetch_nand(int *p) {
70+
// CHECK: test_atomic_fetch_nand
71+
// CHECK: {{%[^ ]*}} = tail call i32 @__atomic_fetch_nand_4(i8* {{%[0-9]+}}, i32 55, i32 5)
72+
return __atomic_fetch_nand(p, 55, memory_order_seq_cst);
73+
}
74+
75+
int test_atomic_add_fetch(int *p) {
76+
// CHECK: test_atomic_add_fetch
77+
// CHECK: {{%[^ ]*}} = tail call i32 @__atomic_add_fetch_4(i8* {{%[0-9]+}}, i32 55, i32 5)
78+
return __atomic_add_fetch(p, 55, memory_order_seq_cst);
79+
}
80+
81+
int test_atomic_sub_fetch(int *p) {
82+
// CHECK: test_atomic_sub_fetch
83+
// CHECK: {{%[^ ]*}} = tail call i32 @__atomic_sub_fetch_4(i8* {{%[0-9]+}}, i32 55, i32 5)
84+
return __atomic_sub_fetch(p, 55, memory_order_seq_cst);
85+
}
86+
87+
int test_atomic_and_fetch(int *p) {
88+
// CHECK: test_atomic_and_fetch
89+
// CHECK: {{%[^ ]*}} = tail call i32 @__atomic_and_fetch_4(i8* {{%[0-9]+}}, i32 55, i32 5)
90+
return __atomic_and_fetch(p, 55, memory_order_seq_cst);
91+
}
92+
93+
int test_atomic_or_fetch(int *p) {
94+
// CHECK: test_atomic_or_fetch
95+
// CHECK: {{%[^ ]*}} = tail call i32 @__atomic_or_fetch_4(i8* {{%[0-9]+}}, i32 55, i32 5)
96+
return __atomic_or_fetch(p, 55, memory_order_seq_cst);
97+
}
98+
99+
int test_atomic_xor_fetch(int *p) {
100+
// CHECK: test_atomic_xor_fetch
101+
// CHECK: {{%[^ ]*}} = tail call i32 @__atomic_xor_fetch_4(i8* {{%[0-9]+}}, i32 55, i32 5)
102+
return __atomic_xor_fetch(p, 55, memory_order_seq_cst);
103+
}
104+
105+
int test_atomic_nand_fetch(int *p) {
106+
// CHECK: test_atomic_nand_fetch
107+
// CHECK: {{%[^ ]*}} = tail call i32 @__atomic_nand_fetch_4(i8* {{%[0-9]+}}, i32 55, i32 5)
108+
return __atomic_nand_fetch(p, 55, memory_order_seq_cst);
109+
}

0 commit comments

Comments
 (0)