-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
8264945: Optimize the code-gen for Math.pow(x, 0.5) #3404
Conversation
/test |
👋 Welcome back jiefu! A progress list of the required criteria for merging this PR into |
@DamonFool |
@DamonFool The |
Does float vector needs same optimization, convert pow to sqrt? |
Thanks @huishi-hs for your review. The same optimization for the vector api is in progress. Thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good.
@DamonFool This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be:
You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 11 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. ➡️ To integrate this PR with the above commit message to the |
Thanks @neliasso . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, verify that result is the same when run with -Xint (Interpreter only) and (-XX:TieredStopAtLevel=1) C1 only. May be they need the same optimization.
Hi @vnkozlov @neliasso and @huishi-hs , Thanks for your review and comments. [1] https://mail.openjdk.java.net/pipermail/core-libs-dev/2021-April/076195.html |
Hi all, According to the discussion [1], we can still perform pow(x, 0.5) => sqrt(x) for x >= 0.0 or x is +Inf/NaN. To better help the code review, the whole optimization has been split into JDK-8265325 and JDK-8264945.
I'll update this pr once JDK-8265325 is finished since it depends on JDK-8265325. Thanks. [1] https://mail.openjdk.java.net/pipermail/core-libs-dev/2021-April/076220.html |
Hi all, This is the follow-up of JDK-8265325, which optimizes the code-gen of C2 for pow(x, 0.5). Before
After
Testing: Thanks. |
/test |
@DamonFool you need to get approval to run the tests in tier1 for commits up until beab8e7 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good.
Yes. Looks good. |
This may be unsafe. AFAIK there is no guarantee about the monotonicity of |
Thanks @theRealAph for your review. What did you mean by |
Hi @theRealAph , I didn't get your point. Could you please make it more clearer and give us an example? |
From the spec, "errors are required to be semi-monotonic: whenever the mathematical function is non-decreasing, so is the floating-point approximation, likewise, whenever the mathematical function is non-increasing, so is the floating-point approximation." nextAfter "Returns the floating-point number adjacent to the first argument in the direction of the second argument." Using sqrt() rather than pow() for pow(x,0.5) will return different results for some arguments. For this optimization you're proposing to be safe, you need to prove that in such cases the "Results must be semi-monotonic" requirement is still satisfied. There may exist values of pow(x,0.5) where, if we substitute sqrt() this requirement is not met. I don't immediately know how to prove this does not happen. I think that to do so would require careful analysis. |
Hi @theRealAph , Thanks for your clarification. Actually, this optimization has been long used for Java. What do you think of the same optimization in the HotSpot which has been applied more than 10 years ago? And also the same optimization in the Java lib?
Thanks. |
OK, fair enough. I still don't know if this optimization is safe, but I guess (?) the analysis was done at the time. In any case, given that this optimization is already done elsewhere, your patch won't make anything any worse. |
/integrate |
@DamonFool Since your change was applied there have been 45 commits pushed to the
Your commit was automatically rebased without conflicts. Pushed as commit ed477da. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
Thanks @theRealAph . And thank you all again. Best regards, |
Hi all,
I'd like to optimize the code-gen for Math.pow(x, 0.5).
And 7x ~ 14x performance improvement is observed by the jmh micro-benchmarks.
While I was optimizing a machine learning program, I found both Math.pow(x, 2) and Math.pow(x, 0.5) are used.
To my surprise, C2 just optimizes the case for Math.pow(x, 2) [1], but still not for Math.pow(x, 0.5) yet.
The patch just replace Math.pow(x, 0.5) with Math.sqrt(x).
Before:
After:
Testing:
Thanks,
Best regards,
Jie
[1] https://github.com/openjdk/jdk/blob/master/src/hotspot/share/opto/library_call.cpp#L1680
Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/3404/head:pull/3404
$ git checkout pull/3404
Update a local copy of the PR:
$ git checkout pull/3404
$ git pull https://git.openjdk.java.net/jdk pull/3404/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 3404
View PR using the GUI difftool:
$ git pr show -t 3404
Using diff file
Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/3404.diff