Skip to content

Commit 64c4c0c

Browse files
authored
Cache the runtime library in memory. (#699)
* Cache the runtime library in memory. * SPIRV: Simplify process spawning.
2 parents 046ceba + b1f8912 commit 64c4c0c

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

src/rtlib.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ end
121121

122122
const runtime_lock = ReentrantLock()
123123

124+
const runtime_cache = Dict{String, Vector{UInt8}}()
125+
124126
@locked function load_runtime(@nospecialize(job::CompilerJob))
125127
global compile_cache
126128
if compile_cache === nothing # during precompilation
@@ -135,15 +137,14 @@ const runtime_lock = ReentrantLock()
135137
name = "runtime_$(slug).bc"
136138
path = joinpath(compile_cache, name)
137139

138-
lib = try
139-
if ispath(path)
140-
open(path) do io
141-
parse(LLVM.Module, read(io))
142-
end
140+
# cache the runtime library on disk and in memory
141+
lib = if haskey(runtime_cache, slug)
142+
parse(LLVM.Module, runtime_cache[slug])
143+
elseif ispath(path)
144+
runtime_cache[slug] = open(path) do io
145+
read(io)
143146
end
144-
catch ex
145-
@warn "Failed to load GPU runtime library at $path" exception=(ex, catch_backtrace())
146-
nothing
147+
parse(LLVM.Module, runtime_cache[slug])
147148
end
148149

149150
if lib === nothing

src/spirv.jl

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,32 +148,33 @@ end
148148
cmd = `$(cmd) --spirv-max-version=$(job.config.target.version.major).$(job.config.target.version.minor)`
149149
end
150150
end
151-
proc = run(ignorestatus(cmd))
152-
if !success(proc)
151+
try
152+
run(cmd)
153+
catch e
153154
error("""Failed to translate LLVM code to SPIR-V.
154155
If you think this is a bug, please file an issue and attach $(input).""")
155156
end
156157

157158
# validate
158159
if job.config.target.validate
159-
cmd = `$(SPIRV_Tools_jll.spirv_val()) $translated`
160-
proc = run(ignorestatus(cmd))
161-
if !success(proc)
162-
run(`$(SPIRV_Tools_jll.spirv_dis()) $translated -o -`)
160+
try
161+
run(`$(SPIRV_Tools_jll.spirv_val()) $translated`)
162+
catch e
163163
error("""Failed to validate generated SPIR-V.
164164
If you think this is a bug, please file an issue and attach $(input) and $(translated).""")
165-
end
165+
end
166166
end
167167

168168
# optimize
169169
optimized = tempname(cleanup=false) * ".spv"
170170
if job.config.target.optimize
171-
cmd = `$(SPIRV_Tools_jll.spirv_opt()) -O --skip-validation $translated -o $optimized`
172-
proc = run(ignorestatus(cmd))
173-
if !success(proc)
174-
error("""Failed to optimize generated SPIR-V.
175-
If you think this is a bug, please file an issue and attach $(input) and $(translated).""")
176-
end
171+
try
172+
run(```$(SPIRV_Tools_jll.spirv_opt()) -O --skip-validation
173+
$translated -o $optimized```)
174+
catch
175+
error("""Failed to optimize generated SPIR-V.
176+
If you think this is a bug, please file an issue and attach $(input) and $(translated).""")
177+
end
177178
else
178179
cp(translated, optimized)
179180
end

0 commit comments

Comments
 (0)