Skip to content

Make @fastmath aware of Base.literal_pow#21099

Closed
ajkeller34 wants to merge 1 commit intoJuliaLang:masterfrom
ajkeller34:val_fastmath
Closed

Make @fastmath aware of Base.literal_pow#21099
ajkeller34 wants to merge 1 commit intoJuliaLang:masterfrom
ajkeller34:val_fastmath

Conversation

@ajkeller34
Copy link
Copy Markdown
Contributor

as Base.literal_pow(Base.FastMath.pow_fast, x, Val{p}).

Thanks to PRs #20530 and #20889, exponentiation of a dimensionful quantity can be type-stable in some important circumstances. However, because macro expansion occurs before the Val lowering happens, @fastmath x^2 cannot be type-stable for dimensionful quantities, whereas x^2 could be. This is because @fastmath x^2 is macro-expanded to Base.FastMath.pow_fast(x,2) and so we can never catch the ^ symbol. See the tests I added to get an idea of how this works.

Provided the premise of this PR isn't flawed somehow, it would probably good to have someone more familiar with julia_syntax.scm see if there’s a better way to implement this change. Because I tie into the mechanism introduced in #20889 I don't expect any "surprising behaviors" to be introduced here, though I haven't checked to see if there are any performance implications.

@ararslan ararslan added compiler:lowering Syntax lowering (compiler front end, 2nd stage) maths Mathematical functions labels Mar 20, 2017
@tkelman
Copy link
Copy Markdown
Contributor

tkelman commented Mar 20, 2017

dunno if nanosoldier will really notice lowering performance, but may as well trigger @nanosoldier runbenchmarks(ALL, vs = ":master")

@nanosoldier
Copy link
Copy Markdown
Collaborator

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @jrevels

@TotalVerb
Copy link
Copy Markdown
Contributor

It would make more sense to do this in the @fastmath macro than in lowering.

@StefanKarpinski
Copy link
Copy Markdown
Member

It would make more sense to do this in the @fastmath macro than in lowering.

100% – we can't go around having special cases for every macro anyone might need special behavior for in the parser.

@ajkeller34
Copy link
Copy Markdown
Contributor Author

Yeah, good point, I'll see what I can do.

@ajkeller34
Copy link
Copy Markdown
Contributor Author

Updated, this approach seems much better and obvious in retrospect. I guess this is what happens when you learn about how lisp works for the first time and get a little too exuberant.

@ajkeller34 ajkeller34 changed the title RFC: Lower Base.FastMath.pow_fast(x,p) for integer literal p... RFC: Make @fastmath aware of Base.literal_pow Mar 20, 2017
@KristofferC
Copy link
Copy Markdown
Member

Let's rerun CI and Nanosoldier and try get this merged.

@KristofferC KristofferC reopened this Jul 21, 2017
@KristofferC
Copy link
Copy Markdown
Member

@nanosoldier runbenchmarks(ALL, vs = ":master")

@stevengj
Copy link
Copy Markdown
Member

It would be nice to have @fastmath convert x^literal to an optimal chain of multiplications ala #20637, but we can leave that for a future PR.

@nanosoldier
Copy link
Copy Markdown
Collaborator

Something went wrong when running your job:

NanosoldierError: failed to run benchmarks against primary commit: failed process: Process(`sudo cset shield -e su nanosoldier -- -c ./benchscript.sh`, ProcessExited(1)) [1]

Logs and partial data can be found here
cc @ararslan

@KristofferC
Copy link
Copy Markdown
Member

Tests seems to fail now. Is it because the Val optimization is only made to "hardware numbertypes" now?

@ajkeller34
Copy link
Copy Markdown
Contributor Author

I updated the PR, squashed, and rebased. fastmath tests passed locally. The problem was because the PR hadn't adopted the new Val(x) syntax from #22475.

@ajkeller34
Copy link
Copy Markdown
Contributor Author

@KristofferC want to try triggering nanosoldier again? Looks like the tests passed (travis timed out for likely unrelated reasons).

@ararslan
Copy link
Copy Markdown
Member

ararslan commented Jul 29, 2017

Another Nanosoldier run is unlikely to work, Nanosoldier itself is still having problems (unrelated to this PR)

@KristofferC
Copy link
Copy Markdown
Member

I thought it worked a few times yesterday @nanosoldier runbenchmarks(ALL, vs = ":master")

@nanosoldier
Copy link
Copy Markdown
Collaborator

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @ararslan

@ararslan
Copy link
Copy Markdown
Member

It's been working randomly but failing most of the time

@ajkeller34 ajkeller34 changed the title RFC: Make @fastmath aware of Base.literal_pow Make @fastmath aware of Base.literal_pow Aug 1, 2017
@ajkeller34
Copy link
Copy Markdown
Contributor Author

This is ready to merge as far as I can tell. Does anything else need to be done here?

Comment thread test/fastmath.jl
@JeffBezanson JeffBezanson added this to the 1.0.x milestone Apr 19, 2018
@stevengj
Copy link
Copy Markdown
Member

stevengj commented Jan 4, 2019

Bump.

@stevengj
Copy link
Copy Markdown
Member

stevengj commented Apr 5, 2020

Any chance of merging this?

@StefanKarpinski StefanKarpinski added the triage This should be discussed on a triage call label Apr 5, 2020
@JeffBezanson
Copy link
Copy Markdown
Member

From triage: seems fine, just rebase please.

@stevengj
Copy link
Copy Markdown
Member

stevengj commented Jan 7, 2021

Rebased.

@oscardssmith
Copy link
Copy Markdown
Member

The test this PR adds is the one that's failing.

@stevengj
Copy link
Copy Markdown
Member

stevengj commented Jan 8, 2021

Interacting badly with #24240, will fix.

@stevengj
Copy link
Copy Markdown
Member

stevengj commented Jan 8, 2021

Actually, #24240 already seems to fix this issue in another way — @fastmath x^2 goes to pow_fast(x, Val{2}()) which calls literal_pow by default.

So maybe this can be closed?

@oscardssmith oscardssmith removed the triage This should be discussed on a triage call label Jan 8, 2021
@vtjnash vtjnash closed this Apr 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler:lowering Syntax lowering (compiler front end, 2nd stage) maths Mathematical functions

Projects

None yet

Development

Successfully merging this pull request may close these issues.