From 8f05136256d9c9a3fd4b0543fe4e6088cc4f3a38 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Sat, 30 Nov 2024 14:44:23 +0100 Subject: [PATCH 1/4] Add test for Zero-sized types --- test/runtests.jl | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index 9ee4bfe..c0556cd 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -10,6 +10,10 @@ function check_default_ordering(T::Type) xs = T[rand(T), rand(T)] x1 = rand(T) x2 = rand(T) + check_default_ordering(xs, x1, x2) +end + +function check_default_ordering(xs::AbstractArray{T}, x1::T, x2::T,) where T @debug "xs=$(repr(xs)) x1=$(repr(x1)) x2=$(repr(x2))" ptr = llvmptr(xs, 1) @@ -17,6 +21,7 @@ function check_default_ordering(T::Type) @test UnsafeAtomics.load(ptr) === xs[1] UnsafeAtomics.store!(ptr, x1) @test xs[1] === x1 + sizeof(T) == 0 && return # CAS hangs on zero sized data... desired = (old = x1, success = true) @test UnsafeAtomics.cas!(ptr, x1, x2) === (old = x1, success = true) @test xs[1] === x2 @@ -37,6 +42,10 @@ function test_explicit_ordering(T::Type = UInt) xs = T[rand(T), rand(T)] x1 = rand(T) x2 = rand(T) + test_explicit_ordering(xs, x1, x2) +end + +function test_explicit_ordering(xs::AbstractArray{T}, x1::T, x2::T,) where T @debug "xs=$(repr(xs)) x1=$(repr(x1)) x2=$(repr(x2))" ptr = llvmptr(xs, 1) @@ -45,6 +54,7 @@ function test_explicit_ordering(T::Type = UInt) @test UnsafeAtomics.load(ptr, acquire) === xs[1] UnsafeAtomics.store!(ptr, x1, release) @test xs[1] === x1 + sizeof(T) == 0 && return # CAS hangs on zero sized data... desired = (old = x1, success = true) @test UnsafeAtomics.cas!(ptr, x1, x2, acq_rel, acquire) === desired @test xs[1] === x2 @@ -79,4 +89,10 @@ end check_default_ordering(T) test_explicit_ordering(T) end + + @testset "Zero-sized types" begin + @test sizeof(Nothing) == 0 + check_default_ordering([nothing, nothing], nothing, nothing) + test_explicit_ordering([nothing, nothing], nothing, nothing) + end end From 2cbcfe99e5b2067302499f3edce45f58cf155919 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Sat, 30 Nov 2024 14:47:41 +0100 Subject: [PATCH 2/4] Fix fence usage --- src/atomics.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/atomics.jl b/src/atomics.jl index 9b1e504..6a9c8f2 100644 --- a/src/atomics.jl +++ b/src/atomics.jl @@ -191,9 +191,9 @@ end if sizeof(T) == 0 # Mimicking what `Core.Intrinsics.atomic_pointerset` generates. # See: https://github.com/JuliaLang/julia/blob/v1.7.2/src/cgutils.cpp#L1570-L1572 - is_stronger_than_monotonic(order) || return :ptr return quote - Core.Intrinsics.fence($(QuoteNode(order))) + is_stronger_than_monotonic(_valueof(order)) || return :ptr + Core.Intrinsics.atomic_fence(_valueof(order)) ptr end end From a4143650e2e8975a06fc9d5f97e8d55b3d08e43e Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Sat, 30 Nov 2024 14:48:12 +0100 Subject: [PATCH 3/4] Bump package version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 5c4191f..1a8a8f6 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "UnsafeAtomicsLLVM" uuid = "d80eeb9a-aca5-4d75-85e5-170c8b632249" authors = ["Takafumi Arakaki and contributors"] -version = "0.2.1" +version = "0.2.2" [deps] LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" From f890dcc57c059ad37e50489e5a1850dc3e25389f Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Sat, 30 Nov 2024 14:50:47 +0100 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/runtests.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index c0556cd..da5139a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -13,7 +13,7 @@ function check_default_ordering(T::Type) check_default_ordering(xs, x1, x2) end -function check_default_ordering(xs::AbstractArray{T}, x1::T, x2::T,) where T +function check_default_ordering(xs::AbstractArray{T}, x1::T, x2::T) where {T} @debug "xs=$(repr(xs)) x1=$(repr(x1)) x2=$(repr(x2))" ptr = llvmptr(xs, 1) @@ -45,7 +45,7 @@ function test_explicit_ordering(T::Type = UInt) test_explicit_ordering(xs, x1, x2) end -function test_explicit_ordering(xs::AbstractArray{T}, x1::T, x2::T,) where T +function test_explicit_ordering(xs::AbstractArray{T}, x1::T, x2::T) where {T} @debug "xs=$(repr(xs)) x1=$(repr(x1)) x2=$(repr(x2))" ptr = llvmptr(xs, 1)