diff --git a/src/rtlib.jl b/src/rtlib.jl index 1d8c00f6..c790b23c 100644 --- a/src/rtlib.jl +++ b/src/rtlib.jl @@ -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 @@ -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 diff --git a/src/spirv.jl b/src/spirv.jl index 2015125d..2afd4f60 100644 --- a/src/spirv.jl +++ b/src/spirv.jl @@ -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