1 /* 2 * Copyright (c) 2025, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package hat.test; 26 27 import hat.Accelerator; 28 import hat.ComputeContext; 29 import hat.ComputeRange; 30 import hat.GlobalMesh1D; 31 import hat.KernelContext; 32 import hat.backend.Backend; 33 import hat.buffer.S32Array; 34 import jdk.incubator.code.CodeReflection; 35 import hat.test.annotation.HatTest; 36 import hat.test.engine.HatAsserts; 37 38 import java.lang.invoke.MethodHandles; 39 40 import static hat.ifacemapper.MappableIface.*; 41 42 public class TestConstants { 43 44 public static final int CONSTANT = 100; 45 46 @CodeReflection 47 public static void vectorWithConstants(@RO KernelContext kc, @RO S32Array arrayA, @RO S32Array arrayB, @RW S32Array arrayC) { 48 final int BM = 100; 49 if (kc.x < kc.gsx) { 50 final int valueA = arrayA.array(kc.x); 51 final int valueB = arrayB.array(kc.x); 52 arrayC.array(kc.x, (BM + valueA + valueB)); 53 } 54 } 55 56 @CodeReflection 57 public static void vectorWithConstants(@RO ComputeContext cc, @RO S32Array arrayA, @RO S32Array arrayB, @RW S32Array arrayC) { 58 ComputeRange computeRange = new ComputeRange(new GlobalMesh1D(arrayA.length())); 59 cc.dispatchKernel(computeRange, kc -> vectorWithConstants(kc, arrayA, arrayB, arrayC)); 60 } 61 62 /** 63 * Test to check if final values are represented in the generated code. 64 */ 65 @HatTest 66 public static void testConstants01() { 67 final int size = 1024; 68 var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST); 69 var arrayA = S32Array.create(accelerator, size); 70 var arrayB = S32Array.create(accelerator, size); 71 var arrayC = S32Array.create(accelerator, size); 72 73 arrayA.fill(i -> i); 74 arrayB.fill(i -> 100 + i); 75 76 accelerator.compute(cc -> 77 TestConstants.vectorWithConstants(cc, arrayA, arrayB, arrayC)); 78 79 S32Array test = S32Array.create(accelerator, size); 80 81 for (int i = 0; i < test.length(); i++) { 82 test.array(i, CONSTANT + arrayA.array(i) + arrayB.array(i)); 83 } 84 85 for (int i = 0; i < test.length(); i++) { 86 HatAsserts.assertEquals(test.array(i), arrayC.array(i)); 87 } 88 } 89 90 @CodeReflection 91 public static int compute(final int valueA, final int valueB) { 92 final int BM = 100; 93 return BM + valueA + valueB; 94 } 95 96 @CodeReflection 97 public static void vectorWithConstants2(@RO KernelContext kc, @RO S32Array arrayA, @RO S32Array arrayB, @RW S32Array arrayC) { 98 if (kc.x < kc.gsx) { 99 final int valueA = arrayA.array(kc.x); 100 final int valueB = arrayB.array(kc.x); 101 final int result = compute(valueA, valueB); 102 arrayC.array(kc.x, result); 103 } 104 } 105 106 @CodeReflection 107 public static void vectorWithConstants2(@RO ComputeContext cc, @RO S32Array arrayA, @RO S32Array arrayB, @RW S32Array arrayC) { 108 ComputeRange computeRange = new ComputeRange(new GlobalMesh1D(arrayA.length())); 109 cc.dispatchKernel(computeRange, kc -> vectorWithConstants2(kc, arrayA, arrayB, arrayC)); 110 } 111 112 /** 113 * Test to check multiple method calls that contains constants. 114 * This triggers the code model analysis for each of the reachable method before the 115 * final code gen. 116 */ 117 @HatTest 118 public static void testConstants02() { 119 final int size = 1024; 120 var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST); 121 var arrayA = S32Array.create(accelerator, size); 122 var arrayB = S32Array.create(accelerator, size); 123 var arrayC = S32Array.create(accelerator, size); 124 125 arrayA.fill(i -> i); 126 arrayB.fill(i -> 100 + i); 127 128 accelerator.compute(cc -> 129 TestConstants.vectorWithConstants2(cc, arrayA, arrayB, arrayC)); 130 131 S32Array test = S32Array.create(accelerator, size); 132 133 for (int i = 0; i < test.length(); i++) { 134 test.array(i, CONSTANT + arrayA.array(i) + arrayB.array(i)); 135 } 136 137 for (int i = 0; i < test.length(); i++) { 138 HatAsserts.assertEquals(test.array(i), arrayC.array(i)); 139 } 140 } 141 }