Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,8 @@
(`CompiledFunctions`) and the `CodegenOutput`. `CGModule` takes the IR source.
- `CodegenConfig`: `gcstack_arg` (specsig entries without the pgcstack argument, in the C
calling convention) and `safepoint_on_entry`, as Julia's `CodegenParams`.
- The JIT driver: `JIT`, `jit_compile` and `jit_call` compile calls into Julia's JIT
(LLVM.jl's `JuliaOJIT`) through `emit_into!`, with the IR of `JITInterpreter` (native
inference, with CodeInstances of its own that keep their sources, `collect_codeinfos`),
cached by CodeInstance (so redefining a method recompiles what depends on it), with
the objects the code refers to linked and kept alive, and thread-safe.
6 changes: 6 additions & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ JuliaCodegen.AddrSpace
```@docs
JuliaCodegen.emit_into!
JuliaCodegen.CompiledFunctions
JuliaCodegen.JIT
JuliaCodegen.jit_compile
JuliaCodegen.jit_call
JuliaCodegen.JITFunction
JuliaCodegen.JITInterpreter
JuliaCodegen.collect_codeinfos
```

## Output and pipelines
Expand Down
1 change: 1 addition & 0 deletions src/JuliaCodegen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ include("cfunction.jl")
include("ir.jl")
include("output.jl")
include("emit_into.jl")
include("jit.jl")

end
217 changes: 217 additions & 0 deletions src/jit.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
# The JIT driver: compile calls with JuliaCodegen into Julia's JIT (LLVM.jl's `JuliaOJIT`),
# with the IR of an interpreter of its own (Julia's native inference, whose CodeInstances
# keep their sources), cached by CodeInstance, so that redefining a method recompiles what
# depends on it. Linking binds the objects the code refers to to their addresses (and
# keeps them alive with the code, which is never freed).

"""
JuliaCodegen.JITInterpreter(world)

The interpreter of the [`JIT`](@ref JuliaCodegen.JIT): Julia's native inference, with
CodeInstances of its own cache owner that keep their (uncompressed) sources.
"""
struct JITInterpreter <: CC.AbstractInterpreter
world::UInt
inf_cache::Any
end
# (Julia 1.15 indexes the local inference cache)
JITInterpreter(world::UInt) =
JITInterpreter(world, isdefined(CC, :InferenceCache) ? CC.InferenceCache() : CC.InferenceResult[])
CC.InferenceParams(::JITInterpreter) = CC.InferenceParams()
CC.OptimizationParams(::JITInterpreter) = CC.OptimizationParams()
CC.get_inference_world(i::JITInterpreter) = i.world
CC.get_inference_cache(i::JITInterpreter) = i.inf_cache
CC.cache_owner(::JITInterpreter) = :JuliaCodegen
CC.may_discard_trees(::JITInterpreter) = false
CC.may_compress(::JITInterpreter) = false

"""
JuliaCodegen.collect_codeinfos(interp, mi) -> Vector{Pair{CodeInstance,CodeInfo}}

The CodeInstances that `interp` infers for `mi` and, transitively, for what its code
`:invoke`s, with their sources: the closed set that
[`emit_into!`](@ref JuliaCodegen.emit_into!) compiles. The first one is `mi`'s.
"""
function collect_codeinfos(interp::CC.AbstractInterpreter, mi::Core.MethodInstance)
out = Pair{Core.CodeInstance,Core.CodeInfo}[]
seen = IdSet{Core.MethodInstance}()
work = [mi]
while !isempty(work)
m = popfirst!(work)
m in seen && continue
push!(seen, m)
# (varargs methods are called through the runtime, for now)
m.def isa Method && m !== mi && m.def.isva && continue
ci = CC.typeinf_ext_toplevel(interp, m, CC.SOURCE_MODE_ABI)
src = ci.inferred
if !(src isa Core.CodeInfo)
# (inference keeps no source of functions with a constant result)
src = CC.typeinf_code(interp, m, true)
src isa Core.CodeInfo || unsupported("the code of ", m, ", which inference gives no source of")
end
push!(out, ci => src)
for stmt in src.code
Meta.isexpr(stmt, :invoke) || continue
push!(work, invoke_target(stmt.args[1]))
end
end
return out
end

