1 /*
  2  * Copyright (c) 2023, Red Hat, Inc. 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 package compiler.c2.irTests;
 25 
 26 import compiler.lib.ir_framework.*;
 27 import jdk.test.lib.Utils;
 28 import jdk.test.whitebox.WhiteBox;
 29 import jdk.internal.misc.Unsafe;
 30 import java.util.Random;
 31 import java.util.Arrays;
 32 import java.nio.ByteOrder;
 33 
 34 /*
 35  * @test
 36  * @bug 8300258
 37  * @key randomness
 38  * @requires (os.simpleArch == "x64") | (os.simpleArch == "aarch64")
 39  * @summary C2: vectorization fails on simple ByteBuffer loop
 40  * @modules java.base/jdk.internal.misc
 41  * @library /test/lib /
 42  * @build jdk.test.whitebox.WhiteBox
 43  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 44  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI compiler.c2.irTests.TestVectorizationMismatchedAccess
 45  */
 46 
 47 public class TestVectorizationMismatchedAccess {
 48     private static final Unsafe UNSAFE = Unsafe.getUnsafe();
 49     private static final Random RANDOM = Utils.getRandomInstance();
 50     private final static WhiteBox wb = WhiteBox.getWhiteBox();
 51 
 52     public static void main(String[] args) {
 53         Object alignVector = wb.getVMFlag("AlignVector");
 54         if (alignVector != null && !((Boolean)alignVector)) {
 55             if (ByteOrder.nativeOrder() != ByteOrder.LITTLE_ENDIAN) {
 56                 throw new RuntimeException("fix test that was written for a little endian platform");
 57             }
 58             TestFramework.runWithFlags("--add-modules", "java.base", "--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED");
 59         }
 60     }
 61 
 62     static int size = 1024;
 63     static byte[] byteArray = new byte[size * 8];
 64     static long[] longArray = new long[size];
 65     static byte[] verifyByteArray = new byte[size * 8];
 66     static long[] verifyLongArray = new long[size];
 67     static long baseOffset = 0;
 68     static long baseOffHeap = UNSAFE.allocateMemory(size * 8);
 69 
 70 
 71     static {
 72         for (int i = 0; i < verifyByteArray.length; i++) {
 73             verifyByteArray[i] = (byte)RANDOM.nextInt(Byte.MAX_VALUE);
 74         }
 75         for (int i = 0; i < verifyLongArray.length; i++) {
 76             verifyLongArray[i] = 0;
 77             for (int j = 0; j < 8; j++) {
 78                 verifyLongArray[i] = verifyLongArray[i] | (((long)verifyByteArray[8 * i + j]) << 8 * j);
 79             }
 80         }
 81     }
 82 
 83     static private void runAndVerify(Runnable test, int offset) {
 84         System.arraycopy(verifyLongArray, 0, longArray, 0, longArray.length);
 85         Arrays.fill(byteArray, (byte)0);
 86         test.run();
 87         int i;
 88         for (i = 0; i < Math.max(offset, 0); i++) {
 89             if (byteArray[i] != 0) {
 90                 throw new RuntimeException("Incorrect result at " + i + " " + byteArray[i] + " != 0");
 91             }
 92         }
 93         for (; i < Math.min(byteArray.length + offset, byteArray.length); i++) {
 94             if (byteArray[i] != verifyByteArray[i - offset]) {
 95                 throw new RuntimeException("Incorrect result at " + i + " " + byteArray[i] + " != " + verifyByteArray[i-offset]);
 96             }
 97         }
 98         for (; i < byteArray.length; i++) {
 99             if (byteArray[i] != 0) {
100                 throw new RuntimeException("Incorrect result at " + i + " " + byteArray[i] + " != 0");
101             }
102         }
103     }
104 
105     static private void runAndVerify2(Runnable test, int offset) {
106         System.arraycopy(verifyByteArray, 0, byteArray, 0, byteArray.length);
107         test.run();
108         int i;
109         for (i = 0; i < Math.max(offset, 0); i++) {
110             if (byteArray[i] != verifyByteArray[i]) {
111                 throw new RuntimeException("Incorrect result at " + i + " " + byteArray[i] + " != " + verifyByteArray[i]);
112             }
113         }
114         for (; i < Math.min(byteArray.length + offset, byteArray.length); i++) {
115             int val = offset > 0 ? verifyByteArray[(i-offset) % 8] : verifyByteArray[i-offset];
116             if (byteArray[i] != val) {
117                 throw new RuntimeException("Incorrect result at " + i + " " + byteArray[i] + " != " + verifyByteArray[i-offset]);
118             }
119         }
120         for (; i < byteArray.length; i++) {
121             if (byteArray[i] != verifyByteArray[i]) {
122                 throw new RuntimeException("Incorrect result at " + i + " " + byteArray[i] + " != " + verifyByteArray[i]);
123             }
124         }
125     }
126 
127 
128     static private void runAndVerify3(Runnable test, int offset) {
129         System.arraycopy(verifyLongArray, 0, longArray, 0, longArray.length);
130         for (int i = 0; i < size * 8; i++) {
131             UNSAFE.putByte(null, baseOffHeap + i, (byte)0);
132         }
133         test.run();
134         int i;
135         for (i = 0; i < Math.max(offset, 0); i++) {
136             if (UNSAFE.getByte(null, baseOffHeap + i) != 0) {
137                 throw new RuntimeException("Incorrect result at " + i + " " + byteArray[i] + " != 0");
138             }
139         }
140         for (; i < Math.min(size * 8 + offset, size * 8); i++) {
141             if (UNSAFE.getByte(null, baseOffHeap + i) != verifyByteArray[i - offset]) {
142                 throw new RuntimeException("Incorrect result at " + i + " " + byteArray[i] + " != " + verifyByteArray[i-offset]);
143             }
144         }
145         for (; i < byteArray.length; i++) {
146             if (UNSAFE.getByte(null, baseOffHeap + i) != 0) {
147                 throw new RuntimeException("Incorrect result at " + i + " " + byteArray[i] + " != 0");
148             }
149         }
150     }
151 
152     @Test
153     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
154     public static void testByteLong1(byte[] dest, long[] src) {
155         for (int i = 0; i < src.length; i++) {
156             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * i, src[i]);
157         }
158     }
159 
160     @Run(test = "testByteLong1")
161     public static void testByteLong1_runner() {
162         runAndVerify(() -> testByteLong1(byteArray, longArray), 0);
163     }
164 
165     @Test
166     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
167     public static void testByteLong2(byte[] dest, long[] src) {
168         for (int i = 1; i < src.length; i++) {
169             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * (i - 1), src[i]);
170         }
171     }
172 
173     @Run(test = "testByteLong2")
174     public static void testByteLong2_runner() {
175         runAndVerify(() -> testByteLong2(byteArray, longArray), -8);
176     }
177 
178     @Test
179     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
180     public static void testByteLong3(byte[] dest, long[] src) {
181         for (int i = 0; i < src.length - 1; i++) {
182             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * (i + 1), src[i]);
183         }
184     }
185 
186     @Run(test = "testByteLong3")
187     public static void testByteLong3_runner() {
188         runAndVerify(() -> testByteLong3(byteArray, longArray), 8);
189     }
190 
191     @Test
192     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
193     public static void testByteLong4(byte[] dest, long[] src, int start, int stop) {
194         for (int i = start; i < stop; i++) {
195             UNSAFE.putLongUnaligned(dest, 8 * i + baseOffset, src[i]);
196         }
197     }
198 
199     @Run(test = "testByteLong4")
200     public static void testByteLong4_runner() {
201         baseOffset = UNSAFE.ARRAY_BYTE_BASE_OFFSET;
202         runAndVerify(() -> testByteLong4(byteArray, longArray, 0, size), 0);
203     }
204 
205     @Test
206     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
207     public static void testByteLong5(byte[] dest, long[] src, int start, int stop) {
208         for (int i = start; i < stop; i++) {
209             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * (i + baseOffset), src[i]);
210         }
211     }
212 
213     @Run(test = "testByteLong5")
214     public static void testByteLong5_runner() {
215         baseOffset = 1;
216         runAndVerify(() -> testByteLong5(byteArray, longArray, 0, size-1), 8);
217     }
218 
219     @Test
220     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
221     public static void testByteByte1(byte[] dest, byte[] src) {
222         for (int i = 0; i < src.length / 8; i++) {
223             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * i, UNSAFE.getLongUnaligned(src, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * i));
224         }
225     }
226 
227     @Run(test = "testByteByte1")
228     public static void testByteByte1_runner() {
229         runAndVerify2(() -> testByteByte1(byteArray, byteArray), 0);
230     }
231 
232     // It would be legal to vectorize this one but it's not currently
233     @Test
234     //@IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
235     public static void testByteByte2(byte[] dest, byte[] src) {
236         for (int i = 1; i < src.length / 8; i++) {
237             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * (i - 1), UNSAFE.getLongUnaligned(src, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * i));
238         }
239     }
240 
241     @Run(test = "testByteByte2")
242     public static void testByteByte2_runner() {
243         runAndVerify2(() -> testByteByte2(byteArray, byteArray), -8);
244     }
245 
246     @Test
247     @IR(failOn = { IRNode.LOAD_VECTOR_L, IRNode.STORE_VECTOR })
248     public static void testByteByte3(byte[] dest, byte[] src) {
249         for (int i = 0; i < src.length / 8 - 1; i++) {
250             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * (i + 1), UNSAFE.getLongUnaligned(src, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * i));
251         }
252     }
253 
254     @Run(test = "testByteByte3")
255     public static void testByteByte3_runner() {
256         runAndVerify2(() -> testByteByte3(byteArray, byteArray), 8);
257     }
258 
259     @Test
260     @IR(failOn = { IRNode.LOAD_VECTOR_L, IRNode.STORE_VECTOR })
261     public static void testByteByte4(byte[] dest, byte[] src, int start, int stop) {
262         for (int i = start; i < stop; i++) {
263             UNSAFE.putLongUnaligned(dest, 8 * i + baseOffset, UNSAFE.getLongUnaligned(src, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * i));
264         }
265     }
266 
267     @Run(test = "testByteByte4")
268     public static void testByteByte4_runner() {
269         baseOffset = UNSAFE.ARRAY_BYTE_BASE_OFFSET;
270         runAndVerify2(() -> testByteByte4(byteArray, byteArray, 0, size), 0);
271     }
272 
273     @Test
274     @IR(failOn = { IRNode.LOAD_VECTOR_L, IRNode.STORE_VECTOR })
275     public static void testByteByte5(byte[] dest, byte[] src, int start, int stop) {
276         for (int i = start; i < stop; i++) {
277             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * (i + baseOffset), UNSAFE.getLongUnaligned(src, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * i));
278         }
279     }
280 
281     @Run(test = "testByteByte5")
282     public static void testByteByte5_runner() {
283         baseOffset = 1;
284         runAndVerify2(() -> testByteByte5(byteArray, byteArray, 0, size-1), 8);
285     }
286 
287     @Test
288     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
289     public static void testOffHeapLong1(long dest, long[] src) {
290         for (int i = 0; i < src.length; i++) {
291             UNSAFE.putLongUnaligned(null, dest + 8 * i, src[i]);
292         }
293     }
294 
295     @Run(test = "testOffHeapLong1")
296     public static void testOffHeapLong1_runner() {
297         runAndVerify3(() -> testOffHeapLong1(baseOffHeap, longArray), 0);
298     }
299 
300     @Test
301     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
302     public static void testOffHeapLong2(long dest, long[] src) {
303         for (int i = 1; i < src.length; i++) {
304             UNSAFE.putLongUnaligned(null, dest + 8 * (i - 1), src[i]);
305         }
306     }
307 
308     @Run(test = "testOffHeapLong2")
309     public static void testOffHeapLong2_runner() {
310         runAndVerify3(() -> testOffHeapLong2(baseOffHeap, longArray), -8);
311     }
312 
313     @Test
314     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
315     public static void testOffHeapLong3(long dest, long[] src) {
316         for (int i = 0; i < src.length - 1; i++) {
317             UNSAFE.putLongUnaligned(null, dest + 8 * (i + 1), src[i]);
318         }
319     }
320 
321     @Run(test = "testOffHeapLong3")
322     public static void testOffHeapLong3_runner() {
323         runAndVerify3(() -> testOffHeapLong3(baseOffHeap, longArray), 8);
324     }
325 
326     @Test
327     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
328     public static void testOffHeapLong4(long dest, long[] src, int start, int stop) {
329         for (int i = start; i < stop; i++) {
330             UNSAFE.putLongUnaligned(null, dest + 8 * i + baseOffset, src[i]);
331         }
332     }
333 
334     @Run(test = "testOffHeapLong4")
335     public static void testOffHeapLong4_runner() {
336         baseOffset = 8;
337         runAndVerify3(() -> testOffHeapLong4(baseOffHeap, longArray, 0, size-1), 8);
338     }
339 }