Skip to content

Conversation

@XiaohongGong
Copy link

@XiaohongGong XiaohongGong commented May 30, 2025

C2 compiler fails to recognize counted loops when the induction variable is constrained by multiple consecutive CastII nodes.
This prevents optimizations like range check elimination, loop unrolling and auto-vectorization for these loops. Please refer
to the detailed discussion for a related performance issue from [1].

The ideal graph of such a loop typically looks like:

                          /-----------|
                         |            |
                         |   ConI     |
               loop      |  /        /
                 |       | /        /
                  \     AddI       /
      RangeCheck   \    /         |
              |     \  /          |
             IfTrue  Phi          |
                 \    |           |
    RangeCheck    \   |           |
             \    CastII          /     <- Range check #1
              |        |         /
             IfTrue    |        |
                  \    |        |
                  CastII        |       <- Range check #2
                      |        /
                      |-------/

For a counted loop, the loop induction variable (i.e Phi) should be the input of AddI ideally. However, in above case, it is used
by two consecutive CastII nodes generated by two different range check operations. Compiler should skip all such kind of CastII when recognizing a counted loop.

This patch modifies the counted loop recognition code to iteratively uncast the loop iv until no CastII nodes remain, enabling proper counted loop recognition even when the induction variable undergoes multiple range constraint operations.

Test:

  • Tested tier1, tier2, tier3, and no regressions are found.
  • An additional test case is added to verify the fix.

Performance:
Here is the performance gain on a NVIDIA Grace machine which is an AArch64 architecture:

Benchmark                      Mode   Cnt Unit   Before      After        Gain
CountedLoopCastIV.loop_iv_int  thrpt  30  ops/s  941482.597  4389292.439  4.66
CountedLoopCastIV.loop_iv_long thrpt  30  ops/s  884563.232  1441485.455  1.62

We can also observe the similar uplift on a x86_64 machine.

[1] #25138 (comment)


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-8357726: Improve C2 to recognize counted loops with multiple casts in trip counter (Enhancement - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 25539

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented May 30, 2025

👋 Welcome back xgong! 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 30, 2025

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

8357726: Improve C2 to recognize counted loops with multiple casts in trip counter

Reviewed-by: chagedorn, epeter, galder

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 26 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 the rfr Pull request is ready for review label May 30, 2025
@openjdk
Copy link

openjdk bot commented May 30, 2025

@XiaohongGong 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 30, 2025
@mlbridge
Copy link

mlbridge bot commented May 30, 2025

Webrevs

@eme64
Copy link
Contributor

eme64 commented Jun 2, 2025

@XiaohongGong Nice work! @chhagedorn And I quickly discussed it offline, and we think this is a good approach.

@eme64
Copy link
Contributor

eme64 commented Jun 2, 2025

@XiaohongGong I suggest you change the title from:
8357726: C2 fails to recognize the counted loop when induction variable range is changed multiple times
to
8357726: C2 recognize loops with multiple casts in trip counter
or even:
8357726: C2 recognize loops with multiple casts in trip counter: phi -> CastII* -> AddI -> phi

@XiaohongGong
Copy link
Author

@XiaohongGong I suggest you change the title from: 8357726: C2 fails to recognize the counted loop when induction variable range is changed multiple times to 8357726: C2 recognize loops with multiple casts in trip counter or even: 8357726: C2 recognize loops with multiple casts in trip counter: phi -> CastII* -> AddI -> phi

Thanks for your suggestion! Sounds better to me. How about changing the title to Improve C2 to recognize counted loops with multiple casts in trip counter ?

@eme64
Copy link
Contributor

eme64 commented Jun 3, 2025

Thanks for your suggestion! Sounds better to me. How about changing the title to Improve C2 to recognize counted loops with multiple casts in trip counter ?

@XiaohongGong Sounds good too :)

@XiaohongGong XiaohongGong changed the title 8357726: C2 fails to recognize the counted loop when induction variable range is changed multiple times 8357726: Improve C2 to recognize counted loops with multiple casts in trip counter Jun 4, 2025
@XiaohongGong
Copy link
Author

Hi @eme64 , I'v updated the IR test and JMH based on your comments. Could you please help review whether it's fine to you. Thanks for all your suggestion!

Following shows the performance data of the new JMH test on Grace (the performance gain is almost the same on my x64 machine):

Benchmark                      Mode   Cnt limit Unit    Before    Error (99.9%)    After     Error (99.9%) Gain
CountedLoopCastIV.loop_iv_int  thrpt  30  1024  ops/s 1225620.536 39505.158362   5778120.132 4781.602088   4.71
CountedLoopCastIV.loop_iv_int  thrpt  30  1536  ops/s  830600.832 14758.561182   3839404.338 3362.727083   4.62
CountedLoopCastIV.loop_iv_int  thrpt  30  2048  ops/s  618114.174 36999.511727   2890853.495  416.969862   4.67
CountedLoopCastIV.loop_iv_long thrpt  30  1024  ops/s 1063902.078  4616.608855   1314828.963 1267.470199   1.23
CountedLoopCastIV.loop_iv_long thrpt  30  1536  ops/s  714538.178   630.085477    870801.472  753.347684   1.21
CountedLoopCastIV.loop_iv_long thrpt  30  2048  ops/s  536724.086   131.313178    652775.363  539.107806   1.21

The error term is larger as before. But I don't think this is caused by the large variance of loop iterations. Does the new benchmark look fine to you? Thanks!

@eme64
Copy link
Contributor

eme64 commented Jun 4, 2025

@XiaohongGong Nice, thanks for the updates! Especially the IR rules and reduction in JMH benchmark variance, excellent :)

Please ping me again once you have addressed my comment above, and then I can run some internal testing for you!

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.

Looks good to me, too!

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jun 4, 2025
@eme64
Copy link
Contributor

eme64 commented Jun 4, 2025

@XiaohongGong Let's please delay this until after Thursday, so that this does not go into JDK25 yet, and we have more time to fix it if something goes wrong down the line.

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Jun 4, 2025
@XiaohongGong
Copy link
Author

@XiaohongGong Let's please delay this until after Thursday, so that this does not go into JDK25 yet, and we have more time to fix it if something goes wrong down the line.

Sure. That makes sense to me. Thanks! BTW, I'v updated the test according to your comment. So could you please help run all the tests? Thanks again!

@XiaohongGong
Copy link
Author

@XiaohongGong Let's please delay this until after Thursday, so that this does not go into JDK25 yet, and we have more time to fix it if something goes wrong down the line.

Sure. That makes sense to me. Thanks! BTW, I'v updated the test according to your comment. So could you please help run all the tests? Thanks again!

Hi @eme64 , may I ask what the status of the testing is for this PR? Any feedback please let me know. Thanks a lot!

Change-Id: I0c10a563a3873b2220ce4d4c9b999c52159f578f
@XiaohongGong
Copy link
Author

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

Still good!

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jun 19, 2025
@chhagedorn
Copy link
Member

Let me submit some testing for it before integration.

@XiaohongGong
Copy link
Author

Let me submit some testing for it before integration.

That's great! Thanks for your help!

@chhagedorn
Copy link
Member

Testing looked 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.

LGTM, thanks for the work you put in :)

@XiaohongGong
Copy link
Author

Thanks so much for your review @eme64 @chhagedorn @galderz !

@XiaohongGong
Copy link
Author

/integrate

@openjdk
Copy link

openjdk bot commented Jun 25, 2025

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

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Jun 25, 2025

@XiaohongGong Pushed as commit 7d6c902.

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

4 participants