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

Add inline macro via :meta expressions #8297

Merged
merged 3 commits into from
Sep 20, 2014
Merged

Add inline macro via :meta expressions #8297

merged 3 commits into from
Sep 20, 2014

Conversation

timholy
Copy link
Sponsor Member

@timholy 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
Copy link
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);

@vtjnash
Copy link
Sponsor Member

vtjnash commented Sep 11, 2014

I suspect your missing usage is in codegen itself:

if (valuepos || !(head == line_sym || head == type_goto_sym)) {

@timholy
Copy link
Sponsor Member Author

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 WIP: add inline macro via :meta expressions Add inline macro via :meta expressions Sep 11, 2014
@timholy timholy force-pushed the teh/inline_meta branch 3 times, most recently from 7f7d9de to 54b0ae4 Compare September 11, 2014 19:08
@timholy
Copy link
Sponsor Member Author

timholy commented Sep 11, 2014

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

timholy added a commit that referenced this pull request Sep 20, 2014
Add inline macro via :meta expressions
@timholy timholy merged commit c9c563c into master Sep 20, 2014
@timholy timholy deleted the teh/inline_meta branch September 20, 2014 23:34
@carlobaldassi
Copy link
Member

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

@timholy
Copy link
Sponsor Member Author

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants