Skip to content

Commit 927fc30

Browse files
committed
add some more comments
1 parent e532812 commit 927fc30

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

src/driver.jl

+5-3
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ const __llvm_initialized = Ref(false)
191191
entry = finish_module!(job, ir, entry)
192192

193193
# deferred code generation
194+
run_optimization_for_deferred = false
194195
if haskey(functions(ir), "gpuc.lookup")
196+
run_optimization_for_deferred = true
195197
dyn_marker = functions(ir)["gpuc.lookup"]
196198

197199
# gpuc.deferred is lowered to a gpuc.lookup foreigncall, so we need to extract the
@@ -262,9 +264,9 @@ const __llvm_initialized = Ref(false)
262264
unsafe_delete!(ir, dyn_marker)
263265
end
264266
## old, deprecated implementation
265-
has_deferred_jobs = toplevel && !only_entry && haskey(functions(ir), "deferred_codegen")
266267
jobs = Dict{CompilerJob, String}(job => entry_fn)
267-
if has_deferred_jobs
268+
if toplevel && !only_entry && haskey(functions(ir), "deferred_codegen")
269+
run_optimization_for_deferred = true
268270
dyn_marker = functions(ir)["deferred_codegen"]
269271

270272
# iterative compilation (non-recursive)
@@ -395,7 +397,7 @@ const __llvm_initialized = Ref(false)
395397
# deferred codegen has some special optimization requirements,
396398
# which also need to happen _after_ regular optimization.
397399
# XXX: make these part of the optimizer pipeline?
398-
if has_deferred_jobs
400+
if run_optimization_for_deferred
399401
@dispose pb=NewPMPassBuilder() begin
400402
add!(pb, NewPMFunctionPassManager()) do fpm
401403
add!(fpm, InstCombinePass())

src/irgen.jl

+5
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ function irgen(@nospecialize(job::CompilerJob))
8080
compiled[job.source] =
8181
(; compiled[job.source].ci, func, specfunc)
8282

83+
# Earlier we sanitize global names, this invalidates the
84+
# func, specfunc names safed in compiled. Update the names now,
85+
# such that when when use the compiled mappings to lookup the
86+
# llvm function for a methodinstance (deferred codegen) we have
87+
# valid targets.
8388
for mi in keys(compiled)
8489
mi == job.source && continue
8590
ci, func, specfunc = compiled[mi]

src/jlgen.jl

+11-3
Original file line numberDiff line numberDiff line change
@@ -692,16 +692,22 @@ function compile_method_instance(@nospecialize(job::CompilerJob))
692692
error("Cannot compile $(job.source) for world $(job.world); method is only valid in worlds $(job.source.def.primary_world) to $(job.source.def.deleted_world)")
693693
end
694694

695+
# A poor man's worklist implementation.
696+
# `compiled` contains a mapping from `mi->ci, func, specfunc`
697+
# FIXME: Since we are disabling Julia internal caching we might
698+
# generate for the same mi multiple LLVM functions.
699+
# `outstanding` are the missing edges that were not compiled by `compile_method_instance`
700+
# Currently these edges are generated through deferred codegen.
695701
compiled = IdDict()
696702
llvm_mod, outstanding = compile_method_instance(job, compiled)
697703
worklist = outstanding
698704
while !isempty(worklist)
699705
source = pop!(worklist)
700-
haskey(compiled, source) && continue
706+
haskey(compiled, source) && continue # We have fulfilled the request already
707+
# Create a new compiler job for this edge, reusing the config settings from the inital one
701708
job2 = CompilerJob(source, job.config)
702-
@debug "Processing..." job2
703709
llvm_mod2, outstanding = compile_method_instance(job2, compiled)
704-
append!(worklist, outstanding)
710+
append!(worklist, outstanding) # merge worklist with new outstanding edges
705711
@assert context(llvm_mod) == context(llvm_mod2)
706712
link!(llvm_mod, llvm_mod2)
707713
end
@@ -818,6 +824,8 @@ function compile_method_instance(@nospecialize(job::CompilerJob), compiled::IdDi
818824

819825
# NOTE: it's not safe to store raw LLVM functions here, since those may get
820826
# removed or renamed during optimization, so we store their name instead.
827+
# FIXME: Enable this assert when we have a fully featured worklist
828+
# @assert !haskey(compiled, mi)
821829
compiled[mi] = (; ci, func=llvm_func, specfunc=llvm_specfunc)
822830
end
823831

0 commit comments

Comments
 (0)