1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
/*
* 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 8357530
* @summary Test the effect of AutoVectorizationOverrideProfitability.
* @library /test/lib /
* @run driver compiler.loopopts.superword.TestAutoVectorizationOverrideProfitability
*/
package compiler.loopopts.superword;
import compiler.lib.ir_framework.*;
import compiler.lib.verify.*;
import compiler.lib.generators.Generator;
import static compiler.lib.generators.Generators.G;
public class TestAutoVectorizationOverrideProfitability {
public static final Generator<Integer> GEN_I = G.ints();
public static final Generator<Float> GEN_F = G.floats();
public static int[] aI = new int[10_000];
public static int[] rI = new int[10_000];
public static float[] aF = new float[10_000];
public static float[] rF = new float[10_000];
static {
G.fill(GEN_I, aI);
G.fill(GEN_F, aF);
}
public static void main(String[] args) throws Exception {
// Do not vectorize, even if profitable.
TestFramework.runWithFlags("-XX:+UnlockDiagnosticVMOptions", "-XX:AutoVectorizationOverrideProfitability=0");
// Normal run, i.e. with normal heuristic. In some cases this vectorizes, in some not.
// By default, we have AutoVectorizationOverrideProfitability=1
TestFramework.run();
// Vectorize even if not profitable.
TestFramework.runWithFlags("-XX:+UnlockDiagnosticVMOptions", "-XX:AutoVectorizationOverrideProfitability=2");
}
public static final float GOLD_SIMPLE_FLOAT_REDUCTION = simpleFloatReduction();
@Test
@Warmup(10)
@IR(applyIfCPUFeatureOr = {"avx", "true"},
applyIf = {"AutoVectorizationOverrideProfitability", "= 2"},
counts = {IRNode.ADD_REDUCTION_VF, "> 0"})
@IR(applyIfCPUFeatureOr = {"avx", "true"},
applyIf = {"AutoVectorizationOverrideProfitability", "< 2"},
counts = {IRNode.ADD_REDUCTION_VF, "= 0"})
// The simple float reduction is not profitable. We need to sequentially
// add up the values, and so we cannot move the reduction out of the loop.
private static float simpleFloatReduction() {
float sum = 0;
for (int i = 0; i < aF.length; i++) {
sum += aF[i];
}
return sum;
}
@Check(test="simpleFloatReduction")
public static void checkSimpleFloatReduction(float result) {
Verify.checkEQ(GOLD_SIMPLE_FLOAT_REDUCTION, result);
}
static { simpleFloatCopy(); }
public static final float[] GOLD_SIMPLE_FLOAT_COPY = rF.clone();
@Test
@Warmup(10)
@IR(applyIfCPUFeatureOr = {"avx", "true"},
applyIf = {"AutoVectorizationOverrideProfitability", "> 0"},
counts = {IRNode.LOAD_VECTOR_F, "> 0"})
@IR(applyIfCPUFeatureOr = {"avx", "true"},
applyIf = {"AutoVectorizationOverrideProfitability", "= 0"},
counts = {IRNode.LOAD_VECTOR_F, "= 0"})
// The simple float copy is always profitable.
private static void simpleFloatCopy() {
for (int i = 0; i < aF.length; i++) {
rF[i] = aF[i];
}
}
@Check(test="simpleFloatCopy")
public static void checkSimpleFloatCopy() {
Verify.checkEQ(GOLD_SIMPLE_FLOAT_COPY, rF);
}
public static final int GOLD_SIMPLE_INT_REDUCTION = simpleIntReduction();
@Test
@Warmup(10)
@IR(applyIfCPUFeatureOr = {"avx", "true"},
applyIf = {"AutoVectorizationOverrideProfitability", "= 2"},
counts = {IRNode.ADD_REDUCTION_VI, "> 0", IRNode.ADD_VI, "> 0"})
@IR(applyIfCPUFeatureOr = {"avx", "true"},
applyIf = {"AutoVectorizationOverrideProfitability", "< 2"},
counts = {IRNode.ADD_REDUCTION_VI, "= 0", IRNode.ADD_VI, "= 0"})
// Current heuristics say that this simple int reduction is not profitable.
// But it would actually be profitable, since we are able to move the
// reduction out of the loop (we can reorder the reduction). When moving
// the reduction out of the loop, we instead accumulate with a simple
// ADD_VI inside the loop.
// See: JDK-8307516 JDK-8345044
private static int simpleIntReduction() {
int sum = 0;
for (int i = 0; i < aI.length; i++) {
sum += aI[i];
}
return sum;
}
@Check(test="simpleIntReduction")
public static void checkSimpleIntReduction(int result) {
Verify.checkEQ(GOLD_SIMPLE_INT_REDUCTION, result);
}
static { simpleIntCopy(); }
public static final int[] GOLD_SIMPLE_INT_COPY = rI.clone();
@Test
@Warmup(10)
@IR(applyIfCPUFeatureOr = {"avx", "true"},
applyIf = {"AutoVectorizationOverrideProfitability", "> 0"},
counts = {IRNode.LOAD_VECTOR_I, "> 0"})
@IR(applyIfCPUFeatureOr = {"avx", "true"},
applyIf = {"AutoVectorizationOverrideProfitability", "= 0"},
counts = {IRNode.LOAD_VECTOR_I, "= 0"})
// The simple int copy is always profitable.
private static void simpleIntCopy() {
for (int i = 0; i < aI.length; i++) {
rI[i] = aI[i];
}
}
@Check(test="simpleIntCopy")
public static void checkSimpleIntCopy() {
Verify.checkEQ(GOLD_SIMPLE_INT_COPY, rI);
}
}
|