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

8303278: Imprecise bottom type of ExtractB/UB #13070

Closed
wants to merge 2 commits into from

Conversation

e1iu
Copy link
Member

@e1iu e1iu commented Mar 17, 2023

This is a trivial patch, which fixes the bottom type of ExtractB/UB nodes.

ExtractNode can be generated by Vector API Vector.lane(int), which gets the lane element at the given index. A more precise type of range can help to optimize out unnecessary type conversion in some cases.

Below shows a typical case used ExtractBNode

  public static byte byteLt16() {
   ByteVector vecb = ByteVector.broadcast(ByteVector.SPECIES_128, 1);
   return vecb.lane(1);
  }

In this case, c2 constructs IR graph like:

ExtractB  ConI(24)
   |     __|
   |    /  |
LShiftI  __|
   |    /
RShiftI

which generates AArch64 code:

movi v16.16b, #0x1
smov x11, v16.b[1]
sxtb w0, w11

with this patch, this shift pair can be optimized out by RShiftI's identity [1]. The code is optimized to:

movi v16.16b, #0x1
smov x0, v16.b[1]

[TEST]

Full jtreg passed except 4 files on x86:

jdk/incubator/vector/Byte128VectorTests.java
jdk/incubator/vector/Byte256VectorTests.java
jdk/incubator/vector/Byte512VectorTests.java
jdk/incubator/vector/Byte64VectorTests.java

They are caused by a known issue on x86 [2].

[1]

