Skip to content

Subverting type inference? #31429

@timholy

Description

@timholy

I'm trying some dirty tricks and getting crashes. Here the goal is to measure how much time inference spends on each function: my thought was I could define

const __inf_timing__ = Tuple{Float64,Core.MethodInstance}[]

function typeinf_ext_timed(linfo::Core.MethodInstance, params::Core.Compiler.Params)
    tstart = ccall(:jl_clock_now, Float64, ())
    ret = Core.Compiler.typeinf_ext(linfo, params)
    tstop = ccall(:jl_clock_now, Float64, ())
    push!(__inf_timing__, (tstop-tstart, linfo))
    return ret
end

and then be really sneaky and swap this in place of typeinf_ext:

macro snoopi(args...)
    # some preparatory work
    quote
        empty!($__inf_timing__)
        ccall(:jl_set_typeinf_func, Cvoid, (Any,), $typeinf_ext_timed)
        try
            $(esc(cmd))
        finally
            ccall(:jl_set_typeinf_func, Cvoid, (Any,), Core.Compiler.typeinf_ext)
        end
        $sort_timed_inf($tmin)
    end
end

Even though I make sure typeinf_ext_timed is compiled before it gets called, I get a crash when I first switch to it:

Internal error: encountered unexpected error in runtime:
MethodError(f=typeof(SnoopCompile.typeinf_ext_timed)(), args=((::Type{BoundsError})(Any, Tuple{Base.IteratorsMD.CartesianIndex{0}}), 0x00000000000063e2), world=0x00000000000063e1)
rec_backtrace at /home/tim/src/julia-1/src/stackwalk.c:94
record_backtrace at /home/tim/src/julia-1/src/task.c:217 [inlined]
jl_throw at /home/tim/src/julia-1/src/task.c:417
jl_method_error_bare at /home/tim/src/julia-1/src/gf.c:1649
jl_method_error at /home/tim/src/julia-1/src/gf.c:1667
jl_apply_generic at /home/tim/src/julia-1/src/gf.c:2195
jl_apply at /home/tim/src/julia-1/src/julia.h:1571 [inlined]
jl_type_infer at /home/tim/src/julia-1/src/gf.c:277
jl_set_typeinf_func at /home/tim/src/julia-1/src/gf.c:558
top-level scope at /home/tim/.julia/dev/SnoopCompile/src/SnoopCompile.jl:52
jl_fptr_trampoline at /home/tim/src/julia-1/src/gf.c:1864
jl_toplevel_eval_flex at /home/tim/src/julia-1/src/toplevel.c:758
jl_parse_eval_all at /home/tim/src/julia-1/src/ast.c:883
jl_load at /home/tim/src/julia-1/src/toplevel.c:826
include at ./boot.jl:326 [inlined]
include_relative at ./loading.jl:1038
include at ./sysimg.jl:29
jl_apply_generic at /home/tim/src/julia-1/src/gf.c:2219
exec_options at ./client.jl:267
_start at ./client.jl:436
jl_apply_generic at /home/tim/src/julia-1/src/gf.c:2219
jl_apply at /home/tim/src/julia-1/ui/../src/julia.h:1571 [inlined]
true_main at /home/tim/src/julia-1/ui/repl.c:96
main at /home/tim/src/julia-1/ui/repl.c:217
__libc_start_main at /build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.c:310
_start at /home/tim/src/julia-1/julia (unknown line)
Internal error: encountered unexpected error in runtime:
MethodError(f=typeof(SnoopCompile.typeinf_ext_timed)(), args=((::Type{BoundsError})(Any, Base.LinearIndices{1, Tuple{Base.OneTo{Int64}}}), 0x00000000000063e2), world=0x00000000000063e1)
...

This happens independently of whether I try to be really sneaky and set the min_world on the specialization of typeinf_ext_timed to 0.

Is there a workaround? Another (potentially important) application for this general idea is JuliaDebug/JuliaInterpreter.jl#204.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions