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
26 /*
27 * @test
28 * @bug 8304720
29 * @summary Test some examples where non-vectorized memops also need to
30 * be reordered during SuperWord::schedule.
31 * @modules java.base/jdk.internal.misc
32 * @library /test/lib /
33 * @run driver compiler.loopopts.superword.TestScheduleReordersScalarMemops nCOH_nAV
34 * @run driver compiler.loopopts.superword.TestScheduleReordersScalarMemops nCOH_yAV
35 * @run driver compiler.loopopts.superword.TestScheduleReordersScalarMemops yCOH_nAV
36 * @run driver compiler.loopopts.superword.TestScheduleReordersScalarMemops yCOH_yAV
37 */
38
39 package compiler.loopopts.superword;
40
41 import jdk.internal.misc.Unsafe;
42 import jdk.test.lib.Asserts;
43 import compiler.lib.ir_framework.*;
44
45 public class TestScheduleReordersScalarMemops {
46 static final int RANGE = 1024;
47 static final int ITER = 10_000;
48 static Unsafe unsafe = Unsafe.getUnsafe();
49
50 int[] goldI0 = new int[RANGE];
51 float[] goldF0 = new float[RANGE];
52 int[] goldI1 = new int[RANGE];
53 float[] goldF1 = new float[RANGE];
54
55 public static void main(String args[]) {
56 TestFramework framework = new TestFramework(TestScheduleReordersScalarMemops.class);
57 framework.addFlags("--add-modules", "java.base", "--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED",
58 "-XX:CompileCommand=compileonly,compiler.loopopts.superword.TestScheduleReordersScalarMemops::test*",
59 "-XX:CompileCommand=compileonly,compiler.loopopts.superword.TestScheduleReordersScalarMemops::verify",
60 "-XX:CompileCommand=compileonly,compiler.loopopts.superword.TestScheduleReordersScalarMemops::init",
61 "-XX:-TieredCompilation", "-Xbatch",
62 "-XX:+IgnoreUnrecognizedVMOptions", "-XX:LoopUnrollLimit=1000");
63 switch (args[0]) {
64 case "nCOH_nAV" -> { framework.addFlags("-XX:-UseCompactObjectHeaders", "-XX:-AlignVector"); }
65 case "nCOH_yAV" -> { framework.addFlags("-XX:-UseCompactObjectHeaders", "-XX:+AlignVector"); }
66 case "yCOH_nAV" -> { framework.addFlags("-XX:+UseCompactObjectHeaders", "-XX:-AlignVector"); }
67 case "yCOH_yAV" -> { framework.addFlags("-XX:+UseCompactObjectHeaders", "-XX:+AlignVector"); }
68 default -> { throw new RuntimeException("Test argument not recognized: " + args[0]); }
69 };
70 framework.start();
71 }
72
73 TestScheduleReordersScalarMemops() {
74 // compute the gold standard in interpreter mode
75 init(goldI0, goldF0);
76 test0(goldI0, goldI0, goldF0, goldF0);
77 init(goldI1, goldF1);
78 test1(goldI1, goldI1, goldF1, goldF1);
79 }
80
81 @Run(test = "test0")
82 @Warmup(100)
83 public void runTest0() {
84 int[] dataI = new int[RANGE];
85 float[] dataF = new float[RANGE];
86 init(dataI, dataF);
87 test0(dataI, dataI, dataF, dataF);
88 verify("test0", dataI, goldI0);
89 verify("test0", dataF, goldF0);
90 }
91
92 @Test
93 @IR(counts = {IRNode.MUL_VI, "> 0"},
94 applyIfCPUFeatureOr = {"avx2", "true", "asimd", "true"})
95 static void test0(int[] dataIa, int[] dataIb, float[] dataFa, float[] dataFb) {
96 for (int i = 0; i < RANGE; i+=2) {
97 // We have dependency edges:
98 // A -> X
99 // Y -> B
100 // Still, we can vectorize [X,Y].
101 // We do not vectorize A and B, because they are not isomorphic (add vs mul).
102 //
103 // Imagine this is unrolled at least 2x.
104 // We get order: A0 X0 Y0 B0 A1 X1 Y1 B1
105 // Vectorized: X0 Y0 X1 Y1
106 // Scalar: A0 B0 A1 B1
107 //
108 // However, since the As need to be before, and the Bs after the vector operations,
109 // we need to have all As before all Bs. This means we need to reorder the scalar
110 // operations, and not just the vectorized ones.
111 //
112 // A correct reordering would be: A0 A1 [X0, Y0, X1, Y1] B0 B1
113 //
114 dataFa[i + 0] = dataIa[i + 0] * 1.3f; // A *1.3
115 dataIb[i + 0] = (int)dataFb[i + 0] * 11; // X *11
116 dataIb[i + 1] = (int)dataFb[i + 1] * 11; // Y *11
117 dataFa[i + 1] = dataIa[i + 1] + 1.2f; // B +1.2
118 }
119 }
120
121 @Run(test = "test1")
122 @Warmup(100)
123 public void runTest1() {
124 int[] dataI = new int[RANGE];
125 float[] dataF = new float[RANGE];
126 init(dataI, dataF);
127 test1(dataI, dataI, dataF, dataF);
128 verify("test1", dataI, goldI1);
129 verify("test1", dataF, goldF1);
130 }
131
132 @Test
133 @IR(counts = {IRNode.MUL_VI, "> 0"},
134 applyIfCPUFeatureOr = {"avx2", "true", "asimd", "true"})
135 static void test1(int[] dataIa, int[] dataIb, float[] dataFa, float[] dataFb) {
136 for (int i = 0; i < RANGE; i+=2) {
137 // Do the same as test0, but without int-float conversion.
138 // This should reproduce on machines where conversion is not implemented.
139 unsafe.putInt(dataFa, unsafe.ARRAY_FLOAT_BASE_OFFSET + 4L * i + 0, dataIa[i+0] + 1); // A +1
140 dataIb[i+0] = 11 * unsafe.getInt(dataFb, unsafe.ARRAY_INT_BASE_OFFSET + 4L * i + 0); // X
141 dataIb[i+1] = 11 * unsafe.getInt(dataFb, unsafe.ARRAY_INT_BASE_OFFSET + 4L * i + 4); // Y
142 unsafe.putInt(dataFa, unsafe.ARRAY_FLOAT_BASE_OFFSET + 4L * i + 4, dataIa[i+1] * 11); // B *11
143 }
144 }
145
146 static void init(int[] dataI, float[] dataF) {
147 for (int i = 0; i < RANGE; i++) {
148 dataI[i] = i + 1;
149 dataF[i] = i + 0.1f;
150 }
151 }
152
153 static void verify(String name, int[] data, int[] gold) {
154 for (int i = 0; i < RANGE; i++) {
155 if (data[i] != gold[i]) {
156 throw new RuntimeException(" Invalid " + name + " result: data[" + i + "]: " + data[i] + " != " + gold[i]);
157 }
158 }
159 }
160
161 static void verify(String name, float[] data, float[] gold) {
162 for (int i = 0; i < RANGE; i++) {
163 int datav = unsafe.getInt(data, unsafe.ARRAY_FLOAT_BASE_OFFSET + 4 * i);
164 int goldv = unsafe.getInt(gold, unsafe.ARRAY_FLOAT_BASE_OFFSET + 4 * i);
165 if (datav != goldv) {
166 throw new RuntimeException(" Invalid " + name + " result: dataF[" + i + "]: " + datav + " != " + goldv);
167 }
168 }
169 }
170 }