if (lo <= t11->_lo && t11->_hi <= hi) {

[2] https://bugs.openjdk.org/browse/JDK-8303508


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/13070/head:pull/13070
$ git checkout pull/13070

Update a local copy of the PR:
$ git checkout pull/13070
$ git pull https://git.openjdk.org/jdk.git pull/13070/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 13070

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/13070.diff

Webrev

Link to Webrev Comment

This is a trivial patch, which fixes the bottom type of ExtractB/UB
nodes.

ExtractNode can be generated by Vector API Vector.lane(int), which gets
the lane element at the given index. A more precise type of range can
help to optimize out unnecessary type conversion in some cases.

Below shows a typical case used ExtractBNode

```
  public static byte byteLt16() {
   ByteVector vecb = ByteVector.broadcast(ByteVector.SPECIES_128, 1);
   return vecb.lane(1);
  }

```
In this case, c2 constructs IR graph like:

    ExtractB  ConI(24)
       |     __|
       |    /  |
    LShiftI  __|
       |    /
    RShiftI

which generates AArch64 code:

  movi    v16.16b, #0x1
  smov    x11, v16.b[1]
  sxtb    w0, w11

with this patch, this shift pair can be optimized out by RShiftI's
identity [1]. The code is optimized to:

  movi    v16.16b, #0x1
  smov    x0, v16.b[1]

[TEST]

Full jtreg passed except 4 files on x86:

jdk/incubator/vector/Byte128VectorTests.java
jdk/incubator/vector/Byte256VectorTests.java
jdk/incubator/vector/Byte512VectorTests.java
jdk/incubator/vector/Byte64VectorTests.java

They are caused by a known issue on x86 [2].

[1] https://github.com/openjdk/jdk/blob/742bc041eaba1ff9beb7f5b6d896e4f382b030ea/src/hotspot/share/opto/mulnode.cpp#L1052
[2] https://bugs.openjdk.org/browse/JDK-8303508

Change-Id: Ibea9aeacb41b4d1c5b2621c7a97494429394b599
@bridgekeeper
Copy link

bridgekeeper bot commented Mar 17, 2023

👋 Welcome back eliu! 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.

@openjdk openjdk bot added the rfr Pull request is ready for review label Mar 17, 2023
@openjdk
Copy link

openjdk bot commented Mar 17, 2023

@theRealELiu The following label will be automatically applied to this pull request:

  • hotspot-compiler

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the hotspot-compiler hotspot-compiler-dev@openjdk.org label Mar 17, 2023
@mlbridge
Copy link

mlbridge bot commented Mar 17, 2023

Webrevs

Copy link
Member

@TobiHartmann TobiHartmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me too.

Copy link
Member

@TobiHartmann TobiHartmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This triggers failures in testing:

jdk/incubator/vector/Byte64VectorTests.java

java.lang.Exception: failures: 1
	at com.sun.javatest.regtest.agent.TestNGRunner.main(TestNGRunner.java:95)
	at com.sun.javatest.regtest.agent.TestNGRunner.main(TestNGRunner.java:53)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
	at java.base/java.lang.reflect.Method.invoke(Method.java:578)
	at com.sun.javatest.regtest.agent.MainWrapper$MainThread.run(MainWrapper.java:125)
	at java.base/java.lang.Thread.run(Thread.java:1623)

@e1iu
Copy link
Member Author

e1iu commented Mar 21, 2023

This triggers failures in testing:

jdk/incubator/vector/Byte64VectorTests.java

java.lang.Exception: failures: 1
	at com.sun.javatest.regtest.agent.TestNGRunner.main(TestNGRunner.java:95)
	at com.sun.javatest.regtest.agent.TestNGRunner.main(TestNGRunner.java:53)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
	at java.base/java.lang.reflect.Method.invoke(Method.java:578)
	at com.sun.javatest.regtest.agent.MainWrapper$MainThread.run(MainWrapper.java:125)
	at java.base/java.lang.Thread.run(Thread.java:1623)

I think this should be caused by https://bugs.openjdk.org/browse/JDK-8303508. The removed narrowing exposed this issue on x86, that the generated code of ExtractB perhaps does not handle sign-bit correctly. TBH I'm not familiar with x86 instructions... @jatin-bhateja Could you help to take a look at this bug?

@merykitty
Copy link
Member

Yes x86 does not handle signed extension correctly. pextrb and pextrw zeroes the upper bits instead of signed extending them. A simple fix is to add movsx after those.

void C2_MacroAssembler::extract(BasicType bt, Register dst, XMMRegister src, int idx) {

@jatin-bhateja
Copy link
Member

jatin-bhateja commented Mar 22, 2023

Yes x86 does not handle signed extension correctly. pextrb and pextrw zeroes the upper bits instead of signed extending them. A simple fix is to add movsx after those.

void C2_MacroAssembler::extract(BasicType bt, Register dst, XMMRegister src, int idx) {

image

  • As can be seen above C2 creates ExtractS node with TypeInt::SHORT which is then succeeded by ConvI2L node since expander returns a long value.
  • Return value is again down casted back short value though an explicit cast operation in java code.

image

Currently C2 folds following IR sequence

ExtractS  -> ConvI2L -> ConvL2I ->  LShift (16) -> RShift(16)                   ==>  ExtractS -> return_value
    [Inline expander]    [ Java side down cast long to short -> l2i + i2s]  

Because ideal type of ExtractS is TypeInt::SHORT hence entire chain of conversion operations are folded by compiler during idealizations.

There are two ways to address this issue:-

  1. We can set the ideal type of ExtractS node to TypeInt::INT as is done for ExtractB/UB since subsequent java code will handle down casting to generate correct result. This will also fix JDK-8303508.
  2. Enforce strict semantics of ExtractS/B IR node and thus backends should emit an explicit sign extension instruction movsx for sub-word types.

I agree with @merykitty that 2) solution looks more robust since it will enforce accurate semantics of ExtractS IR.

Change-Id: I40cce803da09bae31cd74b86bf93607a08219545
@e1iu e1iu requested a review from TobiHartmann March 29, 2023 02:41
@e1iu
Copy link
Member Author

e1iu commented Mar 30, 2023

@TobiHartmann Could you help to take a look?

Copy link
Member

@TobiHartmann TobiHartmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll re-run testing.

Copy link
Member

@TobiHartmann TobiHartmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. All tests passed.

@openjdk
Copy link

openjdk bot commented Apr 3, 2023

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

8303278: Imprecise bottom type of ExtractB/UB

Reviewed-by: qamai, thartmann

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 112 new commits pushed to the master branch:

  • b062b1b: 8304743: Compile_lock and SystemDictionary updates
  • df819cf: 8304945: StringBuilder and StringBuffer should implement Appendable explicitly
  • 312bbe7: 8305485: Problemlist runtime/Thread/TestAlwaysPreTouchStacks.java
  • 50e31e0: 8305442: (bf) Direct and view implementations of CharBuffer.toString(int, int) do not need to catch SIOBE
  • 85e3974: 8304014: Convert test/jdk/java/util/zip/ZipFile/CorruptedZipFiles.java to junit
  • 40aea04: 8278268: (ch) InputStream returned by Channels.newInputStream should have fast path for FileChannel targets
  • 9b9b5a7: 8302323: Add repeat methods to StringBuilder/StringBuffer
  • dd7ca75: 8305478: [REDO] disable gtest/NMTGtests.java sub-tests failing due to JDK-8305414
  • f9827ad: 8288109: HttpExchangeImpl.setAttribute does not allow null value after JDK-8266897
  • 6010de0: 8305417: disable gtest/NMTGtests.java sub-tests failing due to JDK-8305414
  • ... and 102 more: https://git.openjdk.org/jdk/compare/38e17148faef7799515478bd834ed2fa1a5153de...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.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Apr 3, 2023
@e1iu
Copy link
Member Author

e1iu commented Apr 4, 2023

/integrate

@openjdk
Copy link

openjdk bot commented Apr 4, 2023

Going to push as commit ac898e9.
Since your change was applied there have been 116 commits pushed to the master branch:

  • a1a9ec6: 8297286: runtime/vthread tests crashing after JDK-8296324
  • c6bd489: 8305206: Add @SPEC tags in java.base/java.* (part 1)
  • ccbb0e8: 8303798: REDO - Remove fdlibm C sources
  • 9ce5fdc: 8305421: Work around JDK-8305420 in CDSJDITest.java
  • b062b1b: 8304743: Compile_lock and SystemDictionary updates
  • df819cf: 8304945: StringBuilder and StringBuffer should implement Appendable explicitly
  • 312bbe7: 8305485: Problemlist runtime/Thread/TestAlwaysPreTouchStacks.java
  • 50e31e0: 8305442: (bf) Direct and view implementations of CharBuffer.toString(int, int) do not need to catch SIOBE
  • 85e3974: 8304014: Convert test/jdk/java/util/zip/ZipFile/CorruptedZipFiles.java to junit
  • 40aea04: 8278268: (ch) InputStream returned by Channels.newInputStream should have fast path for FileChannel targets
  • ... and 106 more: https://git.openjdk.org/jdk/compare/38e17148faef7799515478bd834ed2fa1a5153de...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Apr 4, 2023
@openjdk openjdk bot closed this Apr 4, 2023
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Apr 4, 2023
@openjdk
Copy link

openjdk bot commented Apr 4, 2023

@theRealELiu Pushed as commit ac898e9.

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

@e1iu e1iu deleted the ENTLLT-5267-external branch July 10, 2023 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot-compiler hotspot-compiler-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

4 participants