Skip to content

Commit 111685f

Browse files
authored
Use CC.add_invalidation_callback! if available (#529)
JuliaLang/julia#51769 allows us to not traverse backedge list by ourselves.
1 parent 1d58bff commit 111685f

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

src/jlgen.jl

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,22 +180,51 @@ const GLOBAL_CI_CACHES_LOCK = ReentrantLock()
180180

181181
function CC.setindex!(cache::CodeCache, ci::CodeInstance, mi::MethodInstance)
182182
# make sure the invalidation callback is attached to the method instance
183-
callback(mi, max_world) = invalidate_code_cache(cache, mi, max_world)
183+
add_codecache_callback!(cache, mi)
184+
cis = get!(cache.dict, mi, CodeInstance[])
185+
push!(cis, ci)
186+
end
187+
188+
# invalidation (like invalidate_method_instance, but for our cache)
189+
struct CodeCacheCallback
190+
cache::CodeCache
191+
end
192+
193+
@static if VERSION v"1.11.0-DEV.798"
194+
195+
function add_codecache_callback!(cache::CodeCache, mi::MethodInstance)
196+
callback = CodeCacheCallback(cache)
197+
CC.add_invalidation_callback!(callback, mi)
198+
end
199+
function (callback::CodeCacheCallback)(replaced::MethodInstance, max_world::UInt32)
200+
cis = get(callback.cache.dict, replaced, nothing)
201+
if cis === nothing
202+
return
203+
end
204+
for ci in cis
205+
if ci.max_world == ~0 % Csize_t
206+
@assert ci.min_world - 1 <= max_world "attempting to set illogical constraints"
207+
ci.max_world = max_world
208+
end
209+
@assert ci.max_world <= max_world
210+
end
211+
end
212+
213+
else
214+
215+
function add_codecache_callback!(cache::CodeCache, mi::MethodInstance)
216+
callback = CodeCacheCallback(cache)
184217
if !isdefined(mi, :callbacks)
185218
mi.callbacks = Any[callback]
186219
elseif !in(callback, mi.callbacks)
187220
push!(mi.callbacks, callback)
188221
end
189-
190-
cis = get!(cache.dict, mi, CodeInstance[])
191-
push!(cis, ci)
192222
end
193-
194-
# invalidation (like invalidate_method_instance, but for our cache)
195-
function invalidate_code_cache(cache::CodeCache, replaced::MethodInstance, max_world, seen=Set{MethodInstance}())
223+
function (callback::CodeCacheCallback)(replaced::MethodInstance, max_world::UInt32,
224+
seen::Set{MethodInstance}=Set{MethodInstance}())
196225
push!(seen, replaced)
197226

198-
cis = get(cache.dict, replaced, nothing)
227+
cis = get(callback.cache.dict, replaced, nothing)
199228
if cis === nothing
200229
return
201230
end
@@ -225,11 +254,12 @@ function invalidate_code_cache(cache::CodeCache, replaced::MethodInstance, max_w
225254
# replaced.backedges = Any[]
226255

227256
for mi in backedges
228-
invalidate_code_cache(cache, mi::MethodInstance, max_world, seen)
257+
callback(mi::MethodInstance, max_world, seen)
229258
end
230259
end
231260
end
232261

262+
end
233263

234264
## method overrides
235265

0 commit comments

Comments
 (0)