-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Allow generated functions to return a
CodeInstance
This PR allows generated functions to return a `CodeInstance` containing optimized IR, allowing them to bypass inference and directly adding inferred code into the ordinary course of execution. This is an enabling capability for various external compiler implementations that may want to provide compilation results to the Julia runtime. As a minimal demonstrator of this capability, this adds a Cassette-like `with_new_compiler` higher-order function, which will compile/execute its arguments with the currently loaded `Compiler` package. Unlike `@activate Compiler[:codegen]`, this change is not global and the cache is fully partitioned. This by itself is a very useful feature when developing Compiler code to be able to test the full end-to-end codegen behavior before the changes are capable of fully self-hosting. A key enabler for this was the recent merging of #54899. This PR includes a hacky version of the second TODO left at the end of that PR, just to make everthing work end-to-end. This PR is working end-to-end, but all three parts of it (the CodeInstance return from generated functions, the `with_new_compiler` feature, and the interpreter integration) need some additional cleanup. This PR is mostly intended as a discussion point for what that additional work needs to be.
- Loading branch information
Showing
8 changed files
with
132 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# This file is machine-generated - editing it directly is not advised | ||
|
||
julia_version = "1.12.0-DEV" | ||
manifest_format = "2.0" | ||
project_hash = "84f495a1bf065c95f732a48af36dd0cd2cefb9d5" | ||
|
||
[[deps.Compiler]] | ||
path = "../.." | ||
uuid = "807dbc54-b67e-4c79-8afb-eafe4df6f2e1" | ||
version = "0.0.2" | ||
|
||
[[deps.CompilerDevTools]] | ||
path = "." | ||
uuid = "92b2d91f-d2bd-4c05-9214-4609ac33433f" | ||
version = "0.0.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name = "CompilerDevTools" | ||
uuid = "92b2d91f-d2bd-4c05-9214-4609ac33433f" | ||
|
||
[deps] | ||
Compiler = "807dbc54-b67e-4c79-8afb-eafe4df6f2e1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
module CompilerDevTools | ||
|
||
using Compiler | ||
using Core.IR | ||
|
||
include(joinpath(dirname(pathof(Compiler)), "..", "test", "newinterp.jl")) | ||
|
||
@newinterp SplitCacheInterp | ||
|
||
function generate_codeinst(world::UInt, #=source=#::LineNumberNode, this, fargtypes) | ||
tt = Base.to_tuple_type(fargtypes) | ||
match = Base._which(tt; raise=false, world) | ||
match === nothing && return nothing # method match failed – the fallback implementation will raise a proper MethodError | ||
mi = Compiler.specialize_method(match) | ||
interp = SplitCacheInterp(; world) | ||
new_compiler_ci = Compiler.typeinf_ext(interp, mi, Compiler.SOURCE_MODE_ABI) | ||
|
||
# Construct a CodeInstance that matches `with_new_compiler` and forwards | ||
# to new_compiler_ci | ||
|
||
src = ccall(:jl_new_code_info_uninit, Ref{CodeInfo}, ()) | ||
src.slotnames = Symbol[:self, :args] | ||
src.slotflags = fill(zero(UInt8), 2) | ||
src.slottypes = Any[this, fargtypes] | ||
src.isva = true | ||
src.nargs = 2 | ||
|
||
code = Any[] | ||
ssavaluetypes = Any[] | ||
ncalleeargs = length(fargtypes) | ||
for i = 1:ncalleeargs | ||
push!(code, Expr(:call, getfield, Core.Argument(2), i)) | ||
push!(ssavaluetypes, fargtypes[i]) | ||
end | ||
push!(code, Expr(:invoke, new_compiler_ci, (SSAValue(i) for i = 1:ncalleeargs)...)) | ||
push!(ssavaluetypes, new_compiler_ci.rettype) | ||
push!(code, ReturnNode(SSAValue(ncalleeargs+1))) | ||
push!(ssavaluetypes, Union{}) | ||
src.code = code | ||
src.ssavaluetypes = ssavaluetypes | ||
|
||
return CodeInstance( | ||
mi, nothing, new_compiler_ci.rettype, new_compiler_ci.exctype, | ||
isdefined(new_compiler_ci, :rettype_const) ? new_compiler_ci.rettype_const : nothing, | ||
src, | ||
isdefined(new_compiler_ci, :rettype_const) ? Int32(0x02) : Int32(0x00), | ||
new_compiler_ci.min_world, new_compiler_ci.max_world, | ||
new_compiler_ci.ipo_purity_bits, nothing, new_compiler_ci.relocatability, | ||
nothing, Core.svec(new_compiler_ci)) | ||
end | ||
|
||
function refresh() | ||
@eval function with_new_compiler(args...) | ||
$(Expr(:meta, :generated_only)) | ||
$(Expr(:meta, :generated, generate_codeinst)) | ||
end | ||
end | ||
refresh() | ||
|
||
export with_new_compiler | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters