Mjolnir.jl icon indicating copy to clipboard operation
Mjolnir.jl copied to clipboard

simple for loop tries to trace through a dispatch with Union

Open shashi opened this issue 5 years ago • 5 comments

julia> g(x) = for i=eachindex(x); x[i] += 1; end
g (generic function with 1 method)

julia> @trace g([1,2,3])
ERROR: Tracing Error: No IR for Tuple{typeof(getfield),Union{Nothing, Tuple{Int64,Int64}},Int64}
in Tuple{typeof(g),Array{Int64,1}}
in Tuple{typeof(getfield),Union{Nothing, Tuple{Int64,Int64}},Int64}
Stacktrace:
 [1] trace(::Mjolnir.Multi{Tuple{Mjolnir.Basic,Mjolnir.Numeric}}, ::Mjolnir.Const{typeof(g)}, ::Vararg{Any,N} where N) at /home/shashi/.julia/dev/Mjolnir/src/trace.jl:249
 [2] top-level scope at REPL[26]:1
 [3] run_backend(::REPL.REPLBackend) at /home/shashi/.julia/packages/Revise/MgvIv/src/Revise.jl:1023
 [4] top-level scope at none:0
caused by [exception 1]
No IR for Tuple{typeof(getfield),Union{Nothing, Tuple{Int64,Int64}},Int64}
Stacktrace:
 [1] tracecall!(::Mjolnir.Trace, ::Array{Any,1}, ::Mjolnir.Const{typeof(getfield)}, ::Vararg{Any,N} where N) at /home/shashi/.julia/dev/Mjolnir/src/trace.jl:212
 [2] traceblock!(::Mjolnir.Trace, ::Dict{Any,Any}, ::IRTools.Inner.Block) at /home/shashi/.julia/dev/Mjolnir/src/trace.jl:172

related to #4 (I think).

Maybe this can be fixed by making trace try all the options in the union separately and then trying to do a union of the types.

shashi avatar May 12 '20 14:05 shashi

Maybe this can be fixed by making trace try all the options in the union separately and then trying to do a union of the types.

That's probably a good way to go for a general fix; basically try and generate an appropriate switch statement that calls the right method.

In this specific case we should really just do type narrowing; i.e. knowing that we are in the body of the loop, so i can't be nothing.

If you have a need for this I can take a look at implementing it. Right now I haven't had a use case since loops get unrolled for e.g. XLA.

MikeInnes avatar May 12 '20 14:05 MikeInnes

No need immediately. Just thought there should be an issue.

shashi avatar May 12 '20 14:05 shashi

In this specific case we should really just do type narrowing; i.e. knowing that we are in the body of the loop, so i can't be nothing.

good point, I think that would also lead to this actually compute the result as opposed to an IR with switch statements.

shashi avatar May 12 '20 14:05 shashi

I got this error too, is there any update on this?

Roger-luo avatar Oct 03 '20 06:10 Roger-luo

The error is now different on master:

ERROR: LoadError: Tracing Error: MethodError: abstract(::Main.BrokenUnion.Mjolnir.Basic, ::Main.BrokenUnion.Mjolnir.Const{typeof(getindex)}, ::Main.BrokenUnion.Mjolnir.Const{Array{Int64,1}}, ::Main.BrokenUnion.Mjolnir.Const{Int64}) is ambiguous. Candidates:
  abstract(::Main.BrokenUnion.Mjolnir.Basic, 461::Union{Main.BrokenUnion.Mjolnir.Const{typeof(getindex)}, Main.BrokenUnion.Mjolnir.Node{typeof(getindex)}, Type{typeof(getindex)}, Main.BrokenUnion.Mjolnir.Partial{typeof(getindex)}, Main.BrokenUnion.Mjolnir.Shape{typeof(getindex)}}, xs::Main.BrokenUnion.Mjolnir.Const{var"#s35"} where var"#s35"<:Array, i::Main.BrokenUnion.Mjolnir.Const...) in Main.BrokenUnion.Mjolnir at /home/mccoy/code/julia/Mjolnir.jl/src/macros.jl:55
  abstract(::Main.BrokenUnion.Mjolnir.Basic, 475::Union{Main.BrokenUnion.Mjolnir.Const{typeof(getindex)}, Main.BrokenUnion.Mjolnir.Node{typeof(getindex)}, Type{typeof(getindex)}, Main.BrokenUnion.Mjolnir.Partial{typeof(getindex)}, Main.BrokenUnion.Mjolnir.Shape{typeof(getindex)}}, xs::Union{Type{var"#s34"}, Main.BrokenUnion.Mjolnir.Const{var"#s34"}, Main.BrokenUnion.Mjolnir.Node{var"#s34"}, Main.BrokenUnion.Mjolnir.Partial{var"#s34"}, Main.BrokenUnion.Mjolnir.Shape{var"#s34"}} where var"#s34"<:(AbstractArray{T,N} where N), is::Union{Type{var"#s28"}, Main.BrokenUnion.Mjolnir.Const{var"#s28"}, Main.BrokenUnion.Mjolnir.Node{var"#s28"}, Main.BrokenUnion.Mjolnir.Partial{var"#s28"}, Main.BrokenUnion.Mjolnir.Shape{var"#s28"}} where var"#s28"<:Integer...) where T in Main.BrokenUnion.Mjolnir at /home/mccoy/code/julia/Mjolnir.jl/src/macros.jl:55
To resolve the ambiguity, try making one of the methods more specific, or adding a new method more specific than any of the existing applicable methods.
in Tuple{typeof(Main.BrokenUnion.g),Array{Int64,1}}

femtomc avatar Oct 08 '20 00:10 femtomc