Skip to content

Feature request: isconstant function or macro #50176

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

Open
ojwoodford opened this issue Jun 15, 2023 · 3 comments
Open

Feature request: isconstant function or macro #50176

ojwoodford opened this issue Jun 15, 2023 · 3 comments
Labels
feature Indicates new feature / enhancement requests

Comments

@ojwoodford
Copy link

ojwoodford commented Jun 15, 2023

It would be useful to have an isconstant function or macro which returns true if the input is a constant, otherwise returns false. What do I mean by "constant"? I mean that if v is not a constant, then somefunc(Val(v)) will trigger a dynamic dispatch, otherwise it won't.

Having such a function/macro would enable at least two useful things:

  • Compile-time branch to faster code if a value is constant. See this example.
  • Make constant propagation regressions in core Julia easy to detect in unit tests.
@ojwoodford ojwoodford changed the title Feature request: iscontsant function or macro Feature request: isconstant function or macro Jun 15, 2023
@ojwoodford
Copy link
Author

ojwoodford commented Jun 15, 2023

The StaticInt functionality would provide an alternative mechanism for compile-time branching to faster code if a value is constant. However, I'm not sure it would help with detecting constant propagation regressions.

@MasonProtter
Copy link
Contributor

MasonProtter commented Jun 15, 2023

This thread https://discourse.julialang.org/t/branch-on-whether-a-value-is-known-to-the-compiler/34850 lays out the usecase and expecation pretty clearly. i.e.

@inline function ^(x, p::Integer)
    if @isconstant p
       literal_pow(^, x, Val(p))
    else
       power_by_squaring(x, p)
    end
end

this says that if p is a constant known to the compiler, then we do code generation based on it's value via literal_pow but if it's not a constant then we do the slower power_by_squaring.

A lot like if @generated but for values.

@jishnub
Copy link
Member

jishnub commented Jun 16, 2023

I'm not sure it would help with detecting constant propagation regressions.

Would this not be possible using JET?

@adienes adienes added the feature Indicates new feature / enhancement requests label Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Indicates new feature / enhancement requests
Projects
None yet
Development

No branches or pull requests

4 participants