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

8283094: Add Ideal transformation: x + (con - y) -> (x - y) + con #7795

Closed
wants to merge 7 commits into from

Conversation

CptGit
Copy link
Contributor

@CptGit CptGit commented Mar 11, 2022

Hello,

x + (con - y) -> (x - y) + con is a widely seen pattern; however it is missing in current implementation, which prevents some obvious constant folding from happening, such as x + (1 - y) + 2 will be not optimized at all, rather than into x - y + 3.

This pull request adds this transformation.


Progress

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

Issue

  • JDK-8283094: Add Ideal transformation: x + (con - y) -> (x - y) + con

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 7795

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

Using diff file

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

@bridgekeeper
Copy link

bridgekeeper bot commented Mar 11, 2022

👋 Welcome back CptGit! 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
Copy link

openjdk bot commented Mar 11, 2022

@CptGit this pull request can not be integrated into master due to one or more merge conflicts. To resolve these merge conflicts and update this pull request you can run the following commands in the local repository for your personal fork:

git checkout addnode-PNewXPlus_ConMinusY_
git fetch https://git.openjdk.java.net/jdk master
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge master"
git push

@openjdk openjdk bot added the merge-conflict Pull request has merge conflict with target branch label Mar 11, 2022
@openjdk
Copy link

openjdk bot commented Mar 11, 2022

@CptGit 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 11, 2022
@openjdk openjdk bot removed the merge-conflict Pull request has merge conflict with target branch label Mar 11, 2022
@merykitty
Copy link
Member

I think I have a better idea for this. During idealisation, we will transform every subtraction into an addition with the negation of the second operand, pushing the negation down the computation graph. This will simplify numerous rules we have with subtractions. Addition presents nice commutative and associative properties so the graph transformation during idealisation with be much easier. Before matching, we may have another strength reduction phase which will push the negations up the computation graph, transform the computations to be as compact as possible.

This problem seems a lot like the problem we face in #7700 when eagerly strength reducing operations hurt our ability to perform other transformations by complicating the elements inside the graph. I believe this solution would simplify our arithmetic transformations and allow better numeric calculations. Similar to what has been discussed in #7395 , a more general solution would be much more valuable than trying to match every possible combinations.

Thank you very much.

@TheShermanTanker
Copy link
Contributor

TheShermanTanker commented Mar 12, 2022

I share the same opinion that John has from the linked #7395, to me it would be better if we would combine all these operations into a single method that can then decide on the specific optimization after analysis. This would also have the added bonus that related optimizations in the future could then leverage this framework and have an easier time implementing what they'd like to do. This may or may not require changes to C1 and the Interpreter to gather all the extra profiling information needed, I can't be the judge of that given my relative inexperience with C2.

The only thing I'm unsure about is whether all the previous optimizations before this are all scattered across C2 or whether they all share variations of the same calculation, because we could take advantage of existing code if it's the latter. @merykitty would it be feasible to search for all of them in the C2 codebase?

Should I create a JBS issue for this PR?

@CptGit
Copy link
Contributor Author

CptGit commented Mar 13, 2022

@merykitty Thank you for your quick response. I agree a general solution is better and I also think addition is generally easier to optimize than subtraction. However considering this case, I noticed there are some existing transformation that does not follow this: for example

// Convert "x+(0-y)" into "(x-y)"
, which seems doing exactly the opposite thing.

Does the transformation hold all the time?

During idealisation, we will transform every subtraction into an addition with the negation of the second operand, pushing the negation down the computation graph.

@merykitty
Copy link
Member

That is what I mean by saying that careless strength reduction restricts our ability to perform more efficient transformations. What I propose is we delay your mentioned transformation after idealisation has settled on the whole graph.
Thanks.

@CptGit CptGit changed the title Add Ideal transformation: x + (con - y) -> (x - y) + con 8283094: Add Ideal transformation: x + (con - y) -> (x - y) + con Mar 14, 2022
@openjdk openjdk bot added the rfr Pull request is ready for review label Mar 14, 2022
@mlbridge
Copy link

mlbridge bot commented Mar 14, 2022

Webrevs

@CptGit
Copy link
Contributor Author

CptGit commented Mar 20, 2022

That is what I mean by saying that careless strength reduction restricts our ability to perform more efficient transformations. What I propose is we delay your mentioned transformation after idealisation has settled on the whole graph. Thanks.

Thank you. I was wondering if you mean other stage, for example Identity (afaik Identity is invoked after Ideal), by saying "after idealisation has settled on the whole graph"? I am not sure where I should move transformation.

@rwestrel
Copy link
Contributor

I think I have a better idea for this. During idealisation, we will transform every subtraction into an addition with the negation of the second operand, pushing the negation down the computation graph.

One issue with such an approach is that it's then required to update all code that pattern match an expression with SubX node to now expect an (AddX (NegX ..) ..). PhaseIdealLoop::is_scaled_iv_plus_offset() for instance. And maybe there are not that many instances of code that require updating, but it's still needed to proceed carefully and double check, possibly update some code, write tests that verify the change does what's intended etc. So what appears relatively simple is going to be more work than expected and the risk associated with the change is higher than one might think. I doubt that's worth it.

@merykitty
Copy link
Member

@rwestrel Thanks a lot for your information, it seems that the idea would turn out to be problematic with little applicable benefits then.

@@ -43,7 +43,9 @@ public static void main(String[] args) {
"test8", "test9", "test10",
"test11", "test12", "test13",
"test14", "test15", "test16",
"test17", "test18", "test19"})
"test17", "test18", "test19",
"testXPlus_PosConMinusY_", "testXPlus_NegConMinusY_",
Copy link
Member

Choose a reason for hiding this comment

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

Please make the name uniform and put them in correct order, thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed, does it look good to you now? Thanks.

Copy link
Contributor

@vnkozlov vnkozlov left a comment

Choose a reason for hiding this comment

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

Good.

@openjdk
Copy link

openjdk bot commented Mar 29, 2022

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

8283094: Add Ideal transformation: x + (con - y) -> (x - y) + con

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

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 (@vnkozlov, @TobiHartmann) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Mar 29, 2022
@CptGit
Copy link
Contributor Author

CptGit commented Mar 30, 2022

/reviewers 2

@openjdk
Copy link

openjdk bot commented Mar 30, 2022

@CptGit
The total number of required reviews for this PR (including the jcheck configuration and the last /reviewers command) is now set to 2 (with 1 of role reviewers, 1 of role authors).

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Mar 30, 2022
@CptGit
Copy link
Contributor Author

CptGit commented Apr 2, 2022

Hello, can any reviewer review this if they get a chance? Thank you very much.

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. I'll run some testing and report back once it finished.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Apr 11, 2022
@TobiHartmann
Copy link
Member

All tests passed.

@CptGit
Copy link
Contributor Author

CptGit commented Apr 12, 2022

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Apr 12, 2022
@openjdk
Copy link

openjdk bot commented Apr 12, 2022

@CptGit
Your change (at version 00b7992) is now ready to be sponsored by a Committer.

@CptGit
Copy link
Contributor Author

CptGit commented Apr 12, 2022

@TobiHartmann Can you please sponsor the PR? thank you.

@vnkozlov
Copy link
Contributor

/sponsor

@openjdk
Copy link

openjdk bot commented Apr 13, 2022

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

  • e245f9d: 8284274: Error reporting crashes because missing ResourceMarks
  • 8ee2944: 8284754: print more interesting env variables in hs_err and VM.info
  • 70251b0: 8194946: Regression automated Test 'javax/swing/JFileChooser/6738668/bug6738668.java' fails
  • 507dc41: 8284720: IntelliJ: JIRA integration
  • 280aa42: 8284579: Improve VarHandle checks for interpreter
  • c355902: 8282528: AArch64: Incorrect replicate2L_zero rule
  • 5691a3b: 8284702: Add @SInCE for java.time.LocalDate.EPOCH
  • 7920e8b: 8283714: REDO - Unexpected TypeElement in ANALYZE TaskEvent
  • bc12e86: 8284752: Zero does not build on Mac OS X due to missing os::current_thread_enable_wx implementation
  • cafde7f: 8284732: FFI_GO_CLOSURES macro not defined but required for zero build on Mac OS X
  • ... and 213 more: https://git.openjdk.java.net/jdk/compare/f4fd53d0aee67319bf2c7bcaa671c2e97e66383f...master

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Apr 13, 2022

@vnkozlov @CptGit Pushed as commit c7755b8.

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

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.

6 participants