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