1 /* 2 * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 8310886 8325252 8320622 27 * @summary Test MulAddS2I vectorization. 28 * @library /test/lib / 29 * @run driver compiler.loopopts.superword.TestMulAddS2I 30 */ 31 32 package compiler.loopopts.superword; 33 34 import compiler.lib.ir_framework.*; 35 import jdk.test.lib.Asserts; 36 import jdk.test.lib.Platform; 37 38 public class TestMulAddS2I { 39 static final int RANGE = 1024*16; 40 static final int ITER = RANGE/2 - 1; 41 42 static short[] sArr1 = new short[RANGE]; 43 static short[] sArr2 = new short[RANGE]; 44 static final int[] GOLDEN_A; 45 static final int[] GOLDEN_B; 46 static final int[] GOLDEN_C; 47 static final int[] GOLDEN_D; 48 static final int[] GOLDEN_E; 49 static final int[] GOLDEN_F; 50 static final int[] GOLDEN_G; 51 static final int[] GOLDEN_H; 52 static final int[] GOLDEN_I; 53 static final int[] GOLDEN_J; 54 static final int[] GOLDEN_K; 55 static final int[] GOLDEN_L; 56 57 static { 58 for (int i = 0; i < RANGE; i++) { 59 sArr1[i] = (short)(AbstractInfo.getRandom().nextInt()); 60 sArr2[i] = (short)(AbstractInfo.getRandom().nextInt()); 61 } 62 GOLDEN_A = testa(); 63 GOLDEN_B = testb(); 64 GOLDEN_C = testc(new int[ITER]); 65 GOLDEN_D = testd(new int[ITER]); 66 GOLDEN_E = teste(new int[ITER]); 67 GOLDEN_F = testf(new int[ITER]); 68 GOLDEN_G = testg(new int[ITER]); 69 GOLDEN_H = testh(new int[ITER]); 70 GOLDEN_I = testi(new int[ITER]); 71 GOLDEN_J = testj(new int[ITER]); 72 GOLDEN_K = testk(new int[ITER]); 73 GOLDEN_L = testl(new int[ITER]); 74 } 75 76 77 public static void main(String[] args) { 78 TestFramework.runWithFlags("-XX:+IgnoreUnrecognizedVMOptions", "-XX:+AlignVector"); 79 TestFramework.runWithFlags("-XX:+IgnoreUnrecognizedVMOptions", "-XX:-AlignVector"); 80 } 81 82 @Run(test = {"testa", "testb", "testc", "testd", "teste", "testf", "testg", "testh", 83 "testi", "testj", "testk", "testl"}) 84 @Warmup(0) 85 public static void run() { 86 compare(testa(), GOLDEN_A, "testa"); 87 compare(testb(), GOLDEN_B, "testb"); 88 compare(testc(new int[ITER]), GOLDEN_C, "testc"); 89 compare(testd(new int[ITER]), GOLDEN_D, "testd"); 90 compare(teste(new int[ITER]), GOLDEN_E, "teste"); 91 compare(testf(new int[ITER]), GOLDEN_F, "testf"); 92 compare(testg(new int[ITER]), GOLDEN_G, "testg"); 93 compare(testh(new int[ITER]), GOLDEN_H, "testh"); 94 compare(testi(new int[ITER]), GOLDEN_I, "testi"); 95 compare(testj(new int[ITER]), GOLDEN_J, "testj"); 96 compare(testk(new int[ITER]), GOLDEN_K, "testk"); 97 compare(testl(new int[ITER]), GOLDEN_L, "testl"); 98 } 99 100 public static void compare(int[] out, int[] golden, String name) { 101 for (int i = 0; i < ITER; i++) { 102 Asserts.assertEQ(out[i], golden[i], "wrong result for '" + name + "' out[" + i + "]"); 103 } 104 } 105 106 @Test 107 @IR(applyIfCPUFeature = {"sse2", "true"}, 108 applyIfPlatform = {"64-bit", "true"}, 109 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI, "> 0"}) 110 @IR(applyIfCPUFeature = {"asimd", "true"}, 111 applyIf = {"MaxVectorSize", "16"}, // AD file requires vector_length = 16 112 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI, "> 0"}) 113 @IR(applyIfCPUFeature = {"avx512_vnni", "true"}, 114 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI_VNNI, "> 0"}) 115 public static int[] testa() { 116 int[] out = new int[ITER]; 117 int[] out2 = new int[ITER]; 118 for (int i = 0; i < ITER; i++) { 119 out[i] += ((sArr1[2*i] * sArr1[2*i]) + (sArr1[2*i+1] * sArr1[2*i+1])); 120 out2[i] += out[i]; 121 } 122 return out; 123 } 124 125 @Test 126 @IR(applyIfCPUFeature = {"sse2", "true"}, 127 applyIfPlatform = {"64-bit", "true"}, 128 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI, "> 0"}) 129 @IR(applyIfCPUFeature = {"asimd", "true"}, 130 applyIf = {"MaxVectorSize", "16"}, // AD file requires vector_length = 16 131 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI, "> 0"}) 132 @IR(applyIfCPUFeature = {"avx512_vnni", "true"}, 133 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI_VNNI, "> 0"}) 134 public static int[] testb() { 135 int[] out = new int[ITER]; 136 int[] out2 = new int[ITER]; 137 for (int i = 0; i < ITER; i++) { 138 out[i] += ((sArr1[2*i] * sArr2[2*i]) + (sArr1[2*i+1] * sArr2[2*i+1])); 139 out2[i] += out[i]; 140 } 141 return out; 142 } 143 144 @Test 145 @IR(applyIfCPUFeature = {"sse2", "true"}, 146 applyIfPlatform = {"64-bit", "true"}, 147 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI, "> 0"}) 148 @IR(applyIfCPUFeature = {"asimd", "true"}, 149 applyIf = {"MaxVectorSize", "16"}, // AD file requires vector_length = 16 150 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI, "> 0"}) 151 @IR(applyIfCPUFeature = {"avx512_vnni", "true"}, 152 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI_VNNI, "> 0"}) 153 public static int[] testc(int[] out) { 154 for (int i = 0; i < ITER; i++) { 155 out[i] += ((sArr1[2*i] * sArr2[2*i]) + (sArr1[2*i+1] * sArr2[2*i+1])); 156 } 157 return out; 158 } 159 160 @Test 161 @IR(applyIfCPUFeature = {"sse2", "true"}, 162 applyIfPlatform = {"64-bit", "true"}, 163 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI, "> 0"}) 164 @IR(applyIfCPUFeature = {"asimd", "true"}, 165 applyIf = {"MaxVectorSize", "16"}, // AD file requires vector_length = 16 166 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI, "> 0"}) 167 @IR(applyIfCPUFeature = {"avx512_vnni", "true"}, 168 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI_VNNI, "> 0"}) 169 public static int[] testd(int[] out) { 170 for (int i = 0; i < ITER-2; i+=2) { 171 // Unrolled, with the same structure. 172 out[i+0] += ((sArr1[2*i+0] * sArr2[2*i+0]) + (sArr1[2*i+1] * sArr2[2*i+1])); 173 out[i+1] += ((sArr1[2*i+2] * sArr2[2*i+2]) + (sArr1[2*i+3] * sArr2[2*i+3])); 174 } 175 return out; 176 } 177 178 @Test 179 @IR(applyIfCPUFeature = {"sse2", "true"}, 180 applyIfPlatform = {"64-bit", "true"}, 181 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI, "> 0"}) 182 @IR(applyIfCPUFeature = {"asimd", "true"}, 183 applyIf = {"MaxVectorSize", "16"}, // AD file requires vector_length = 16 184 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI, "> 0"}) 185 @IR(applyIfCPUFeature = {"avx512_vnni", "true"}, 186 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI_VNNI, "> 0"}) 187 public static int[] teste(int[] out) { 188 for (int i = 0; i < ITER-2; i+=2) { 189 // Unrolled, with some swaps. 190 out[i+0] += ((sArr1[2*i+0] * sArr2[2*i+0]) + (sArr1[2*i+1] * sArr2[2*i+1])); 191 out[i+1] += ((sArr2[2*i+2] * sArr1[2*i+2]) + (sArr1[2*i+3] * sArr2[2*i+3])); // swap(1 2) 192 } 193 return out; 194 } 195 196 @Test 197 @IR(applyIfCPUFeature = {"sse2", "true"}, 198 applyIfPlatform = {"64-bit", "true"}, 199 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI, "> 0"}) 200 @IR(applyIfCPUFeature = {"asimd", "true"}, 201 applyIf = {"MaxVectorSize", "16"}, // AD file requires vector_length = 16 202 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI, "> 0"}) 203 @IR(applyIfCPUFeature = {"avx512_vnni", "true"}, 204 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI_VNNI, "> 0"}) 205 public static int[] testf(int[] out) { 206 for (int i = 0; i < ITER-2; i+=2) { 207 // Unrolled, with some swaps. 208 out[i+0] += ((sArr1[2*i+0] * sArr2[2*i+0]) + (sArr1[2*i+1] * sArr2[2*i+1])); 209 out[i+1] += ((sArr2[2*i+2] * sArr1[2*i+2]) + (sArr2[2*i+3] * sArr1[2*i+3])); // swap(1 2), swap(3 4) 210 } 211 return out; 212 } 213 214 @Test 215 @IR(applyIfCPUFeature = {"sse2", "true"}, 216 applyIfPlatform = {"64-bit", "true"}, 217 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI, "> 0"}) 218 @IR(applyIfCPUFeature = {"asimd", "true"}, 219 applyIf = {"MaxVectorSize", "16"}, // AD file requires vector_length = 16 220 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI, "> 0"}) 221 @IR(applyIfCPUFeature = {"avx512_vnni", "true"}, 222 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI_VNNI, "> 0"}) 223 public static int[] testg(int[] out) { 224 for (int i = 0; i < ITER-2; i+=2) { 225 // Unrolled, with some swaps. 226 out[i+0] += ((sArr1[2*i+0] * sArr2[2*i+0]) + (sArr1[2*i+1] * sArr2[2*i+1])); 227 out[i+1] += ((sArr1[2*i+3] * sArr2[2*i+3]) + (sArr1[2*i+2] * sArr2[2*i+2])); // swap(1 3), swap(2 4) 228 } 229 return out; 230 } 231 232 @Test 233 @IR(applyIfCPUFeature = {"sse2", "true"}, 234 applyIfPlatform = {"64-bit", "true"}, 235 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI, "> 0"}) 236 @IR(applyIfCPUFeature = {"asimd", "true"}, 237 applyIf = {"MaxVectorSize", "16"}, // AD file requires vector_length = 16 238 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI, "> 0"}) 239 @IR(applyIfCPUFeature = {"avx512_vnni", "true"}, 240 counts = {IRNode.MUL_ADD_S2I, "> 0", IRNode.MUL_ADD_VS2VI_VNNI, "> 0"}) 241 public static int[] testh(int[] out) { 242 for (int i = 0; i < ITER-2; i+=2) { 243 // Unrolled, with some swaps. 244 out[i+0] += ((sArr1[2*i+0] * sArr2[2*i+0]) + (sArr1[2*i+1] * sArr2[2*i+1])); 245 out[i+1] += ((sArr2[2*i+3] * sArr1[2*i+3]) + (sArr2[2*i+2] * sArr1[2*i+2])); // swap(1 4), swap(2 3) 246 } 247 return out; 248 } 249 250 @Test 251 @IR(counts = {IRNode.MUL_ADD_S2I, "> 0"}, 252 applyIfCPUFeatureOr = {"sse2", "true", "asimd", "true"}) 253 @IR(counts = {IRNode.MUL_ADD_VS2VI, "= 0"}) 254 public static int[] testi(int[] out) { 255 for (int i = 0; i < ITER-2; i+=2) { 256 // Unrolled, with some swaps that prevent vectorization. 257 out[i+0] += ((sArr1[2*i+0] * sArr2[2*i+0]) + (sArr1[2*i+1] * sArr2[2*i+1])); // ok 258 out[i+1] += ((sArr1[2*i+2] * sArr2[2*i+3]) + (sArr1[2*i+3] * sArr2[2*i+2])); // bad 259 } 260 return out; 261 } 262 263 @Test 264 @IR(counts = {IRNode.MUL_ADD_S2I, "> 0"}, 265 applyIfCPUFeatureOr = {"sse2", "true", "asimd", "true"}) 266 @IR(counts = {IRNode.MUL_ADD_VS2VI, "= 0"}) 267 public static int[] testj(int[] out) { 268 for (int i = 0; i < ITER-2; i+=2) { 269 // Unrolled, with some swaps that prevent vectorization. 270 out[i+0] += ((sArr1[2*i+0] * sArr2[2*i+1]) + (sArr1[2*i+1] * sArr2[2*i+0])); // bad 271 out[i+1] += ((sArr1[2*i+2] * sArr2[2*i+3]) + (sArr1[2*i+3] * sArr2[2*i+2])); // bad 272 } 273 return out; 274 } 275 276 @Test 277 @IR(counts = {IRNode.MUL_ADD_S2I, "> 0"}, 278 applyIfCPUFeatureOr = {"sse2", "true", "asimd", "true"}) 279 @IR(counts = {IRNode.MUL_ADD_VS2VI, "= 0"}) 280 public static int[] testk(int[] out) { 281 for (int i = 0; i < ITER-2; i+=2) { 282 // Unrolled, with some swaps that prevent vectorization. 283 out[i+0] += ((sArr1[2*i+0] * sArr2[2*i+1]) + (sArr1[2*i+1] * sArr2[2*i+0])); // bad 284 out[i+1] += ((sArr1[2*i+2] * sArr2[2*i+2]) + (sArr1[2*i+3] * sArr2[2*i+3])); // ok 285 } 286 return out; 287 } 288 289 @Test 290 @IR(counts = {IRNode.MUL_ADD_S2I, "> 0"}, 291 applyIfCPUFeatureOr = {"sse2", "true", "asimd", "true"}) 292 @IR(counts = {IRNode.MUL_ADD_VS2VI, "= 0"}) 293 public static int[] testl(int[] out) { 294 for (int i = 0; i < ITER-2; i+=2) { 295 // Unrolled, with some swaps that prevent vectorization. 296 out[i+0] += ((sArr1[2*i+1] * sArr2[2*i+1]) + (sArr1[2*i+0] * sArr2[2*i+0])); // ok 297 out[i+1] += ((sArr1[2*i+2] * sArr2[2*i+3]) + (sArr1[2*i+3] * sArr2[2*i+2])); // bad 298 } 299 return out; 300 } 301 302 }