Skip to content

Conversation

rwestrel
Copy link
Contributor

@rwestrel rwestrel commented Oct 2, 2025

In the test1() method of the test case:

inlined2() calls clone() for an object loaded from field field
that has inexact type A at parse time. The intrinsic for clone()
inserts an Allocate and an ArrayCopy nodes. When igvn runs, the
load of field is optimized out because it reads back a newly
allocated B written to field in the same method. ArrayCopy can
now be optimized because the type of its src input is known. The
type of its dest input is the CheckCastPP from the allocation of
the cloned object created at parse time. That one has type A. A
series of Loads/Stores are created to copy the fields of class B
from src (of type B) to dest of (type A).

Writting to dest with offsets for fields that don't exist in A,
causes this code in Compile::flatten_alias_type():

    } else if (offset < 0 || offset >= ik->layout_helper_size_in_bytes()) {
      // Static fields are in the space above the normal instance
      // fields in the java.lang.Class instance.
      if (ik != ciEnv::current()->Class_klass()) {
        to = nullptr;
        tj = TypeOopPtr::BOTTOM;
        offset = tj->offset();
      }

to assign it some slice that doesn't match the one that's used at the
same offset in B.

That causes an assert in ArrayCopyNode::try_clone_instance() to
fire. With a release build, execution proceeds. test1() also has a
non escaping allocation. That one causes EA to run and
ConnectionGraph::split_unique_types() to move the store to the non
escaping allocation to a new slice. In the process, when it iterates
over MergeMem nodes, it notices the stores added by
ArrayCopyNode::try_clone_instance(), finds that some are not on the
right slice, tries to move them to the correct slice (expecting they
are from a non escaping EA). That causes some of the Stores to be
disconnected. When the resulting code runs, execution fails as some
fields are not copied.

The fix I propose is to skip ArrayCopyNode::try_clone_instance()
when src and dest classes don't match as this seems like a rare
enough corner case.


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-8339526: C2: store incorrectly removed for clone() transformed to series of loads/stores (Bug - P3)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 27604

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Oct 2, 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 Oct 2, 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:

8339526: C2: store incorrectly removed for clone() transformed to series of loads/stores

Reviewed-by: chagedorn

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 424 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
Copy link

openjdk bot commented Oct 2, 2025

@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 rfr Pull request is ready for review label Oct 2, 2025
@mlbridge
Copy link

mlbridge bot commented Oct 2, 2025

Webrevs

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.

That looks reasonable to me.

}
}

static A field;
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 move the field up to the other field?

Comment on lines +217 to +220
// At parse time, exact type of the object to clone was not known. That inexact type was captured by the CheckCastPP
// of the newly allocated cloned object (in dest). Exact type is now known (in src), but type for the cloned object
// (dest) was not updated. When copying fields below, Store nodes may write to offsets for fields that don't exist in
// the inexact class. The stores would then be assigned an incorrect slice.
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
// At parse time, exact type of the object to clone was not known. That inexact type was captured by the CheckCastPP
// of the newly allocated cloned object (in dest). Exact type is now known (in src), but type for the cloned object
// (dest) was not updated. When copying fields below, Store nodes may write to offsets for fields that don't exist in
// the inexact class. The stores would then be assigned an incorrect slice.
// At parse time, the exact type of the object to clone was not known. That inexact type was captured by the CheckCastPP
// of the newly allocated cloned object (in dest). The exact type is now known (in src), but the type for the cloned object
// (dest) was not updated. When copying the fields below, Store nodes may write to offsets for fields that don't exist in
// the inexact class. The stores would then be assigned an incorrect slice.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Oct 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot-compiler [email protected] ready Pull request is ready to be integrated rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

2 participants