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

8267239: C1: RangeCheckElimination for % operator if divisor is IntConstant #4083

Closed
wants to merge 3 commits into from

Conversation

y1yang0
Copy link
Member

@y1yang0 y1yang0 commented May 18, 2021

% operator follows from this rule that the result of the remainder operation can be negative only if the dividend is negative, and can be positive only if the dividend is positive. Moreover, the magnitude of the result is always less than the magnitude of the divisor(See LS 15.17.3).

So if y is a constant integer and not equal to 0, then we can deduce the bound of remainder operation:

  • x % -y ==> [0, y - 1] RCE
  • x % y ==> [0, y - 1] RCE
  • -x % y ==> [-y + 1, 0]
  • -x % -y ==> [-y + 1, 0]

Based on above rationale, we can apply RCE for the remainder operations whose dividend is constant integer and >= 0, e.g.:

for(int i=0;i<1000;i++){
  int top5 = arr[i%5];  // Apply RCE if arr is a loop invariant
  ....
}

For more detailed RCE results, please check out the attachment on JBS, it was generated by ArithmeticRemRCE with additional flags -XX:+TraceRangeCheckElimination -XX:+PrintIR.

Testing:

  • test/hotspot/jtreg/compiler/c1/(slowdebug)

Progress

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

Issue

  • JDK-8267239: C1: RangeCheckElimination for % operator if divisor is IntConstant

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 4083

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

Using diff file

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

@bridgekeeper
Copy link

bridgekeeper bot commented May 18, 2021

👋 Welcome back yyang! 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 May 18, 2021
@openjdk
Copy link

openjdk bot commented May 18, 2021

@kelthuzadx 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 18, 2021
@y1yang0
Copy link
Member Author

y1yang0 commented May 18, 2021

/label add hotspot

The inference is not documented in JLS, so add hotspot-dev for more logical checking ;)

@mlbridge
Copy link

mlbridge bot commented May 18, 2021

Webrevs

@openjdk
Copy link

openjdk bot commented May 18, 2021

@kelthuzadx
The hotspot label was successfully added.

@openjdk openjdk bot added the hotspot hotspot-dev@openjdk.org label May 18, 2021
@neliasso
Copy link

Have you checked how C2 handles this case?

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 like C2 does not implement this optimization (should be in ModINode::Value). We should add it.

} else {
_bound = new Bound();
}
}else {
Copy link
Member

Choose a reason for hiding this comment

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

Missing whitespace }else

Copy link
Member Author

Choose a reason for hiding this comment

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

Looks like C2 does not implement this optimization (should be in ModINode::Value). We should add it.

Thanks for confirming the C2 part, I will double-check it and file an issue for that~

Missing whitespace }else

Fixed.

@y1yang0 y1yang0 changed the title 8267239: C1: RangeCheckElimination for % operator 8267239: C1: RangeCheckElimination for % operator if divisor is IntConstant May 19, 2021
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.

@openjdk
Copy link

openjdk bot commented May 19, 2021

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

8267239: C1: RangeCheckElimination for % operator if divisor is IntConstant

Reviewed-by: thartmann, neliasso

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

  • 70f6c67: 8233380: CHT: Node allocation and freeing
  • 2563a6a: 8266962: Add arch supporting check for "Op_VectorLoadConst" before creating the node
  • 4954383: 8267364: Remove mask.incr which is introduced by JDK-8256973
  • c2b50f9: 8266480: Implicit null check optimization does not update control of hoisted memory operation
  • 3f883e8: 8267351: runtime/cds/SharedBaseAddress.java fails on x86_32 due to Unrecognized VM option 'UseCompressedOops'
  • 7aa6568: 8256973: Intrinsic creation for VectorMask query (lastTrue,firstTrue,trueCount) APIs
  • 65a8bf5: 8265126: [REDO] unified handling for VectorMask object re-materialization during de-optimization
  • ff84577: 8267098: AArch64: C1 StubFrames end confusingly
  • 0daec49: 8267246: -XX:MaxRAMPercentage=0 is unreasonable for jtreg tests on many-core machines
  • 324defe: 8267212: test/jdk/java/util/Collections/FindSubList.java intermittent crash with "no reachable node should have no use"
  • ... and 304 more: https://git.openjdk.java.net/jdk/compare/e879f8c6c874cc7039d344f19430cce7cfb138ea...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.

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, @neliasso) 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 May 19, 2021
Copy link

@neliasso neliasso 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.

@y1yang0
Copy link
Member Author

y1yang0 commented May 19, 2021

Thank you Nils and Tobias for taking the time to review this patch! I've filed https://bugs.openjdk.java.net/browse/JDK-8267376 for investigating if it's possible to apply C2 RCE for % operator when divisor != 0.

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label May 19, 2021
@openjdk
Copy link

