Skip to content

8349361: C2: RShiftL should support all applicable transformations that RShiftI does#23438

Closed
rwestrel wants to merge 30 commits intoopenjdk:masterfrom
rwestrel:JDK-8349361
Closed

8349361: C2: RShiftL should support all applicable transformations that RShiftI does#23438
rwestrel wants to merge 30 commits intoopenjdk:masterfrom
rwestrel:JDK-8349361

Conversation

@rwestrel
Copy link
Contributor

@rwestrel rwestrel commented Feb 4, 2025

This change refactors RShiftI/RshiftL Ideal, Identity and
Value because the int and long versions are very similar and so
there's no logic duplication. In the process, support for some extra
transformations is added to RShiftL. I also added some new test
cases.

/cc hotspot-compiler


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-8349361: C2: RShiftL should support all applicable transformations that RShiftI does (Enhancement - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 23438

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Feb 4, 2025

👋 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 Feb 4, 2025

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

8349361: C2: RShiftL should support all applicable transformations that RShiftI does

Reviewed-by: epeter, chagedorn, jkarthikeyan

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 91 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.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added rfr Pull request is ready for review hotspot-compiler hotspot-compiler-dev@openjdk.org labels Feb 4, 2025
@openjdk
Copy link

openjdk bot commented Feb 4, 2025

@rwestrel
The hotspot-compiler label was successfully added.

@mlbridge
Copy link

mlbridge bot commented Feb 4, 2025

Copy link
Contributor

@eme64 eme64 left a comment

Choose a reason for hiding this comment

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

Drive-by code style comment ;)

