Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
/ jdk17 Public archive

8268347: C2: nested locks optimization may create unbalanced monitor enter/exit code #38

Closed
wants to merge 3 commits into from

Conversation

vnkozlov
Copy link
Contributor

@vnkozlov vnkozlov commented Jun 12, 2021

There are several restrictions when nested locks should be optimized. One of them is nested locks optimization (NLO) requires one BoxLock node per one lock/unlock region. Unfortunately loop unswitching optimization can create such case because loop cloning does not clone BoxLock nodes which are outside loops (they are pinned to Root node). Note, Lock/Unlock nodes are subclass of Call node - all loop optimizations are skipped, except unswitching.

NLO happens during Macro nodes elimination. But before that Lock/Unlock coarsening optimization happens during IGVN as early as right after parsing - even before Escape analysis.

Failing case:

while (a) { 
    synchronized(obj) { // OLR (outer lock region) 
          if (loop_invar_check) { // class check for biomorphic call in the test case
              synchronized(obj) { // LR1 } // (lock region 1) 
          } else { 
             // no synchronization 
          } 
          synchronized(obj) { // LR2 } // (lock region 2) 
    } 
} 

And after loop unswitching:

 if (loop_invar_check) { 
    while (a) { 
        synchronized(obj) { // OLR (outer lock region) 
             synchronized(obj) { // LR1 } // (lock region 1) 
             synchronized(obj) { // LR2 } // (lock region 2) 
        } 
    } 
} else { 
    while (a) { 
        synchronized(obj) { // COLR (Clone of outer lock region) 
             synchronized(obj) { // CLR2 } // (Clone of lock region 2) 
        } 
    } 
} 

After loop unswitching for the code in first branch Lock/Unlock coarsening optimization marked Unlock node from LR1 and Lock from LR2 as "Coarsened".

When NLO (nested lock optimization) started, it overwrite state for Unlock node from LR1 as "Nested" because it passed all conditions: it belong to simple region with one BoxLock node and inside outer OLR lock region.
But NLO did not overwrite state of Lock node from LR2 because this region shares BoxLock node with its clone CLR2 in second branch of unswitched loop. As result this Lock node stays "Coarsened". Later, when locks are eliminated, Lock/Unlock nodes in LR1 are eliminated as "Nested". And Lock node in LR2 is eliminated as "Coarsened" leaving behind un-matching Unlock node from LR2.

The solution I propose is to catch the case when coarsened lock and unlocks are unbalanced and recompile method without "Coarsening locks" optimization. For that each group of coarsened locks/unlocks is recorded in Compile's growable array in add_coarsened_locks() and before lock elimination in macro.cpp consistency locks/unlocks in recorded groups is verified in coarsened_locks_consistent ():

  • all stay coarsened
  • all were changed to other king (nested or noescobj)
  • all were removed during IR graph transformations when other Macro nodes are removed

I also added more debugging information for case when nested lock optimization checks are failing.
And more information about locks elimination in LogCompilation output.

Added regression test.
Tested tier1-9.


Progress

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

Issue

  • JDK-8268347: C2: nested locks optimization may create unbalanced monitor enter/exit code

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 38

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

Using diff file

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

@bridgekeeper
Copy link

bridgekeeper bot commented Jun 12, 2021

👋 Welcome back kvn! 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 Jun 12, 2021
@openjdk
Copy link

openjdk bot commented Jun 12, 2021

@vnkozlov The following label will be automatically applied to this pull request:

  • hotspot

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 hotspot-dev@openjdk.java.net label Jun 12, 2021
@vnkozlov
Copy link
Contributor Author

/label add hotspot-compiler

@openjdk openjdk bot added the hotspot-compiler hotspot-compiler-dev@openjdk.java.net label Jun 12, 2021
@openjdk
Copy link

openjdk bot commented Jun 12, 2021

@vnkozlov
The hotspot-compiler label was successfully added.

@vnkozlov
Copy link
Contributor Author

/label remove hotspot

@openjdk openjdk bot removed the hotspot hotspot-dev@openjdk.java.net label Jun 12, 2021
@openjdk
Copy link

openjdk bot commented Jun 12, 2021

@vnkozlov
The hotspot label was successfully removed.

@mlbridge
Copy link

mlbridge bot commented Jun 12, 2021

Webrevs

Copy link
Contributor

@rwestrel rwestrel left a comment

Choose a reason for hiding this comment

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

Looks reasonable to me.

@openjdk
Copy link

openjdk bot commented Jun 14, 2021

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

8268347: C2: nested locks optimization may create unbalanced monitor enter/exit code

Reviewed-by: roland, vlivanov, dcubed

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

  • a5bf5e0: 8266614: update manpage for -Xlog:async
  • f69e2d5: 8267397: AlgorithmId's OID cache is never refreshed
  • ee30159: 8268621: SunJCE provider may throw unexpected NPE for un-initialized AES KW/KWP Ciphers
  • 702e3ff: 8268366: Incorrect calculation of has_fpu_registers in C1 linear scan
  • bca914b: 8268670: yield statements doesn't allow ~ or ! unary operators in expression
  • c088d09: 8268522: InstanceKlass::can_be_verified_at_dumptime() returns opposite value
  • abe20c1: 8268333: javac crashes when pattern matching switch contains default case which is not last
  • b318535: 8267579: Thread::cooked_allocated_bytes() hits assert(left >= right) failed: avoid underflow
  • fe48ea9: 8268342: java/foreign/channels/TestAsyncSocketChannels.java fails with "IllegalStateException: This segment is already closed"

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 Jun 14, 2021
@vnkozlov
Copy link
Contributor Author

Thank you, Roland

Copy link

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

Copy link
Member

@dcubed-ojdk dcubed-ojdk left a comment

Choose a reason for hiding this comment

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

I only reviewed the test. It looks good.

return (buffers.isEmpty() == false);
}

TestNestedLocksElimination getHolder(TestNestedLocksElimination s1, TestNestedLocksElimination s2, int count) {
Copy link
Member

Choose a reason for hiding this comment

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

Should have this comment here also:

`// Don't inline`

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do.

TestNestedLocksElimination s2 = new TestNestedLocksEliminationSub();

char[] c = new char[100];
// warmup
Copy link
Member

Choose a reason for hiding this comment

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

Usually when I see a warmup comment, there's a second loop
that does the real run. In this case, I see the warmup comment
and then the end of the test and I wonder if the real run is missing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will remove. Left over from test development.

@vnkozlov
Copy link
Contributor Author

Thank you, Vladimir I. and Dan.

@vnkozlov
Copy link
Contributor Author

/integrate

@openjdk
Copy link

openjdk bot commented Jun 14, 2021

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

  • 4a6da99: 8268643: SVML lib shouldn't be generated when C2 is absent
  • a5bf5e0: 8266614: update manpage for -Xlog:async
  • f69e2d5: 8267397: AlgorithmId's OID cache is never refreshed
  • ee30159: 8268621: SunJCE provider may throw unexpected NPE for un-initialized AES KW/KWP Ciphers
  • 702e3ff: 8268366: Incorrect calculation of has_fpu_registers in C1 linear scan
  • bca914b: 8268670: yield statements doesn't allow ~ or ! unary operators in expression
  • c088d09: 8268522: InstanceKlass::can_be_verified_at_dumptime() returns opposite value
  • abe20c1: 8268333: javac crashes when pattern matching switch contains default case which is not last
  • b318535: 8267579: Thread::cooked_allocated_bytes() hits assert(left >= right) failed: avoid underflow
  • fe48ea9: 8268342: java/foreign/channels/TestAsyncSocketChannels.java fails with "IllegalStateException: This segment is already closed"

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Jun 14, 2021

@vnkozlov Pushed as commit 4d8b5c7.

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

@vnkozlov vnkozlov deleted the JDK-8268347 branch June 14, 2021 23:45
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
hotspot-compiler hotspot-compiler-dev@openjdk.java.net integrated Pull request has been integrated
4 participants