Skip to content

Commit

Permalink
fixup! Use ThreadSafeModule/Context on Julia 1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsamaroo committed May 3, 2022
1 parent fd71d9b commit c8f7657
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 39 deletions.
7 changes: 6 additions & 1 deletion src/driver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ function JuliaContext(f)
end
end

if VERSION >= v"1.9.0-DEV.303"
unwrap_context(ctx::ThreadSafeContext) = context(ctx)
end
unwrap_context(ctx::Context) = ctx

function codegen(output::Symbol, @nospecialize(job::CompilerJob);
libraries::Bool=true, deferred_codegen::Bool=true, optimize::Bool=true,
strip::Bool=false, validate::Bool=true, only_entry::Bool=false,
Expand Down Expand Up @@ -291,7 +296,7 @@ const __llvm_initialized = Ref(false)
deferred_codegen=false, parent_job=job, ctx)
dyn_entry_fn = LLVM.name(dyn_meta.entry)
merge!(compiled, dyn_meta.compiled)
@assert context(dyn_ir) == ctx
@assert context(dyn_ir) == unwrap_context(ctx)
link!(ir, dyn_ir)
changed = true
dyn_entry_fn
Expand Down
34 changes: 0 additions & 34 deletions src/gcn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,40 +62,6 @@ function finish_module!(@nospecialize(job::CompilerJob{GCNCompilerTarget}),
return entry
end

function optimize!(job::CompilerJob{GCNCompilerTarget}, mod::LLVM.Module)
# we have to fake our target early in the pipeline because Julia's
# optimization passes weren't designed for a non-0 stack addrspace, and the
# AMDGPU target is very strict about which addrspaces are permitted for
# various code patterns
triple!(mod, llvm_triple(NativeCompilerTarget()))
datalayout!(mod, julia_datalayout(NativeCompilerTarget()))

invoke(optimize!, Tuple{CompilerJob, LLVM.Module}, job, mod)
end

# We need to do alloca rewriting (from 0 to 5) after Julia's optimization
# passes because of two reasons:
# 1. Debug builds call the target verifier first, which would trip if AMDGPU
# was the target at that time
# 2. We don't want any chance of messing with Julia's optimizations, since they
# eliminate target-unsafe IR patterns
function optimize_module!(job::CompilerJob{GCNCompilerTarget}, mod::LLVM.Module)
# revert back to the AMDGPU target
triple!(mod, llvm_triple(job.target))
datalayout!(mod, julia_datalayout(job.target))

tm = llvm_machine(job.target)
ModulePassManager() do pm
add_library_info!(pm, triple(mod))
add_transform_info!(pm, tm)

add!(pm, FunctionPass("FixAllocaAddrspace", fix_alloca_addrspace!))

run!(pm, mod)
end
end


## LLVM passes

function lower_throw_extra!(mod::LLVM.Module)
Expand Down
2 changes: 1 addition & 1 deletion src/irgen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function irgen(@nospecialize(job::CompilerJob), method_instance::Core.MethodInst
@timeit_debug to "clean-up" begin
for llvmf in functions(mod)
# only occurs in debug builds
delete!(function_attributes(llvmf), EnumAttribute("sspstrong", 0; ctx=context(ctx)))
delete!(function_attributes(llvmf), EnumAttribute("sspstrong", 0; ctx=unwrap_context(ctx)))

if Sys.iswindows()
personality!(llvmf, nothing)
Expand Down
2 changes: 1 addition & 1 deletion src/jlgen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ function compile_method_instance(@nospecialize(job::CompilerJob),
# generate IR
GC.@preserve lookup_cb begin
native_code = if VERSION >= v"1.9.0-DEV.303"
mod = LLVM.Module("start"; ctx=context(ctx))
mod = LLVM.Module("start"; ctx=unwrap_context(ctx))

# configure the module
triple!(mod, llvm_triple(job.target))
Expand Down
4 changes: 2 additions & 2 deletions src/rtlib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function emit_function!(mod, @nospecialize(job::CompilerJob), f, method; ctx::Ju
end

function build_runtime(@nospecialize(job::CompilerJob); ctx)
mod = LLVM.Module("GPUCompiler run-time library"; ctx=context(ctx))
mod = LLVM.Module("GPUCompiler run-time library"; ctx=unwrap_context(ctx))

# the compiler job passed into here is identifies the job that requires the runtime.
# derive a job that represents the runtime itself (notably with kernel=false).
Expand Down Expand Up @@ -165,7 +165,7 @@ const runtime_lock = ReentrantLock()
lib = try
if ispath(path)
open(path) do io
parse(LLVM.Module, read(io); ctx=context(ctx))
parse(LLVM.Module, read(io); ctx=unwrap_context(ctx))
end
end
catch ex
Expand Down

0 comments on commit c8f7657

Please sign in to comment.