//=============================================================================
//------------------------------Identity---------------------------------------
Node* RShiftINode::Identity(PhaseGVN* phase) {
Node *RShiftNode::IdealIL(PhaseGVN *phase, bool can_reshape, BasicType bt) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Drive-by: fix position of *

Node *RShiftNode::IdealIL(PhaseGVN *phase, bool can_reshape, BasicType bt) {
// Inputs may be TOP if they are dead.
const TypeInteger* t1 = phase->type(in(1))->isa_integer(bt);
if (!t1) return NodeSentinel; // Left input is an integer
Copy link
Contributor

Choose a reason for hiding this comment

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

Drive-by: don't use implicit null-check, make comparison with nullptr explicit. And add curly braces.

@TobiHartmann
Copy link
Member

Fails to build on Mac AArch64:

[2025-02-05T06:43:04,925Z] * For target hotspot_variant-server_libjvm_objs_mulnode.o:
[2025-02-05T06:43:04,925Z] [...]workspace/open/src/hotspot/share/opto/mulnode.cpp:1400:13: error: use of bitwise '&' with boolean operands [-Werror,-Wbitwise-instead-of-logical]
[2025-02-05T06:43:04,925Z]      assert((checked_cast<jint>(lo) == lo_verify) & (checked_cast<jint>(hi) == hi_verify), "inconsistent");

@rwestrel
Copy link
Contributor Author

rwestrel commented Feb 5, 2025

Fails to build on Mac AArch64:

[2025-02-05T06:43:04,925Z] * For target hotspot_variant-server_libjvm_objs_mulnode.o:
[2025-02-05T06:43:04,925Z] [...]workspace/open/src/hotspot/share/opto/mulnode.cpp:1400:13: error: use of bitwise '&' with boolean operands [-Werror,-Wbitwise-instead-of-logical]
[2025-02-05T06:43:04,925Z]      assert((checked_cast<jint>(lo) == lo_verify) & (checked_cast<jint>(hi) == hi_verify), "inconsistent");

Thanks for the report. Should be fixed now. I also took @eme64's comments into account.

Copy link
Member

@jaskarth jaskarth left a comment

Choose a reason for hiding this comment

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

This is really nice! I'd wondered why there was no RShiftL::Ideal, and it's nice to have it handled it in a generic way with the integer version. I left mostly code style comments here.

if (bt == T_INT) {
return BitsPerJavaInteger;
}
return BitsPerJavaLong;
Copy link
Member

Choose a reason for hiding this comment

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

I think it'd be nice to add assert(bt == T_LONG, "unsupported"); before the last return, like in the helper methods above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed. Added in new commit.

jlong hi = r1->hi_as_long() >> (jint)shift;
assert(lo <= hi, "must have valid bounds");
#ifdef ASSERT
if (bt ==T_INT) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (bt ==T_INT) {
if (bt == T_INT) {

Could this assert be generic to also handle T_LONG too?

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 assert checks that, for the int case:

long lo;
assert((int)(lo >> shift) == (((int)lo) >> shift, "");

For long, it would be:

long lo;
assert((long)(lo >> shift) == (((long)lo) >> shift, "");

Given everything is already a long, that's:

long lo;
assert(lo >> shift == lo >> shift, "");

Copy link
Member

Choose a reason for hiding this comment

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

Ah I see, thank you for the explanation!

rwestrel and others added 7 commits February 7, 2025 09:44
Co-authored-by: Jasmine Karthikeyan <25208576+jaskarth@users.noreply.github.com>
Co-authored-by: Jasmine Karthikeyan <25208576+jaskarth@users.noreply.github.com>
Co-authored-by: Jasmine Karthikeyan <25208576+jaskarth@users.noreply.github.com>
Co-authored-by: Jasmine Karthikeyan <25208576+jaskarth@users.noreply.github.com>
Co-authored-by: Jasmine Karthikeyan <25208576+jaskarth@users.noreply.github.com>
Co-authored-by: Jasmine Karthikeyan <25208576+jaskarth@users.noreply.github.com>
@rwestrel
Copy link
Contributor Author

rwestrel commented Feb 7, 2025

This is really nice! I'd wondered why there was no RShiftL::Ideal, and it's nice to have it handled it in a generic way with the integer version. I left mostly code style comments here.

Thanks for reviewing this. I pushed a new commit that takes your comments into account.

Copy link
Member

@jaskarth jaskarth left a comment

Choose a reason for hiding this comment

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

Thanks for the update, it looks good!

Copy link
Contributor

@eme64 eme64 left a comment

Choose a reason for hiding this comment

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

@rwestrel nice work, looks like a good step to unify the code a little!

I left some comments / suggestions.

I'm also wondering about testing. How good do you think test coverage is? Are all cases covered? How about the edge-cases? Could we improve the coverage with randomization somehow?

if (t1 == nullptr) {
return NodeSentinel; // Left input is an integer
}
const TypeInteger* t3; // type of in(1).in(2)
Copy link
Contributor

Choose a reason for hiding this comment

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

I know that you only moved this code, but it looks bad 🙈
For one, why is it defined up here already when it is only used 10 lines later?
And why not give it a better name so we don't need the comment?

Suggested change
const TypeInteger* t3; // type of in(1).in(2)

(t3 = phase->type(mask->in(2))->isa_integer(bt)) &&
t3->is_con()) {
jlong maskbits = t3->get_con_as_long(bt);
// Convert to "(x >> shift) & (mask >> shift)"
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a nice comment. It could come as a motivation above. Because it suggests that we can then constant fold the mask >> shift, right?

Comment on lines +1325 to +1329
const Node* mask = in(1);
if (mask->Opcode() == Op_And(bt) &&
(t3 = phase->type(mask->in(2))->isa_integer(bt)) &&
t3->is_con()) {
jlong maskbits = t3->get_con_as_long(bt);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is also quite bad. It seems mask here is in(1), which is not even the mask at all, but x & <real_mask>.

I'd suggest to clean it up a little and use better names.

if (progress != nullptr) {
return progress;
}
const TypeInt* t3; // type of in(1).in(2)
Copy link
Contributor

Choose a reason for hiding this comment

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

Also refactor the use of t3 here, please.

}

@Run(test = { "test1", "test2", "test3", "test4" })
@Run(test = { "test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8", "test9" })
Copy link
Contributor

Choose a reason for hiding this comment

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

You should add the bug id above.

Comment on lines +117 to +119
final int test7Shift = 42;
final long test7Min = -1L << (64 - test7Shift -1);
final long test7Max = ~test7Min;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we randomize these tests, so that we would get better coverage?

rwestrel and others added 6 commits February 13, 2025 17:54
Co-authored-by: Emanuel Peter <emanuel.peter@oracle.com>
Co-authored-by: Emanuel Peter <emanuel.peter@oracle.com>
@rwestrel
Copy link
Contributor Author

I left some comments / suggestions.

Thanks for reviewing this. I pushed new commits that I think cover your comments.

I'm also wondering about testing. How good do you think test coverage is? Are all cases covered? How about the edge-cases? Could we improve the coverage with randomization somehow?

Transformations that I refactored and now apply to both the long and int RShift should now have test cases. I tweaked the test cases so there are now randomized.

@rwestrel
Copy link
Contributor Author

@eme64 can you take another look?

rwestrel and others added 2 commits March 13, 2025 08:53
Co-authored-by: Emanuel Peter <emanuel.peter@oracle.com>
…Tests.java

Co-authored-by: Emanuel Peter <emanuel.peter@oracle.com>
Copy link
Member

@chhagedorn chhagedorn left a comment

Choose a reason for hiding this comment

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

Nice refactoring! I have a few small comments - mostly code style. Otherwise, looks good to me, too.

Comment on lines +1470 to +1476
if (t1 == Type::TOP) return Type::TOP;
if (t2 == Type::TOP) return Type::TOP;

// Left input is ZERO ==> the result is ZERO.
if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
if (t1 == TypeInteger::zero(bt)) return TypeInteger::zero(bt);
// Shift by zero does nothing
if( t2 == TypeInt::ZERO ) return t1;
if (t2 == TypeInt::ZERO) return t1;
Copy link
Member

Choose a reason for hiding this comment

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

Can you add braces here for safety?

int hi = ~lo; // 00007FFF
const TypeInt* t11 = phase->type(in(1)->in(1))->isa_int();
jlong lo = (-1 << (bits_per_java_integer(bt) - ((uint)count)-1)); // FFFF8000
jlong hi = ~lo; // 00007FFF
Copy link
Member

@chhagedorn chhagedorn Mar 13, 2025

Choose a reason for hiding this comment

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

Seems strangely aligned. Maybe either align it to the comment above or convert to an unaligned comment.

Comment on lines +1359 to 1361
if (in(1)->Opcode() == Op_LShift(bt) &&
in(1)->req() == 3 &&
in(1)->in(2) == in(2)) {
Copy link
Member

Choose a reason for hiding this comment

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

Generally, is there notifaction code for this pattern to re-add the node to the IGVN worklist? If not, I don't think you need to handle it here if it's missing (it's just a missed opportunity but no correctness issue) but would be good to file a follow-up bug to handle it - especially when we want to add IGVN verification for Ideal and Identity with JDK-8347273.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. I fixed a case that was handled for int but not for long. There are others that are missing for both AFAICT.
If I file a follow up bug, writing a test case for it is going to be very hard. So testing a fix is also hard. Shouldn't we wait for JDK-8347273 and fix whatever follows up of that?

Copy link
Member

Choose a reason for hiding this comment

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

If I file a follow up bug, writing a test case for it is going to be very hard

Yes, it will be. There is currently no way to really verify that reliably without the additional verification. I think it's okay to wait for JDK-8347273. Maybe you can add a note there or file a separate issue to keep track of the missing bits detected in this investigation..

return TypeLong::make(lo, hi, w);
}

const TypeInteger* TypeInteger::make(jlong lo, BasicType bt) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe you want to rename lo to con since we set lo == hi.

@@ -27,7 +27,7 @@

Copy link
Member

Choose a reason for hiding this comment

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

Up to you if you want to update the copyright year or add your company's copyright. Same in the other test.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Mar 13, 2025
rwestrel and others added 2 commits March 13, 2025 14:56
Co-authored-by: Christian Hagedorn <christian.hagedorn@oracle.com>
Co-authored-by: Christian Hagedorn <christian.hagedorn@oracle.com>
@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Mar 13, 2025
rwestrel and others added 6 commits March 13, 2025 14:59
Co-authored-by: Christian Hagedorn <christian.hagedorn@oracle.com>
Co-authored-by: Christian Hagedorn <christian.hagedorn@oracle.com>
Co-authored-by: Christian Hagedorn <christian.hagedorn@oracle.com>
@rwestrel
Copy link
Contributor Author

Nice refactoring! I have a few small comments - mostly code style. Otherwise, looks good to me, too.

Thanks for the review.
New commit should address all your comments.
Now that the long min/max intrinsic is integrated, I also changed the long tests so they use long min/max and that triggered a bug in the code (missing CONST64) that I fixed.

@eme64
Copy link
Contributor

eme64 commented Mar 14, 2025

@rwestrel I saw this in testing from yesterday: linux-aarch64-debug with -XX:+UnlockExperimentalVMOptions -XX:+UseCompactObjectHeaders.

Failed IR Rules (2) of Methods (2)
----------------------------------
1) Method "public long compiler.c2.irTests.RShiftLNodeIdealizationTests.test8(long)" - [Failed IR rules: 1]:
   * @IR rule 1: "@compiler.lib.ir_framework.IR(phase={DEFAULT}, applyIfPlatformAnd={}, applyIfCPUFeatureOr={}, counts={"_#RSHIFT_L#_", "1", "_#LSHIFT_L#_", "1"}, failOn={}, applyIfPlatform={}, applyIfPlatformOr={}, applyIfOr={}, applyIfCPUFeatureAnd={}, applyIf={}, applyIfCPUFeature={}, applyIfAnd={}, applyIfNot={})"
     > Phase "PrintIdeal":
       - counts: Graph contains wrong number of nodes:
         * Constraint 1: "(\\d+(\\s){2}(RShiftL.*)+(\\s){2}===.*)"
           - Failed comparison: [found] 0 = 1 [given]
           - No nodes matched!
         * Constraint 2: "(\\d+(\\s){2}(LShiftL.*)+(\\s){2}===.*)"
           - Failed comparison: [found] 0 = 1 [given]
           - No nodes matched!

2) Method "public long compiler.c2.irTests.RShiftLNodeIdealizationTests.test9(long)" - [Failed IR rules: 1]:
   * @IR rule 1: "@compiler.lib.ir_framework.IR(phase={DEFAULT}, applyIfPlatformAnd={}, applyIfCPUFeatureOr={}, counts={"_#RSHIFT_L#_", "1", "_#LSHIFT_L#_", "1"}, failOn={}, applyIfPlatform={}, applyIfPlatformOr={}, applyIfOr={}, applyIfCPUFeatureAnd={}, applyIf={}, applyIfCPUFeature={}, applyIfAnd={}, applyIfNot={})"
     > Phase "PrintIdeal":
       - counts: Graph contains wrong number of nodes:
         * Constraint 1: "(\\d+(\\s){2}(RShiftL.*)+(\\s){2}===.*)"
           - Failed comparison: [found] 0 = 1 [given]
           - No nodes matched!
         * Constraint 2: "(\\d+(\\s){2}(LShiftL.*)+(\\s){2}===.*)"
           - Failed comparison: [found] 0 = 1 [given]
           - No nodes matched!

@eme64
Copy link
Contributor

eme64 commented Mar 14, 2025

Not sure if that still reproduces after your changes. LMK when I should run testing again.

Copy link
Member

@chhagedorn chhagedorn left a comment

Choose a reason for hiding this comment

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

Apart from Emanuel's report, it looks good to me, thanks for the update!

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Mar 14, 2025
@rwestrel
Copy link
Contributor Author

rwestrel commented Mar 14, 2025

Not sure if that still reproduces after your changes. LMK when I should run testing again.

I could reproduce that one by forcing one of the random values to a particular constant. It's a bug in the test that was fixed since. @eme64 could you run testing again, please?

@rwestrel
Copy link
Contributor Author

@eme64 any update on testing?

@eme64
Copy link
Contributor

eme64 commented Mar 21, 2025

@rwestrel Testing looks good, thanks for the ping :)

Copy link
Contributor

@eme64 eme64 left a comment

Choose a reason for hiding this comment

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

Gave it a quick pass again, I think this is good to go (though better to integrate after the weekend...)

@rwestrel thanks for the work and all the updates ☺️

@rwestrel
Copy link
Contributor Author

@eme64 thanks for running tests and for the review.
@chhagedorn @jaskarth thanks for the reviews.

@rwestrel
Copy link
Contributor Author

/integrate

@openjdk
Copy link

openjdk bot commented Mar 26, 2025

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

  • eef6aef: 8352623: MultiExchange should cancel exchange impl if responseFilters throws
  • e2a461b: 8351332: Line breaks in search tag descriptions corrupt JSON search index
  • c14bbea: 8352740: Introduce new factory method HtmlTree.IMG
  • 84d3dc7: 8352965: [BACKOUT] 8302459: Missing late inline cleanup causes compiler/vectorapi/VectorLogicalOpIdentityTest.java IR failure
  • b4dc364: 8346931: Replace divisions by zero in sharedRuntimeTrans.cpp
  • bc5cde1: 8352692: Add support for extra jlink options
  • 059f190: 8352490: Fatal error message for unhandled bytecode needs more detail
  • ee710fe: 8345169: Implement JEP 503: Remove the 32-bit x86 Port
  • eb6e828: 8351002: com/sun/management/OperatingSystemMXBean cpuLoad tests fail intermittently
  • e32a0c9: 8352706: httpclient HeadTest does not run on HTTP2
  • ... and 152 more: https://git.openjdk.org/jdk/compare/4e51a8c9ad4e5345d05cf32ce1e82b7158f80e93...master

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Mar 26, 2025

@rwestrel Pushed as commit 79bffe2.

💡 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.

5 participants