-
-
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.
Extremely WIP/RFC: Extend
invoke
to accept CodeInstance
This is an alternative mechanism to #56650 that largely achieves the same result, but by hooking into `invoke` rather than a generated function. They are orthogonal mechanisms, and its possible we want both. However, in #56650, both Jameson and Valentin were skeptical of the generated function signature bottleneck. This PR is sort of a hybrid of mechanism in #52964 and what I proposed in #56650 (comment). In particular, this PR: 1. Extends `invoke` to support a CodeInstance in place of its usual `types` argument. 2. Adds a new `typeinf` optimized generic. The semantics of this optimized generic allow the compiler to instead call a companion `typeinf_edge` function, allowing a mid-inference interpreter switch (like #52964), without being forced through a concrete signature bottleneck. However, if calling `typeinf_edge` does not work (e.g. because the compiler version is mismatched), this still has well defined semantics, you just don't get inference support. The additional benefit of the `typeinf` optimized generic is that it lets custom cache owners tell the runtime how to "cure" code instances that have lost their native code. Currently the runtime only knows how to do that for `owner == nothing` CodeInstances (by re-running inference). This extension is not implemented, but the idea is that the runtime would be permitted to call the `typeinf` optimized generic on the dead CodeInstance's `owner` and `def` fields to obtain a cured CodeInstance (or a user-actionable error from the plugin). This PR includes an implementation of `with_new_compiler` from #56650. This PR includes just enough compiler support to make the compiler optimize this to the same code that #56650 produced: ``` julia> @code_typed with_new_compiler(sin, 1.0) CodeInfo( 1 ─ $(Expr(:foreigncall, :(:jl_get_tls_world_age), UInt64, svec(), 0, :(:ccall)))::UInt64 │ %2 = builtin Core.getfield(args, 1)::Float64 │ %3 = invoke sin(%2::Float64)::Float64 └── return %3 ) => Float64 ``` However, the implementation here is extremely incomplete. I'm putting it up only as a directional sketch to see if people prefer it over #56650. If so, I would prepare a cleaned up version of this PR that has the optimized generics as well as the curing support, but not the full inference integration (which needs a fair bit more work).
- Loading branch information
Showing
9 changed files
with
181 additions
and
7 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,45 @@ | ||
__precompile__(false) | ||
module CompilerDevTools | ||
|
||
using Compiler | ||
using Core.IR | ||
|
||
include(joinpath(dirname(pathof(Compiler)), "..", "test", "newinterp.jl")) | ||
|
||
@newinterp SplitCacheInterp | ||
struct SplitCacheOwner; end | ||
|
||
import Core.OptimizedGenerics.CompilerPlugins: typeinf, typeinf_edge | ||
|
||
Compiler.cache_owner(::SplitCacheInterp) = SplitCacheOwner() | ||
let typeinf_world_age = Base.tls_world_age() | ||
@eval @noinline typeinf(::SplitCacheOwner, mi::MethodInstance, source_mode::UInt8) = | ||
Base.invoke_in_world($typeinf_world_age, Compiler.typeinf_ext, SplitCacheInterp(; world=Base.tls_world_age()), mi, source_mode) | ||
|
||
@eval @noinline function typeinf_edge(::SplitCacheOwner, mi::MethodInstance, parent_frame::Compiler.InferenceState, world::UInt, source_mode::UInt8) | ||
# TODO: This isn't quite right, we're just sketching things for now | ||
interp = SplitCacheInterp(; world) | ||
Compiler.typeinf_edge(interp, mi.def, mi.specTypes, Core.svec(), parent_frame, false, false) | ||
end | ||
end | ||
|
||
# TODO: This needs special compiler support to properly case split for multiple | ||
# method matches, etc. This annotation is not sound, but just for demo purpoes. | ||
@Base.assume_effects :total @noinline function mi_for_tt(tt, world=Base.tls_world_age()) | ||
interp = SplitCacheInterp(; world) | ||
match, _ = Compiler.findsup(tt, Compiler.method_table(interp)) | ||
Base.specialize_method(match) | ||
end | ||
|
||
function with_new_compiler(f, args...) | ||
tt = Base.signature_type(f, typeof(args)) | ||
world = Base.tls_world_age() | ||
new_compiler_ci = Core.OptimizedGenerics.CompilerPlugins.typeinf( | ||
SplitCacheOwner(), mi_for_tt(tt), Compiler.SOURCE_MODE_ABI | ||
) | ||
invoke(f, new_compiler_ci, args...) | ||
end | ||
|
||
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
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