Skip to content

Commit daf0be4

Browse files
authored
update to opaque ptr (#22)
1 parent 0437ba7 commit daf0be4

4 files changed

Lines changed: 44 additions & 19 deletions

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ jobs:
1515
julia-version:
1616
- 'lts'
1717
- '1.11'
18-
- '1.12-nightly'
18+
- '1.12'
19+
- '1.13-nightly'
1920
- 'nightly'
2021
fail-fast: false
2122
name: Test Julia ${{ matrix.julia-version }}

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "UnsafeAtomics"
22
uuid = "013be700-e6cd-48c3-b4a1-df204f14c38f"
33
authors = ["Takafumi Arakaki <aka.tkf@gmail.com> and contributors"]
4-
version = "0.3.0"
4+
version = "0.3.1"
55

66
[weakdeps]
77
LLVM = "929cbde3-209d-540e-8aea-75f648917ca0"

ext/UnsafeAtomicsLLVM/atomics.jl

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,18 @@ end
137137
if sizeof(T) == 0
138138
# Mimicking what `Core.Intrinsics.atomic_pointerset` generates.
139139
# See: https://github.com/JuliaLang/julia/blob/v1.7.2/src/cgutils.cpp#L1570-L1572
140-
return quote
141-
is_stronger_than_monotonic(_valueof(order)) || return ptr
142-
Core.Intrinsics.atomic_fence(_valueof(order))
143-
return ptr
140+
if VERSION < v"1.14.0-DEV.1371"
141+
return quote
142+
is_stronger_than_monotonic(_valueof(order)) || return ptr
143+
Core.Intrinsics.atomic_fence(_valueof(order))
144+
return ptr
145+
end
146+
else
147+
return quote
148+
is_stronger_than_monotonic(_valueof(order)) || return ptr
149+
Core.Intrinsics.atomic_fence(_valueof(order), :system)
150+
return ptr
151+
end
144152
end
145153
end
146154
llvm_order = _valueof(llvm_from_julia_ordering(order()))

src/core.jl

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,19 @@ const MAX_ATOMIC_SIZE = 8
6767
const MAX_POINTERATOMIC_SIZE = 8
6868
end
6969

70+
71+
if VERSION < v"1.12.0"
72+
ptr(typ) = typ*"*"
73+
inttoptr(typ, arg) = "inttoptr i$WORD_SIZE $arg to $(ptr(typ))"
74+
else
75+
ptr(typ) = "ptr"
76+
inttoptr(_, arg) = "bitcast ptr $arg to ptr"
77+
end
78+
7079
# Based on: https://github.com/JuliaLang/julia/blob/v1.6.3/base/atomics.jl
7180
for typ in (inttypes..., floattypes...)
7281
lt = llvmtypes[typ]
73-
rt = "$lt, $lt*"
82+
rt = "$lt, $(ptr(lt))"
7483

7584
for ord in orderings
7685
ord in (release, acq_rel) && continue
@@ -84,7 +93,7 @@ for typ in (inttypes..., floattypes...)
8493
@eval function UnsafeAtomics.load(x::Ptr{$typ}, ::$(typeof(ord)), ::$(typeof(sync)))
8594
return llvmcall(
8695
$("""
87-
%ptr = inttoptr i$WORD_SIZE %0 to $lt*
96+
%ptr = $(inttoptr(lt, "%0"))
8897
%rv = load atomic $rt %ptr $ord, align $(sizeof(typ))
8998
ret $lt %rv
9099
"""),
@@ -110,8 +119,8 @@ for typ in (inttypes..., floattypes...)
110119
@eval function UnsafeAtomics.store!(x::Ptr{$typ}, v::$typ, ::$(typeof(ord)), ::$(typeof(sync)))
111120
return llvmcall(
112121
$("""
113-
%ptr = inttoptr i$WORD_SIZE %0 to $lt*
114-
store atomic $lt %1, $lt* %ptr $ord, align $(sizeof(typ))
122+
%ptr = $(inttoptr(lt, "%0"))
123+
store atomic $lt %1, $(ptr(lt)) %ptr $ord, align $(sizeof(typ))
115124
ret void
116125
"""),
117126
Cvoid,
@@ -161,13 +170,13 @@ for typ in (inttypes..., floattypes...)
161170
old = llvmcall(
162171
$(
163172
"""
164-
%ptr = inttoptr i$WORD_SIZE %0 to $lt*
165-
%rs = cmpxchg $lt* %ptr, $lt %1, $lt %2 $success_ordering $failure_ordering
173+
%ptr = $(inttoptr(lt, "%0"))
174+
%rs = cmpxchg $(ptr(lt)) %ptr, $lt %1, $lt %2 $success_ordering $failure_ordering
166175
%rv = extractvalue { $lt, i1 } %rs, 0
167176
%s1 = extractvalue { $lt, i1 } %rs, 1
168177
%s8 = zext i1 %s1 to i8
169-
%sptr = inttoptr i$WORD_SIZE %3 to i8*
170-
store i8 %s8, i8* %sptr
178+
%sptr = $(inttoptr("i8", "%3"))
179+
store i8 %s8, $(ptr("i8")) %sptr
171180
ret $lt %rv
172181
"""
173182
),
@@ -224,8 +233,8 @@ for typ in (inttypes..., floattypes...)
224233
)
225234
old = llvmcall(
226235
$("""
227-
%ptr = inttoptr i$WORD_SIZE %0 to $lt*
228-
%rv = atomicrmw $rmw $lt* %ptr, $lt %1 $ord
236+
%ptr = $(inttoptr(lt, "%0"))
237+
%rv = atomicrmw $rmw $(ptr(lt)) %ptr, $lt %1 $ord
229238
ret $lt %rv
230239
"""),
231240
$typ,
@@ -244,9 +253,16 @@ end
244253
for sync in syncscopes
245254
if sync == none
246255
# Core.Intrinsics.atomic_fence was introduced in 1.10
247-
@eval function UnsafeAtomics.fence(ord::Ordering, ::$(typeof(sync)))
248-
Core.Intrinsics.atomic_fence(base_ordering(ord))
249-
return nothing
256+
if VERSION < v"1.14.0-DEV.1371"
257+
@eval function UnsafeAtomics.fence(ord::Ordering, ::$(typeof(sync)))
258+
Core.Intrinsics.atomic_fence(base_ordering(ord))
259+
return nothing
260+
end
261+
else
262+
@eval function UnsafeAtomics.fence(ord::Ordering, ::$(typeof(sync)))
263+
Core.Intrinsics.atomic_fence(base_ordering(ord), :system)
264+
return nothing
265+
end
250266
end
251267
if Sys.ARCH == :x86_64
252268
# FIXME: Disable this once on LLVM 19

0 commit comments

Comments
 (0)