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