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

8266028: C2 computes -0.0 for Math.pow(-0.0, 0.5) #3712

Closed
wants to merge 1 commit into from

Conversation

@DamonFool
Copy link
Member

@DamonFool DamonFool commented Apr 27, 2021

Hi all,

C2 computes -0.0 for Math.pow(-0.0, 0.5) which should be 0.0 according to the API specs.
This bug was found while I was implementing the same optimization for x86_32 (JDK-8265940).

The reason is that CmpDNode [1] doesn't distinguish -0.0 from 0.0.
Math.pow(-0.0, 0.5) was replaced with Math.sqrt(-0.0) incorrectly since CmpDNode says -0.0 < 0.0 if false.

The fix excludes C2's optimization for x=0.0/-0.0.
And the jtreg test has been improved to make sure the corner cases are covered by the compilers.

Thanks.
Best regards,
Jie

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


Progress

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

Issue

  • JDK-8266028: C2 computes -0.0 for Math.pow(-0.0, 0.5)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/3712/head:pull/3712
$ git checkout pull/3712

Update a local copy of the PR:
$ git checkout pull/3712
$ git pull https://git.openjdk.java.net/jdk pull/3712/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 3712

View PR using the GUI difftool:
$ git pr show -t 3712

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/3712.diff

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented Apr 27, 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

@DamonFool
Copy link
Member Author

@DamonFool DamonFool commented Apr 27, 2021

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

Loading

@openjdk
Copy link

@openjdk openjdk bot commented Apr 27, 2021

@DamonFool
The hotspot-compiler label was successfully added.

Loading

@openjdk
Copy link

@openjdk openjdk bot commented Apr 27, 2021

@DamonFool The hotspot-compiler label was already applied.

Loading

@mlbridge
Copy link

@mlbridge mlbridge bot commented Apr 27, 2021

Webrevs

Loading

@openjdk
Copy link

@openjdk openjdk bot commented Apr 27, 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:

8266028: C2 computes -0.0 for Math.pow(-0.0, 0.5)

Reviewed-by: aph, 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 19 new commits pushed to the master branch:

  • eb72950: 8266154: mark hotspot compiler/oracle tests which ignore VM flags
  • 7f4a9f6: 8266088: compiler/arguments/TestPrintOptoAssemblyLineNumbers test should user driver mode
  • f560b89: 8264873: Dependencies: Split ClassHierarchyWalker
  • 0a88f0a: 8255915: jdk/incubator/vector/AddTest.java timed out
  • ab2aec2: 8265938: C2's conditional move optimization does not handle top Phi
  • 5634f20: 8265932: Move safepoint related fields from class Thread to JavaThread
  • b67b2b1: 8265690: Use the latest Ubuntu base image version in Docker testing
  • b2628d1: 8263972: C2: LoadVector/StoreVector type mismatch in MemNode::can_see_stored_value()
  • 377b346: 8264752: SIGFPE crash with option FlightRecorderOptions:threadbuffersize=30M
  • dc323a9: 8263421: Module image file is opened twice during VM startup
  • ... and 9 more: https://git.openjdk.java.net/jdk/compare/4785e112ae1246e80ed2223691f991736da70117...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 27, 2021
@DamonFool
Copy link
Member Author

@DamonFool DamonFool commented Apr 27, 2021

Thanks @theRealAph for your review.

Loading

Copy link
Contributor

@neliasso neliasso left a comment

Looks good.

Loading

@DamonFool
Copy link
Member Author

@DamonFool DamonFool commented Apr 27, 2021

Looks good.

Thanks @neliasso .

Loading

@vnkozlov
Copy link
Contributor

@vnkozlov vnkozlov commented Apr 27, 2021

I am fine with your fix - it is rare case so we can skip 0 case. But we need to verify that Interpreter and stubs (and Java) give the same result for -0.0 in 32- and 64-bit VMs.

