Skip to content

Commit df484de

Browse files
authored
Merge pull request #418 from JuliaGPU/tb/compile_entrypoint
Improve usability of compiler entrypoint.
2 parents 14fadbc + dcc257e commit df484de

File tree

5 files changed

+20
-18
lines changed

5 files changed

+20
-18
lines changed

src/cache.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ end
5050

5151
# compile
5252
if asm === nothing
53-
if compile_hook[] !== nothing
54-
compile_hook[](job)
55-
end
56-
5753
asm = compiler(job)
5854
end
5955

src/driver.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function codegen(output::Symbol, @nospecialize(job::CompilerJob);
140140
asm, asm_meta = emit_asm(job, ir; strip, validate, format)
141141

142142
if output == :asm || output == :obj
143-
return asm, asm_meta
143+
return asm, (; asm_meta..., ir_meta..., ir)
144144
end
145145

146146

src/reflection.jl

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ function code_llvm(io::IO, @nospecialize(job::CompilerJob); optimize::Bool=true,
122122
debuginfo::Symbol=:default, dump_module::Bool=false, kwargs...)
123123
# NOTE: jl_dump_function_ir supports stripping metadata, so don't do it in the driver
124124
str = JuliaContext() do ctx
125-
ir, meta = codegen(:llvm, job; optimize=optimize, strip=false, validate=false, ctx, kwargs...)
125+
ir, meta = compile(:llvm, job; optimize=optimize, strip=false, validate=false, ctx, kwargs...)
126126
@static if VERSION >= v"1.9.0-DEV.516"
127127
ts_mod = ThreadSafeModule(ir; ctx)
128128
if VERSION >= v"1.9.0-DEV.672"
@@ -169,7 +169,7 @@ The following keyword arguments are supported:
169169
See also: [`@device_code_native`](@ref), `InteractiveUtils.code_llvm`
170170
"""
171171
function code_native(io::IO, @nospecialize(job::CompilerJob); raw::Bool=false, dump_module::Bool=false)
172-
asm, meta = codegen(:asm, job; strip=!raw, only_entry=!dump_module, validate=false)
172+
asm, meta = compile(:asm, job; strip=!raw, only_entry=!dump_module, validate=false)
173173
highlight(io, asm, source_code(job.config.target))
174174
end
175175
code_native(@nospecialize(job::CompilerJob); kwargs...) =
@@ -184,25 +184,31 @@ function emit_hooked_compilation(inner_hook, ex...)
184184
user_code = ex[end]
185185
user_kwargs = ex[1:end-1]
186186
quote
187-
local kernels = Set()
187+
# we only want to invoke the hook once for every compilation job
188+
jobs = Set()
188189
function outer_hook(job)
189-
if !in(job, kernels)
190-
$inner_hook(job; $(map(esc, user_kwargs)...))
191-
push!(kernels, job)
190+
if !in(job, jobs)
191+
# the user hook might invoke the compiler again, so disable the hook
192+
old_hook = $compile_hook[]
193+
try
194+
$compile_hook[] = nothing
195+
$inner_hook(job; $(map(esc, user_kwargs)...))
196+
finally
197+
$compile_hook[] = old_hook
198+
end
199+
push!(jobs, job)
192200
end
193201
end
194202

195-
if $compile_hook[] !== nothing
196-
error("Chaining multiple @device_code calls is unsupported")
197-
end
203+
# now invoke the user code with this hook in place
198204
try
199205
$compile_hook[] = outer_hook
200206
$(esc(user_code))
201207
finally
202208
$compile_hook[] = nothing
203209
end
204210

205-
if isempty(kernels)
211+
if isempty(jobs)
206212
error("no kernels executed while evaluating the given expression")
207213
end
208214

src/spirv.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ end
147147

148148
# reimplementation that uses `spirv-dis`, giving much more pleasant output
149149
function code_native(io::IO, job::CompilerJob{SPIRVCompilerTarget}; raw::Bool=false, dump_module::Bool=false)
150-
obj, _ = codegen(:obj, job; strip=!raw, only_entry=!dump_module, validate=false)
150+
obj, _ = compile(:obj, job; strip=!raw, only_entry=!dump_module, validate=false)
151151
mktemp() do input_path, input_io
152152
write(input_io, obj)
153153
flush(input_io)

test/definitions/native.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ module LazyCodegen
115115
job = cc.job
116116

117117
entry_name, jitted_mod = JuliaContext() do ctx
118-
ir, meta = GPUCompiler.codegen(:llvm, job; validate=false, ctx)
118+
ir, meta = GPUCompiler.compile(:llvm, job; validate=false, ctx)
119119
name(meta.entry), compile!(orc, ir)
120120
end
121121

@@ -231,7 +231,7 @@ module LazyCodegen
231231

232232
function materialize(mr)
233233
JuliaContext() do ctx
234-
ir, meta = GPUCompiler.codegen(:llvm, job; validate=false, ctx)
234+
ir, meta = GPUCompiler.compile(:llvm, job; validate=false, ctx)
235235

236236
# Rename entry to match target_sym
237237
LLVM.name!(meta.entry, target_sym)

0 commit comments

Comments
 (0)