Skip to content

Reducing estimated cost of not_int to zero for more reasonable inlining #35697

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
kimikage opened this issue May 2, 2020 · 1 comment · Fixed by #35707
Closed

Reducing estimated cost of not_int to zero for more reasonable inlining #35697

kimikage opened this issue May 2, 2020 · 1 comment · Fixed by #35707
Labels
compiler:optimizer Optimization passes (mostly in base/compiler/ssair/)

Comments

@kimikage
Copy link
Contributor

kimikage commented May 2, 2020

Surprisingly(?), there is a non-trivial difference between the two functions below:

f(x) = x == zero(x) ? "zero" : "non-zero"

g(x) = x != zero(x) ? "non-zero" : "zero"

The estimated cost of g(x::Int) is 1 higher than f(x::Int) due to the extra Base.not_int.

add_tfunc(not_int, 1, 1, math_tfunc, 1)

Although you might think the difference is trivial, this can cause speed differences of ~10 times depending on the SIMD vectorization. (For example, JuliaGraphics/Colors.jl#407)

As you know, the cost model of inlining has room for improvement, and the modeling is "extremely" difficult. However my suggestion is just to change one character on the line shown above.

I think we seldom use Base.not_int(::Int64), and often use Base.not_int(::Bool). It is usually required in conditional expressions, and in most CPU instruction sets there are instructions or special operands for negative conditions (e.g. "not equal", "not less than"). Therefore, IMO, the cost 0 is reasonable.

@StefanKarpinski
Copy link
Member

Makes sense: negating a condition basically never increases the instruction count.

@JeffBezanson JeffBezanson added the compiler:optimizer Optimization passes (mostly in base/compiler/ssair/) label May 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:optimizer Optimization passes (mostly in base/compiler/ssair/)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants