diff --git a/src/hotspot/share/opto/vectorIntrinsics.cpp b/src/hotspot/share/opto/vectorIntrinsics.cpp index cee221c8b75..139dd62dc28 100644 --- a/src/hotspot/share/opto/vectorIntrinsics.cpp +++ b/src/hotspot/share/opto/vectorIntrinsics.cpp @@ -1037,8 +1037,20 @@ bool LibraryCallKit::inline_vector_mem_operation(bool is_store) { const bool needs_cpu_membar = is_mixed_access || is_mismatched_access; - bool mismatched_ms = from_ms->get_con() && !is_mask && arr_type != nullptr && arr_type->elem()->array_element_basic_type() != elem_bt; + // For non-masked mismatched memory segment vector read/write accesses, intrinsification can continue + // with unknown backing storage type and compiler can skip inserting explicit reinterpretation IR after + // loading from or before storing to backing storage which is mandatory for semantic correctness of + // big-endian memory layout. + bool mismatched_ms = LITTLE_ENDIAN_ONLY(false) + BIG_ENDIAN_ONLY(from_ms->get_con() && !is_mask && arr_type != nullptr && + arr_type->elem()->array_element_basic_type() != elem_bt); BasicType mem_elem_bt = mismatched_ms ? arr_type->elem()->array_element_basic_type() : elem_bt; + if (!is_java_primitive(mem_elem_bt)) { + if (C->print_intrinsics()) { + tty->print_cr(" ** non-primitive array element type"); + } + return false; + } int mem_num_elem = mismatched_ms ? (num_elem * type2aelembytes(elem_bt)) / type2aelembytes(mem_elem_bt) : num_elem; if (arr_type != nullptr && !is_mask && !elem_consistent_with_arr(elem_bt, arr_type, mismatched_ms)) { if (C->print_intrinsics()) { diff --git a/test/hotspot/jtreg/compiler/vectorapi/TestBiMorphicMismatchedMemSegment.java b/test/hotspot/jtreg/compiler/vectorapi/TestBiMorphicMismatchedMemSegment.java new file mode 100644 index 00000000000..efe1d377454 --- /dev/null +++ b/test/hotspot/jtreg/compiler/vectorapi/TestBiMorphicMismatchedMemSegment.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2024, 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.vectorapi; + +/* + * @test + * @bug 8329555 + * @modules jdk.incubator.vector + * + * @run main/othervm -Xbatch -XX:+TieredCompilation compiler.vectorapi.TestBiMorphicMismatchedMemSegment + */ + + +import jdk.incubator.vector.ByteVector; + +import java.lang.foreign.MemorySegment; +import java.nio.ByteOrder; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicLong; +import java.util.stream.IntStream; + +public class TestBiMorphicMismatchedMemSegment { + public static void main(String[] args) { + AtomicLong aLong = new AtomicLong(); + + IntStream.range(0, 10000).forEach(j -> { + byte[] bytes = new byte[64]; + ThreadLocalRandom.current().nextBytes(bytes); + var byteSegment = MemorySegment.ofArray(bytes); + var byteFragment = ByteVector.SPECIES_PREFERRED.fromMemorySegment(byteSegment, 0, ByteOrder.LITTLE_ENDIAN); + float[] floats = new float[128]; + byte[] targetBytes = new byte[512]; + var floatSegment = MemorySegment.ofArray(floats); + var targetByteSegment = MemorySegment.ofArray(targetBytes); + byteFragment.intoMemorySegment(floatSegment, ThreadLocalRandom.current().nextInt(0, 448), ByteOrder.LITTLE_ENDIAN); + byteFragment.intoMemorySegment(targetByteSegment, ThreadLocalRandom.current().nextInt(0, 448), ByteOrder.LITTLE_ENDIAN); + var l = 0; + for (int i = 0; i < floats.length; i++) { + l += (int) floats[i]; + } + aLong.addAndGet(l); + }); + + System.out.println(aLong.get()); + } +}