Skip to content

Commit

Permalink
fixup! fixup! add back old code
Browse files Browse the repository at this point in the history
  • Loading branch information
vchuravy committed Dec 5, 2024
1 parent f66264e commit 06675ab
Showing 1 changed file with 3 additions and 85 deletions.
88 changes: 3 additions & 85 deletions ext/UnsafeAtomicsLLVM/atomics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -240,105 +240,23 @@ const AtomicRMWBinOpVal = Union{(Val{binop} for (_, _, binop) in binoptable)...}
end
end

@generated function llvm_atomic_op(
binop::AtomicRMWBinOpVal,
ptr::LLVMPtr{T,A},
val::T,
order::LLVMOrderingVal,
syncscope::Val{S},
) where {T,A,S}
@dispose ctx = Context() begin
T_val = convert(LLVMType, T)
T_ptr = convert(LLVMType, ptr)

T_typed_ptr = LLVM.PointerType(T_val, A)
llvm_f, _ = create_function(T_val, [T_ptr, T_val])

@dispose builder = IRBuilder() begin
entry = BasicBlock(llvm_f, "entry")
position!(builder, entry)

typed_ptr = bitcast!(builder, parameters(llvm_f)[1], T_typed_ptr)
rv = atomic_rmw!(
builder,
_valueof(binop()),
typed_ptr,
parameters(llvm_f)[2],
_valueof(order()),
syncscope_to_string(syncscope),
)

ret!(builder, rv)
end
call_function(llvm_f, T, Tuple{LLVMPtr{T,A},T}, :ptr, :val)
end
end

@inline function atomic_pointermodify(
ptr::LLVMPtr{T},
::typeof(right),
x::T,
order::AtomicOrdering,
) where {T}
sync::Val{S}
) where {T, S}
old = llvm_atomic_op(
Val(LLVM.API.LLVMAtomicRMWBinOpXchg),
ptr,
x,
llvm_from_julia_ordering(order),
sync
)
return old => x
end

const atomictypes = Any[
Int8,
Int16,
Int32,
Int64,
Int128,
UInt8,
UInt16,
UInt32,
UInt64,
UInt128,
Float16,
Float32,
Float64,
]

for (opname, op, llvmop) in binoptable
opname === :xchg && continue
types = if opname in (:min, :max)
filter(t -> t <: Signed, atomictypes)
elseif opname in (:umin, :umax)
filter(t -> t <: Unsigned, atomictypes)
elseif opname in (:fadd, :fsub, :fmin, :fmax)
filter(t -> t <: AbstractFloat, atomictypes)
else
filter(t -> t <: Integer, atomictypes)
end
for T in types
@eval @inline function atomic_pointermodify(
ptr::LLVMPtr{$T},
::$(typeof(op)),
x::$T,
order::AtomicOrdering,
syncscope::Val{S} = Val{:system}(),
) where {S}
old =
syncscope isa Val{:system} ?
llvm_atomic_op($(Val(llvmop)), ptr, x, llvm_from_julia_ordering(order)) :
llvm_atomic_op(
$(Val(llvmop)),
ptr,
x,
llvm_from_julia_ordering(order),
syncscope,
)
return old => $op(old, x)
end
end
end

@inline atomic_pointerswap(pointer, new) = first(atomic_pointermodify(pointer, right, new))
@inline atomic_pointerswap(pointer, new, order) =
first(atomic_pointermodify(pointer, right, new, order))
Expand Down

0 comments on commit 06675ab

Please sign in to comment.