Skip to content

Commit 59d74d4

Browse files
committed
inference,codegen: connect source directly to jit
This avoids unnecessary compression when running (not generating code). While generating code, we continue the legacy behavior of storing compressed code, since restarting from a ji without that is quite slow. Eventually, we should also remove that code also once we have generated the object file from it. This replaces the defective SOURCE_MODE_FORCE_SOURCE option with a new `typeinf_ext_toplevel` batch-mode interface for compilation which returns all required source code. Only two options remain now: SOURCE_MODE_NOT_REQUIRED : Require only that the IPO information (e.g. rettype and friends) is present. SOURCE_MODE_FORCE_ABI : Require that the IPO information is present (for ABI computation) and that the returned CodeInstance can be invoked on the host target (preferably after inference, called directly, but perfectly acceptable for Base.Compiler to instead force the runtime to use a stub there or call into it with the interpreter instead by having failed to provide any code). This replaces the awkward `jl_create_native` interface (which is now just a shim for calling the new batch-mode `typeinf_ext_toplevel`) with a simpler `jl_emit_native` API, which does not do any inference or other callbacks, but simply is a batch-mode call to `jl_emit_codeinfo` and the work to build the external wrapper around them for linkage.
1 parent 835c8ac commit 59d74d4

20 files changed

+501
-502
lines changed

Compiler/src/bootstrap.jl

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ function activate_codegen!()
1616
end
1717

1818
function bootstrap!()
19+
global bootstrapping_compiler = true
1920
let time() = ccall(:jl_clock_now, Float64, ())
2021
println("Compiling the compiler. This may take several minutes ...")
21-
interp = NativeInterpreter()
2222

2323
ssa_inlining_pass!_tt = Tuple{typeof(ssa_inlining_pass!), IRCode, InliningState{NativeInterpreter}, Bool}
2424
optimize_tt = Tuple{typeof(optimize), NativeInterpreter, OptimizationState{NativeInterpreter}, InferenceResult}
@@ -45,13 +45,15 @@ function bootstrap!()
4545
end
4646
end
4747
starttime = time()
48+
methods = Any[]
49+
world = get_world_counter()
4850
for f in fs
4951
if isa(f, DataType) && f.name === typename(Tuple)
5052
tt = f
5153
else
5254
tt = Tuple{typeof(f), Vararg{Any}}
5355
end
54-
matches = _methods_by_ftype(tt, 10, get_world_counter())::Vector
56+
matches = _methods_by_ftype(tt, 10, world)::Vector
5557
if isempty(matches)
5658
println(stderr, "WARNING: no matching method found for `", tt, "`")
5759
else
@@ -62,14 +64,25 @@ function bootstrap!()
6264
for i = 1:length(params)
6365
params[i] = unwraptv(params[i])
6466
end
65-
typeinf_type(interp, m.method, Tuple{params...}, m.sparams)
67+
mi = specialize_method(m.method, Tuple{params...}, m.sparams)
68+
#isa_compileable_sig(mi) || println(stderr, "WARNING: inferring `", mi, "` which isn't expected to be called.")
69+
push!(methods, mi)
6670
end
6771
end
6872
end
73+
codeinfos = typeinf_ext_toplevel(methods, [world], false)
74+
for i = 1:2:length(codeinfos)
75+
ci = codeinfos[i]::CodeInstance
76+
src = codeinfos[i + 1]::CodeInfo
77+
isa_compileable_sig(ci.def) || continue # println(stderr, "WARNING: compiling `", ci.def, "` which isn't expected to be called.")
78+
ccall(:jl_add_codeinst_to_jit, Cvoid, (Any, Any), ci, src)
79+
end
6980
endtime = time()
7081
println("Base.Compiler ──── ", sub_float(endtime,starttime), " seconds")
7182
end
7283
activate_codegen!()
84+
global bootstrapping_compiler = false
85+
nothing
7386
end
7487

7588
function activate!(; reflection=true, codegen=false)

Compiler/src/tfuncs.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,10 @@ macro nospecs(ex)
4242
push!(names, arg)
4343
end
4444
@assert isexpr(body, :block)
45-
if !isempty(names)
46-
lin = first(body.args)::LineNumberNode
47-
nospec = Expr(:macrocall, Symbol("@nospecialize"), lin, names...)
48-
insert!(body.args, 2, nospec)
49-
end
45+
isempty(names) && throw(ArgumentError("no arguments for @nospec"))
46+
lin = first(body.args)::LineNumberNode
47+
nospec = Expr(:macrocall, GlobalRef(@__MODULE__, :var"@nospecialize"), lin, names...)
48+
insert!(body.args, 2, nospec)
5049
return esc(ex)
5150
end
5251

@@ -2115,7 +2114,7 @@ add_tfunc(memoryrefoffset, 1, 1, memoryrefoffset_tfunc, 5)
21152114
return true
21162115
end
21172116

2118-
@nospecs function memoryref_elemtype(@nospecialize mem)
2117+
@nospecs function memoryref_elemtype(mem)
21192118
m = widenconst(mem)
21202119
if !has_free_typevars(m) && m <: GenericMemoryRef
21212120
m0 = m
@@ -2131,7 +2130,7 @@ end
21312130
return Any
21322131
end
21332132

2134-
@nospecs function _memoryref_elemtype(@nospecialize mem)
2133+
@nospecs function _memoryref_elemtype(mem)
21352134
m = widenconst(mem)
21362135
if !has_free_typevars(m) && m <: GenericMemoryRef
21372136
m0 = m
@@ -2166,7 +2165,7 @@ end
21662165
end
21672166

21682167
# whether getindex for the elements can potentially throw UndefRef
2169-
function array_type_undefable(@nospecialize(arytype))
2168+
@nospecs function array_type_undefable(arytype)
21702169
arytype = unwrap_unionall(arytype)
21712170
if isa(arytype, Union)
21722171
return array_type_undefable(arytype.a) || array_type_undefable(arytype.b)
@@ -2247,7 +2246,7 @@ end
22472246
return boundscheck Bool && memtype GenericMemoryRef && order Symbol
22482247
end
22492248

2250-
@nospecs function memorynew_nothrow(argtypes::Vector{Any})
2249+
function memorynew_nothrow(argtypes::Vector{Any})
22512250
if !(argtypes[1] isa Const && argtypes[2] isa Const)
22522251
return false
22532252
end
@@ -2263,6 +2262,7 @@ end
22632262
overflows = checked_smul_int(len, elsz)[2]
22642263
return !overflows
22652264
end
2265+
22662266
# Query whether the given builtin is guaranteed not to throw given the `argtypes`.
22672267
# `argtypes` can be assumed not to contain varargs.
22682268
function _builtin_nothrow(𝕃::AbstractLattice, @nospecialize(f::Builtin), argtypes::Vector{Any},

0 commit comments

Comments
 (0)