Skip to content

Commit ac95ac1

Browse files
authored
Write the compiled runtime in a scratch directory. (#406)
1 parent c81e6ba commit ac95ac1

File tree

4 files changed

+14
-37
lines changed

4 files changed

+14
-37
lines changed

Manifest.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
114114
[[SHA]]
115115
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
116116

117+
[[Scratch]]
118+
deps = ["Dates"]
119+
git-tree-sha1 = "30449ee12237627992a99d5e30ae63e4d78cd24a"
120+
uuid = "6c6a2e73-6563-6170-7368-637461726353"
121+
version = "1.2.0"
122+
117123
[[Serialization]]
118124
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
119125

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
99
LLVM = "929cbde3-209d-540e-8aea-75f648917ca0"
1010
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
1111
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
12+
Scratch = "6c6a2e73-6563-6170-7368-637461726353"
1213
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
1314
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
1415

src/GPUCompiler.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ using ExprTools: splitdef, combinedef
99

1010
using Libdl
1111

12+
using Scratch: @get_scratch!
13+
1214
include("utils.jl")
1315

1416
# compiler interface and implementations
@@ -43,6 +45,8 @@ _precompile_()
4345

4446
function __init__()
4547
STDERR_HAS_COLOR[] = get(stderr, :color, false)
48+
49+
global compile_cache = @get_scratch!("compiled")
4650
end
4751

4852
end # module

src/rtlib.jl

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ end
1515
# GPU run-time library
1616
#
1717

18-
# get the path to a directory where we can put cache files (machine-specific, ephemeral)
19-
# NOTE: maybe we should use XDG_CACHE_PATH/%LOCALAPPDATA%, but other Julia cache files
20-
# are put in .julia anyway so let's just follow suit for now.
21-
function cachedir(depot=DEPOT_PATH[1])
22-
# this mimicks Base.compilecache. we can't just call the function, or we might actually
23-
# _generate_ a cache file, e.g., when running with `--compiled-modules=no`.
24-
entrypath, entryfile = Base.cache_file_entry(Base.PkgId(GPUCompiler))
25-
abspath(depot, entrypath, entryfile)
26-
end
27-
2818

2919
## higher-level functionality to work with runtime functions
3020

@@ -125,30 +115,9 @@ const runtime_lock = ReentrantLock()
125115

126116
@locked function load_runtime(@nospecialize(job::CompilerJob); ctx)
127117
lock(runtime_lock) do
128-
# find the first existing cache directory (for when dealing with layered depots)
129-
cachedirs = [cachedir(depot) for depot in DEPOT_PATH]
130-
filter!(isdir, cachedirs)
131-
input_dir = if isempty(cachedirs)
132-
nothing
133-
else
134-
first(cachedirs)
135-
end
136-
137-
# we are only guaranteed to be able to write in the current depot
138-
output_dir = cachedir()
139-
140-
# if both aren't equal, copy pregenerated runtime libraries to our depot
141-
# NOTE: we don't just lazily read from the one and write to the other, because
142-
# once we generate additional runtimes in the output dir we don't know if
143-
# it's safe to load from other layers (since those could have been invalidated)
144-
if input_dir !== nothing && input_dir != output_dir
145-
mkpath(dirname(output_dir))
146-
cp(input_dir, output_dir)
147-
end
148-
149118
slug = runtime_slug(job)
150119
name = "runtime_$(slug).bc"
151-
path = joinpath(output_dir, name)
120+
path = joinpath(compile_cache, name)
152121

153122
lib = try
154123
if ispath(path)
@@ -163,7 +132,7 @@ const runtime_lock = ReentrantLock()
163132

164133
if lib === nothing
165134
@debug "Building the GPU runtime library at $path"
166-
mkpath(output_dir)
135+
mkpath(compile_cache)
167136
lib = build_runtime(job; ctx)
168137

169138
# atomic write to disk
@@ -181,10 +150,7 @@ end
181150
# NOTE: call this function from global scope, so any change triggers recompilation.
182151
function reset_runtime()
183152
lock(runtime_lock) do
184-
rm(cachedir(); recursive=true, force=true)
185-
# create an empty cache directory. since we only ever load from the first existing cachedir,
186-
# this effectively invalidates preexisting caches in lower layers of the depot.
187-
mkpath(cachedir())
153+
rm(compile_cache; recursive=true, force=true)
188154
end
189155

190156
return

0 commit comments

Comments
 (0)