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         if (ByteOrder.nativeOrder() != ByteOrder.LITTLE_ENDIAN) {
 54             throw new RuntimeException("fix test that was written for a little endian platform");
 55         }
 56         TestFramework.runWithFlags("--add-modules", "java.base", "--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED");
 57     }
 58 
 59     static int size = 1024;
 60     static byte[] byteArray = new byte[size * 8];
 61     static long[] longArray = new long[size];
 62     static byte[] verifyByteArray = new byte[size * 8];
 63     static long[] verifyLongArray = new long[size];
 64     static long baseOffset = 0;
 65     static long baseOffHeap = UNSAFE.allocateMemory(size * 8);
 66 
 67 
 68     static {
 69         for (int i = 0; i < verifyByteArray.length; i++) {
 70             verifyByteArray[i] = (byte)RANDOM.nextInt(Byte.MAX_VALUE);
 71         }
 72         for (int i = 0; i < verifyLongArray.length; i++) {
 73             verifyLongArray[i] = 0;
 74             for (int j = 0; j < 8; j++) {
 75                 verifyLongArray[i] = verifyLongArray[i] | (((long)verifyByteArray[8 * i + j]) << 8 * j);
 76             }
 77         }
 78     }
 79 
 80     static private void runAndVerify(Runnable test, int offset) {
 81         System.arraycopy(verifyLongArray, 0, longArray, 0, longArray.length);
 82         Arrays.fill(byteArray, (byte)0);
 83         test.run();
 84         int i;
 85         for (i = 0; i < Math.max(offset, 0); i++) {
 86             if (byteArray[i] != 0) {
 87                 throw new RuntimeException("Incorrect result at " + i + " " + byteArray[i] + " != 0");
 88             }
 89         }
 90         for (; i < Math.min(byteArray.length + offset, byteArray.length); i++) {
 91             if (byteArray[i] != verifyByteArray[i - offset]) {
 92                 throw new RuntimeException("Incorrect result at " + i + " " + byteArray[i] + " != " + verifyByteArray[i-offset]);
 93             }
 94         }
 95         for (; i < byteArray.length; i++) {
 96             if (byteArray[i] != 0) {
 97                 throw new RuntimeException("Incorrect result at " + i + " " + byteArray[i] + " != 0");
 98             }
 99         }
100     }
101 
102     static private void runAndVerify2(Runnable test, int offset) {
103         System.arraycopy(verifyByteArray, 0, byteArray, 0, byteArray.length);
104         test.run();
105         int i;
106         for (i = 0; i < Math.max(offset, 0); i++) {
107             if (byteArray[i] != verifyByteArray[i]) {
108                 throw new RuntimeException("Incorrect result at " + i + " " + byteArray[i] + " != " + verifyByteArray[i]);
109             }
110         }
111         for (; i < Math.min(byteArray.length + offset, byteArray.length); i++) {
112             int val = offset > 0 ? verifyByteArray[(i-offset) % 8] : verifyByteArray[i-offset];
113             if (byteArray[i] != val) {
114                 throw new RuntimeException("Incorrect result at " + i + " " + byteArray[i] + " != " + verifyByteArray[i-offset]);
115             }
116         }
117         for (; i < byteArray.length; i++) {
118             if (byteArray[i] != verifyByteArray[i]) {
119                 throw new RuntimeException("Incorrect result at " + i + " " + byteArray[i] + " != " + verifyByteArray[i]);
120             }
121         }
122     }
123 
124 
125     static private void runAndVerify3(Runnable test, int offset) {
126         System.arraycopy(verifyLongArray, 0, longArray, 0, longArray.length);
127         for (int i = 0; i < size * 8; i++) {
128             UNSAFE.putByte(null, baseOffHeap + i, (byte)0);
129         }
130         test.run();
131         int i;
132         for (i = 0; i < Math.max(offset, 0); i++) {
133             if (UNSAFE.getByte(null, baseOffHeap + i) != 0) {
134                 throw new RuntimeException("Incorrect result at " + i + " " + byteArray[i] + " != 0");
135             }
136         }
137         for (; i < Math.min(size * 8 + offset, size * 8); i++) {
138             if (UNSAFE.getByte(null, baseOffHeap + i) != verifyByteArray[i - offset]) {
139                 throw new RuntimeException("Incorrect result at " + i + " " + byteArray[i] + " != " + verifyByteArray[i-offset]);
140             }
141         }
142         for (; i < byteArray.length; i++) {
143             if (UNSAFE.getByte(null, baseOffHeap + i) != 0) {
144                 throw new RuntimeException("Incorrect result at " + i + " " + byteArray[i] + " != 0");
145             }
146         }
147     }
148 
149     @Test
150     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
151     public static void testByteLong1(byte[] dest, long[] src) {
152         for (int i = 0; i < src.length; i++) {
153             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * i, src[i]);
154         }
155     }
156 
157     @Run(test = "testByteLong1")
158     public static void testByteLong1_runner() {
159         runAndVerify(() -> testByteLong1(byteArray, longArray), 0);
160     }
161 
162     @Test
163     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
164     public static void testByteLong2(byte[] dest, long[] src) {
165         for (int i = 1; i < src.length; i++) {
166             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * (i - 1), src[i]);
167         }
168     }
169 
170     @Run(test = "testByteLong2")
171     public static void testByteLong2_runner() {
172         runAndVerify(() -> testByteLong2(byteArray, longArray), -8);
173     }
174 
175     @Test
176     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
177     public static void testByteLong3(byte[] dest, long[] src) {
178         for (int i = 0; i < src.length - 1; i++) {
179             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * (i + 1), src[i]);
180         }
181     }
182 
183     @Run(test = "testByteLong3")
184     public static void testByteLong3_runner() {
185         runAndVerify(() -> testByteLong3(byteArray, longArray), 8);
186     }
187 
188     @Test
189     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" },
190         applyIf = {"AlignVector", "false"})
191     // AlignVector cannot guarantee that invar is aligned.
192     public static void testByteLong4(byte[] dest, long[] src, int start, int stop) {
193         for (int i = start; i < stop; i++) {
194             UNSAFE.putLongUnaligned(dest, 8 * i + baseOffset, src[i]);
195         }
196     }
197 
198     @Run(test = "testByteLong4")
199     public static void testByteLong4_runner() {
200         baseOffset = UNSAFE.ARRAY_BYTE_BASE_OFFSET;
201         runAndVerify(() -> testByteLong4(byteArray, longArray, 0, size), 0);
202     }
203 
204     @Test
205     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
206     public static void testByteLong5(byte[] dest, long[] src, int start, int stop) {
207         for (int i = start; i < stop; i++) {
208             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * (i + baseOffset), src[i]);
209         }
210     }
211 
212     @Run(test = "testByteLong5")
213     public static void testByteLong5_runner() {
214         baseOffset = 1;
215         runAndVerify(() -> testByteLong5(byteArray, longArray, 0, size-1), 8);
216     }
217 
218     @Test
219     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
220     public static void testByteByte1(byte[] dest, byte[] src) {
221         for (int i = 0; i < src.length / 8; i++) {
222             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * i, UNSAFE.getLongUnaligned(src, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * i));
223         }
224     }
225 
226     @Run(test = "testByteByte1")
227     public static void testByteByte1_runner() {
228         runAndVerify2(() -> testByteByte1(byteArray, byteArray), 0);
229     }
230 
231     @Test
232     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
233     public static void testByteByte2(byte[] dest, byte[] src) {
234         for (int i = 1; i < src.length / 8; i++) {
235             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * (i - 1), UNSAFE.getLongUnaligned(src, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * i));
236         }
237     }
238 
239     @Run(test = "testByteByte2")
240     public static void testByteByte2_runner() {
241         runAndVerify2(() -> testByteByte2(byteArray, byteArray), -8);
242     }
243 
244     @Test
245     @IR(failOn = { IRNode.LOAD_VECTOR_L, IRNode.STORE_VECTOR })
246     public static void testByteByte3(byte[] dest, byte[] src) {
247         for (int i = 0; i < src.length / 8 - 1; i++) {
248             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * (i + 1), UNSAFE.getLongUnaligned(src, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * i));
249         }
250     }
251 
252     @Run(test = "testByteByte3")
253     public static void testByteByte3_runner() {
254         runAndVerify2(() -> testByteByte3(byteArray, byteArray), 8);
255     }
256 
257     @Test
258     @IR(failOn = { IRNode.LOAD_VECTOR_L, IRNode.STORE_VECTOR })
259     public static void testByteByte4(byte[] dest, byte[] src, int start, int stop) {
260         for (int i = start; i < stop; i++) {
261             UNSAFE.putLongUnaligned(dest, 8 * i + baseOffset, UNSAFE.getLongUnaligned(src, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * i));
262         }
263     }
264 
265     @Run(test = "testByteByte4")
266     public static void testByteByte4_runner() {
267         baseOffset = UNSAFE.ARRAY_BYTE_BASE_OFFSET;
268         runAndVerify2(() -> testByteByte4(byteArray, byteArray, 0, size), 0);
269     }
270 
271     @Test
272     @IR(failOn = { IRNode.LOAD_VECTOR_L, IRNode.STORE_VECTOR })
273     public static void testByteByte5(byte[] dest, byte[] src, int start, int stop) {
274         for (int i = start; i < stop; i++) {
275             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * (i + baseOffset), UNSAFE.getLongUnaligned(src, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * i));
276         }
277     }
278 
279     @Run(test = "testByteByte5")
280     public static void testByteByte5_runner() {
281         baseOffset = 1;
282         runAndVerify2(() -> testByteByte5(byteArray, byteArray, 0, size-1), 8);
283     }
284 
285     @Test
286     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
287     public static void testOffHeapLong1(long dest, long[] src) {
288         for (int i = 0; i < src.length; i++) {
289             UNSAFE.putLongUnaligned(null, dest + 8 * i, src[i]);
290         }
291     }
292 
293     @Run(test = "testOffHeapLong1")
294     public static void testOffHeapLong1_runner() {
295         runAndVerify3(() -> testOffHeapLong1(baseOffHeap, longArray), 0);
296     }
297 
298     @Test
299     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
300     public static void testOffHeapLong2(long dest, long[] src) {
301         for (int i = 1; i < src.length; i++) {
302             UNSAFE.putLongUnaligned(null, dest + 8 * (i - 1), src[i]);
303         }
304     }
305 
306     @Run(test = "testOffHeapLong2")
307     public static void testOffHeapLong2_runner() {
308         runAndVerify3(() -> testOffHeapLong2(baseOffHeap, longArray), -8);
309     }
310 
311     @Test
312     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
313     public static void testOffHeapLong3(long dest, long[] src) {
314         for (int i = 0; i < src.length - 1; i++) {
315             UNSAFE.putLongUnaligned(null, dest + 8 * (i + 1), src[i]);
316         }
317     }
318 
319     @Run(test = "testOffHeapLong3")
320     public static void testOffHeapLong3_runner() {
321         runAndVerify3(() -> testOffHeapLong3(baseOffHeap, longArray), 8);
322     }
323 
324     @Test
325     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" },
326         applyIf = {"AlignVector", "false"})
327     // AlignVector cannot guarantee that invar is aligned.
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 }