openjdk bot commented May 19, 2021

@kelthuzadx
Your change (at version 35aaa37) is now ready to be sponsored by a Committer.

@TobiHartmann
Copy link
Member

/sponsor

@openjdk openjdk bot closed this May 19, 2021
@openjdk openjdk bot added integrated Pull request has been integrated and removed sponsor Pull request is ready to be sponsored ready Pull request is ready to be integrated rfr Pull request is ready for review labels May 19, 2021
@openjdk
Copy link

openjdk bot commented May 19, 2021

@TobiHartmann @kelthuzadx Since your change was applied there have been 315 commits pushed to the master branch:

  • 2d407e1: 8267293: vmTestbase/vm/mlvm/anonloader/stress/oome/metaspace/Test.java fails when JTREG_JOBS > 25
  • 70f6c67: 8233380: CHT: Node allocation and freeing
  • 2563a6a: 8266962: Add arch supporting check for "Op_VectorLoadConst" before creating the node
  • 4954383: 8267364: Remove mask.incr which is introduced by JDK-8256973
  • c2b50f9: 8266480: Implicit null check optimization does not update control of hoisted memory operation
  • 3f883e8: 8267351: runtime/cds/SharedBaseAddress.java fails on x86_32 due to Unrecognized VM option 'UseCompressedOops'
  • 7aa6568: 8256973: Intrinsic creation for VectorMask query (lastTrue,firstTrue,trueCount) APIs
  • 65a8bf5: 8265126: [REDO] unified handling for VectorMask object re-materialization during de-optimization
  • ff84577: 8267098: AArch64: C1 StubFrames end confusingly
  • 0daec49: 8267246: -XX:MaxRAMPercentage=0 is unreasonable for jtreg tests on many-core machines
  • ... and 305 more: https://git.openjdk.java.net/jdk/compare/e879f8c6c874cc7039d344f19430cce7cfb138ea...master

Your commit was automatically rebased without conflicts.

Pushed as commit 0cf7e57.

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

@y1yang0 y1yang0 deleted the rce_opt branch May 19, 2021 09:15
@y1yang0
Copy link
Member Author

y1yang0 commented May 24, 2021

I think it is not trivial to eliminate array access range check as easy as C1 since C2 idealizes ModINode to SubINode if divisor is constant integer for common caset(e.g. arr[k%6]):

// Divide using the transform from DivI to MulL
Node *result = transform_int_divide( phase, in(1), pos_con );
if (result != NULL) {

jint magic_const;
jint shift_const;
if (magic_int_divide_constants(d, magic_const, shift_const)) {
Node *magic = phase->longcon(magic_const);
Node *dividend_long = phase->transform(new ConvI2LNode(dividend));
// Compute the high half of the dividend x magic multiplication
Node *mul_hi = phase->transform(new MulLNode(dividend_long, magic));
if (magic_const < 0) {
mul_hi = phase->transform(new RShiftLNode(mul_hi, phase->intcon(N)));
mul_hi = phase->transform(new ConvL2INode(mul_hi));
// The magic multiplier is too large for a 32 bit constant. We've adjusted
// it down by 2^32, but have to add 1 dividend back in after the multiplication.
// This handles the "overflow" case described by Granlund and Montgomery.
mul_hi = phase->transform(new AddINode(dividend, mul_hi));
// Shift over the (adjusted) mulhi
if (shift_const != 0) {
mul_hi = phase->transform(new RShiftINode(mul_hi, phase->intcon(shift_const)));
}
} else {
// No add is required, we can merge the shifts together.
mul_hi = phase->transform(new RShiftLNode(mul_hi, phase->intcon(N + shift_const)));
mul_hi = phase->transform(new ConvL2INode(mul_hi));
}
// Get a 0 or -1 from the sign of the dividend.
Node *addend0 = mul_hi;
Node *addend1 = phase->transform(new RShiftINode(dividend, phase->intcon(N-1)));
// If the divisor is negative, swap the order of the input addends;
// this has the effect of negating the quotient.
if (!d_pos) {
Node *temp = addend0; addend0 = addend1; addend1 = temp;
}
// Adjust the final quotient by subtracting -1 (adding 1)
// from the mul_hi.
q = new SubINode(addend0, addend1);

So even if ModINode::Value is properly handled, it would not be reached and RangeCheckNode is still generated when accessing array element:

bool need_range_check = true;
if (idxtype->_hi < sizetype->_lo && idxtype->_lo >= 0) {
need_range_check = false;
if (C->log() != NULL) C->log()->elem("observe that='!need_range_check'");
}

Regardless of eliminating array access range check, it still reasonable to deduce the bound of % operation if the divisor is known constant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot hotspot-dev@openjdk.org hotspot-compiler hotspot-compiler-dev@openjdk.org integrated Pull request has been integrated
3 participants