Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add atomic_rmw! with syncscope #431

Merged
merged 6 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions deps/LLVMExtra/include/LLVMExtra.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,5 +261,10 @@ LLVMErrorRef LLVMRunJuliaPasses(LLVMModuleRef M, const char *Passes,
LLVMTargetMachineRef TM, LLVMPassBuilderOptionsRef Options,
LLVMPassBuilderExtensionsRef Extensions);

LLVMValueRef LLVMBuildAtomicRMWSyncScope(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
LLVMValueRef PTR, LLVMValueRef Val,
LLVMAtomicOrdering ordering,
const char* syncscope);

LLVM_C_EXTERN_C_END
#endif
49 changes: 49 additions & 0 deletions deps/LLVMExtra/lib/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,4 +731,53 @@ void LLVMSetMetadata2(LLVMValueRef Ref, unsigned KindID, LLVMValueRef MAV) {
assert(0 && "Expected an instruction or a global object");
}

static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
switch (Ordering) {
case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
case LLVMAtomicOrderingAcquireRelease:
return AtomicOrdering::AcquireRelease;
case LLVMAtomicOrderingSequentiallyConsistent:
return AtomicOrdering::SequentiallyConsistent;
}

llvm_unreachable("Invalid LLVMAtomicOrdering value!");
}

static AtomicRMWInst::BinOp mapFromLLVMRMWBinOp(LLVMAtomicRMWBinOp BinOp) {
switch (BinOp) {
case LLVMAtomicRMWBinOpXchg: return AtomicRMWInst::Xchg;
case LLVMAtomicRMWBinOpAdd: return AtomicRMWInst::Add;
case LLVMAtomicRMWBinOpSub: return AtomicRMWInst::Sub;
case LLVMAtomicRMWBinOpAnd: return AtomicRMWInst::And;
case LLVMAtomicRMWBinOpNand: return AtomicRMWInst::Nand;
case LLVMAtomicRMWBinOpOr: return AtomicRMWInst::Or;
case LLVMAtomicRMWBinOpXor: return AtomicRMWInst::Xor;
case LLVMAtomicRMWBinOpMax: return AtomicRMWInst::Max;
case LLVMAtomicRMWBinOpMin: return AtomicRMWInst::Min;
case LLVMAtomicRMWBinOpUMax: return AtomicRMWInst::UMax;
case LLVMAtomicRMWBinOpUMin: return AtomicRMWInst::UMin;
case LLVMAtomicRMWBinOpFAdd: return AtomicRMWInst::FAdd;
case LLVMAtomicRMWBinOpFSub: return AtomicRMWInst::FSub;
case LLVMAtomicRMWBinOpFMax: return AtomicRMWInst::FMax;
case LLVMAtomicRMWBinOpFMin: return AtomicRMWInst::FMin;
}

llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");
}

LLVMValueRef LLVMBuildAtomicRMWSyncScope(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
LLVMValueRef PTR, LLVMValueRef Val,
LLVMAtomicOrdering ordering,
const char* syncscope) {
AtomicRMWInst::BinOp intop = mapFromLLVMRMWBinOp(op);
return wrap(unwrap(B)->CreateAtomicRMW(
intop, unwrap(PTR), unwrap(Val), MaybeAlign(),
mapFromLLVMOrdering(ordering),
unwrap(LLVMGetBuilderContext(B))->getOrInsertSyncScopeID(syncscope)));
}

#endif
4 changes: 4 additions & 0 deletions lib/13/libLLVM_extra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,7 @@ function LLVMRunJuliaPasses(M, Passes, TM, Options, Extensions)
ccall((:LLVMRunJuliaPasses, libLLVMExtra), LLVMErrorRef, (LLVMModuleRef, Cstring, LLVMTargetMachineRef, LLVMPassBuilderOptionsRef, LLVMPassBuilderExtensionsRef), M, Passes, TM, Options, Extensions)
end

function LLVMBuildAtomicRMWSyncScope(B, op, PTR, Val, ordering, syncscope)
ccall((:LLVMBuildAtomicRMWSyncScope, libLLVMExtra), LLVMValueRef, (LLVMBuilderRef, LLVMAtomicRMWBinOp, LLVMValueRef, LLVMValueRef, LLVMAtomicOrdering, Cstring), B, op, PTR, Val, ordering, syncscope)
end

4 changes: 4 additions & 0 deletions lib/14/libLLVM_extra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,7 @@ function LLVMRunJuliaPasses(M, Passes, TM, Options, Extensions)
ccall((:LLVMRunJuliaPasses, libLLVMExtra), LLVMErrorRef, (LLVMModuleRef, Cstring, LLVMTargetMachineRef, LLVMPassBuilderOptionsRef, LLVMPassBuilderExtensionsRef), M, Passes, TM, Options, Extensions)
end

function LLVMBuildAtomicRMWSyncScope(B, op, PTR, Val, ordering, syncscope)
ccall((:LLVMBuildAtomicRMWSyncScope, libLLVMExtra), LLVMValueRef, (LLVMBuilderRef, LLVMAtomicRMWBinOp, LLVMValueRef, LLVMValueRef, LLVMAtomicOrdering, Cstring), B, op, PTR, Val, ordering, syncscope)
end

