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

JDK-8153837: AArch64: Handle special cases for MaxINode & MinINode #11570

Closed
wants to merge 6 commits into from

Conversation

dchuyko
Copy link
Member

@dchuyko dchuyko commented Dec 7, 2022

This is a return to an older optimization suggestion for integer Math.min() and Math.max(). See original thread at https://mail.openjdk.org/pipermail/hotspot-compiler-dev/2016-April/022367.html

In a case when one of the arguments is a constant 0, 1 or -1 this constant is currently materialized (like zero is moved to a dedicated register). Instead of that we can produce denoted values from ZR zero register using CSEL, CSINC or CSINV. Thus the register usage and the load instruction are removed.

The implementation adds 3 additional matching rules for min and 3 for max in aarch64.ad file. As constants currently can be in any MinI/MaxI node input, ideal transformation for that nodes is changed to put a constant into the first input. It allows to have a single rule for each value instead of two. First input is not very natural, it is selected because of #3513 optimization that added right-spline transformation for MinI/MaxI. I think it can be symmetrically changed to left-spline but it was not a subject of this PR. Each match rule generates 2 'instruct' intructions following the pattern introduced in fde854e. They are newly added ones with one peculiarity. They try to follow regular naming scheme but lack one of operands that corresponds to a constant that is not actually needed. E.g. 'instruct cmovI_reg_immM1_ge(iRegINoSp dst, iRegI src1, rFlagsReg cr)' that don't have an 'immI_M1' input.

New TestMinMaxIntrinsics jtreg test compares results produced by intrinsics for generic and specialized versions with Java implementation of min and max. Intrinsics are used in lambdas that are compiled with the Whitebox API. The cases include -1, 0, 1 and a couple of regular values. This test can be used to check the generated assembly by adding -XX:+PrintCompilation -XX:+PrintOptoAssembly. Nano-benchmarks where a specialized version is called from a not inlined method also show the changed code with -prof perfasm.

Typical nano-benchmark with a loop and a Blackhole over array shows no difference in performance as the constant is anyway moved out of the loop and usually there are enough registers. However special nano-benchmarks can be considered, e.g.

    @Benchmark
    @OperationsPerInvocation(TESTSIZE)
    public int max0_use8_i() {
        int sum = 0;
        for(int i = 0; i < TESTSIZE; i++) {
            use8(0, 1, 2, 3, 4, 5, 6, 7);
            sum += Math.max(i, 0);
        }
        return sum;
    }
    
    @CompilerControl(CompilerControl.Mode.DONT_INLINE)
    public void use8(int p0, int p1, int p2, int p3, int p4, int p5, int p6, int p7) {
    }

Saving ~1 L1 icache load makes it ~9% faster, and more than a half of the cost is use8() helper.

New version passes new TestMinMaxIntrinsics test on x86 and aarch64 and tier1,2 tests on that platforms (release build).

Zero case is especially interesting. In general, I wonder how it could be possible to allocate ZR as a register for zero constants so the load there could be a no-op and we might get rid of special rules with immI0.


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

  • JDK-8153837: AArch64: Handle special cases for MaxINode & MinINode

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 11570

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

Using diff file

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

@bridgekeeper
Copy link

bridgekeeper bot commented Dec 7, 2022

👋 Welcome back dchuyko! 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 Dec 7, 2022
@openjdk
Copy link

openjdk bot commented Dec 7, 2022

@dchuyko 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 Dec 7, 2022
@mlbridge
Copy link

mlbridge bot commented Dec 7, 2022

Webrevs

@dchuyko dchuyko marked this pull request as draft December 8, 2022 09:16
@openjdk openjdk bot removed the rfr Pull request is ready for review label Dec 8, 2022
@dchuyko
Copy link
Member Author

dchuyko commented Dec 17, 2022

I extended min/max matching to have iRegIorL2I operands. It is a bit controversial that iRegIorL2I can be passed down to instruction that operates iRegI but it works. iRegIorL2I can't be used in effect() which is needed to provide a mask for the instruct. The other solutions would be either to stay with max/min(iRegI, iRegI) only or not to issue split intruct-s.

@dchuyko
Copy link
Member Author

dchuyko commented Dec 19, 2022

/label rfr

@openjdk
Copy link

openjdk bot commented Dec 19, 2022

@dchuyko
The label rfr is not a valid label.
These labels are valid:

  • serviceability
  • hotspot
  • hotspot-compiler
  • ide-support
  • kulla
  • i18n
  • shenandoah
  • jdk
  • javadoc
  • security
  • hotspot-runtime
  • jmx
  • build
  • nio
  • client
  • core-libs
  • compiler
  • net
  • hotspot-gc
  • hotspot-jfr

@dchuyko dchuyko marked this pull request as ready for review December 19, 2022 13:13
@openjdk openjdk bot added the rfr Pull request is ready for review label Dec 19, 2022
src/hotspot/cpu/aarch64/aarch64.ad Outdated Show resolved Hide resolved
Copy link
Contributor

@theRealAph theRealAph left a comment

Choose a reason for hiding this comment

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

Yes, that's very thorough.

@openjdk
Copy link

openjdk bot commented Jan 18, 2023

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

8153837: AArch64: Handle special cases for MaxINode & MinINode

Reviewed-by: fyang, aph

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

  • 15a9186: 8300169: Build failure with clang-15
  • d918042: 8300267: Remove duplicated setter/getter for do_not_unlock

Please see this link for an up-to-date comparison between the source branch of this pull request and 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.

➡️ 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 Jan 18, 2023
@dchuyko
Copy link
Member Author

dchuyko commented Jan 18, 2023

/integrate

@openjdk
Copy link

openjdk bot commented Jan 18, 2023

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

  • 754f6e6: 8300237: Minor improvements in MethodHandles
  • 85d8bac: 8300166: Unused array allocation in ProcessPath.doProcessPath
  • c464ef1: 8292741: Convert JvmtiTagMapTable to ResourceHashtable
  • 1aded82: 8300488: Incorrect usage of CATCH_BAD_ALLOC as a macro call
  • bd5ca95: 8300222: Replace NULL with nullptr in share/logging
  • 15a9186: 8300169: Build failure with clang-15
  • d918042: 8300267: Remove duplicated setter/getter for do_not_unlock

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Jan 18, 2023

@dchuyko Pushed as commit b3684f4.

💡 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
5 participants