Push of code reflection prototype
Hannes Greule
hannesgreule at outlook.de
Tue Jan 16 22:28:14 UTC 2024
Hi Paul,
first of all, great work by the team!
I already played around with it, and I already ran into some limitations
(as expected at this state). After some trial-and-error, I ended up with
a main method that interprets itself:
import java.lang.runtime.CodeReflection;
import java.lang.reflect.code.interpreter.Interpreter;
import java.lang.reflect.Method;
import java.lang.reflect.code.op.CoreOps;
import java.lang.invoke.MethodHandles;
import java.util.List;
public class FirstSteps {
@CodeReflection
public static void main(String[] args) throws Throwable {
Method method =
FirstSteps.class.getDeclaredMethod("main", String.class.arrayType());
CoreOps.FuncOp func = method.getCodeModel().orElseThrow();
Interpreter.invoke(MethodHandles.lookup(), func,
List.of((Object) args));
}
}
As expected, this method results in an exception sooner or later, but I
was surprised when seeing the type of the exception:
Exception in thread "main" java.lang.ClassCastException: class
java.lang.StackOverflowError cannot be cast to class
java.lang.RuntimeException (java.lang.StackOverflowError and
java.lang.RuntimeException are in module java.base of loader 'bootstrap')
I had to look closely at other "sneakyThrows" methods to understand the
issue. The throw has to be *in* the erase method[1] instead of returning
the exception. Other implementations often seem to be void, but having
an exception as return type and throwing at the call site might actually
be cleaner for understanding and code analysis.
Simply changing the return to throw was enough for me to get the actual
exception in the stacktrace.
Now, I could open a PR fixing this small issue, but I'm wondering if you
already want "outside" contributions (probably also addressing other
unsupported things?), and if so, does the repo also needs a bug entry
per PR?
I'm looking forward for more of this project.
Greetings,
Hannes
[1]
https://github.com/openjdk/babylon/blob/code-reflection/src/java.base/share/classes/java/lang/reflect/code/interpreter/Interpreter.java#L363
On 16.01.2024 21:29, Paul Sandoz wrote:
> Hi,
>
> I have just pushed the code reflection prototype to the babylon repository under the code-reflection branch [1]. The Babylon JDK builds like any other JDK.
>
> As of now there is minimal documentation. I recommend looking at the tests located here:
>
> - Runtime tests
> test/jdk/java/lang/reflect/code/
> https://github.com/openjdk/babylon/tree/code-reflection/test/jdk/java/lang/reflect/code
>
> - Compiler tests (these are useful to help understand how Java programs are modeled)
> test/langtools/tools/javac/reflect/
> https://github.com/openjdk/babylon/tree/code-reflection/test/langtools/tools/javac/reflect/
>
> We have used this prototype to experiment with various code reflection use cases, which in turn has helped provide important feedback on the design. We shall now continue this process in the open. I would encourage others to do the same. Since it’s a prototype it may well fail in unfriendly ways or behave strangely so please be gentle and understanding :-)
>
> Over the next few weeks we will publish some documents explaining code reflection using specific use cases. Two of those already have tests in the repository, forward auto differentiation [2], and emulating C# LINQ [3]. We will write and publish a third document with code on implementing Triton programs ("GPU programming for neural networks”) in Java [4].
>
> In addition will be, when ready, releasing work related to our Heterogenous Accelerator Toolkit (HAT) and GPU programing model for Java that leverages Babylon and Panama.
>
> Thanks,
> Paul.
>
> [1] https://github.com/openjdk/babylon/tree/code-reflection
> [2] https://github.com/openjdk/babylon/tree/code-reflection/test/jdk/java/lang/reflect/code/ad
> [3] https://github.com/openjdk/babylon/blob/code-reflection/test/jdk/java/lang/reflect/code/TestLinqUsingQuotable.java
> [4] https://openai.com/research/triton
More information about the babylon-dev
mailing list