Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/*
* 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}
*/

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);
}
}