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(applyIf = {"UseCompactObjectHeaders", "false"},
151         counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
152     public static void testByteLong1(byte[] dest, long[] src) {
153         for (int i = 0; i < src.length; i++) {
154             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * i, src[i]);
155         }
156     }
157 
158     @Run(test = "testByteLong1")
159     public static void testByteLong1_runner() {
160         runAndVerify(() -> testByteLong1(byteArray, longArray), 0);
161     }
162 
163     @Test
164     @IR(applyIf = {"UseCompactObjectHeaders", "false"},
165         counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
166     public static void testByteLong2(byte[] dest, long[] src) {
167         for (int i = 1; i < src.length; i++) {
168             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * (i - 1), src[i]);
169         }
170     }
171 
172     @Run(test = "testByteLong2")
173     public static void testByteLong2_runner() {
174         runAndVerify(() -> testByteLong2(byteArray, longArray), -8);
175     }
176 
177     @Test
178     @IR(applyIf = {"UseCompactObjectHeaders", "false"},
179         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         applyIf = {"AlignVector", "false"})
194     // AlignVector cannot guarantee that invar is aligned.
195     public static void testByteLong4(byte[] dest, long[] src, int start, int stop) {
196         for (int i = start; i < stop; i++) {
197             UNSAFE.putLongUnaligned(dest, 8 * i + baseOffset, src[i]);
198         }
199     }
200 
201     @Run(test = "testByteLong4")
202     public static void testByteLong4_runner() {
203         baseOffset = UNSAFE.ARRAY_BYTE_BASE_OFFSET;
204         runAndVerify(() -> testByteLong4(byteArray, longArray, 0, size), 0);
205     }
206 
207     @Test
208     @IR(applyIf = {"UseCompactObjectHeaders", "false"},
209         counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
210     public static void testByteLong5(byte[] dest, long[] src, int start, int stop) {
211         for (int i = start; i < stop; i++) {
212             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * (i + baseOffset), src[i]);
213         }
214     }
215 
216     @Run(test = "testByteLong5")
217     public static void testByteLong5_runner() {
218         baseOffset = 1;
219         runAndVerify(() -> testByteLong5(byteArray, longArray, 0, size-1), 8);
220     }
221 
222     @Test
223     @IR(applyIf = {"UseCompactObjectHeaders", "false"},
224         counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
225     public static void testByteByte1(byte[] dest, byte[] src) {
226         for (int i = 0; i < src.length / 8; i++) {
227             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * i, UNSAFE.getLongUnaligned(src, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * i));
228         }
229     }
230 
231     @Run(test = "testByteByte1")
232     public static void testByteByte1_runner() {
233         runAndVerify2(() -> testByteByte1(byteArray, byteArray), 0);
234     }
235 
236     @Test
237     @IR(applyIf = {"UseCompactObjectHeaders", "false"},
238         counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
239     public static void testByteByte2(byte[] dest, byte[] src) {
240         for (int i = 1; i < src.length / 8; i++) {
241             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * (i - 1), UNSAFE.getLongUnaligned(src, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * i));
242         }
243     }
244 
245     @Run(test = "testByteByte2")
246     public static void testByteByte2_runner() {
247         runAndVerify2(() -> testByteByte2(byteArray, byteArray), -8);
248     }
249 
250     @Test
251     @IR(failOn = { IRNode.LOAD_VECTOR_L, IRNode.STORE_VECTOR })
252     public static void testByteByte3(byte[] dest, byte[] src) {
253         for (int i = 0; i < src.length / 8 - 1; i++) {
254             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * (i + 1), UNSAFE.getLongUnaligned(src, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * i));
255         }
256     }
257 
258     @Run(test = "testByteByte3")
259     public static void testByteByte3_runner() {
260         runAndVerify2(() -> testByteByte3(byteArray, byteArray), 8);
261     }
262 
263     @Test
264     @IR(failOn = { IRNode.LOAD_VECTOR_L, IRNode.STORE_VECTOR })
265     public static void testByteByte4(byte[] dest, byte[] src, int start, int stop) {
266         for (int i = start; i < stop; i++) {
267             UNSAFE.putLongUnaligned(dest, 8 * i + baseOffset, UNSAFE.getLongUnaligned(src, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * i));
268         }
269     }
270 
271     @Run(test = "testByteByte4")
272     public static void testByteByte4_runner() {
273         baseOffset = UNSAFE.ARRAY_BYTE_BASE_OFFSET;
274         runAndVerify2(() -> testByteByte4(byteArray, byteArray, 0, size), 0);
275     }
276 
277     @Test
278     @IR(failOn = { IRNode.LOAD_VECTOR_L, IRNode.STORE_VECTOR })
279     public static void testByteByte5(byte[] dest, byte[] src, int start, int stop) {
280         for (int i = start; i < stop; i++) {
281             UNSAFE.putLongUnaligned(dest, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * (i + baseOffset), UNSAFE.getLongUnaligned(src, UNSAFE.ARRAY_BYTE_BASE_OFFSET + 8 * i));
282         }
283     }
284 
285     @Run(test = "testByteByte5")
286     public static void testByteByte5_runner() {
287         baseOffset = 1;
288         runAndVerify2(() -> testByteByte5(byteArray, byteArray, 0, size-1), 8);
289     }
290 
291     @Test
292     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
293     public static void testOffHeapLong1(long dest, long[] src) {
294         for (int i = 0; i < src.length; i++) {
295             UNSAFE.putLongUnaligned(null, dest + 8 * i, src[i]);
296         }
297     }
298 
299     @Run(test = "testOffHeapLong1")
300     public static void testOffHeapLong1_runner() {
301         runAndVerify3(() -> testOffHeapLong1(baseOffHeap, longArray), 0);
302     }
303 
304     @Test
305     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
306     public static void testOffHeapLong2(long dest, long[] src) {
307         for (int i = 1; i < src.length; i++) {
308             UNSAFE.putLongUnaligned(null, dest + 8 * (i - 1), src[i]);
309         }
310     }
311 
312     @Run(test = "testOffHeapLong2")
313     public static void testOffHeapLong2_runner() {
314         runAndVerify3(() -> testOffHeapLong2(baseOffHeap, longArray), -8);
315     }
316 
317     @Test
318     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" })
319     public static void testOffHeapLong3(long dest, long[] src) {
320         for (int i = 0; i < src.length - 1; i++) {
321             UNSAFE.putLongUnaligned(null, dest + 8 * (i + 1), src[i]);
322         }
323     }
324 
325     @Run(test = "testOffHeapLong3")
326     public static void testOffHeapLong3_runner() {
327         runAndVerify3(() -> testOffHeapLong3(baseOffHeap, longArray), 8);
328     }
329 
330     @Test
331     @IR(counts = { IRNode.LOAD_VECTOR_L, ">=1", IRNode.STORE_VECTOR, ">=1" },
332         applyIf = {"AlignVector", "false"})
333     // AlignVector cannot guarantee that invar is aligned.
334     public static void testOffHeapLong4(long dest, long[] src, int start, int stop) {
335         for (int i = start; i < stop; i++) {
336             UNSAFE.putLongUnaligned(null, dest + 8 * i + baseOffset, src[i]);
337         }
338     }
339 
340     @Run(test = "testOffHeapLong4")
341     public static void testOffHeapLong4_runner() {
342         baseOffset = 8;
343         runAndVerify3(() -> testOffHeapLong4(baseOffHeap, longArray, 0, size-1), 8);
344     }
345 }