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

RFC: Add @will_specialize macro and will_specialize function to InteractiveUtils #33146

Closed
wants to merge 1 commit into from
Closed

RFC: Add @will_specialize macro and will_specialize function to InteractiveUtils #33146

wants to merge 1 commit into from

Conversation

DilumAluthge
Copy link
Member

@DilumAluthge DilumAluthge commented Sep 3, 2019

See also: #33142

This pull request adds the InteractiveUtils.@will_specialize macro and the InteractiveUtils.will_specialize function.

  • InteractiveUtils.@will_specialize f(x, y, z...) returns true if the call to f(x, y, z...) will be fully specialized on its arguments, and false otherwise.

  • InteractiveUtils.will_specialize f(x, Tuple{R, S, T...} returns true if the call to f with argument types R, S, T... will be fully specialized on its arguments, and false otherwise.

Examples

julia> f1(x) = Base.typemax(x)
f1 (generic function with 1 method)

julia> @will_specialize f1(Int)
false

julia> g(x) = x + x
g (generic function with 1 method)

julia> @will_specialize g(1)
true

julia> foo(t::Type) = t
foo (generic function with 1 method)

julia> @will_specialize foo(Int)
false

julia> function foo2(t::Type)
           x = ones(t, 10)
           return sum(map(sin, x))
       end
foo2 (generic function with 1 method)

julia> @will_specialize foo2(Int)
false

Motivation

As discussed in #23749, #32834, and #32817:

@code_typed and friends will always show you specialized code, even if Julia
would not normally specialize that method call

The InteractiveUtils.@will_specialize macro and InteractiveUtils.will_specialize function introduced in this pull request provide a convenient way to find out whether or not Julia would normally specialize a given method call.

Copy link
Contributor

@nickrobinson251 nickrobinson251 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems cool! I am not well-placed to review the implementation (sorry), but some thoughts on docs below :)

@will_specialize

Evaluates the arguments to the function or macro call, determines their types, and calls
[`will_specialize`](@ref) on the resulting expression.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it is worth cross-referencing Base's at-specialize / at-nospecialize for extra context?

e.g.

Suggested change
[`will_specialize`](@ref) on the resulting expression.
[`will_specialize`](@ref) on the resulting expression.
See also: [`specialize`](@ref), [`nospecialize`](@ref)

(^ I think at-ref to Base function will "just work" but not 100% sure if this is the right suntax for it)

will_specialize(f, types)

Returns `true` if the specialization on this method signature will be compiled
into the cache for this method.
Copy link
Contributor

@nickrobinson251 nickrobinson251 Sep 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps worth making even more explicit that this means "Returns true if the call will be specialized on all its arguments"?

I really like the explanation you gave in the opening post of the PR :)

Could some of the examples you give be included in the docstring (as doctests)?

@DilumAluthge DilumAluthge changed the title Add @will_specialize macro and will_specialize function to InteractiveUtils RFC: Add @will_specialize macro and will_specialize function to InteractiveUtils Sep 21, 2019
@DilumAluthge DilumAluthge reopened this Sep 21, 2019
@DilumAluthge
Copy link
Member Author

This is a note to myself:

This pull request only deals with positional arguments. We will need to also find a way to deal with inaccurate specialization on keyword arguments.

@DilumAluthge DilumAluthge deleted the da/will-specialize branch November 14, 2019 05:21
@iamed2
Copy link
Contributor

iamed2 commented Sep 28, 2021

@DilumAluthge Was this work continued or replaced elsewhere or did you decide to stop working on it?

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.

3 participants