78
78
79
79
cache_path () = @get_scratch! (" cache" )
80
80
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
89
81
90
82
const cache_lock = ReentrantLock ()
91
83
@@ -133,6 +125,30 @@ function cached_compilation(cache::AbstractDict{<:Any,V},
133
125
return obj:: V
134
126
end
135
127
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
+
136
152
@noinline function actual_compilation (cache:: AbstractDict , src:: MethodInstance , world:: UInt ,
137
153
cfg:: CompilerConfig , compiler:: Function , linker:: Function )
138
154
job = CompilerJob (src, cfg, world)
@@ -148,18 +164,22 @@ end
148
164
# slow path: compile and link
149
165
if obj === nothing || compile_hook[] != = nothing
150
166
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
160
180
end
161
181
end
162
- else
182
+ end
163
183
164
184
if asm === nothing
165
185
asm = compiler (job)
@@ -169,13 +189,15 @@ end
169
189
return obj
170
190
end
171
191
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
179
201
end
180
202
181
203
obj = linker (job, asm)
0 commit comments