Skip to content

Constant-folding in inference for getfield #9345

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

Closed
timholy opened this issue Dec 13, 2014 · 2 comments
Closed

Constant-folding in inference for getfield #9345

timholy opened this issue Dec 13, 2014 · 2 comments

Comments

@timholy
Copy link
Member

timholy commented Dec 13, 2014

If possible, it would be ideal to perform constant folding before running inference on getfield. Here's a demo:

julia> abstract Container{N}

julia> type Container1d <: Container{1}
           center1::Float32
           child::Container1d
       end

julia> type Container2d <: Container{2}
           center1::Float32
           center2::Float32
           child::Container2d
       end

julia> getchild{N}(parent::Container{N}) = getfield(parent, N+1)
getchild (generic function with 1 method)

julia> function childpositive(c::Container)
           child = getchild(c)
           child.center1 > 0
       end
childpositive (generic function with 1 method)

julia> code_typed(childpositive, (Container1d,))
1-element Array{Any,1}:
 :($(Expr(:lambda, Any[:c], Any[Any[:child,:_var0,:_var3,:_var4],Any[Any[:c,Container1d,0],Any[:child,Union(Container1d,Float32),18],Any[:_var0,Float32,18],Any[:_var3,(Int64,),0],Any[:_var4,Float32,18]],Any[]], :(begin  # none, line 2:
        child = getfield(c::Container1d,(top(box))(Int64,(top(add_int))(1,1))::Int64)::Union(Container1d,Float32) # line 3:
        _var4 = (top(getfield))(child::Union(Container1d,Float32),:center1)::Float32
        _var0 = (top(box))(Float32,(top(sitofp))(Float32,0))::Float32
        return (top(box))(Bool,(top(or_int))((top(lt_float))(_var0::Float32,_var4::Float32)::Bool,(top(box))(Bool,(top(and_int))((top(eq_float))(_var0::Float32,_var4::Float32)::Bool,(top(box))(Bool,(top(or_int))((top(eq_float))(_var0::Float32,9.223372f18)::Bool,(top(slt_int))(0,(top(box))(Int64,(top(fptosi))(Int64,_var0::Float32))::Int64)::Bool))::Bool))::Bool))::Bool
    end::Bool))))

Note the Union(Container1d,Float32) for the result of getchild(c), which arises presumably because add_int(1,1) isn't evaluated.

For comparison,

julia> function centerpositive(c::Container)
           c.center1 > 0
       end
centerpositive (generic function with 1 method)

julia> code_typed(centerpositive, (Container1d,))
1-element Array{Any,1}:
 :($(Expr(:lambda, Any[:c], Any[Any[:_var0,:_var3,:_var4],Any[Any[:c,Container1d,0],Any[:_var0,Float32,18],Any[:_var3,(Int64,),0],Any[:_var4,Float32,18]],Any[]], :(begin  # none, line 2:
        _var4 = (top(getfield))(c::Container1d,:center1)::Float32
        _var0 = (top(box))(Float32,(top(sitofp))(Float32,0))::Float32
        return (top(box))(Bool,(top(or_int))((top(lt_float))(_var0::Float32,_var4::Float32)::Bool,(top(box))(Bool,(top(and_int))((top(eq_float))(_var0::Float32,_var4::Float32)::Bool,(top(box))(Bool,(top(or_int))((top(eq_float))(_var0::Float32,9.223372f18)::Bool,(top(slt_int))(0,(top(box))(Int64,(top(fptosi))(Int64,_var0::Float32))::Int64)::Bool))::Bool))::Bool))::Bool
    end::Bool))))

is, of course, inferred beautifully.

Now that we have some parse-time constant folding in cartesian.jl, perhaps we could just use that?

@JeffBezanson
Copy link
Member

I think this is a dup of #5560.

@timholy
Copy link
Member Author

timholy commented Dec 14, 2014

Agreed. I did search, but used the term "fold."

@timholy timholy closed this as completed Dec 14, 2014
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

2 participants