Skip to content

Commit 13cae6b

Browse files
committed
use object_build_id
1 parent 397825d commit 13cae6b

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

src/execution.jl

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,6 @@ end
7878

7979
cache_path() = @get_scratch!("cache")
8080
clear_disk_cache!() = rm(cache_path(); recursive=true, force=true)
81-
function cache_path(key)
82-
return joinpath(
83-
cache_path(),
84-
# TODO: Use object_build_id from https://github.com/JuliaLang/julia/pull/53943
85-
# Should we disk cache "runtime compilation".
86-
string(Base.module_build_id(GPUCompiler)), # captures dependencies as well
87-
string(cache_key), "ir.jls")
88-
end
8981

9082
const cache_lock = ReentrantLock()
9183

@@ -133,6 +125,30 @@ function cached_compilation(cache::AbstractDict{<:Any,V},
133125
return obj::V
134126
end
135127

128+
@noinline function cache_file(ci::CodeInstance, cfg::CompilerConfig)
129+
@static if isdefined(Base, :object_build_id)
130+
id = Base.object_build_id(ci)
131+
if id === nothing # CI is from a runtime compilation, not worth caching on disk
132+
return nothing
133+
else
134+
id = id % UInt64 # The upper 64bit are a checksum, unavailable during precompilation
135+
end
136+
else
137+
id = Base.objectid(ci)
138+
end
139+
140+
gpucompiler_buildid = Base.module_build_id(@__MODULE__)
141+
if (gpucompiler_buildid >> 64) % UInt64 == 0xffffffffffffffff
142+
return nothing # Don't cache during precompilation of GPUCompiler
143+
end
144+
145+
return joinpath(
146+
cache_path(),
147+
# bifurcate the cache by build id of GPUCompiler
148+
string(gpucompiler_buildid),
149+
string(hash(cfg, hash(id)), ".jls"))
150+
end
151+
136152
@noinline function actual_compilation(cache::AbstractDict, src::MethodInstance, world::UInt,
137153
cfg::CompilerConfig, compiler::Function, linker::Function)
138154
job = CompilerJob(src, cfg, world)
@@ -148,18 +164,22 @@ end
148164
# slow path: compile and link
149165
if obj === nothing || compile_hook[] !== nothing
150166
asm = nothing
151-
@static if VERSION >= v"1.11.0-" && disk_cache()
152-
cache_key = Base.objectid(ci)
153-
path = cache_path(cache_key)
154-
if isfile(path)
155-
try
156-
@debug "Loading compiled kernel for $spec from $path"
157-
asm = deserialize(path)
158-
catch ex
159-
@warn "Failed to load compiled kernel at $path" exception=(ex, catch_backtrace())
167+
path = nothing
168+
ondisk_hit = false
169+
@static if VERSION >= v"1.11.0-"
170+
if disk_cache() # TODO: (Should we allow backends to opt out?)
171+
path = cache_file(ci, cfg)
172+
if path !== nothing && isfile(path)
173+
ondisk_hit = true
174+
try
175+
@debug "Loading compiled kernel for $spec from $path"
176+
asm = deserialize(path)
177+
catch ex
178+
@warn "Failed to load compiled kernel at $path" exception=(ex, catch_backtrace())
179+
end
160180
end
161181
end
162-
else
182+
end
163183

164184
if asm === nothing
165185
asm = compiler(job)
@@ -169,13 +189,15 @@ end
169189
return obj
170190
end
171191

172-
@static if VERSION >= v"1.11.0-" && disk_cache() && !isfile(path)
173-
# TODO: Should we only write out during precompilation?
174-
tmppath, io = mktemp(;cleanup=false)
175-
serialize(io, asm)
176-
close(io)
177-
# atomic move
178-
Base.rename(tmppath, path, force=true)
192+
@static if VERSION >= v"1.11.0-"
193+
if !ondisk_hit && path !== nothing && disk_cache()
194+
# TODO: Do we want to serialize some more metadata to make sure the asm matches?
195+
tmppath, io = mktemp(;cleanup=false)
196+
serialize(io, asm)
197+
close(io)
198+
# atomic move
199+
Base.rename(tmppath, path, force=true)
200+
end
179201
end
180202

181203
obj = linker(job, asm)

0 commit comments

Comments
 (0)