-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor cache logic for easy replacement
This is the next step in the line of work started by #33955, though a lot of enabling work towards this was previously done by Jameson in his codegen-norecursion branch. The basic thrust here is to allow external packages to manage their own cache of compiled code that may have been generated using entirely difference inference or compiler options. The GPU compilers are one such example, but there are several others, including generating code using offload compilers, such as XLA or compilers for secure computation. A lot of this is just moving code arround to make it clear exactly which parts of the code are accessing the internal code cache (which is now its own type to make it obvious when it's being accessed), as well as providing clear extension points for custom cache implementations. The second part is to refactor CodeInstance construction to separate construction and insertion into the internal cache (so it can be inserted into an external cache instead if desired). The last part of the change is to give cgparams another hook that lets the caller replace the cache lookup to be used by codegen.
- Loading branch information
Showing
15 changed files
with
234 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
""" | ||
struct InternalCodeCache | ||
Internally, each `MethodInstance` keep a unique global cache of code instances | ||
that have been created for the given method instance, stratified by world age | ||
ranges. This struct abstracts over access to this cache. | ||
""" | ||
struct InternalCodeCache | ||
end | ||
|
||
function setindex!(cache::InternalCodeCache, ci::CodeInstance, mi::MethodInstance) | ||
ccall(:jl_mi_cache_insert, Cvoid, (Any, Any), mi, ci) | ||
end | ||
|
||
const GLOBAL_CI_CACHE = InternalCodeCache() | ||
|
||
""" | ||
struct WorldView | ||
Takes a given cache and provides access to the cache contents for the given | ||
range of world ages, rather than defaulting to the current active world age. | ||
""" | ||
struct WorldView{Cache} | ||
cache::Cache | ||
min_world::UInt | ||
max_world::UInt | ||
end | ||
WorldView(cache, r::UnitRange) = WorldView(cache, first(r), last(r)) | ||
WorldView(cache, world::UInt) = WorldView(cache, world, world) | ||
WorldView(wvc::WorldView{InternalCodeCache}, min_world::UInt, max_world::UInt) = | ||
WorldView(wvc.cache, min_world, max_world) | ||
|
||
function haskey(wvc::WorldView{InternalCodeCache}, mi::MethodInstance) | ||
ccall(:jl_rettype_inferred, Any, (Any, UInt, UInt), mi, wvc.min_world, wvc.max_world)::Union{Nothing, CodeInstance} !== nothing | ||
end | ||
|
||
function get(wvc::WorldView{InternalCodeCache}, mi::MethodInstance, default) | ||
r = ccall(:jl_rettype_inferred, Any, (Any, UInt, UInt), mi, wvc.min_world, wvc.max_world)::Union{Nothing, CodeInstance} | ||
if r === nothing | ||
return default | ||
end | ||
return r::CodeInstance | ||
end | ||
|
||
function getindex(wvc::WorldView{InternalCodeCache}, mi::MethodInstance) | ||
r = get(wvc, mi, nothing) | ||
r === nothing && throw(KeyError(mi)) | ||
return r::CodeInstance | ||
end | ||
|
||
setindex!(wvc::WorldView{InternalCodeCache}, ci::CodeInstance, mi::MethodInstance) = | ||
setindex!(wvc.cache, ci, mi) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.