Skip to content

8387073: C2: StoreNode::Ideal wrongly optimizes away scalar store before masked vector store#31695

Open
mhaessig wants to merge 3 commits into
openjdk:masterfrom
mhaessig:JDK-8387073-masked
Open

8387073: C2: StoreNode::Ideal wrongly optimizes away scalar store before masked vector store#31695
mhaessig wants to merge 3 commits into
openjdk:masterfrom
mhaessig:JDK-8387073-masked

Conversation

@mhaessig

@mhaessig mhaessig commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

StoreNode::Ideal() optimizes back-to-back stores to the same address by removing the earlier store if it is the same size or narrower than the later store when we have UseAVX >= 3 or UseSVE > 0. This optimization is incorrect if the later store is a masked vector store as it might not overwrite all bits of the preceding store.

This PR fixes this issue by plainly disabling the optimization for masked stores. We could conceivably enable the optimization for masked stores if we can prove that the mask overwrites all of the preceding store, but this fix is aimed at correctness before the 27 release. Thus, such optimizations are filed as follow-up RFEs.

Testing:

  • Github Actions
  • tier1,tier2,tier3 plus additional stress testing and an additional run on AVX-512 machines only.
  • tier4,tier5


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed (2 reviews required, with at least 1 Reviewer, 1 Author)

Issue

  • JDK-8387073: C2: StoreNode::Ideal wrongly optimizes away scalar store before masked vector store (Bug - P4)(⚠️ The fixVersion in this issue is [27] but the fixVersion in .jcheck/conf is 28, a new backport will be created when this pr is integrated.)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 31695

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper

bridgekeeper Bot commented Jun 26, 2026

Copy link
Copy Markdown

👋 Welcome back mhaessig! 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 commented Jun 26, 2026

Copy link
Copy Markdown

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk openjdk Bot added the hotspot-compiler hotspot-compiler-dev@openjdk.org label Jun 26, 2026
@openjdk

openjdk Bot commented Jun 26, 2026

Copy link
Copy Markdown

@mhaessig 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 commented Jun 26, 2026

Copy link
Copy Markdown

The total number of required reviews for this PR has been set to 2 based on the presence of this label: hotspot-compiler. This can be overridden with the /reviewers command.

@openjdk openjdk Bot added the rfr Pull request is ready for review label Jun 26, 2026
@mlbridge

mlbridge Bot commented Jun 26, 2026

Copy link
Copy Markdown

Webrevs

@mhaessig mhaessig marked this pull request as draft June 26, 2026 14:59
@mhaessig mhaessig force-pushed the JDK-8387073-masked branch from 11644fc to 28eab9b Compare June 26, 2026 15:00
@openjdk openjdk Bot removed the rfr Pull request is ready for review label Jun 26, 2026
@mhaessig mhaessig marked this pull request as ready for review June 26, 2026 15:00
@openjdk openjdk Bot added the rfr Pull request is ready for review label Jun 26, 2026

@chhagedorn chhagedorn left a comment

Copy link
Copy Markdown
Member

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. Have you already filed a follow-up RFE to revisit it in JDK 28?

@eme64 eme64 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for tackling this one @mhaessig 😊

st->as_Store()->memory_size() <= this->memory_size()) {
st->as_Store()->memory_size() <= this->memory_size() &&
!this->is_StoreVectorMasked() && !this->is_StoreVectorScatterMasked() &&
!this->is_predicated_vector()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is is_predicated_vector actually required here? Or could it be an assert?

Comment on lines +38 to +48
static boolean[] boolArr = new boolean[] {true, true};
static byte[] byteArr = new byte[] {(byte)42};

static boolean[] testBool() {
return Arrays.copyOf(boolArr, 4);
}

static byte[] testByte() {
// Should be zero-padded, but we seem to get non-zero values.
return Arrays.copyOf(byteArr, 4);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It would be nice if we could have a bit more testing in this area.
I was super surprised that we have not caught this earlier.

Idea: look for similar methods, and test them all for random arrays and lengths (constant and variable).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also: maybe more types, including oop?

final List<TemplateToken> tests = new ArrayList<>();
tests.addAll(CodeGenerationDataNameType.VECTOR_VECTOR_TYPES
.stream()
.filter(vec -> vec.byteSize() <= maxVecByteSize)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do you do this filtering? At least a comment would be nice ;)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What I would recommend: guard the IR rules instead, using MaxVectorSize. That would allow us to still do result verification for the other cases.


if (st->in(MemNode::Address)->eqv_uncast(address) &&
st->as_Store()->memory_size() <= this->memory_size()) {
st->as_Store()->memory_size() <= this->memory_size() &&

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Optional: Can you also file an RFE to allow overlaps that don't require the address to be the same?
Neither of us probably has time, but I think we could probably find someone for it in the wider community ;)

Comment on lines +102 to +106
// For IR-verification we need more than two lanes. To prevent flakyness, we skip verification
// for vectors of longs because there are other StoreL nodes in this method.
if (vec.length <= 2 || vec.elementType.name().equals("long")) {
return scope("");
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we get the IR matching to be "sharper" by matching more parts of the dump string, somehow?

Comment on lines +110 to +126
// IR verification of vectors of int needs special handling due to an additional
// node in used for address computation when not using COH.
if (vec.elementType.name().equals("int")) {
return scope(
let("ptyIR", ptyIR),
"""
@IR(counts = {IRNode.STORE_#ptyIR, "= 1",
IRNode.VECTOR_STORE_MASK, "= 1"},
applyIf = {"UseCompactObjectHeaders", "true"},
applyIfCPUFeatureOr = {"avx512", "true", "sve", "true"})
@IR(counts = {IRNode.STORE_#ptyIR, "= 2",
IRNode.VECTOR_STORE_MASK, "= 1"},
applyIf = {"UseCompactObjectHeaders", "false"},
applyIfCPUFeatureOr = {"avx512", "true", "sve", "true"})
"""
);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you explain the IR shapes a bit more here? What exactly makes the difference why we end up with one or two stores?

Comment on lines +172 to +173
// Mask with one element set to false.
VectorMask<#boxedTy> mask = VectorMask.fromLong(#species, -1 - (1 << #idx));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Having a completely random mask would also be nice, no? Or does that mess with something?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe you need it to not constant fold away? I suppose we could also try with a local constant mask, and a mask that gets loaded from somewhere, i.e. a static field. To both cover possible constant folding and variable mask.

Comment on lines +175 to +176
var v = #vecTy.broadcast(#species, broadcastVal);
v.intoArray(a, 0, mask);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do you know why this is needed? I did not spend enough time on it when I found the reproducer. But it seems a bit strange to me.

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 rfr Pull request is ready for review

Development

Successfully merging this pull request may close these issues.

3 participants