This function helped me find a couple of type problems in a somewhat 
complex code base. The idea is to first run your code, then use this 
function to dig up the type signatures that were actually called and 
identify type problematic variables from code_typed.

I strongly suspect that the inspection of f.env.cache_arg1 is not the 
proper way to find the used type signatures, so if someone who understands 
the internals could clean this up I think it could become a valuable tool.

function inspect(modulename, filename)
    for name in names(modulename)
        f = getfield(modulename, name)
        if isgeneric(f)
            if basename(string(f.env.defs.func.code.file)) == filename
                println(f)
                c = f.env.cache_arg1
                for k = 1:length(c)
                    if isdefined(c, k)
                        sig = c[k].sig
                        println("  ", sig)
                        t = code_typed(f, sig)
                        vars = t[1].args[2][2]
                        for var in vars
                            if !isleaftype(var[2])
                                println("    $(var[1]) $(var[2])")
                            end
                        end
                    end
                end
            end
        end
    end
end




Den tisdagen den 16:e september 2014 kl. 06:02:54 UTC+2 skrev Simon 
Kornblith:
>
> There is most certainly a type problem. You're not getting type 
> information for sparse_grid.lvl_l which deoptimizes a lot of things. In 
> your code, you have:
>
> type sparse_grid        d::Int64        q::Int64        n::Int64        
> grid::Array{Float64}        ind::Array{Int64}        lvl::Array{Int64}   
>      lvl_l::Array{Int64}        lvl_s::Array{Float64}        bounds::Array
> {Float64}        gtype        B        Bf        ubend
>
> Try specifying the number of dimensions for the arrays, e.g. 
> grid::Array{Float64,2}. Also using any of the untyped fields will slow 
> code down, but I don't see them in that function.
>
> A hackish way to find type issues is:
>
> filter!(x->!isleaftype(x[2]), @code_typed(f(args))[1].args[2][2])
>
> which returns a list of variables where type inference couldn't infer a 
> concrete/leaf type. In some cases these will be "hidden" variables so you 
> may still need to inspect the output of code_typed. TypeCheck.jl and 
> Lint.jl may also provide more sophisticated tools for this purpose.
>
> Simon
>
> On Monday, September 15, 2014 5:16:36 AM UTC-4, Zac wrote:
>>
>> https://gist.github.com/Zac12345/519bd7a503a1fd1b8d98 has the updated 
>> function and code_typed output
>>
>> Pkg.clone("https://github.com/Zac12345/Sparse";) should be fine for the 
>> test as the gist does'nt require functions using the shared lib.
>>
>> Are there any specific things i should be looking for in the code_typed 
>> results?
>>
>

Reply via email to