"""
JuliaCodegen.JITFunction

A function compiled by the [`JIT`](@ref JuliaCodegen.JIT), for the CodeInstance `ci`:
`fptr`, its entry in the jlcall convention, and `specptr`, its entry in the specsig
convention (`C_NULL` if it has none), and the functions that call native code instead
(`fallbacks`). Calling it, `jf(f, args...)`, calls the jlcall entry.
"""
struct JITFunction
ci::Core.CodeInstance
fptr::Ptr{Cvoid}
specptr::Ptr{Cvoid}
fallbacks::Vector{Pair{Core.MethodInstance,Any}}
end

function (jf::JITFunction)(@nospecialize(f), args...)
argv = Any[args...]
return GC.@preserve argv ccall(jf.fptr, Any, (Any, Ptr{Any}, UInt32), f, argv, length(argv))
end

"""
JuliaCodegen.JIT(; config=CodegenConfig(), pipeline=JuliaCppPipeline(), relocatable=true)

A JIT that compiles calls with JuliaCodegen (with the configuration `config` and the pass
pipeline `pipeline`) into Julia's JIT, through LLVM.jl's `JuliaOJIT`. With `relocatable`,
every module is checked to embed no addresses before it is linked. Thread-safe.

See [`jit_compile`](@ref JuliaCodegen.jit_compile) and [`jit_call`](@ref
JuliaCodegen.jit_call).
"""
mutable struct JIT
config::CodegenConfig
pipeline::Pipeline
relocatable::Bool
ojit::Any
lock::ReentrantLock
cache::IdDict{Core.CodeInstance,JITFunction}
"The objects the code refers to, kept alive with it."
roots::Vector{Any}
end

JIT(; config::CodegenConfig=CodegenConfig(), pipeline::Pipeline=JuliaCppPipeline(),
relocatable::Bool=true) =
JIT(config, pipeline, relocatable, nothing, ReentrantLock(),
IdDict{Core.CodeInstance,JITFunction}(), Any[])

# the modules added to Julia's JIT, which every `JIT` shares, for unique names
const JIT_MODULES = Threads.Atomic{Int}(0)

"""
JuliaCodegen.jit_compile(jit, tt; world=Base.get_world_counter()) -> JITFunction

The function `jit` compiled for a call of the signature `tt` in `world`: cached by the
CodeInstance of the call, compiled (with its callees) on first use.
"""
function jit_compile(jit::JIT, @nospecialize(tt::Type); world::UInt=Base.get_world_counter())
mi = method_instance(tt, world)
@lock jit.lock begin
interp = JITInterpreter(world)
# (inferring again makes another CodeInstance; the cache has the first)
ci = CC.get(CC.code_cache(interp), mi, nothing)
ci === nothing && (ci = CC.typeinf_ext_toplevel(interp, mi, CC.SOURCE_MODE_ABI))
get!(jit.cache, ci) do
jit_add!(jit, collect_codeinfos(interp, mi), world)
end
end
end

"""
JuliaCodegen.jit_call(jit, f, args...)

Call `f(args...)` with the code `jit` compiled for it.
"""
jit_call(jit::JIT, @nospecialize(f), args...) =
jit_compile(jit, Tuple{Core.Typeof(f), map(Core.Typeof, args)...})(f, args...)

# compile the set `codeinfos` into a module of its own, whose entries are the first
# CodeInstance's, and add it to the JIT
function jit_add!(jit::JIT, codeinfos, world::UInt)
jit.ojit === nothing && (jit.ojit = LLVM.JuliaOJIT())
prefix = "jlcodegen_$(Threads.atomic_add!(JIT_MODULES, 1) + 1)"
root = first(first(codeinfos))
names = nothing
fallbacks = Pair{Core.MethodInstance,Any}[]
tsctx = LLVM.ThreadSafeContext()
tsm = LLVM.ThreadSafeModule(prefix)
err = nothing
tsm() do mod
try
cgm = CGModule(mod, jit.config; world)
compiled, out = emit_into!(cgm, codeinfos)
append!(fallbacks, out.fallbacks)
if jit.relocatable
problems = check_relocatable(mod, jit.config)
isempty(problems) || throw(InternalError(root, ErrorException(
"the module embeds addresses: " * join(problems, "; ")), nothing))
end
# the entries of the root, under names unique in the JIT; all else is internal
fs = compiled[root]
entries = Dict(fs.func => prefix)
fs.specfunc === nothing || (entries[fs.specfunc] = prefix * "_specsig")
for f in LLVM.functions(mod)
LLVM.isdeclaration(f) && continue
name = get(entries, LLVM.name(f), nothing)
if name === nothing
LLVM.linkage!(f, LLVM.API.LLVMInternalLinkage)
else
LLVM.name!(f, name)
end
end
names = (prefix, fs.specfunc === nothing ? nothing : prefix * "_specsig")
link_literals!(jit, mod, out.literals)
run_pipeline!(jit.pipeline, mod, jit.config)
bind_pgcstack_resolver!(mod)
catch e
# (LLVM.jl wraps exceptions thrown in this callback)
err = e
end
end
LLVM.dispose(tsctx) # (the module keeps the context alive)
err === nothing || throw(err)
ojit = jit.ojit
jd = LLVM.supports_jit_dylib_creation(ojit) ? LLVM.JITDylib(ojit, prefix) : ojit.external_dylib
LLVM.add!(ojit, jd, tsm)
fptr = pointer(LLVM.lookup(ojit, jd, names[1]))
specptr = names[2] === nothing ? C_NULL : pointer(LLVM.lookup(ojit, jd, names[2]))
return JITFunction(root, fptr, specptr, fallbacks)
end

# give the globals of the objects the code refers to their addresses, keeping the objects
# alive
function link_literals!(jit::JIT, mod::LLVM.Module, literals)
iptr = LLVM.IntType(8 * sizeof(Ptr{Cvoid}))
for (name, x) in literals
push!(jit.roots, x)
addr = ccall(:jl_value_ptr, Ptr{Cvoid}, (Any,), x)
gv = LLVM.globals(mod)[name]
LLVM.initializer!(gv, LLVM.const_inttoptr(LLVM.ConstantInt(iptr, UInt(addr)), T_ptr()))
LLVM.constant!(gv, true)
LLVM.linkage!(gv, LLVM.API.LLVMPrivateLinkage)
end
end

# Where TLS isn't ELF TLS (macOS, Windows), Julia's LowerPTLS calls
# `jl_get_pgcstack_resolved`, which Julia's own JIT registers as a named global. The
# dylibs LLVM.jl creates on Julia 1.14 and later don't see it, so the JIT binds it to the
# runtime's getter.
function bind_pgcstack_resolver!(mod::LLVM.Module)
haskey(LLVM.functions(mod), "jl_get_pgcstack_resolved") || return
f = LLVM.functions(mod)["jl_get_pgcstack_resolved"]
LLVM.isdeclaration(f) || return
getter, key = Ref{Ptr{Cvoid}}(), Ref{Ptr{Cvoid}}()
ccall(:jl_pgcstack_getkey, Cvoid, (Ptr{Ptr{Cvoid}}, Ptr{Ptr{Cvoid}}), getter, key)
iptr = LLVM.IntType(8 * sizeof(Ptr{Cvoid}))
LLVM.replace_uses!(f, LLVM.const_inttoptr(LLVM.ConstantInt(iptr, UInt(getter[])), LLVM.value_type(f)))
LLVM.erase!(f)
end
21 changes: 4 additions & 17 deletions test/jit.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# A minimal JIT for the tests: compile a call signature with JuliaCodegen, check that the
# module is relocatable, give the globals of the objects the code refers to their
# addresses, run Julia's pipeline, and add the module to Julia's JIT through LLVM.jl. The
# drivers of the package come later.
# addresses, run Julia's pipeline, and add the module to Julia's JIT through LLVM.jl. Unlike
# the package's JIT driver (`JuliaCodegen.JIT`), it compiles with native inference's IR
# and lets tests choose the entry they call.

const TEST_CONFIG = Ref{Any}(nothing)
function test_config()
Expand Down Expand Up @@ -73,7 +74,7 @@ function test_compile(@nospecialize(tt); optimize::Bool=true, config=test_config
verify_ir(mod)
JuliaCodegen.run_pipeline!(JuliaCodegen.JuliaCppPipeline(; opt_level=optimize ? 2 : 0),
mod, config)
bind_pgcstack_resolver!(mod)
JuliaCodegen.bind_pgcstack_resolver!(mod)
catch e
# (LLVM.jl wraps exceptions thrown in this callback)
err = e
Expand All @@ -88,20 +89,6 @@ function test_compile(@nospecialize(tt); optimize::Bool=true, config=test_config
end
end

# Where TLS isn't ELF TLS (macOS, Windows), LowerPTLS calls `jl_get_pgcstack_resolved`,
# which Julia's JIT registers as a named global. The dylibs LLVM.jl creates on Julia 1.14
# and later don't see it, so the test JIT binds it to the runtime's getter itself.
function bind_pgcstack_resolver!(mod::LLVM.Module)
haskey(mod.functions, "jl_get_pgcstack_resolved") || return
f = mod.functions["jl_get_pgcstack_resolved"]
LLVM.isdeclaration(f) || return
getter, key = Ref{Ptr{Cvoid}}(), Ref{Ptr{Cvoid}}()
ccall(:jl_pgcstack_getkey, Cvoid, (Ptr{Ptr{Cvoid}}, Ptr{Ptr{Cvoid}}), getter, key)
iptr = LLVM.IntType(8 * sizeof(Ptr{Cvoid}))
LLVM.replace_uses!(f, LLVM.const_inttoptr(LLVM.ConstantInt(iptr, UInt(getter[])), LLVM.value_type(f)))
LLVM.erase!(f)
end

"""
Call `f(args...)` with code compiled by JuliaCodegen (with the configuration `config`,
through the entry `emit` emits, see [`test_compile`](@ref)).
Expand Down
81 changes: 81 additions & 0 deletions test/jitdriver.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# The JIT driver: calls compiled into Julia's JIT, cached by CodeInstance.

jd_fib(n) = n < 2 ? n : jd_fib(n - 1) + jd_fib(n - 2)
jd_greet(x) = string("hello ", x)
jd_sum(v) = (s = zero(eltype(v)); for x in v; s += x; end; s)
jd_try(x) = try x > 0 ? x : error("negative") catch; -1 end
jd_struct(x) = (Ref(x), x + 0.5)
jd_later(x) = x + 1
jd_call_later(x) = jd_later(x) * 2
@noinline jd_vararg(xs...) = sum(xs)
jd_call_vararg(x) = jd_vararg(x, x)
# (a calling convention JuliaCodegen doesn't support; native code compiles it only for x86)
@noinline jd_stdcall(s::String) = Int(ccall(:strlen, stdcall, Csize_t, (Cstring,), s))
jd_call_stdcall(s) = jd_stdcall(s) + 1

const JD_JIT = JuliaCodegen.JIT(; config=test_config())
jd_run(f, args...) = JuliaCodegen.jit_call(JD_JIT, f, args...)

@testset "JIT driver" begin

@testset "calls" begin
@cgtest run=jd_run jd_fib(20)
@cgtest run=jd_run jd_greet(42)
@cgtest run=jd_run jd_sum([1.0, 2.0, 3.0])
@cgtest run=jd_run jd_try(3)
@cgtest run=jd_run jd_try(-3)
@cgtest run=jd_run jd_struct(1)
end

@testset "cache" begin
a = JuliaCodegen.jit_compile(JD_JIT, Tuple{typeof(jd_fib), Int})
b = JuliaCodegen.jit_compile(JD_JIT, Tuple{typeof(jd_fib), Int})
@test a.fptr == b.fptr && a.ci === b.ci
@test a.ci.def === JuliaCodegen.method_instance(Tuple{typeof(jd_fib), Int}, Base.get_world_counter())
@test a.specptr != C_NULL
end

@testset "invalidation" begin
@test jd_run(jd_call_later, 1) == 4
old = JuliaCodegen.jit_compile(JD_JIT, Tuple{typeof(jd_call_later), Int})
@eval jd_later(x) = x + 100
@test jd_run(jd_call_later, 1) == 202
@test JuliaCodegen.jit_compile(JD_JIT, Tuple{typeof(jd_call_later), Int}) !== old
end

@testset "fallbacks" begin
fallback = JuliaCodegen.JIT(; config=JuliaCodegen.CodegenConfig(; on_unsupported=:fallback,
on_internal_error=:error))
if Sys.ARCH in (:x86_64, :i686)
@test JuliaCodegen.jit_call(fallback, jd_call_stdcall, "four") == 5
jf = JuliaCodegen.jit_compile(fallback, Tuple{typeof(jd_call_stdcall), String})
@test any(((mi, why),) -> mi.def.name === :jd_stdcall, jf.fallbacks)
@test_throws JuliaCodegen.Unsupported jd_run(jd_call_stdcall, "four")
end
# varargs callees are called through the runtime
@test jd_run(jd_call_vararg, 3) == 6
end

@testset "the specsig entry" begin
# without the pgcstack argument, a C function
jit = JuliaCodegen.JIT(; config=JuliaCodegen.CodegenConfig(; on_unsupported=:error,
on_internal_error=:error,
gcstack_arg=false))
jf = JuliaCodegen.jit_compile(jit, Tuple{typeof(jd_fib), Int})
@test ccall(jf.specptr, Int, (Int,), 20) == jd_fib(20)
end

@testset "GC and threads" begin
# the objects the code refers to stay alive
GC.gc(true)
@test jd_run(jd_greet, 7) == "hello 7"
jit = JuliaCodegen.JIT(; config=test_config())
results = Vector{Any}(undef, 8)
Threads.@threads for i in 1:8
results[i] = JuliaCodegen.jit_call(jit, jd_fib, 10 + i % 2)
end
@test results == [jd_fib(10 + i % 2) for i in 1:8]
@test length(jit.cache) == 1
end

end
5 changes: 3 additions & 2 deletions test/memory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ end
# (compared with a call that does no work, for what `cgrun` itself allocates)
v = collect(1:100)
cgrun(m_sum_found, v), cgrun(m_length, v)
work = @allocated cgrun(m_sum_found, v)
baseline = @allocated cgrun(m_length, v)
# (the least of a few measurements: a GC or another task may allocate meanwhile)
work = minimum(_ -> @allocated(cgrun(m_sum_found, v)), 1:3)
baseline = minimum(_ -> @allocated(cgrun(m_length, v)), 1:3)
@test work - baseline <= 64
# element accesses are tied to their memory
@test @filecheck begin
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ include("specsig.jl")
include("cfunction.jl")
include("output.jl")
include("emit_into.jl")
include("jitdriver.jl")
include("cabi_targets.jl")

@testset "Aqua" begin
Expand Down
Loading