C code uses if(hx>=0) so what result will it give for -0.0? :
https://github.com/openjdk/jdk/blob/master/src/hotspot/share/runtime/sharedRuntimeTrans.cpp#L497

64-bit stub code use Integer compare which should be fine I think:
https://github.com/openjdk/jdk/blob/master/src/hotspot/cpu/x86/macroAssembler_x86_pow.cpp#L839

Java code adds +0.0 to get correct result (would be nice to see what code C2 generates when it see such bytecode):
https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/FdLibm.java#L364

Loading

@DamonFool
Copy link
Member Author

@DamonFool DamonFool commented Apr 27, 2021

I am fine with your fix - it is rare case so we can skip 0 case. But we need to verify that Interpreter and stubs (and Java) give the same result for -0.0 in 32- and 64-bit VMs.

C code uses if(hx>=0) so what result will it give for -0.0? :
https://github.com/openjdk/jdk/blob/master/src/hotspot/share/runtime/sharedRuntimeTrans.cpp#L497

64-bit stub code use Integer compare which should be fine I think:
https://github.com/openjdk/jdk/blob/master/src/hotspot/cpu/x86/macroAssembler_x86_pow.cpp#L839

Java code adds +0.0 to get correct result (would be nice to see what code C2 generates when it see such bytecode):
https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/FdLibm.java#L364

Hi @vnkozlov ,

Thanks for your review.

The C code actually uses integer compare.
So I think it's fine otherwise non-x86 platforms would report errors.

As for the Java code, C2 will emit a vaddsd instruction.

Java method

  public static double test(double d) {
    return d + 0.0;
  }

Generated code by C2

000     B1: #   out( N1 ) <- BLOCK HEAD IS JUNK  Freq: 1
000     # stack bang (96 bytes)
        pushq   rbp     # Save rbp
        subq    rsp, #16        # Create frame

00c     # MachConstantBaseNode (empty encoding)
00c     vaddsd  XMM0, XMM0, [constant table base + #0]  # load from constant table: double=#0.000000
014     addq    rsp, 16 # Destroy frame
        popq    rbp
        cmpq     rsp, poll_offset[r15_thread]
        ja       #safepoint_stub        # Safepoint: poll for GC

026     ret

Loading

Copy link
Contributor

@vnkozlov vnkozlov left a comment

Good. Thank you for checking other places.

Loading

@DamonFool
Copy link
Member Author

@DamonFool DamonFool commented Apr 28, 2021

/integrate

Loading

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

@openjdk openjdk bot commented Apr 28, 2021

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

  • ca37be1: 8197800: Test java/awt/Focus/NonFocusableWindowTest/NoEventsTest.java fails on Windows
  • cf92693: 8198619: java/awt/Focus/FocusTraversalPolicy/ButtonGroupLayoutTraversal/ButtonGroupLayoutTraversalTest.java fails on mac
  • 2201e11: 8266055: ZGC: ZHeap::print_extended_on() doesn't disable deferred delete
  • ce48f04: 8198617: java/awt/Focus/6382144/EndlessLoopTest.java fails on mac
  • 0438cea: 8136517: [macosx]Test java/awt/Focus/8073453/AWTFocusTransitionTest.java fails on MacOSX
  • 6b5e5d7: 8266181: compiler/eliminateAutobox/TestEliminateBoxInDebugInfo should be in driver mode
  • 81b1502: 8266161: mark hotspot compiler/rtm tests which ignore VM flags
  • 4a8324d: 8266180: compiler/vectorapi/TestVectorErgonomics should be run in driver mode
  • 0601aba: 8266175: mark hotspot compiler/jsr292 tests which ignore VM flags
  • 69ad8ac: 8266169: mark hotspot compiler/jvmci tests which ignore VM flags
  • ... and 27 more: https://git.openjdk.java.net/jdk/compare/4785e112ae1246e80ed2223691f991736da70117...master

Your commit was automatically rebased without conflicts.

Pushed as commit 75a2354.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
4 participants