Add inline macro via :meta expressions #8297

Merged
merged 3 commits into from Sep 20, 2014

Projects

None yet

4 participants

@timholy
Member
timholy commented Sep 10, 2014

This reinstates @vtjnash's contribution of the ability to declare that a function should be inlined. However, this implementation does it with an @inline macro and a new Expr(:meta, <metadata>...) construct. meta-expressions can be used for other things too (I wonder if @ArchRobison may be interested in using it to introduce @SLPvectorize...)

Demo:

function f(x)
    y = x+5
    z = y*y
    q = z/y
    m = q-3
end

@inline function f_inlined(x)
    y = x+5
    z = y*y
    q = z/y
    m = q-3
end

g(x) = f(2x)
g_inlined(x) = f_inlined(2x)

julia> code_typed(g, (Int,))
1-element Array{Any,1}:
 :($(Expr(:lambda, {:x}, {{},{{:x,Int64,0}},{}}, :(begin  # none, line 1:
        return f((top(box))(Int64,(top(mul_int))(2,x::Int64))::Int64)::Float64
    end::Float64))))

julia> code_typed(g_inlined, (Int,))
1-element Array{Any,1}:
 :($(Expr(:lambda, {:x}, {{:_var0,:_var1,:_var2,:_var3,:_var4},{{:x,Int64,0},{:_var0,Int64,18},{:_var1,Int64,18},{:_var2,Float64,18},{:_var3,Float64,18},{:_var4,Float64,18}},{}}, :(begin  # none, line 1:
        _var0 = (top(box))(Int64,(top(add_int))((top(box))(Int64,(top(mul_int))(2,x::Int64))::Int64,5))::Int64
        _var1 = (top(box))(Int64,(top(mul_int))(_var0::Int64,_var0::Int64))::Int64
        _var2 = (top(box))(Float64,(top(div_float))((top(box))(Float64,(top(sitofp))(Float64,_var1::Int64))::Float64,(top(box))(Float64,(top(sitofp))(Float64,_var0::Int64))::Float64))::Float64
        _var3 = (top(box))(Float64,(top(sub_float))(_var2::Float64,(top(box))(Float64,(top(sitofp))(Float64,3))::Float64))::Float64
        _var4 = _var3::Float64
        return _var3::Float64
    end::Float64))))

Everything seems to work, except for some reason my attempt to "register" the meta_sym doesn't seem to have succeeded(EDIT: see below):

julia> g(3)
8.0

julia> g_inlined(3)
8.0

julia> f(3)
5.0

julia> f_inlined(3)
ERROR: error compiling f_inlined: unsupported or misplaced expression meta in function f_inlined

Anyone know why? @ArchRobison, you had to go through this with simdloop_sym, any tips?

Reference: discussion starting at #3796 (comment).

Also CC @carlobaldassi (cartesian iteration).

@ArchRobison
Contributor

The "registration" is in interpreter.c, in this piece of code:

    else if (ex->head == boundscheck_sym) {
        return (jl_value_t*)jl_nothing;
    }
    else if (ex->head == simdloop_sym) {
        return (jl_value_t*)jl_nothing;
    }
    jl_errorf("unsupported or misplaced expression %s", ex->head->name);
@timholy
Member
timholy commented Sep 11, 2014

Hah, that was it! Having found one source of the error I assumed my check was somehow failing, I should have searched for a second site. Thanks a lot, @vtjnash!

OK, removing WIP tag, proposing this seriously for merging.

@timholy timholy changed the title from WIP: add inline macro via :meta expressions to Add inline macro via :meta expressions Sep 11, 2014
@timholy
Member
timholy commented Sep 11, 2014

...and the new tests are finally green...

@timholy timholy merged commit c9c563c into master Sep 20, 2014

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details
@timholy timholy deleted the teh/inline_meta branch Sep 20, 2014
@mweastwood mweastwood referenced this pull request in Keno/Cxx.jl Sep 21, 2014
Closed

Building on RHEL #16

@carlobaldassi
Member

This is a long-awaited feature, thanks for doing this!

@timholy
Member
timholy commented Sep 21, 2014

You're most welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment