Skip to content
Open
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
9 changes: 6 additions & 3 deletions src/hotspot/cpu/x86/c2_MacroAssembler_x86.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2101,8 +2101,8 @@ void C2_MacroAssembler::mulreduce16B(int opcode, Register dst, Register src1, XM
} else {
pmovsxbw(vtmp2, src2);
reduce8S(opcode, dst, src1, vtmp2, vtmp1, vtmp2);
pshufd(vtmp2, src2, 0x1);
pmovsxbw(vtmp2, src2);
pshufd(vtmp2, src2, 0xe);
pmovsxbw(vtmp2, vtmp2);
reduce8S(opcode, dst, dst, vtmp2, vtmp1, vtmp2);
}
}
Expand All @@ -2111,7 +2111,7 @@ void C2_MacroAssembler::mulreduce32B(int opcode, Register dst, Register src1, XM
if (UseAVX > 2 && VM_Version::supports_avx512bw()) {
int vector_len = Assembler::AVX_512bit;
vpmovsxbw(vtmp1, src2, vector_len);
reduce32S(opcode, dst, src1, vtmp1, vtmp1, vtmp2);
reduce32S(opcode, dst, src1, vtmp1, vtmp2, vtmp1);
} else {
assert(UseAVX >= 2,"Should not reach here.");
mulreduce16B(opcode, dst, src1, src2, vtmp1, vtmp2);
Expand Down Expand Up @@ -2154,6 +2154,7 @@ void C2_MacroAssembler::reduce8S(int opcode, Register dst, Register src1, XMMReg
}
phaddw(vtmp1, src2);
} else {
assert_different_registers(src2, vtmp1);
pshufd(vtmp1, src2, 0xE);
reduce_operation_128(T_SHORT, opcode, vtmp1, src2);
}
Expand All @@ -2166,13 +2167,15 @@ void C2_MacroAssembler::reduce16S(int opcode, Register dst, Register src1, XMMRe
vphaddw(vtmp2, src2, src2, vector_len);
vpermq(vtmp2, vtmp2, 0xD8, vector_len);
} else {
assert_different_registers(src2, vtmp2);
vextracti128_high(vtmp2, src2);
reduce_operation_128(T_SHORT, opcode, vtmp2, src2);
}
reduce8S(opcode, dst, src1, vtmp2, vtmp1, vtmp2);
}

