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

8264945: Optimize the code-gen for Math.pow(x, 0.5) #3404

Closed
wants to merge 4 commits into from

Conversation

@DamonFool
Copy link
Member

@DamonFool DamonFool commented Apr 9, 2021

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:

Benchmark                     (seed)   Mode  Cnt      Score    Error   Units
MathBench.powDouble0Dot5           0  thrpt    8  45525.117 ? 11.686  ops/ms
MathBench.powDouble0Dot5Loop       0  thrpt    8      0.031 ?  0.001  ops/ms

Benchmark                     (seed)   Mode  Cnt      Score    Error   Units
MathBench.powDouble0Dot5           0  thrpt    8  45509.317 ?  6.581  ops/ms
MathBench.powDouble0Dot5Loop       0  thrpt    8      0.031 ?  0.001  ops/ms

After:

Benchmark                     (seed)   Mode  Cnt       Score     Error   Units
MathBench.powDouble0Dot5           0  thrpt    8  343354.892 ? 362.900  ops/ms
MathBench.powDouble0Dot5Loop       0  thrpt    8       0.457 ?   0.001  ops/mso

Benchmark                     (seed)   Mode  Cnt       Score    Error   Units
MathBench.powDouble0Dot5           0  thrpt    8  343421.559 ? 49.326  ops/ms
MathBench.powDouble0Dot5Loop       0  thrpt    8       0.457 ?  0.001  ops/ms

Testing:

  • tier1~3 on Linux/x64

Thanks,
Best regards,
Jie

[1] https://github.com/openjdk/jdk/blob/master/src/hotspot/share/opto/library_call.cpp#L1680


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

  • JDK-8264945: Optimize the code-gen for Math.pow(x, 0.5)

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

@DamonFool
Copy link
Member Author

@DamonFool DamonFool commented Apr 9, 2021

/test
/label add hotspot-compiler
/cc hotspot-compiler

Loading

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented Apr 9, 2021

👋 Welcome back jiefu! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

Loading

@openjdk
Copy link

@openjdk openjdk bot commented Apr 9, 2021

@DamonFool
The hotspot-compiler label was successfully added.

Loading

@openjdk
Copy link

@openjdk openjdk bot commented Apr 9, 2021

@DamonFool The hotspot-compiler label was already applied.

Loading

@mlbridge
Copy link

@mlbridge mlbridge bot commented Apr 9, 2021

Webrevs

Loading

@huishi-hs
Copy link

@huishi-hs huishi-hs commented Apr 9, 2021

Does float vector needs same optimization, convert pow to sqrt?

Loading

@DamonFool
Copy link
Member Author

@DamonFool DamonFool commented Apr 9, 2021

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.
Let's see what the reviewers think of this opt.

Thanks.

Loading

Copy link
Contributor

@neliasso neliasso left a comment

Looks good.

Loading

@openjdk
Copy link

@openjdk openjdk bot commented Apr 9, 2021

@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:

8264945: Optimize the code-gen for Math.pow(x, 0.5)

Reviewed-by: neliasso, kvn

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 master branch:

  • 3990713: 8265463: ProblemList vmTestbase/vm/mlvm/mixed/stress/regression/b6969574/INDIFY_Test.java on Win-X64 -Xcomp
  • 5b43b39: 8263154: [macos] DMG builds have finder errors
  • 54cb388: 8252600: [JVMCI] remove mx configuration
  • b703e0a: 8264569: Remove obsolete error messages from CDSTestUtils.java
  • 235daea: 8265285: Unnecessary inclusion of bytecodeHistogram.hpp
  • 8bec6fe: 8264277: java.xml.crypto module should be granted FilePermission and SocketPermission
  • 5303ccb: 8265135: Reduce work initializing VarForms
  • d9e40dd: 8199407: Skip Rebuild Remset Phase if there are no rebuild candidates
  • 8dbf7aa: 8241306: Add SignatureMethodParameterSpec subclass for RSASSA-PSS params
  • b14e0ee: 8265371: Change to Visual Studio 2019 16.9.3 for building on Windows at Oracle
  • ... and 1 more: https://git.openjdk.java.net/jdk/compare/b64a3fb946a2855c612458aca9d62ef008f8f9be...master

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 master branch, type /integrate in a new comment.

Loading

@openjdk openjdk bot added the ready label Apr 9, 2021
@DamonFool
Copy link
Member Author

@DamonFool DamonFool commented Apr 9, 2021

Looks good.

Thanks @neliasso .

Loading

Copy link
Contributor

@vnkozlov vnkozlov left a comment

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.

Loading

@DamonFool
Copy link
Member Author

@DamonFool DamonFool commented Apr 12, 2021

