Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subverting type inference? #31429

Closed
timholy opened this issue Mar 21, 2019 · 0 comments
Closed

Subverting type inference? #31429

timholy opened this issue Mar 21, 2019 · 0 comments

Comments

@timholy
Copy link
Member

timholy commented Mar 21, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant