Skip to content

Commit 43165d9

Browse files
committed
Expose IRBuilder::CreateAtomicCmpXchg as LLVMBuildAtomicCmpXchg in the C API.
Summary: Also expose getters and setters in the C API, so that the change can be tested. Reviewers: nhaehnle, axw, joker.eph Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D18260 From: Bas Nieuwenhuizen <bas@basnieuwenhuizen.nl> llvm-svn: 263886
1 parent c286b9f commit 43165d9

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

llvm/include/llvm-c/Core.h

+15
Original file line numberDiff line numberDiff line change
@@ -2940,6 +2940,21 @@ LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B, LLVMAtomicRMWBinOp op,
29402940
LLVMValueRef PTR, LLVMValueRef Val,
29412941
LLVMAtomicOrdering ordering,
29422942
LLVMBool singleThread);
2943+
LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
2944+
LLVMValueRef Cmp, LLVMValueRef New,
2945+
LLVMAtomicOrdering SuccessOrdering,
2946+
LLVMAtomicOrdering FailureOrdering,
2947+
LLVMBool SingleThread);
2948+
2949+
LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst);
2950+
void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool SingleThread);
2951+
2952+
LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst);
2953+
void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
2954+
LLVMAtomicOrdering Ordering);
2955+
LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst);
2956+
void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
2957+
LLVMAtomicOrdering Ordering);
29432958

29442959
/**
29452960
* @}

llvm/lib/IR/Core.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -2908,6 +2908,61 @@ LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
29082908
mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
29092909
}
29102910

2911+
LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
2912+
LLVMValueRef Cmp, LLVMValueRef New,
2913+
LLVMAtomicOrdering SuccessOrdering,
2914+
LLVMAtomicOrdering FailureOrdering,
2915+
LLVMBool singleThread) {
2916+
2917+
return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp),
2918+
unwrap(New), mapFromLLVMOrdering(SuccessOrdering),
2919+
mapFromLLVMOrdering(FailureOrdering),
2920+
singleThread ? SingleThread : CrossThread));
2921+
}
2922+
2923+
2924+
LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
2925+
Value *P = unwrap<Value>(AtomicInst);
2926+
2927+
if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
2928+
return I->getSynchScope() == SingleThread;
2929+
return cast<AtomicCmpXchgInst>(P)->getSynchScope() == SingleThread;
2930+
}
2931+
2932+
void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
2933+
Value *P = unwrap<Value>(AtomicInst);
2934+
SynchronizationScope Sync = NewValue ? SingleThread : CrossThread;
2935+
2936+
if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
2937+
return I->setSynchScope(Sync);
2938+
return cast<AtomicCmpXchgInst>(P)->setSynchScope(Sync);
2939+
}
2940+
2941+
LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
2942+
Value *P = unwrap<Value>(CmpXchgInst);
2943+
return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
2944+
}
2945+
2946+
void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
2947+
LLVMAtomicOrdering Ordering) {
2948+
Value *P = unwrap<Value>(CmpXchgInst);
2949+
AtomicOrdering O = mapFromLLVMOrdering(Ordering);
2950+
2951+
return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
2952+
}
2953+
2954+
LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
2955+
Value *P = unwrap<Value>(CmpXchgInst);
2956+
return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
2957+
}
2958+
2959+
void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
2960+
LLVMAtomicOrdering Ordering) {
2961+
Value *P = unwrap<Value>(CmpXchgInst);
2962+
AtomicOrdering O = mapFromLLVMOrdering(Ordering);
2963+
2964+
return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
2965+
}
29112966

29122967
/*===-- Module providers --------------------------------------------------===*/
29132968

llvm/test/Bindings/llvm-c/atomics.ll

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
; RUN: llvm-as < %s | llvm-dis > %t.orig
2+
; RUN: llvm-as < %s | llvm-c-test --echo > %t.echo
3+
; RUN: diff -w %t.orig %t.echo
4+
5+
define i32 @main() {
6+
%1 = alloca i32
7+
%2 = cmpxchg i32* %1, i32 2, i32 3 seq_cst acquire
8+
%3 = extractvalue { i32, i1 } %2, 0
9+
ret i32 %3
10+
}

llvm/tools/llvm-c-test/echo.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,17 @@ struct FunCloner {
522522
Dst = LLVMBuildGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
523523
break;
524524
}
525+
case LLVMAtomicCmpXchg: {
526+
LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
527+
LLVMValueRef Cmp = CloneValue(LLVMGetOperand(Src, 1));
528+
LLVMValueRef New = CloneValue(LLVMGetOperand(Src, 2));
529+
LLVMAtomicOrdering Succ = LLVMGetCmpXchgSuccessOrdering(Src);
530+
LLVMAtomicOrdering Fail = LLVMGetCmpXchgFailureOrdering(Src);
531+
LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
532+
533+
Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail,
534+
SingleThread);
535+
} break;
525536
case LLVMBitCast: {
526537
LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0));
527538
Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name);

0 commit comments

Comments
 (0)