4 changes: 4 additions & 0 deletions lib/15/libLLVM_extra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,7 @@ function LLVMRunJuliaPasses(M, Passes, TM, Options, Extensions)
ccall((:LLVMRunJuliaPasses, libLLVMExtra), LLVMErrorRef, (LLVMModuleRef, Cstring, LLVMTargetMachineRef, LLVMPassBuilderOptionsRef, LLVMPassBuilderExtensionsRef), M, Passes, TM, Options, Extensions)
end

function LLVMBuildAtomicRMWSyncScope(B, op, PTR, Val, ordering, syncscope)
ccall((:LLVMBuildAtomicRMWSyncScope, libLLVMExtra), LLVMValueRef, (LLVMBuilderRef, LLVMAtomicRMWBinOp, LLVMValueRef, LLVMValueRef, LLVMAtomicOrdering, Cstring), B, op, PTR, Val, ordering, syncscope)
end

4 changes: 4 additions & 0 deletions lib/16/libLLVM_extra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,7 @@ function LLVMRunJuliaPasses(M, Passes, TM, Options, Extensions)
ccall((:LLVMRunJuliaPasses, libLLVMExtra), LLVMErrorRef, (LLVMModuleRef, Cstring, LLVMTargetMachineRef, LLVMPassBuilderOptionsRef, LLVMPassBuilderExtensionsRef), M, Passes, TM, Options, Extensions)
end

function LLVMBuildAtomicRMWSyncScope(B, op, PTR, Val, ordering, syncscope)
ccall((:LLVMBuildAtomicRMWSyncScope, libLLVMExtra), LLVMValueRef, (LLVMBuilderRef, LLVMAtomicRMWBinOp, LLVMValueRef, LLVMValueRef, LLVMAtomicOrdering, Cstring), B, op, PTR, Val, ordering, syncscope)
end

4 changes: 4 additions & 0 deletions lib/17/libLLVM_extra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,7 @@ function LLVMRunJuliaPasses(M, Passes, TM, Options, Extensions)
ccall((:LLVMRunJuliaPasses, libLLVMExtra), LLVMErrorRef, (LLVMModuleRef, Cstring, LLVMTargetMachineRef, LLVMPassBuilderOptionsRef, LLVMPassBuilderExtensionsRef), M, Passes, TM, Options, Extensions)
end

function LLVMBuildAtomicRMWSyncScope(B, op, PTR, Val, ordering, syncscope)
ccall((:LLVMBuildAtomicRMWSyncScope, libLLVMExtra), LLVMValueRef, (LLVMBuilderRef, LLVMAtomicRMWBinOp, LLVMValueRef, LLVMValueRef, LLVMAtomicOrdering, Cstring), B, op, PTR, Val, ordering, syncscope)
end

4 changes: 4 additions & 0 deletions src/irbuilder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ atomic_rmw!(builder::IRBuilder, op::API.LLVMAtomicRMWBinOp, Ptr::Value, Val::Val
Instruction(API.LLVMBuildAtomicRMW(builder, op, Ptr, Val, ordering,
singleThread))

atomic_rmw!(builder::IRBuilder, op::API.LLVMAtomicRMWBinOp, Ptr::Value, Val::Value,
ordering::API.LLVMAtomicOrdering, syncscope::String) =
Instruction(API.LLVMBuildAtomicRMWSyncScope(builder, op, Ptr, Val, ordering, syncscope))

atomic_cmpxchg!(builder::IRBuilder, Ptr::Value, Cmp::Value, New::Value,
SuccessOrdering::API.LLVMAtomicOrdering,
FailureOrdering::API.LLVMAtomicOrdering, SingleThread::Bool) =
Expand Down
22 changes: 22 additions & 0 deletions test/instructions_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,28 @@
@check_ir gepinst1 "getelementptr inbounds i32, ptr %4, i32 %0"
end

single_thread = false
atomic_rmw_inst = atomic_rmw!(builder,
LLVM.API.LLVMAtomicRMWBinOpAdd, int1, int2,
LLVM.API.LLVMAtomicOrderingSequentiallyConsistent, single_thread)
@check_ir atomic_rmw_inst "atomicrmw add i32 %0, i32 %1 seq_cst"

single_thread = true
atomic_rmw_inst = atomic_rmw!(builder,
LLVM.API.LLVMAtomicRMWBinOpAdd, int1, int2,
LLVM.API.LLVMAtomicOrderingSequentiallyConsistent, single_thread)
@check_ir atomic_rmw_inst "atomicrmw add i32 %0, i32 %1 syncscope(\"singlethread\") seq_cst"

atomic_rmw_inst = atomic_rmw!(builder,
LLVM.API.LLVMAtomicRMWBinOpAdd, int1, int2,
LLVM.API.LLVMAtomicOrderingSequentiallyConsistent, "agent")
@check_ir atomic_rmw_inst "atomicrmw add i32 %0, i32 %1 syncscope(\"agent\") seq_cst"

atomic_rmw_inst = atomic_rmw!(builder,
LLVM.API.LLVMAtomicRMWBinOpAdd, int1, int2,
LLVM.API.LLVMAtomicOrderingMonotonic, "agent")
@check_ir atomic_rmw_inst "atomicrmw add i32 %0, i32 %1 syncscope(\"agent\") monotonic"

truncinst = trunc!(builder, int1, LLVM.Int16Type())
@check_ir truncinst "trunc i32 %0 to i16"

Expand Down
Loading