Last active
August 30, 2023 14:27
-
-
Save aviatesk/6ef7cc5ce6c48b178a3220f605457bd3 to your computer and use it in GitHub Desktop.
[codegen] custom cache lookup
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
using InteractiveUtils, Test | |
using Core: MethodInstance, CodeInstance | |
using Base: CodegenParams | |
const CC = Core.Compiler | |
include("test/compiler/newinterp.jl") | |
@newinterp ConstInvokeInterp | |
function CC.concrete_eval_eligible(interp::ConstInvokeInterp, | |
@nospecialize(f), result::CC.MethodCallResult, arginfo::CC.ArgInfo, sv::CC.AbsIntState) | |
ret = @invoke CC.concrete_eval_eligible(interp::CC.AbstractInterpreter, | |
f::Any, result::CC.MethodCallResult, arginfo::CC.ArgInfo, sv::CC.AbsIntState) | |
if ret === :semi_concrete_eval | |
return :none # disable semi-concrete interpretation | |
end | |
return ret | |
end | |
Base.@constprop :aggressive @noinline function target(c::Bool, x::Float64) | |
if c | |
y = sin(x) | |
z = nothing | |
else | |
y = cos(x) | |
z = missing | |
end | |
return y, z | |
end | |
function context(x::Float64) | |
return target(true, x) | |
end | |
world = Base.get_world_counter() | |
interp = ConstInvokeInterp(; world) | |
code_typed(context; world, interp) | |
function custom_lookup(mi::MethodInstance, min_world::UInt, max_world::UInt) | |
local matched_mi = nothing | |
for inf_result in interp.inf_cache | |
if inf_result.linfo === mi | |
if CC.any(inf_result.overridden_by_const) | |
return CodeInstance(interp, inf_result, inf_result.src, inf_result.valid_worlds) | |
elseif matched_mi === nothing | |
matched_mi = inf_result.linfo | |
end | |
end | |
end | |
matched_mi === nothing && return nothing | |
return interp.code_cache.dict[matched_mi] | |
end | |
target_mi = CC.specialize_method(only(methods(target)), Tuple{typeof(target),Bool,Float64}, Core.svec()) | |
target_ci = custom_lookup(target_mi, world, world) | |
@test target_ci.rettype === Tuple{Float64,Nothing} # constprop'ed source | |
@ccall jl_uncompress_ir(target_ci.def.def::Any, C_NULL::Ptr{Cvoid}, target_ci.inferred::Any)::Any | |
raw = false | |
lookup = @cfunction(custom_lookup, Any, (Any,Csize_t,Csize_t)) | |
params = CodegenParams(; | |
debug_info_kind=Cint(0), | |
safepoint_on_entry=raw, | |
gcstack_arg=raw, | |
lookup) | |
code_llvm(context, (Float64,); params, dump_module=true) |
Author
aviatesk
commented
Aug 30, 2023
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment