/* * Copyright (c) 2025, 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. */ /* * @test * @bug 8373026 * @summary Test auto vectorization and Vector API with some vector * algorithms. Related benchmark: VectorAlgorithms.java * @library /test/lib / * @modules jdk.incubator.vector * @run driver ${test.main.class} */ package compiler.vectorization; import java.util.Map; import java.util.HashMap; import jdk.test.lib.Utils; import java.util.Random; import compiler.lib.ir_framework.*; import compiler.lib.generators.*; import static compiler.lib.generators.Generators.G; import compiler.lib.verify.*; /** * The goal of this benchmark is to show the power of auto vectorization * and the Vector API. * * Please only modify this benchark in synchronization with the JMH benchmark: * micro/org/openjdk/bench/vm/compiler/VectorAlgorithms.java */ public class TestVectorAlgorithms { private static final Random RANDOM = Utils.getRandomInstance(); private static final RestrictableGenerator INT_GEN = Generators.G.ints(); interface TestFunction { Object run(); } Map> testGroups = new HashMap>(); int[] aI; int[] rI1; int[] rI2; int[] rI3; int[] rI4; public static void main(String[] args) { TestFramework framework = new TestFramework(); // TODO: run with and without SuperWord, and also some intrinsics should be disabled in a run. // make sure that all those flags are also mentioned in the JMH. framework.addFlags("--add-modules=jdk.incubator.vector", "-XX:CompileCommand=inline,*VectorAlgorithmsImpl::*"); framework.start(); } public TestVectorAlgorithms () { // IMPORTANT: // If you want to use some array but do NOT modify it: just use it. // If you want to use it and DO want to modify it: clone it. This // ensures that each test gets a separate copy, and that when we // capture the modified arrays they are different for every method // and run. // An alternative to cloning is to use different return arrays for // different implementations of the same group, e.g. rI1, rI2, ... testGroups.put("fillI", new HashMap()); testGroups.get("fillI").put("fillI_loop", () -> { return fillI_loop(rI1); }); testGroups.get("fillI").put("fillI_VectorAPI", () -> { return fillI_VectorAPI(rI1); }); testGroups.get("fillI").put("fillI_Arrays", () -> { return fillI_Arrays(rI1); }); testGroups.put("iotaI", new HashMap()); testGroups.get("iotaI").put("iotaI_loop", () -> { return iotaI_loop(rI1); }); testGroups.get("iotaI").put("iotaI_VectorAPI", () -> { return iotaI_VectorAPI(rI1); }); testGroups.put("copyI", new HashMap()); testGroups.get("copyI").put("copyI_loop", () -> { return copyI_loop(aI, rI1); }); testGroups.get("copyI").put("copyI_VectorAPI", () -> { return copyI_VectorAPI(aI, rI1); }); testGroups.get("copyI").put("copyI_System_arraycopy", () -> { return copyI_System_arraycopy(aI, rI1); }); testGroups.put("mapI", new HashMap()); testGroups.get("mapI").put("mapI_loop", () -> { return mapI_loop(aI, rI1); }); testGroups.get("mapI").put("mapI_VectorAPI", () -> { return mapI_VectorAPI(aI, rI1); }); testGroups.put("reduceAddI", new HashMap()); testGroups.get("reduceAddI").put("reduceAddI_loop", () -> { return reduceAddI_loop(aI); }); testGroups.get("reduceAddI").put("reduceAddI_reassociate", () -> { return reduceAddI_reassociate(aI); }); testGroups.get("reduceAddI").put("reduceAddI_VectorAPI_naive", () -> { return reduceAddI_VectorAPI_naive(aI); }); testGroups.get("reduceAddI").put("reduceAddI_VectorAPI_reduction_after_loop", () -> { return reduceAddI_VectorAPI_reduction_after_loop(aI); }); testGroups.put("scanAddI", new HashMap()); testGroups.get("scanAddI").put("scanAddI_loop", () -> { return scanAddI_loop(aI, rI1); }); testGroups.get("scanAddI").put("scanAddI_loop_reassociate", () -> { return scanAddI_loop_reassociate(aI, rI2); }); testGroups.get("scanAddI").put("scanAddI_VectorAPI_permute_add", () -> { return scanAddI_VectorAPI_permute_add(aI, rI4); }); testGroups.put("findMinIndexI", new HashMap()); testGroups.get("findMinIndexI").put("findMinIndexI_loop", () -> { return findMinIndexI_loop(aI); }); testGroups.get("findMinIndexI").put("findMinIndexI_VectorAPI", () -> { return findMinIndexI_VectorAPI(aI); }); testGroups.put("reverseI", new HashMap()); testGroups.get("reverseI").put("reverseI_loop", () -> { return reverseI_loop(aI, rI1); }); testGroups.get("reverseI").put("reverseI_VectorAPI", () -> { return reverseI_VectorAPI(aI, rI2); }); } @Warmup(100) @Run(test = {"fillI_loop", "fillI_VectorAPI", "fillI_Arrays", "iotaI_loop", "iotaI_VectorAPI", "copyI_loop", "copyI_VectorAPI", "copyI_System_arraycopy", "mapI_loop", "mapI_VectorAPI", "reduceAddI_loop", "reduceAddI_reassociate", "reduceAddI_VectorAPI_naive", "reduceAddI_VectorAPI_reduction_after_loop", "scanAddI_loop", "scanAddI_loop_reassociate", "scanAddI_VectorAPI_permute_add", "findMinIndexI_loop", "findMinIndexI_VectorAPI", "reverseI_loop", "reverseI_VectorAPI"}) public void runTests(RunInfo info) { // Repeat many times, so that we also have multiple iterations for post-warmup to potentially recompile int iters = info.isWarmUp() ? 1 : 20; for (int iter = 0; iter < iters; iter++) { // Set up random inputs, random size is important to stress tails. int size = 100_000 + RANDOM.nextInt(10_000); aI = new int[size]; G.fill(INT_GEN, aI); //for (int i = 0; i < aI.length; i++) { aI[i] = i; } rI1 = new int[size]; rI2 = new int[size]; rI3 = new int[size]; rI4 = new int[size]; // Run all tests for (Map.Entry> group_entry : testGroups.entrySet()) { String group_name = group_entry.getKey(); Map group = group_entry.getValue(); Object gold = null; String gold_name = "NONE"; for (Map.Entry entry : group.entrySet()) { String name = entry.getKey(); TestFunction test = entry.getValue(); Object result = test.run(); if (gold == null) { gold = result; gold_name = name; } else { try { Verify.checkEQ(gold, result); } catch (VerifyException e) { throw new RuntimeException("Verify.checkEQ failed for group " + group_name + ", gold " + gold_name + ", test " + name, e); } } } } } } @Test //@IR(counts = {IRNode.STORE_VECTOR, "> 0"}, // applyIfCPUFeatureOr = {"sse4.1", "true", "asimd", "true"}) // TODO: // 250 CallLeafNoFP === 208 1 7 8 9 (248 131 251 1 ) [[ 253 254 ]] # arrayof_jint_fill void ... public Object fillI_loop(int[] r) { return VectorAlgorithmsImpl.fillI_loop(r); } @Test public Object fillI_VectorAPI(int[] r) { return VectorAlgorithmsImpl.fillI_VectorAPI(r); } @Test public Object fillI_Arrays(int[] r) { return VectorAlgorithmsImpl.fillI_Arrays(r); } @Test @IR(counts = {IRNode.POPULATE_INDEX, "> 0", IRNode.STORE_VECTOR, "> 0"}, applyIfCPUFeatureOr = {"sse4.1", "true", "asimd", "true"}) public Object iotaI_loop(int[] r) { return VectorAlgorithmsImpl.iotaI_loop(r); } @Test public Object iotaI_VectorAPI(int[] r) { return VectorAlgorithmsImpl.iotaI_VectorAPI(r); } @Test @IR(counts = {IRNode.LOAD_VECTOR_I, "> 0", IRNode.STORE_VECTOR, "> 0"}, applyIfCPUFeatureOr = {"sse4.1", "true", "asimd", "true"}) public Object copyI_loop(int[] a, int[] r) { return VectorAlgorithmsImpl.copyI_loop(a, r); } @Test public Object copyI_VectorAPI(int[] a, int[] r) { return VectorAlgorithmsImpl.copyI_VectorAPI(a, r); } @Test public Object copyI_System_arraycopy(int[] a, int[] r) { return VectorAlgorithmsImpl.copyI_System_arraycopy(a, r); } @Test @IR(counts = {IRNode.LOAD_VECTOR_I, "> 0", IRNode.MUL_VI, "> 0", IRNode.STORE_VECTOR, "> 0"}, applyIfCPUFeatureOr = {"sse4.1", "true", "asimd", "true"}) public Object mapI_loop(int[] a, int[] r) { return VectorAlgorithmsImpl.mapI_loop(a, r); } @Test public Object mapI_VectorAPI(int[] a, int[] r) { return VectorAlgorithmsImpl.mapI_VectorAPI(a, r); } @Test @IR(counts = {IRNode.LOAD_VECTOR_I, "> 0", IRNode.ADD_REDUCTION_VI, "> 0", IRNode.ADD_VI, "> 0"}, applyIfCPUFeatureOr = {"sse4.1", "true", "asimd", "true"}) public int reduceAddI_loop(int[] a) { return VectorAlgorithmsImpl.reduceAddI_loop(a); } @Test public int reduceAddI_reassociate(int[] a) { return VectorAlgorithmsImpl.reduceAddI_reassociate(a); } @Test public int reduceAddI_VectorAPI_naive(int[] a) { return VectorAlgorithmsImpl.reduceAddI_VectorAPI_naive(aI); } @Test public int reduceAddI_VectorAPI_reduction_after_loop(int[] a) { return VectorAlgorithmsImpl.reduceAddI_VectorAPI_reduction_after_loop(aI); } @Test public Object scanAddI_loop(int[] a, int[] r) { return VectorAlgorithmsImpl.scanAddI_loop(a, r); } @Test public Object scanAddI_loop_reassociate(int[] a, int[] r) { return VectorAlgorithmsImpl.scanAddI_loop_reassociate(a, r); } @Test public Object scanAddI_VectorAPI_permute_add(int[] a, int[] r) { return VectorAlgorithmsImpl.scanAddI_VectorAPI_permute_add(a, r); } @Test public int findMinIndexI_loop(int[] a) { return VectorAlgorithmsImpl.findMinIndexI_loop(a); } @Test public int findMinIndexI_VectorAPI(int[] a) { return VectorAlgorithmsImpl.findMinIndexI_VectorAPI(a); } @Test public Object reverseI_loop(int[] a, int[] r) { return VectorAlgorithmsImpl.reverseI_loop(a, r); } @Test public Object reverseI_VectorAPI(int[] a, int[] r) { return VectorAlgorithmsImpl.reverseI_VectorAPI(a, r); } }