Skip to content

Cache the runtime library in memory. #699

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

Merged
merged 2 commits into from
Jun 4, 2025
Merged
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
17 changes: 9 additions & 8 deletions src/rtlib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ end

const runtime_lock = ReentrantLock()

const runtime_cache = Dict{String, Vector{UInt8}}()

@locked function load_runtime(@nospecialize(job::CompilerJob))
global compile_cache
if compile_cache === nothing # during precompilation
Expand All @@ -135,15 +137,14 @@ const runtime_lock = ReentrantLock()
name = "runtime_$(slug).bc"
path = joinpath(compile_cache, name)

lib = try
if ispath(path)
open(path) do io
parse(LLVM.Module, read(io))
end
# cache the runtime library on disk and in memory
lib = if haskey(runtime_cache, slug)
parse(LLVM.Module, runtime_cache[slug])
elseif ispath(path)
runtime_cache[slug] = open(path) do io
read(io)
end
catch ex
@warn "Failed to load GPU runtime library at $path" exception=(ex, catch_backtrace())
nothing
parse(LLVM.Module, runtime_cache[slug])
end

if lib === nothing
Expand Down
27 changes: 14 additions & 13 deletions src/spirv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,32 +148,33 @@ end
cmd = `$(cmd) --spirv-max-version=$(job.config.target.version.major).$(job.config.target.version.minor)`
end
end
proc = run(ignorestatus(cmd))
if !success(proc)
try
run(cmd)
catch e
error("""Failed to translate LLVM code to SPIR-V.
If you think this is a bug, please file an issue and attach $(input).""")
end

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

# optimize
optimized = tempname(cleanup=false) * ".spv"
if job.config.target.optimize
cmd = `$(SPIRV_Tools_jll.spirv_opt()) -O --skip-validation $translated -o $optimized`
proc = run(ignorestatus(cmd))
if !success(proc)
error("""Failed to optimize generated SPIR-V.
If you think this is a bug, please file an issue and attach $(input) and $(translated).""")
end
try
run(```$(SPIRV_Tools_jll.spirv_opt()) -O --skip-validation
$translated -o $optimized```)
catch
error("""Failed to optimize generated SPIR-V.
If you think this is a bug, please file an issue and attach $(input) and $(translated).""")
end
else
cp(translated, optimized)
end
Expand Down
Loading