Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use InternalCodeCache #534

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,19 @@ valid_function_pointer(@nospecialize(job::CompilerJob), ptr::Ptr{Cvoid}) = false

# the codeinfo cache to use
function ci_cache(@nospecialize(job::CompilerJob))
lock(GLOBAL_CI_CACHES_LOCK) do
cache = get!(GLOBAL_CI_CACHES, job.config) do
CodeCache()
@static if isdefined(Core.Compiler, :cache_owner)
# CompilerConfig contains
# - name
# - params (Enzyme puts info here...)
# unecessary bifurcation
return Core.Compiler.InternalCodeCache(job.config)
else
lock(GLOBAL_CI_CACHES_LOCK) do
cache = get!(GLOBAL_CI_CACHES, job.config) do
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maleadt Already now I feel like we are bifurcating the caches to much.

I think this means Enzyme creates a fresh inference cache per top-level request,
due to us using EnzymeCompilerParams to stash information.

https://github.com/EnzymeAD/Enzyme.jl/blob/3f641088fa39cf0cfb00ffd9e4b8c8200564dbfc/src/compiler.jl#L2477

cc: @wsmoses

CodeCache()
end
return cache
end
return cache
end
end

Expand Down
23 changes: 18 additions & 5 deletions src/jlgen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ methodinstance(f, tt) = methodinstance(f, tt, tls_world_age())

end


if !isdefined(CC, :cache_owner)
## code instance cache

struct CodeCache
Expand Down Expand Up @@ -229,6 +229,9 @@ function invalidate_code_cache(cache::CodeCache, replaced::MethodInstance, max_w
end
end
end
else
const CodeCache = CC.InternalCodeCache
end #CodeCache


## method overrides
Expand Down Expand Up @@ -292,9 +295,13 @@ CC.InferenceParams(interp::GPUInterpreter) = interp.inf_params
CC.OptimizationParams(interp::GPUInterpreter) = interp.opt_params
CC.get_world_counter(interp::GPUInterpreter) = interp.world
CC.get_inference_cache(interp::GPUInterpreter) = interp.inf_cache
CC.code_cache(interp::GPUInterpreter) = WorldView(interp.code_cache, interp.world)
CC.code_cache(interp::GPUInterpreter) = CC.WorldView(interp.code_cache, interp.world)
if isdefined(CC, :cache_owner)
CC.cache_owner(interp::GPUCompiler.GPUInterpreter) = interp.code_cache.owner
end

# No need to do any locking since we're not putting our results into the runtime cache
# Q(@aviatesk) do we need to lock?
CC.lock_mi_inference(interp::GPUInterpreter, mi::MethodInstance) = nothing
CC.unlock_mi_inference(interp::GPUInterpreter, mi::MethodInstance) = nothing

Expand Down Expand Up @@ -342,7 +349,7 @@ function CC.concrete_eval_eligible(interp::GPUInterpreter,
end
end


if !isdefined(CC, :cache_owner)
## world view of the cache

using Core.Compiler: WorldView
Expand Down Expand Up @@ -384,6 +391,7 @@ function CC.setindex!(wvc::WorldView{CodeCache}, ci::CodeInstance, mi::MethodIns
end
CC.setindex!(wvc.cache, ci, mi)
end
end


## codegen/inference integration
Expand All @@ -392,7 +400,7 @@ function ci_cache_populate(interp, cache, mi, min_world, max_world)
src = CC.typeinf_ext_toplevel(interp, mi)

# inference populates the cache, so we don't need to jl_get_method_inferred
wvc = WorldView(cache, min_world, max_world)
wvc = CC.WorldView(cache, min_world, max_world)
@assert CC.haskey(wvc, mi)

# if src is rettyp_const, the codeinfo won't cache ci.inferred
Expand All @@ -411,14 +419,19 @@ function ci_cache_populate(interp, cache, mi, min_world, max_world)
end

function ci_cache_lookup(cache, mi, min_world, max_world)
wvc = WorldView(cache, min_world, max_world)
wvc = CC.WorldView(cache, min_world, max_world)
ci = CC.get(wvc, mi, nothing)
if ci !== nothing && ci.inferred === nothing
# if for some reason we did end up with a codeinfo without inferred source, e.g.,
# because of calling `Base.return_types` which only sets rettyp, pretend we didn't
# run inference so that we re-infer now and not during codegen (which is disallowed)
return nothing
end
@static if isdefined(CC, :cache_owner)
if ci !== nothing
@assert ci.owner isa CompilerConfig
end
end
return ci
end

Expand Down