Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/hotspot/share/oops/objArrayKlass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ ObjArrayKlass* ObjArrayKlass::klass_from_description(ArrayDescription ad, TRAPS)
element_klass()->validate_array_description(ad);

const ArrayProperties props = ad._properties;
assert(props.is_valid(), "must be");

if (properties() == props && kind() == ad._kind) {
assert(is_refined_objArray_klass(), "Must be a refined array klass");
Expand All @@ -405,13 +406,15 @@ ObjArrayKlass* ObjArrayKlass::klass_from_description(ArrayDescription ad, TRAPS)

if (next_refined_array_klass() == nullptr) {
ObjArrayKlass* first = this;
if (is_unrefined_objArray_klass() && props != ArrayProperties::Default()) {
assert(props.is_valid(), "must be");
if (is_unrefined_objArray_klass()) {
// Make sure that the first entry in the linked list is always the default refined klass because
// C2 relies on this for a fast lookup (see LibraryCallKit::load_default_refined_array_klass).
ArrayDescription default_ad = array_layout_selection(element_klass(), ArrayProperties::Default());
first = allocate_klass_from_description(default_ad, CHECK_NULL);
release_set_next_refined_klass(first);
if (default_ad._kind != ad._kind || default_ad._properties != ad._properties
|| default_ad._layout_kind != ad._layout_kind) {
first = allocate_klass_from_description(default_ad, CHECK_NULL);
release_set_next_refined_klass(first);
}
}
ak = allocate_klass_from_description(ad, CHECK_NULL);
first->release_set_next_refined_klass(ak);
Expand Down
43 changes: 5 additions & 38 deletions src/hotspot/share/opto/library_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5058,43 +5058,10 @@ bool LibraryCallKit::inline_array_copyOf(bool is_copyOfRange) {
generate_negative_guard(length, bailout, &length);

// Handle inline type arrays
bool can_validate = !too_many_traps(Deoptimization::Reason_class_check);
if (!stopped()) {
// TODO 8251971
if (!orig_t->is_null_free()) {
// Not statically known to be null free, add a check
generate_fair_guard(null_free_array_test(original), bailout);
}
orig_t = _gvn.type(original)->isa_aryptr();
if (orig_t != nullptr && orig_t->is_flat()) {
// Src is flat, check that dest is flat as well
if (exclude_flat) {
// Dest can't be flat, bail out
bailout->add_req(control());
set_control(top());
} else {
generate_fair_guard(flat_array_test(refined_klass_node, /* flat = */ false), bailout);
}
// TODO 8251971 This is not correct anymore. Write tests and fix logic similar to arraycopy.
} else if (UseArrayFlattening && (orig_t == nullptr || !orig_t->is_not_flat()) &&
// If dest is flat, src must be flat as well (guaranteed by src <: dest check if validated).
((!tklass->is_flat() && tklass->can_be_inline_array()) || !can_validate)) {
// Src might be flat and dest might not be flat. Go to the slow path if src is flat.
// TODO 8251971: Optimize for the case when src/dest are later found to be both flat.
generate_fair_guard(flat_array_test(load_object_klass(original)), bailout);
if (orig_t != nullptr) {
orig_t = orig_t->cast_to_not_flat();
original = _gvn.transform(new CheckCastPPNode(control(), original, orig_t));
}
}
if (!can_validate) {
// No validation. The subtype check emitted at macro expansion time will not go to the slow
// path but call checkcast_arraycopy which can not handle flat/null-free inline type arrays.
// TODO 8251971: Optimize for the case when src/dest are later found to be both flat/null-free.
generate_fair_guard(flat_array_test(refined_klass_node), bailout);
generate_fair_guard(null_free_array_test(original), bailout);
}
}
// TODO 8251971 This is too strong
generate_fair_guard(flat_array_test(load_object_klass(original)), bailout);
generate_fair_guard(flat_array_test(refined_klass_node), bailout);
generate_fair_guard(null_free_array_test(original), bailout);

// Bail out if start is larger than the original length
Node* orig_tail = _gvn.transform(new SubINode(orig_length, start));
Expand Down Expand Up @@ -5141,7 +5108,7 @@ bool LibraryCallKit::inline_array_copyOf(bool is_copyOfRange) {
bool validated = false;
// Reason_class_check rather than Reason_intrinsic because we
// want to intrinsify even if this traps.
if (can_validate) {
if (!too_many_traps(Deoptimization::Reason_class_check)) {
Node* not_subtype_ctrl = gen_subtype_check(original, klass_node);

if (not_subtype_ctrl != top()) {
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/opto/memnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2932,7 +2932,7 @@ Node* LoadNode::klass_identity_common(PhaseGVN* phase) {
// introducing a new debug info operator for Klass.java_mirror).
//
// This optimization does not apply to arrays because if k is not a
// constant, it was obtained via load_klass which returns the VM type
// constant, it was obtained via load_klass which returns the refined type
// and '.java_mirror.as_klass' should return the Java type instead.

if (toop->isa_instptr() && toop->is_instptr()->instance_klass() == phase->C->env()->Class_klass()
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/share/opto/subnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1636,8 +1636,8 @@ Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
Node *cmp = in(1);
if( !cmp->is_Sub() ) return nullptr;
int cop = cmp->Opcode();
if( cop == Op_FastLock || cop == Op_FastUnlock ||
cmp->is_SubTypeCheck() || cop == Op_VectorTest ) {
if (cop == Op_FastLock || cop == Op_FastUnlock || cop == Op_FlatArrayCheck ||
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Unrelated fix for an issue I hit during debugging.

cmp->is_SubTypeCheck() || cop == Op_VectorTest) {
return nullptr;
}
Node *cmp1 = cmp->in(1);
Expand Down
30 changes: 17 additions & 13 deletions test/hotspot/jtreg/compiler/valhalla/inlinetypes/TestArrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -2808,10 +2808,11 @@ public void test109_null_verifier() {

// Arrays.copyOf with constant source and destination arrays
@Test
@IR(applyIf = {"UseArrayFlattening", "true"},
counts = {INTRINSIC_OR_TYPE_CHECKED_INLINING_TRAP, "= 1"})
@IR(applyIf = {"UseArrayFlattening", "false"},
failOn = {INTRINSIC_OR_TYPE_CHECKED_INLINING_TRAP, CLASS_CHECK_TRAP})
// TODO 8251971 Re-enable
// @IR(applyIf = {"UseArrayFlattening", "true"},
// counts = {INTRINSIC_OR_TYPE_CHECKED_INLINING_TRAP, "= 1"})
// @IR(applyIf = {"UseArrayFlattening", "false"},
// failOn = {INTRINSIC_OR_TYPE_CHECKED_INLINING_TRAP, CLASS_CHECK_TRAP})
public Object[] test110() {
return Arrays.copyOf(val_src, 8, Object[].class);
}
Expand Down Expand Up @@ -2878,10 +2879,11 @@ public void test113_null_verifier() {
// after the arraycopy intrinsic is emitted (with incremental inlining).

@Test
@IR(applyIf = {"UseArrayFlattening", "true"},
counts = {INTRINSIC_OR_TYPE_CHECKED_INLINING_TRAP, "= 1"})
@IR(applyIf = {"UseArrayFlattening", "false"},
failOn = {INTRINSIC_OR_TYPE_CHECKED_INLINING_TRAP, CLASS_CHECK_TRAP})
// TODO 8251971 Re-enable
// @IR(applyIf = {"UseArrayFlattening", "true"},
// counts = {INTRINSIC_OR_TYPE_CHECKED_INLINING_TRAP, "= 1"})
// @IR(applyIf = {"UseArrayFlattening", "false"},
// failOn = {INTRINSIC_OR_TYPE_CHECKED_INLINING_TRAP, CLASS_CHECK_TRAP})
public Object[] test114() {
return Arrays.copyOf((Object[])get_val_src(), 8, get_obj_class());
}
Expand All @@ -2895,8 +2897,9 @@ public void test114_verifier() {
// TODO 8251971: Should be optimized but we are bailing out because
// at parse time it looks as if src could be flat and dst could be not flat
@Test
@IR(applyIf = {"UseArrayFlattening", "false"},
failOn = {INTRINSIC_OR_TYPE_CHECKED_INLINING_TRAP, CLASS_CHECK_TRAP})
// TODO 8251971 Re-enable
// @IR(applyIf = {"UseArrayFlattening", "false"},
// failOn = {INTRINSIC_OR_TYPE_CHECKED_INLINING_TRAP, CLASS_CHECK_TRAP})
public Object[] test115() {
return Arrays.copyOf((Object[])get_val_src(), 8, get_val_class());
}
Expand Down Expand Up @@ -3194,9 +3197,10 @@ static void verifyTest127a(Object[] src, Object[] dst, int len, int offset) {

// Verify that copyOf with known source and unknown destination class is optimized
@Test
@IR(applyIf = {"UseArrayFlattening", "true"},
counts = {JLONG_ARRAYCOPY, "= 1"},
failOn = CHECKCAST_ARRAYCOPY)
// TODO 8251971 Re-enable
//@IR(applyIf = {"UseArrayFlattening", "true"},
// counts = {JLONG_ARRAYCOPY, "= 1"},
// failOn = CHECKCAST_ARRAYCOPY)
public Object[] test128(Class klass) {
return Arrays.copyOf(val_src, 8, klass);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
* Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package compiler.valhalla.inlinetypes;

import java.lang.reflect.Array;
import java.util.Arrays;

import jdk.internal.value.ValueClass;
import jdk.internal.vm.annotation.LooselyConsistentValue;
import jdk.test.lib.Asserts;
import jdk.test.lib.Utils;
import jdk.test.whitebox.WhiteBox;

/*
* @test
* @key randomness
* @summary Test loading of the default refined array klass from C2 intrinsics.
* @library /test/lib /
* @enablePreview
* @modules java.base/jdk.internal.value
* java.base/jdk.internal.vm.annotation
* @build jdk.test.whitebox.WhiteBox
* @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
* ${test.main.class}
* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
* -XX:-TieredCompilation -Xbatch
* -XX:CompileCommand=dontinline,*::test*
* ${test.main.class}
* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
* -XX:+IgnoreUnrecognizedVMOptions -XX:+StressReflectiveCode
* -XX:CompileCommand=dontinline,*::test*
* ${test.main.class}
*/

public class TestLoadRefinedArrayKlass {
private static final WhiteBox WHITEBOX = WhiteBox.getWhiteBox();
private static final boolean UseArrayFlattening = WHITEBOX.getBooleanVMFlag("UseArrayFlattening");
private static final boolean UseNullableAtomicValueFlattening = WHITEBOX.getBooleanVMFlag("UseNullableAtomicValueFlattening");
private static final boolean UseNullFreeAtomicValueFlattening = WHITEBOX.getBooleanVMFlag("UseNullFreeAtomicValueFlattening");
private static final boolean ForceNonTearable = !WHITEBOX.getStringVMFlag("ForceNonTearable").equals("");

@LooselyConsistentValue
static value class MySimpleValue {
int i;
byte b;

public MySimpleValue(int i) {
this.i = i;
this.b = (byte)i;
}
}

public static void main(String[] args) {
// Randomly trigger creation of a refined array klass of MySimpleValue[] to make
// sure that the order in which they are created does not make a difference.
switch (Utils.getRandomInstance().nextInt(5)) {
case 0:
Object tmp = new MySimpleValue[10];
break;
case 1:
ValueClass.newReferenceArray(MySimpleValue.class, 10);
break;
case 2:
ValueClass.newNullableAtomicArray(MySimpleValue.class, 10);
break;
case 3:
ValueClass.newNullRestrictedNonAtomicArray(MySimpleValue.class, 10, new MySimpleValue(0));
break;
case 4:
ValueClass.newNullRestrictedAtomicArray(MySimpleValue.class, 10, new MySimpleValue(0));
break;
}

Object[] objArray = new Object[10];
MySimpleValue[] defaultArray = new MySimpleValue[10];
MySimpleValue[] refArray = (MySimpleValue[])ValueClass.newReferenceArray(MySimpleValue.class, 10);
MySimpleValue[] nullableAtomicArray = (MySimpleValue[])ValueClass.newNullableAtomicArray(MySimpleValue.class, 10);
MySimpleValue[] nullFreeNonAtomicArray = (MySimpleValue[])ValueClass.newNullRestrictedNonAtomicArray(MySimpleValue.class, 10, new MySimpleValue(0));
MySimpleValue[] nullFreeAtomicArray = (MySimpleValue[])ValueClass.newNullRestrictedAtomicArray(MySimpleValue.class, 10, new MySimpleValue(0));

Asserts.assertEquals(ValueClass.isFlatArray(defaultArray), UseArrayFlattening && UseNullableAtomicValueFlattening);
Asserts.assertFalse(ValueClass.isFlatArray(refArray));
Asserts.assertEquals(ValueClass.isFlatArray(nullableAtomicArray), UseArrayFlattening && UseNullableAtomicValueFlattening);
Asserts.assertEquals(ValueClass.isFlatArray(nullFreeNonAtomicArray), UseArrayFlattening && UseNullFreeAtomicValueFlattening);
Asserts.assertEquals(ValueClass.isFlatArray(nullFreeAtomicArray), UseArrayFlattening && UseNullFreeAtomicValueFlattening);

Asserts.assertFalse(ValueClass.isNullRestrictedArray(defaultArray));
Asserts.assertFalse(ValueClass.isNullRestrictedArray(refArray));
Asserts.assertFalse(ValueClass.isNullRestrictedArray(nullableAtomicArray));
Asserts.assertTrue(ValueClass.isNullRestrictedArray(nullFreeNonAtomicArray));
Asserts.assertTrue(ValueClass.isNullRestrictedArray(nullFreeAtomicArray));

Asserts.assertTrue(ValueClass.isAtomicArray(defaultArray));
Asserts.assertTrue(ValueClass.isAtomicArray(refArray));
Asserts.assertTrue(ValueClass.isAtomicArray(nullableAtomicArray));
Asserts.assertEquals(ValueClass.isAtomicArray(nullFreeNonAtomicArray), ForceNonTearable || !UseArrayFlattening);
Asserts.assertTrue(ValueClass.isAtomicArray(nullFreeAtomicArray));

for (int i = 0; i < objArray.length; i++) {
objArray[i] = new MySimpleValue(i);
defaultArray[i] = new MySimpleValue(i);
refArray[i] = new MySimpleValue(i);
nullableAtomicArray[i] = new MySimpleValue(i);
nullFreeNonAtomicArray[i] = new MySimpleValue(i);
nullFreeAtomicArray[i] = new MySimpleValue(i);
}

for (int i = 0; i < 50_000; i++) {
// Test _copyOf intrinsic
Object[] res = testCopyOf(objArray, MySimpleValue[].class);
checkArray(res, true, false, true);

res = testCopyOf(defaultArray, MySimpleValue[].class);
checkArray(res, true, false, true);

res = testCopyOf(refArray, MySimpleValue[].class);
checkArray(res, true, false, true);

res = testCopyOf(nullableAtomicArray, MySimpleValue[].class);
checkArray(res, true, false, true);

res = testCopyOf(nullFreeNonAtomicArray, MySimpleValue[].class);
checkArray(res, true, true, ForceNonTearable || !UseArrayFlattening);

res = testCopyOf(nullFreeAtomicArray, MySimpleValue[].class);
checkArray(res, true, true, true);

res = testCopyOf(objArray, Object[].class);
checkArray(res, false, false, true);

res = testCopyOf(defaultArray, Object[].class);
checkArray(res, false, false, true);

res = testCopyOf(refArray, Object[].class);
checkArray(res, false, false, true);

res = testCopyOf(nullableAtomicArray, Object[].class);
checkArray(res, false, false, true);

res = testCopyOf(nullFreeNonAtomicArray, Object[].class);
checkArray(res, false, false, true);

res = testCopyOf(nullFreeAtomicArray, Object[].class);
checkArray(res, false, false, true);

// Test _newArray intrinsic
res = testNewInstance(MySimpleValue.class, 10);
Asserts.assertEquals(ValueClass.isFlatArray(res), UseArrayFlattening && UseNullableAtomicValueFlattening);
Asserts.assertFalse(ValueClass.isNullRestrictedArray(res));
Asserts.assertTrue(ValueClass.isAtomicArray(res));
}
}

static <T> T[] testCopyOf(Object[] array, Class<? extends T[]> c) {
return Arrays.copyOf(array, array.length, c);
}

static void checkArray(Object[] array, boolean isValue, boolean nullRestricted, boolean atomic) {
Asserts.assertEquals(ValueClass.isFlatArray(array), isValue && UseArrayFlattening && (nullRestricted ? UseNullFreeAtomicValueFlattening : UseNullableAtomicValueFlattening));
Asserts.assertEquals(ValueClass.isNullRestrictedArray(array), nullRestricted);
Asserts.assertEquals(ValueClass.isAtomicArray(array), atomic);
for (int i = 0; i < array.length; i++) {
MySimpleValue value = (MySimpleValue)array[i];
Asserts.assertEquals(value.i, i);
Asserts.assertEquals(value.b, (byte)i);
}
}

static Object[] testNewInstance(Class<?> c, int len) {
return (Object[])Array.newInstance(c, len);
}
}