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 oracle.code.samples;
 26 
 27 import jdk.incubator.code.Block;
 28 import jdk.incubator.code.CodeReflection;
 29 import jdk.incubator.code.Op;
 30 import jdk.incubator.code.analysis.Inliner;
 31 import jdk.incubator.code.dialect.core.CoreOp;
 32 import jdk.incubator.code.dialect.core.CoreType;
 33 import jdk.incubator.code.dialect.java.JavaType;
 34 import jdk.incubator.code.interpreter.Interpreter;
 35 
 36 import java.lang.invoke.MethodHandles;
 37 import java.lang.reflect.Method;
 38 import java.util.List;
 39 import java.util.Optional;
 40 import java.util.stream.Stream;
 41 
 42 /**
 43  * Example of inlining based on one of the example from the code-reflection unit-tests.
 44  * This example illustrates how to embed a value into a function and propagate that
 45  * change in the code model.
 46  *
 47  * <p>
 48  *     How to run from the terminal?
 49  *     <code>
 50  *      java --add-modules jdk.incubator.code -cp target/crsamples-1.0-SNAPSHOT.jar oracle.code.samples.InlineExample
 51  *     </code>
 52  * </p>
 53  */
 54 public class InlineExample {
 55 
 56     // We are going to inline this function and specialize one of the parameters
 57     // (e.g., parameter b) to a constant value.
 58     // The new function will contain two parameters to perform a * CONSTANT + c
 59     @CodeReflection
 60     private static float fma(float a, float b, float c) {
 61         return a * b + c;
 62     }
 63 
 64     // Utility for building a code model from a given method from a class.
 65     private static CoreOp.FuncOp buildCodeModelForMethod(Class<?> klass, String methodName) {
 66         Optional<Method> function = Stream.of(klass.getDeclaredMethods())
 67                 .filter(m -> m.getName().equals(methodName))
 68                 .findFirst();
 69         Method method = function.get();
 70         CoreOp.FuncOp funcOp = Op.ofMethod(method).get();
 71         return funcOp;
 72     }
 73 
 74     public static void main(String[] args) {
 75 
 76         // 1. Build the code model for the fma static method of this class.
 77         CoreOp.FuncOp fmaCodeModel = buildCodeModelForMethod(InlineExample.class, "fma");
 78 
 79         // 2. Builds a new FuncOp with the copy of the fmaCodeModel and with the specialized values
 80         // This example is useful, for example, to apply partial evaluation of expression at runtime,
 81         CoreOp.FuncOp f = CoreOp.func("myFunction", CoreType.functionType(JavaType.FLOAT, // return type
 82                                                                                     JavaType.FLOAT, // param 1
 83                                                                                     JavaType.FLOAT  // param 2 (the new function has 2 params
 84                                         ))
 85                 .body(blockBuilder -> {
 86                     // Get parameters for the new function
 87                     Block.Parameter parameter1 = blockBuilder.parameters().get(0);
 88                     Block.Parameter parameter2 = blockBuilder.parameters().get(1);
 89 
 90                     // Build a new op with a pre-defined constant. We will place this constant
 91                     // as one of the parameters of the function and then inline with the new values.
 92                     Op.Result myConstantValue = blockBuilder.op(CoreOp.ConstantOp.constant(JavaType.FLOAT, 50.f));
 93 
 94                     // Inline the function with the new values
 95                     Inliner.inline(blockBuilder,
 96                             fmaCodeModel,      // inline the fmaCodeModel
 97                             List.of(parameter1, myConstantValue, parameter2),  // apply the 3 parameters to the function to inline
 98                             Inliner.INLINE_RETURN);
 99                 });
100 
101         // 3. Print the resulting code model
102         System.out.println(f.toText());
103 
104         // 4. Evaluate the code model using the Code Reflection Interpreter
105         var result = Interpreter.invoke(MethodHandles.lookup(), f, 10.f, 20.f);
106         // We expect: 10 * 50 + 20 => 520.0f
107         System.out.println("Result: " + result);
108     }
109 }