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
8276162: Optimise unsigned comparison pattern #6101
Conversation
Hi @merykitty, welcome to this OpenJDK project and thanks for contributing! We do not recognize you as Contributor and need to ensure you have signed the Oracle Contributor Agreement (OCA). If you have not signed the OCA, please follow the instructions. Please fill in your GitHub username in the "Username" field of the application. Once you have signed the OCA, please let us know by writing If you already are an OpenJDK Author, Committer or Reviewer, please click here to open a new issue so that we can record that fact. Please use "Add GitHub user MeryKitty" as summary for the issue. If you are contributing this work on behalf of your employer and your employer has signed the OCA, please let us know by writing |
/signed |
Thank you! Please allow for up to two weeks to process your OCA, although it is usually done within one to two business days. Also, please note that pull requests that are pending an OCA check will not usually be evaluated, so your patience is appreciated! |
@merykitty The following label will be automatically applied to this pull request:
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. |
It would be better if you can provide a micro benchmark to show us the performance improvement. |
I created a simple benchmark, the benchmark is run on Intel i7-7700HQ, the result is as follow:
This is the source code of the benchmark:
Do I need to add the benchmark to the patch? If yes then where should I put it in? Thank you very much. |
I think you can put it under |
I have just pushed the microbenchmark, I am not sure what to put in the copyright line, though. The check failure seems to be due to this PR does not refer to an existing issue. Thank you very much. |
Filed https://bugs.openjdk.java.net/browse/JDK-8276162 for you. |
Webrevs
|
Hi, may someone take a look at this PR, please. |
src/hotspot/share/opto/subnode.cpp
Outdated
const int cmp1_op = cmp1->Opcode(); | ||
const int cmp2_op = cmp2->Opcode(); | ||
|
||
// Change x +- Integer.MIN_VALUE <=> y +- Integer.MIN_VALUE into x u<=> y |
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.
The comment should include cmp2_op == Op_ConI
case.
Also it is not clear from comment and code if different operations are allowed on both side: x - MIN_INT <= y + MIN_INT
src/hotspot/share/opto/subnode.cpp
Outdated
(cmp1_op == Op_AddI || cmp1_op == Op_SubI) && | ||
phase->type(cmp1->in(2)) == TypeInt::MIN) { | ||
if (cmp2_op == Op_ConI) { | ||
Node *ncmp2 = phase->intcon(java_add(cmp2->get_int(), min_jint)); |
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.
What if cmp1_op == Op_SubI
?
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.
Since x + MIN_VALUE == x - MIN_VALUE
, the operation is the same with addition and subtraction operations. However, I realised that x - MIN_VALUE
is idealised into x + MIN_VALUE
beforehand, so I have removed the check for Op_SubI
here.
src/hotspot/share/opto/subnode.cpp
Outdated
Node *ncmp2 = phase->intcon(java_add(cmp2->get_int(), min_jint)); | ||
Node *ncmp = phase->transform(new CmpUNode(cmp1->in(1), ncmp2)); | ||
return new BoolNode(ncmp, _test._test); | ||
} else if ((cmp2_op == Op_AddI || cmp2_op == Op_SubI) && |
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.
Again the question about mismatching cmp1_op and cmp2_op.
Given that there is Integer/Long.compareUnsigned
using this idiom, it seems reasonable to optimize. Some general comments:
- Your benchmark does not cover all the cases you are optimizing. Maybe you should also add the
Integer.compareUnsigned
variants. - You need a correctness test as well, ideally using the IR verification framework to also verify that the optimizations are actually performed.
src/hotspot/share/opto/subnode.cpp
Outdated
@@ -1529,10 +1531,43 @@ Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) { | |||
} | |||
} | |||
|
|||
// Change x + Integer.MIN_VALUE <=> y + Integer.MIN_VALUE into x u<=> y |
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.
The <=>
in the comment is confusing because it usually denotes logical equality. Also, you are only handling <
and >
below. What about the other variants? Shouldn't they be canonicalized in idealize_test
(see ifnode.cpp
)?
I would recommend making it explicit in the comment and use brackets for readability:
// Change (x + Integer.MIN_VALUE < y + Integer.MIN_VALUE) into (x u< y) and
// (x + Integer.MIN_VALUE > y + Integer.MIN_VALUE) into (x u> y).
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.
I have added eq
and ne
to the transformation, leaving the comment simply as cmp (add X min_jint) (add Y min_jint)
.
src/hotspot/share/opto/subnode.cpp
Outdated
return new BoolNode(ncmp, _test._test); | ||
} else if (cmp2_op == Op_AddI && | ||
phase->type(cmp2->in(2)) == TypeInt::MIN) { | ||
Node *ncmp = phase->transform(new CmpUNode(cmp1->in(1), cmp2->in(1))); |
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.
Node *ncmp
-> Node* ncmp
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.
Done, sir.
Thank you for addressing my comments.
I am tentatively approve these changes leaving final approval and testing to @TobiHartmann
|
@merykitty This change now passes all automated pre-integration checks. 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 297 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. As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@TobiHartmann, @vnkozlov) but any other Committer may sponsor as well.
|
@TobiHartmann Thank you very much for the review, I have added the correctness test as well as revised the microbenchmark to cover all the situations including calling to
|
Nice IR verification test. Your changes look good to me and all testing passed.
/integrate |
@merykitty |
Thank @vnkozlov and @TobiHartmann for your reviews and suggestions, thank @DamonFool for your initial supports. Thank you very much. |
/sponsor |
Going to push as commit f3eb501.
Your commit was automatically rebased without conflicts. |
@TobiHartmann @merykitty Pushed as commit f3eb501. |
This introduced a regression on 32-bit x86, @merykitty could you please have a look? |
The problem is missing match rules on x86-32: #6427 |
This introduced a regression on 32-bit x86 for Vector API tests, could someone help to review it? |
This patch changes operations in the form
x +- Integer.MIN_VALUE <=> y +- Integer.MIN_VALUE
, which is a pattern used to do unsigned comparisons, intox u<=> y
.In addition to being basic operations, they may be utilised to implement range checks such as the methods in
jdk.internal.util.Preconditions
, or in places where the compiler cannot deduce the non-negativeness of the bound as injava.util.ArrayList
.Thank you very much.
Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/6101/head:pull/6101
$ git checkout pull/6101
Update a local copy of the PR:
$ git checkout pull/6101
$ git pull https://git.openjdk.java.net/jdk pull/6101/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 6101
View PR using the GUI difftool:
$ git pr show -t 6101
Using diff file
Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/6101.diff