Hi @vnkozlov @neliasso and @huishi-hs ,

Thanks for your review and comments.
While I was implementing the opt for C1 and interpreter, I found Math.pow(x, 0.5) and Math.sqrt(x) would compute different values for x={-0.0, Double.NEGATIVE_INFINITY} [1].
Let's hold on this issue until we have a conclusion about that question.
Thanks.

[1] https://mail.openjdk.java.net/pipermail/core-libs-dev/2021-April/076195.html

Loading

@DamonFool
Copy link
Member Author

@DamonFool DamonFool commented Apr 16, 2021

Hi @vnkozlov @neliasso and @huishi-hs ,

Thanks for your review and comments.
While I was implementing the opt for C1 and interpreter, I found Math.pow(x, 0.5) and Math.sqrt(x) would compute different values for x={-0.0, Double.NEGATIVE_INFINITY} [1].
Let's hold on this issue until we have a conclusion about that question.
Thanks.

[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.

    1. JDK-8265325: Optimize StubRoutines::dpow() for Math.pow(x, 0.5)
    1. JDK-8264945: Optimize the code-gen for Math.pow(x, 0.5)

I'll update this pr once JDK-8265325 is finished since it depends on JDK-8265325.

Thanks.
Best regards,
Jie

[1] https://mail.openjdk.java.net/pipermail/core-libs-dev/2021-April/076220.html

Loading

@DamonFool
Copy link
Member Author

@DamonFool DamonFool commented Apr 19, 2021

Hi all,

This is the follow-up of JDK-8265325, which optimizes the code-gen of C2 for pow(x, 0.5).
Instead of calling into the StubRoutines::dpow(), the compiler can directly generate sqrt for all x >= 0.0.
1.5x ~ 4.2x performance improvement is observed after the opt.

Before

Benchmark                      (seed)   Mode  Cnt       Score       Error   Units
MathBench.powDouble0Dot5Const       0  thrpt    8  203004.302 ?   684.072  ops/ms
MathBench.powDouble0Dot5Loop        0  thrpt    8       0.121 ?     0.001  ops/ms

After

Benchmark                      (seed)   Mode  Cnt       Score      Error   Units
MathBench.powDouble0Dot5Const       0  thrpt    8  308771.237 ? 1604.567  ops/ms   <-- 1.5x
MathBench.powDouble0Dot5Loop        0  thrpt    8       0.508 ?    0.001  ops/ms   <-- 4.2x

Testing:
tier1 ~ tier3 on Linux/x64

Thanks.
Best regards,
Jie

Loading

@DamonFool
Copy link
Member Author

@DamonFool DamonFool commented Apr 19, 2021

/test

Loading

@openjdk
Copy link

@openjdk openjdk bot commented Apr 19, 2021

@DamonFool you need to get approval to run the tests in tier1 for commits up until beab8e7

Loading

@openjdk openjdk bot added the test-request label Apr 19, 2021
Copy link
Contributor

@vnkozlov vnkozlov left a comment

Good.

Loading

@DamonFool
Copy link
Member Author

@DamonFool DamonFool commented Apr 20, 2021

Good.

Thanks @vnkozlov .

Hi @neliasso , are you OK with the latest change?
Thanks.

Loading

@neliasso
Copy link
Contributor

@neliasso neliasso commented Apr 20, 2021

Hi @neliasso , are you OK with the latest change?

Yes. Looks good.

Loading

@DamonFool
Copy link
Member Author

@DamonFool DamonFool commented Apr 20, 2021

Hi @neliasso , are you OK with the latest change?

Yes. Looks good.

Thanks @neliasso .

Will push it tomorrow if there is no objection.

Loading

@theRealAph
Copy link
Contributor

@theRealAph theRealAph commented Apr 20, 2021

This may be unsafe. AFAIK there is no guarantee about the monotonicity of Math.pow(x, y) as y moves from Math.pow(x, nextAfter​(0.5, -1) to Math.pow(x, 0.5) to Math.pow(x, nextAfter​(0.5, 1) for all x. Did anyone look?

Loading

@DamonFool
Copy link
Member Author

@DamonFool DamonFool commented Apr 20, 2021

This may be unsafe. AFAIK there is no guarantee about the monotonicity of Math.pow(x, y) as y moves from Math.pow(x, nextAfter​(0.5, -1) to Math.pow(x, 0.5) to Math.pow(x, nextAfter​(0.5, 1) for all x. Did anyone look?

Thanks @theRealAph for your review.

What did you mean by Math.pow(x, nextAfter​(0.5, -1) and Math.pow(x, nextAfter​(0.5, 1) ?

Loading

@DamonFool
Copy link
Member Author

@DamonFool DamonFool commented Apr 21, 2021

This may be unsafe. AFAIK there is no guarantee about the monotonicity of Math.pow(x, y) as y moves from Math.pow(x, nextAfter​(0.5, -1) to Math.pow(x, 0.5) to Math.pow(x, nextAfter​(0.5, 1) for all x. Did anyone look?

Hi @theRealAph ,

I didn't get your point.

Could you please make it more clearer and give us an example?
Thanks.

Loading

@theRealAph
Copy link
Contributor

@theRealAph theRealAph commented Apr 21, 2021

This may be unsafe. AFAIK there is no guarantee about the monotonicity of Math.pow(x, y) as y moves from Math.pow(x, nextAfter​(0.5, -1) to Math.pow(x, 0.5) to Math.pow(x, nextAfter​(0.5, 1) for all x. Did anyone look?

Thanks @theRealAph for your review.

What did you mean by Math.pow(x, nextAfter​(0.5, -1) and Math.pow(x, nextAfter​(0.5, 1) ?

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.

Loading

@DamonFool
Copy link
Member Author

@DamonFool DamonFool commented Apr 21, 2021

This may be unsafe. AFAIK there is no guarantee about the monotonicity of Math.pow(x, y) as y moves from Math.pow(x, nextAfter​(0.5, -1) to Math.pow(x, 0.5) to Math.pow(x, nextAfter​(0.5, 1) for all x. Did anyone look?

Thanks @theRealAph for your review.
What did you mean by Math.pow(x, nextAfter​(0.5, -1) and Math.pow(x, nextAfter​(0.5, 1) ?

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.
And we don't see any problems of it, right?
So I don't think it will cause problems in the future.

What do you think of the same optimization in the HotSpot which has been applied more than 10 years ago?
https://github.com/openjdk/jdk/blob/master/src/hotspot/share/runtime/sharedRuntimeTrans.cpp#L499

And also the same optimization in the Java lib?

return Math.sqrt(x + 0.0); // Add 0.0 to properly handle x == -0.0

Thanks.

Loading

@theRealAph
Copy link
Contributor

@theRealAph theRealAph commented Apr 21, 2021

Actually, this optimization has been long used for Java.
And we don't see any problems of it, right?
So I don't think it will cause problems in the future.

What do you think of the same optimization in the HotSpot which has been applied more than 10 years ago?
https://github.com/openjdk/jdk/blob/master/src/hotspot/share/runtime/sharedRuntimeTrans.cpp#L499

And also the same optimization in the Java lib?

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.

Loading

@DamonFool
Copy link
Member Author

@DamonFool DamonFool commented Apr 21, 2021

/integrate

Loading

@openjdk openjdk bot closed this Apr 21, 2021
@openjdk openjdk bot added integrated and removed ready rfr labels Apr 21, 2021
@openjdk
Copy link

@openjdk openjdk bot commented Apr 21, 2021

@DamonFool Since your change was applied there have been 45 commits pushed to the master branch:

  • 7146104: 8265421: java/lang/String/StringRepeat.java test is missing a memory requirement
  • b5c92ca: 8265106: IGV: Enforce en-US locale while parsing ideal graph
  • 3de0dcb: 8265483: All-caps “JAVA” in the top navigation bar
  • 739769c: 8265411: Avoid unnecessary Method::init_intrinsic_id calls
  • a22ad03: 8264983: Add gtest for JDK-8264008
  • 91b08b7: 8261779: JCK test api/javax_crypto/EncryptedPrivateKeyInfo/Ctor4.html is failing with assertion error when assertions enabled
  • 2fcd920: 8261183: Follow on to Make lists of normal filenames
  • 40ef00c: 8258457: testlibrary_tests/ctw/JarDirTest.java fails with InvalidPathException on windows
  • 3f0da35: 8261392: Exclude testlibrary_tests/ctw/JarDirTest.java
  • 8d0faaf: 8257001: Improve Http Client Support
  • ... and 35 more: https://git.openjdk.java.net/jdk/compare/b64a3fb946a2855c612458aca9d62ef008f8f9be...master

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.

Loading

@DamonFool
Copy link
Member Author

@DamonFool DamonFool commented Apr 21, 2021

Actually, this optimization has been long used for Java.
And we don't see any problems of it, right?
So I don't think it will cause problems in the future.
What do you think of the same optimization in the HotSpot which has been applied more than 10 years ago?
https://github.com/openjdk/jdk/blob/master/src/hotspot/share/runtime/sharedRuntimeTrans.cpp#L499
And also the same optimization in the Java lib?

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.

Thanks @theRealAph .

And thank you all again.
Much has been learned while discussing with you.

Best regards,
Jie

Loading

@DamonFool DamonFool deleted the JDK-8264945 branch Apr 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
5 participants