void C2_MacroAssembler::reduce32S(int opcode, Register dst, Register src1, XMMRegister src2, XMMRegister vtmp1, XMMRegister vtmp2) {
assert_different_registers(src2, vtmp1);
int vector_len = Assembler::AVX_256bit;
vextracti64x4_high(vtmp1, src2);
reduce_operation_256(T_SHORT, opcode, vtmp1, vtmp1, src2);
Expand Down
133 changes: 133 additions & 0 deletions test/hotspot/jtreg/compiler/vectorapi/TestMultiplyReductionByte.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* 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.vectorapi;

import compiler.lib.ir_framework.*;

import java.util.Arrays;

import jdk.incubator.vector.ByteVector;
import jdk.incubator.vector.VectorOperators;

import jdk.test.lib.Asserts;
import jdk.test.lib.Utils;

/**
* @test
* @bug 8378250
* @summary Verify correctness of byte vector MUL reduction across all species.
* A register aliasing bug in mulreduce32B caused the upper half of
* sign-extended data to overwrite the source, producing wrong results
* when most lanes are 1 and a single lane differs.
* @library /test/lib /
* @modules jdk.incubator.vector
* @run driver ${test.main.class}
*/
public class TestMultiplyReductionByte {

static byte[] input = new byte[64];

static int pos = Utils.getRandomInstance().nextInt(input.length);

static {
Arrays.fill(input, (byte) 1);
input[pos] = -3;
}

@Test
@IR(counts = {IRNode.MUL_REDUCTION_VI, ">=1"},
applyIfCPUFeatureOr = {"avx", "true", "asimd", "true"},
applyIf = {"MaxVectorSize", ">=8"})
static byte testMulReduce64() {
return ByteVector.fromArray(ByteVector.SPECIES_64, input, 0)
.reduceLanes(VectorOperators.MUL);
}

@Run(test = "testMulReduce64")
static void runMulReduce64() {
input[pos] = 1;
pos = (pos + 1) % ByteVector.SPECIES_64.length();
input[pos] = -3;
byte result = testMulReduce64();
Asserts.assertEquals((byte) -3, result, "MUL reduction (64-bit), pos=" + pos);
}

@Test
@IR(counts = {IRNode.MUL_REDUCTION_VI, ">=1"},
applyIfCPUFeatureOr = {"avx", "true", "asimd", "true"},
applyIf = {"MaxVectorSize", ">=16"})
static byte testMulReduce128() {
return ByteVector.fromArray(ByteVector.SPECIES_128, input, 0)
.reduceLanes(VectorOperators.MUL);
}

@Run(test = "testMulReduce128")
static void runMulReduce128() {
input[pos] = 1;
pos = (pos + 1) % ByteVector.SPECIES_128.length();
input[pos] = -3;
byte result = testMulReduce128();
Asserts.assertEquals((byte) -3, result, "MUL reduction (128-bit), pos=" + pos);
}

@Test
@IR(counts = {IRNode.MUL_REDUCTION_VI, ">=1"},
applyIfCPUFeatureOr = {"avx2", "true", "asimd", "true"},
applyIf = {"MaxVectorSize", ">=32"})
static byte testMulReduce256() {
return ByteVector.fromArray(ByteVector.SPECIES_256, input, 0)
.reduceLanes(VectorOperators.MUL);
}

@Run(test = "testMulReduce256")
static void runMulReduce256() {
input[pos] = 1;
pos = (pos + 1) % ByteVector.SPECIES_256.length();
input[pos] = -3;
byte result = testMulReduce256();
Asserts.assertEquals((byte) -3, result, "MUL reduction (256-bit), pos=" + pos);
}

@Test
@IR(counts = {IRNode.MUL_REDUCTION_VI, ">=1"},
applyIfCPUFeatureOr = {"avx512f", "true", "asimd", "true"},
applyIf = {"MaxVectorSize", ">=64"})
static byte testMulReduce512() {
return ByteVector.fromArray(ByteVector.SPECIES_512, input, 0)
.reduceLanes(VectorOperators.MUL);
}

@Run(test = "testMulReduce512")
static void runMulReduce512() {
input[pos] = 1;
pos = (pos + 1) % ByteVector.SPECIES_512.length();
input[pos] = -3;
byte result = testMulReduce512();
Asserts.assertEquals((byte) -3, result, "MUL reduction (512-bit), pos=" + pos);
}

public static void main(String[] args) {
TestFramework.runWithFlags("--add-modules=jdk.incubator.vector");
}
}
4 changes: 4 additions & 0 deletions test/jdk/jdk/incubator/vector/Byte128VectorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,10 @@ static byte bits(byte e) {
return fill(s * BUFFER_REPS,
i -> (((byte)(i + 1) == 0) ? 1 : (byte)(i + 1)));
}),
withToString("byte[smallOddValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (byte)(i % 7 == 0 ? -3 : (i % 3 == 0 ? -1 : 1)));
}),
withToString("byte[cornerCaseValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> cornerCaseValue(i));
Expand Down
4 changes: 4 additions & 0 deletions test/jdk/jdk/incubator/vector/Byte256VectorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,10 @@ static byte bits(byte e) {
return fill(s * BUFFER_REPS,
i -> (((byte)(i + 1) == 0) ? 1 : (byte)(i + 1)));
}),
withToString("byte[smallOddValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (byte)(i % 7 == 0 ? -3 : (i % 3 == 0 ? -1 : 1)));
}),
withToString("byte[cornerCaseValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> cornerCaseValue(i));
Expand Down
4 changes: 4 additions & 0 deletions test/jdk/jdk/incubator/vector/Byte512VectorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,10 @@ static byte bits(byte e) {
return fill(s * BUFFER_REPS,
i -> (((byte)(i + 1) == 0) ? 1 : (byte)(i + 1)));
}),
withToString("byte[smallOddValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (byte)(i % 7 == 0 ? -3 : (i % 3 == 0 ? -1 : 1)));
}),
withToString("byte[cornerCaseValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> cornerCaseValue(i));
Expand Down
4 changes: 4 additions & 0 deletions test/jdk/jdk/incubator/vector/Byte64VectorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,10 @@ static byte bits(byte e) {
return fill(s * BUFFER_REPS,
i -> (((byte)(i + 1) == 0) ? 1 : (byte)(i + 1)));
}),
withToString("byte[smallOddValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (byte)(i % 7 == 0 ? -3 : (i % 3 == 0 ? -1 : 1)));
}),
withToString("byte[cornerCaseValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> cornerCaseValue(i));
Expand Down
4 changes: 4 additions & 0 deletions test/jdk/jdk/incubator/vector/ByteMaxVectorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,10 @@ static byte bits(byte e) {
return fill(s * BUFFER_REPS,
i -> (((byte)(i + 1) == 0) ? 1 : (byte)(i + 1)));
}),
withToString("byte[smallOddValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (byte)(i % 7 == 0 ? -3 : (i % 3 == 0 ? -1 : 1)));
}),
withToString("byte[cornerCaseValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> cornerCaseValue(i));
Expand Down
4 changes: 4 additions & 0 deletions test/jdk/jdk/incubator/vector/Double128VectorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,10 @@ static long bits(double e) {
return fill(s * BUFFER_REPS,
i -> (((double)(i + 1) == 0) ? 1 : (double)(i + 1)));
}),
withToString("double[smallOddValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (double)(i % 7 == 0 ? -3 : (i % 3 == 0 ? -1 : 1)));
}),
withToString("double[0.01 + (i / (i + 1))]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (double)0.01 + ((double)i / (i + 1)));
Expand Down
4 changes: 4 additions & 0 deletions test/jdk/jdk/incubator/vector/Double256VectorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,10 @@ static long bits(double e) {
return fill(s * BUFFER_REPS,
i -> (((double)(i + 1) == 0) ? 1 : (double)(i + 1)));
}),
withToString("double[smallOddValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (double)(i % 7 == 0 ? -3 : (i % 3 == 0 ? -1 : 1)));
}),
withToString("double[0.01 + (i / (i + 1))]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (double)0.01 + ((double)i / (i + 1)));
Expand Down
4 changes: 4 additions & 0 deletions test/jdk/jdk/incubator/vector/Double512VectorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,10 @@ static long bits(double e) {
return fill(s * BUFFER_REPS,
i -> (((double)(i + 1) == 0) ? 1 : (double)(i + 1)));
}),
withToString("double[smallOddValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (double)(i % 7 == 0 ? -3 : (i % 3 == 0 ? -1 : 1)));
}),
withToString("double[0.01 + (i / (i + 1))]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (double)0.01 + ((double)i / (i + 1)));
Expand Down
4 changes: 4 additions & 0 deletions test/jdk/jdk/incubator/vector/Double64VectorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,10 @@ static long bits(double e) {
return fill(s * BUFFER_REPS,
i -> (((double)(i + 1) == 0) ? 1 : (double)(i + 1)));
}),
withToString("double[smallOddValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (double)(i % 7 == 0 ? -3 : (i % 3 == 0 ? -1 : 1)));
}),
withToString("double[0.01 + (i / (i + 1))]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (double)0.01 + ((double)i / (i + 1)));
Expand Down
4 changes: 4 additions & 0 deletions test/jdk/jdk/incubator/vector/DoubleMaxVectorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,10 @@ static long bits(double e) {
return fill(s * BUFFER_REPS,
i -> (((double)(i + 1) == 0) ? 1 : (double)(i + 1)));
}),
withToString("double[smallOddValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (double)(i % 7 == 0 ? -3 : (i % 3 == 0 ? -1 : 1)));
}),
withToString("double[0.01 + (i / (i + 1))]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (double)0.01 + ((double)i / (i + 1)));
Expand Down
4 changes: 4 additions & 0 deletions test/jdk/jdk/incubator/vector/Float128VectorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,10 @@ static int bits(float e) {
return fill(s * BUFFER_REPS,
i -> (((float)(i + 1) == 0) ? 1 : (float)(i + 1)));
}),
withToString("float[smallOddValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (float)(i % 7 == 0 ? -3 : (i % 3 == 0 ? -1 : 1)));
}),
withToString("float[0.01 + (i / (i + 1))]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (float)0.01 + ((float)i / (i + 1)));
Expand Down
4 changes: 4 additions & 0 deletions test/jdk/jdk/incubator/vector/Float256VectorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,10 @@ static int bits(float e) {
return fill(s * BUFFER_REPS,
i -> (((float)(i + 1) == 0) ? 1 : (float)(i + 1)));
}),
withToString("float[smallOddValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (float)(i % 7 == 0 ? -3 : (i % 3 == 0 ? -1 : 1)));
}),
withToString("float[0.01 + (i / (i + 1))]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (float)0.01 + ((float)i / (i + 1)));
Expand Down
4 changes: 4 additions & 0 deletions test/jdk/jdk/incubator/vector/Float512VectorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,10 @@ static int bits(float e) {
return fill(s * BUFFER_REPS,
i -> (((float)(i + 1) == 0) ? 1 : (float)(i + 1)));
}),
withToString("float[smallOddValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (float)(i % 7 == 0 ? -3 : (i % 3 == 0 ? -1 : 1)));
}),
withToString("float[0.01 + (i / (i + 1))]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (float)0.01 + ((float)i / (i + 1)));
Expand Down
4 changes: 4 additions & 0 deletions test/jdk/jdk/incubator/vector/Float64VectorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,10 @@ static int bits(float e) {
return fill(s * BUFFER_REPS,
i -> (((float)(i + 1) == 0) ? 1 : (float)(i + 1)));
}),
withToString("float[smallOddValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (float)(i % 7 == 0 ? -3 : (i % 3 == 0 ? -1 : 1)));
}),
withToString("float[0.01 + (i / (i + 1))]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (float)0.01 + ((float)i / (i + 1)));
Expand Down
4 changes: 4 additions & 0 deletions test/jdk/jdk/incubator/vector/FloatMaxVectorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,10 @@ static int bits(float e) {
return fill(s * BUFFER_REPS,
i -> (((float)(i + 1) == 0) ? 1 : (float)(i + 1)));
}),
withToString("float[smallOddValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (float)(i % 7 == 0 ? -3 : (i % 3 == 0 ? -1 : 1)));
}),
withToString("float[0.01 + (i / (i + 1))]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (float)0.01 + ((float)i / (i + 1)));
Expand Down
4 changes: 4 additions & 0 deletions test/jdk/jdk/incubator/vector/Int128VectorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,10 @@ static int bits(int e) {
return fill(s * BUFFER_REPS,
i -> (((int)(i + 1) == 0) ? 1 : (int)(i + 1)));
}),
withToString("int[smallOddValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (int)(i % 7 == 0 ? -3 : (i % 3 == 0 ? -1 : 1)));
}),
withToString("int[cornerCaseValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> cornerCaseValue(i));
Expand Down
4 changes: 4 additions & 0 deletions test/jdk/jdk/incubator/vector/Int256VectorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,10 @@ static int bits(int e) {
return fill(s * BUFFER_REPS,
i -> (((int)(i + 1) == 0) ? 1 : (int)(i + 1)));
}),
withToString("int[smallOddValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (int)(i % 7 == 0 ? -3 : (i % 3 == 0 ? -1 : 1)));
}),
withToString("int[cornerCaseValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> cornerCaseValue(i));
Expand Down
4 changes: 4 additions & 0 deletions test/jdk/jdk/incubator/vector/Int512VectorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,10 @@ static int bits(int e) {
return fill(s * BUFFER_REPS,
i -> (((int)(i + 1) == 0) ? 1 : (int)(i + 1)));
}),
withToString("int[smallOddValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (int)(i % 7 == 0 ? -3 : (i % 3 == 0 ? -1 : 1)));
}),
withToString("int[cornerCaseValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> cornerCaseValue(i));
Expand Down
4 changes: 4 additions & 0 deletions test/jdk/jdk/incubator/vector/Int64VectorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,10 @@ static int bits(int e) {
return fill(s * BUFFER_REPS,
i -> (((int)(i + 1) == 0) ? 1 : (int)(i + 1)));
}),
withToString("int[smallOddValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (int)(i % 7 == 0 ? -3 : (i % 3 == 0 ? -1 : 1)));
}),
withToString("int[cornerCaseValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> cornerCaseValue(i));
Expand Down
4 changes: 4 additions & 0 deletions test/jdk/jdk/incubator/vector/IntMaxVectorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,10 @@ static int bits(int e) {
return fill(s * BUFFER_REPS,
i -> (((int)(i + 1) == 0) ? 1 : (int)(i + 1)));
}),
withToString("int[smallOddValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> (int)(i % 7 == 0 ? -3 : (i % 3 == 0 ? -1 : 1)));
}),
withToString("int[cornerCaseValue(i)]", (int s) -> {
return fill(s * BUFFER_REPS,
i -> cornerCaseValue(i));
Expand Down
Loading