Opaque closure and code_typed

I’m playing around with opaque closures. I’ve have managed to construct the following toy example:

struct Foo
    x::Float64
end

(x::Foo)(y) = cos(sin(getfield(x, 1) * y))

@eval function make_opaque_closure(x)
    return $(Expr(
        :new_opaque_closure,
        Tuple{Float64},
        Float64,
        Float64,
        Expr(
            :opaque_closure_method,
            nothing,
            1,
            false,
            LineNumberNode(0, nothing),
            only(code_lowered(Foo(5.0), (Float64, ))),
        ),
        :x,
    ))
end

f = make_opaque_closure(5.0)

(I took inspiration from the tests in the main Julia repo).

If I now ask for the @code_typed associated with f, I get:

@code_typed optimize=true f(4.0)
CodeInfo(
1 ─ %1 = Main.getfield(x, 1)
│   %2 = %1 * y
│   %3 = Main.sin(%2)
│   %4 = Main.cos(%3)
└──      return %4
) => Float64

which doesn’t appear to have types associated with it.
However, if I benchmark f(4.0), it looks very much like it’s got good performance, so I assume that type inference must be happening somewhere.

My question: is there a way to get to the actual typed CodeInfo / IRCode associated with an opaque closure that I’ve constructed in this way, or am I going about this in entirely the wrong way?

I’m running Julia Version 1.10.0-beta1

1 Like