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

8286197: C2: Optimize MemorySegment shape in int loop #8555

Closed
wants to merge 7 commits into from

Conversation

rwestrel
Copy link
Contributor

@rwestrel rwestrel commented May 5, 2022

This is another small enhancement for a code shape that showed up in a
MemorySegment micro benchmark. The shape to optimize is the one from test1:

for (int i = 0; i < size; i++) {
    long j = i * UNSAFE.ARRAY_INT_INDEX_SCALE;

    j = Objects.checkIndex(j, size * 4);

    if (((base + j) & 3) != 0) {
        throw new RuntimeException();
    }

    v += UNSAFE.getInt(base + j);
}

In that code shape, the loop iv is first scaled, result is then casted
to long, range checked and finally address of memory location is
computed.

The alignment check is transformed so the loop body has no check In
order to eliminate the range check, that loop is transformed into:

for (int i1 = ..) {
    for (int i2 = ..) {
        long j = (i1 + i2) * UNSAFE.ARRAY_INT_INDEX_SCALE;

        j = Objects.checkIndex(j, size * 4);

        v += UNSAFE.getInt(base + j);
    }
}

The address shape is (AddP base (CastLL (ConvI2L (LShiftI (AddI ...

In this case, the type of the ConvI2L is [min_jint, max_jint] and type
of CastLL is [0, max_jint] (the CastLL has a narrower type).

I propose transforming (CastLL (ConvI2L into (ConvI2L (CastII in that
case. The convI2L and CastII types can be set to [0, max_jint]. The
new address shape is then:

(AddP base (ConvI2L (CastII (LShiftI (AddI ...

which optimize well.

(LShiftI (AddI ...
is transformed into
(AddI (LShiftI ...
because one of the AddI input is loop invariant (i2) and we have:

(AddP base (ConvI2L (CastII (AddI (LShiftI ...

Then because the ConvI2L and CastII types are [0, max_jint], the AddI
is pushed through the ConvI2L and CastII:

(AddP base (AddL (ConvI2L (CastII (LShiftI ...

base and one of the inputs of the AddL are loop invariant so this
transformed into:

(AddP (AddP ...) (ConvI2L (CastII (LShiftI ...

The (AddP ...) is loop invariant so computed before entry. The
(ConvI2L ...) only depends on the loop iv.

The resulting address is a shift + an add. The address before
transformation requires 2 adds + a shift. Also after unrolling, the
adress of the second access in the loop is cheaper to compute as it
can be derived from the address of the first access.

For all of this to work:

  1. I added a CastLL::Ideal transformation:
    (CastLL (ConvI2L into (ConvI2l (CastII

  2. I also had to prevent split if to transform (LShiftI (Phi for the
    iv Phi of a counted loop.

test2 and test3 test 1) and 2) separately.


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-8286197: C2: Optimize MemorySegment shape in int loop

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 8555

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

Using diff file

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

@bridgekeeper
Copy link

bridgekeeper bot commented May 5, 2022

👋 Welcome back roland! 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 May 5, 2022

@rwestrel 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 May 5, 2022
@openjdk openjdk bot added the rfr Pull request is ready for review label May 5, 2022
@mlbridge
Copy link

mlbridge bot commented May 5, 2022

Webrevs

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 suggestion. I have comments.

Comment on lines 1079 to 1084
// Check for having no control input; not pinned. Allow
// Check for having no control input; not pinned. Allow
Copy link
Contributor

Choose a reason for hiding this comment

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

Wrong removed space.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in new commit

if (in1 != NULL && in1->Opcode() == Op_ConvI2L) {
const Type* t = Value(phase);
const Type* t_in = phase->type(in1);
if (t != Type::TOP && t_in != Type::TOP && t != t_in) {
Copy link
Contributor

Choose a reason for hiding this comment

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

t != t_in does not mean that type is narrower in general case. I think we need to check ranges (types meet?).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for looking at this. t is the result of Value() which takes the type of its input into account so, AFAICT, there's no way t can be wider than t_in. Am I missing something? If not I could add an assert. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

There is no specialized CastLLNode::Value() and ConstraintCastNode only calls filter_speculative() which do call join(). May be it is indeed enough. Yes, would be nice to have an assert to make sure we got it right.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The new commit adds an assert

@rwestrel
Copy link
Contributor Author

rwestrel commented May 5, 2022

Good suggestion. I have comments.

Thanks for reviewing this.

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. I will start testing.

@openjdk
Copy link

openjdk bot commented May 6, 2022

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

8286197: C2: Optimize MemorySegment shape in int loop

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

  • 94b473e: 8280454: G1: ClassLoaderData verification keeps CLDs live that causes problems with VerifyDuringGC during Remark
  • 900d967: 8287924: Avoid redundant HashMap.containsKey call in EnvHelp.mapToHashtable
  • d482d7f: 8286160: (fs) Files.exists returns unexpected results with C:\pagefile.sys because it's not readable
  • edff51e: 8284858: Start of release updates for JDK 20
  • 2671443: 8286171: HttpClient/2 : Expect:100-Continue blocks indefinitely when response is not 100
  • 59b0de6: 8288048: Build failure with GCC 6 after JDK-8286562
  • db4405d: 8288078: linux-aarch64-optimized build fails in Tier5 after JDK-8287567
  • 7e948f7: 8287903: Reduce runtime of java.math microbenchmarks
  • 3fa9984: 8287854: Dangling reference in ClassVerifier::verify_class
  • 4285e3d: 8288023: AArch64: disable PAC-RET when preview is enabled
  • ... and 578 more: https://git.openjdk.org/jdk/compare/29c2e54cf6fe472bd75a75fedf4ecf66e204647a...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 May 6, 2022
@vnkozlov
Copy link
Contributor

vnkozlov commented May 7, 2022

Testing passed. It needs second review.

@rwestrel
Copy link
Contributor Author

Any one else for this one?

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 but I'm wondering if we should delay this to JDK 20 as we are late for the JDK 19 release and had many issues with Cast/ConvNodes before.

src/hotspot/share/opto/castnode.cpp Outdated Show resolved Hide resolved
src/hotspot/share/opto/castnode.hpp Outdated Show resolved Hide resolved
rwestrel and others added 3 commits June 7, 2022 16:37
Co-authored-by: Tobias Hartmann <tobias.hartmann@oracle.com>
Co-authored-by: Tobias Hartmann <tobias.hartmann@oracle.com>
….java

Co-authored-by: Tobias Hartmann <tobias.hartmann@oracle.com>
@rwestrel
Copy link
Contributor Author

rwestrel commented Jun 7, 2022

Looks good to me but I'm wondering if we should delay this to JDK 20 as we are late for the JDK 19 release and had many issues with Cast/ConvNodes before.

Thanks for the review. I have no objection to delaying until JDK 20.

@rwestrel
Copy link
Contributor Author

/integrate

@openjdk
Copy link

openjdk bot commented Jun 10, 2022

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

  • 94b473e: 8280454: G1: ClassLoaderData verification keeps CLDs live that causes problems with VerifyDuringGC during Remark
  • 900d967: 8287924: Avoid redundant HashMap.containsKey call in EnvHelp.mapToHashtable
  • d482d7f: 8286160: (fs) Files.exists returns unexpected results with C:\pagefile.sys because it's not readable
  • edff51e: 8284858: Start of release updates for JDK 20
  • 2671443: 8286171: HttpClient/2 : Expect:100-Continue blocks indefinitely when response is not 100
  • 59b0de6: 8288048: Build failure with GCC 6 after JDK-8286562
  • db4405d: 8288078: linux-aarch64-optimized build fails in Tier5 after JDK-8287567
  • 7e948f7: 8287903: Reduce runtime of java.math microbenchmarks
  • 3fa9984: 8287854: Dangling reference in ClassVerifier::verify_class
  • 4285e3d: 8288023: AArch64: disable PAC-RET when preview is enabled
  • ... and 578 more: https://git.openjdk.org/jdk/compare/29c2e54cf6fe472bd75a75fedf4ecf66e204647a...master

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Jun 10, 2022

@rwestrel Pushed as commit dae4c49.

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