[code-reflection] RFR: New Examples: Dialects and Dynamic Function Builds [v2]
Paul Sandoz
psandoz at openjdk.org
Fri Sep 5 19:38:27 UTC 2025
On Fri, 5 Sep 2025 13:57:07 GMT, Juan Fumero <jfumero at openjdk.org> wrote:
>> - `DialectSample`: Example of how to extends the code reflection `Op` to create a new dialect.
>> - `DynamicFunctionBuild`: Example of how to create a new function dynamically to compute the inverse of a square root. The code model is built dynamically for a new method and it is evaluated in the `Interpreter`.
>>
>> How to run?
>>
>>
>> mvn clean package
>> java --enable-preview -cp target/crsamples-1.0-SNAPSHOT.jar oracle.code.samples.DialectSample
>> java --enable-preview -cp target/crsamples-1.0-SNAPSHOT.jar oracle.code.samples.DynamicFunctionBuild
>
> Juan Fumero has updated the pull request incrementally with one additional commit since the last revision:
>
> minor clean up
cr-examples/samples/src/main/java/oracle/code/samples/DialectSample.java line 80:
> 78: private TypeElement typeElement;
> 79:
> 80: MyAdd(String name, List<Value> operands, TypeElement typeElement) {
The operation name should not generally be something the user provides when constructing an operation. The name is for human readability in text, and the key that allows (re-)construction when parsing text back into code models. It's something that should be derived from other inputs and/or constant data. Further, its not something we should really use for pattern matching.
I would like to see if we could remove the name from the state of Op. `OpWriter` of course needs it. Maybe by default the written operation name is the operation (fully qualified) class name, and implementations can override it maybe via a separate interface. We might be able to group the name and externalization of state under one concept for reading/parsing.
cr-examples/samples/src/main/java/oracle/code/samples/DialectSample.java line 101:
> 99: @Override
> 100: public Map<String, Object> externalize() {
> 101: return Map.of("", this.typeElement);
The `typeElement` is already part of the operation, it's result type so this is duplicating information. Instead consider a property of addition as an example e.g., the kind of addition for integral types like signed, unsigned, or saturating.
cr-examples/samples/src/main/java/oracle/code/samples/DialectSample.java line 221:
> 219: @Override
> 220: public Map<String, Object> externalize() {
> 221: return Map.of("", this.typeDescriptor);
This can be the name of the function to invoke, which you are currently using as the operation name.
-------------
PR Review Comment: https://git.openjdk.org/babylon/pull/549#discussion_r2325895385
PR Review Comment: https://git.openjdk.org/babylon/pull/549#discussion_r2325868741
PR Review Comment: https://git.openjdk.org/babylon/pull/549#discussion_r2325896949
More information about the babylon-dev
mailing list