So I know that I can get the lowered code for a method with code_lowered(f, Tuple{...})
, but that requires an instance of the function. What if I only have the type? Usually, you can proceed by just doing F.instance
, but that won't work for a closure.
To be clear, how could this generated function body get the proper code_lowered
for this closure?
julia> cl = let x = 1
y -> x + y
end
#4 (generic function with 1 method)
julia> @generated function foo(f, x)
# need the code_lowered for `f(::typeof(x))` here
end
foo (generic function with 1 method)
given that I can't do
julia> typeof(cl).instance
ERROR: UndefRefError: access to undefined reference
It looks like you can do Base.uncompressed_ir(Base.which(Tuple{typeof(identity),Int}))
after 1.6.
There's gotta be a way that works with all julia versions though, right? Since cassette.jl and IRTools.jl worth through generated functions and worked on 1.0
Yeah, I was just too lazy to cook it up. I know you can use Base.uncompressed_ast
for backward compatibility. Not sure what's the backward-compatible way to emulate which(::Type)
, though.
since which
is a public API, it'd make sense to put it in Compat.jl
Ah I see
It looks like you can just copy & paste the last few lines of which
:
julia> function getmethod(tt)
m = ccall(:jl_gf_invoke_lookup, Any, (Any, UInt), tt, typemax(UInt))
if m === nothing
error("no unique matching method found for the specified argument types")
end
return m.func::Method
end
getmethod (generic function with 1 method)
julia> getmethod(Tuple{typeof(identity),Int})
identity(x) in Base at operators.jl:511
(works in 1.0 and 1.5)
beautiful, thank you!
Last updated: Nov 22 2024 at 04:41 UTC