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.NDRange;
 30 import hat.KernelContext;
 31 import hat.backend.Backend;
 32 import hat.buffer.S32Array;
 33 import jdk.incubator.code.CodeReflection;
 34 import hat.test.annotation.HatTest;
 35 import hat.test.engine.HATAsserts;
 36 
 37 import java.lang.invoke.MethodHandles;
 38 
 39 import static hat.ifacemapper.MappableIface.*;
 40 
 41 public class TestConstants {
 42 
 43     public static final int CONSTANT = 100;
 44 
 45     @CodeReflection
 46     public static void vectorWithConstants(@RO KernelContext kc, @RO S32Array arrayA, @RO S32Array arrayB, @RW S32Array arrayC) {
 47         final int BM = 100;
 48         if (kc.gix < kc.gsx) {
 49             final int valueA = arrayA.array(kc.gix);
 50             final int valueB = arrayB.array(kc.gix);
 51             arrayC.array(kc.gix, (BM + valueA + valueB));
 52         }
 53     }
 54 
 55     @CodeReflection
 56     public static void vectorWithConstants(@RO ComputeContext cc, @RO S32Array arrayA, @RO S32Array arrayB, @RW S32Array arrayC) {
 57         NDRange ndRange = NDRange.of(NDRange.Global1D.of(arrayA.length()));
 58         cc.dispatchKernel(ndRange, kc -> vectorWithConstants(kc, arrayA, arrayB, arrayC));
 59     }
 60 
 61     /**
 62      * Test to check if final values are represented in the generated code.
 63      */
 64     @HatTest
 65     public static void testConstants01() {
 66         final int size = 1024;
 67         var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
 68         var arrayA = S32Array.create(accelerator, size);
 69         var arrayB = S32Array.create(accelerator, size);
 70         var arrayC = S32Array.create(accelerator, size);
 71 
 72         arrayA.fill(i -> i);
 73         arrayB.fill(i -> 100 + i);
 74 
 75         accelerator.compute(cc ->
 76                 TestConstants.vectorWithConstants(cc, arrayA, arrayB, arrayC));
 77 
 78         S32Array test = S32Array.create(accelerator, size);
 79 
 80         for (int i = 0; i < test.length(); i++) {
 81             test.array(i, CONSTANT + arrayA.array(i) + arrayB.array(i));
 82         }
 83 
 84         for (int i = 0; i < test.length(); i++) {
 85             HATAsserts.assertEquals(test.array(i), arrayC.array(i));
 86         }
 87     }
 88 
 89     @CodeReflection
 90     public static int compute(final int valueA, final int valueB) {
 91         final int BM = 100;
 92         return BM + valueA + valueB;
 93     }
 94 
 95     @CodeReflection
 96     public static void vectorWithConstants2(@RO KernelContext kc, @RO S32Array arrayA, @RO S32Array arrayB, @RW S32Array arrayC) {
 97         if (kc.gix < kc.gsx) {
 98             final int valueA = arrayA.array(kc.gix);
 99             final int valueB = arrayB.array(kc.gix);
100             final int result = compute(valueA, valueB);
101             arrayC.array(kc.gix, result);
102         }
103     }
104 
105     @CodeReflection
106     public static void vectorWithConstants2(@RO ComputeContext cc, @RO S32Array arrayA, @RO S32Array arrayB, @RW S32Array arrayC) {
107         NDRange ndRange = NDRange.of(NDRange.Global1D.of(arrayA.length()));
108         cc.dispatchKernel(ndRange, kc -> vectorWithConstants2(kc, arrayA, arrayB, arrayC));
109     }
110 
111     /**
112      * Test to check multiple method calls that contains constants.
113      * This triggers the code model analysis for each of the reachable method before the
114      * final code gen.
115      */
116     @HatTest
117     public static void testConstants02() {
118         final int size = 1024;
119         var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
120         var arrayA = S32Array.create(accelerator, size);
121         var arrayB = S32Array.create(accelerator, size);
122         var arrayC = S32Array.create(accelerator, size);
123 
124         arrayA.fill(i -> i);
125         arrayB.fill(i -> 100 + i);
126 
127         accelerator.compute(cc ->
128                 TestConstants.vectorWithConstants2(cc, arrayA, arrayB, arrayC));
129 
130         S32Array test = S32Array.create(accelerator, size);
131 
132         for (int i = 0; i < test.length(); i++) {
133             test.array(i, CONSTANT + arrayA.array(i) + arrayB.array(i));
134         }
135 
136         for (int i = 0; i < test.length(); i++) {
137             HATAsserts.assertEquals(test.array(i), arrayC.array(i));
138         }
139     }
140 }