Skip to content

Conversation

@andreaTP
Copy link
Collaborator

@andreaTP andreaTP commented Oct 3, 2024

This builds on top of #552 , so review only the second commit here.

Supersede and finalize the effort started in #494

Notes:

  • I couldn't get rid of the generated Machine implementation to get the interoperability working
  • this can be ported to an annotation processor if required, but there are a few limitations in reading files from the disk there

Copy link
Collaborator

@electrum electrum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!

@electrum
Copy link
Collaborator

electrum commented Oct 4, 2024

Let me see if I can change the generated module so that we don't need to generate the machine.

@electrum
Copy link
Collaborator

electrum commented Oct 4, 2024

It's mostly done, but I'll need to finish it tomorrow: https://github.com/electrum/chicory/commits/aot-machine

@andreaTP
Copy link
Collaborator Author

andreaTP commented Oct 4, 2024

Let me see if I can change the generated module so that we don't need to generate the machine.

That would be really cool, we still need to generate a wrapper(or something) usable by the user as directly referencing the generated .class horribly breaks the IDE.

@andreaTP andreaTP force-pushed the aot-maven-plugin-rev2 branch 3 times, most recently from 0ece6c8 to 617d65e Compare October 4, 2024 15:40
@andreaTP
Copy link
Collaborator Author

andreaTP commented Oct 4, 2024

Things are coming together pretty nicely!
Thanks a lot @electrum for supporting the effort.

return this;
}

public Builder withSkipImportMapping(boolean s) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding this, we should refactor the AOT compiler so that it doesn't need Instance, as it actually only uses Module. The reason it takes Instance is because it implements the Machine interface. But the compilation itself doesn't use it.

I'll put up a PR to refactor it. I like that these changes push the code to have a better structure.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this approach and I'll follow up, let me have a little more slack time 😅


<name>Chicory - wabt</name>
<description>wabt tools running in pure Java with Chicory</description>
<description>wabt tools compiled to pure Java with Chicory</description>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technical "compiled to pure JVM bytecode" but that doesn't sound as nice 😆

wabt/pom.xml Outdated
<dependencies>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>aot-runtime</artifactId>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is why we need to add it to the BOM

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding here is that we want to add to the bom only modules that are actually going to make the "supported" 1.0.
The aot is still out of this category imho(needs a bit more investment), I aim for it to be next, and I'm happy to help whenever possible, but I'm a bit on the fence of adding it to the bom now as it might create misaligned expectations.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the latest changes in #565, aot-runtime is no longer needed at all!

private Instance.Builder quickJsInstanceBuilder() {
return Instance.builder(
Parser.parse(
MachinesTest.class.getResourceAsStream(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use getclass()

}

@Test
public void shouldUseMachineCallOnlyForExport() throws Exception {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move this test to the AOT module? It's not dependent on precompilation

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good suggestion 👍 it's just a sanity check from the prev implementation of call_indirect at the end of the day.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out that is not dependent on precompilation, but it uses wabt and moving it to aot introduces a circular dependency.
Let's keep it as a follow up.

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<configuration>
<goalPrefix>prefix</goalPrefix>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be chicory?

Comment on lines 71 to 78
var instance =
Instance.builder(Parser.parse(wasmFile))
.withMachineFactory(inst -> new AotMachine(name, inst))
.withStart(false)
.withInitialize(false)
.withSkipImportMapping(true)
.build();
var compiled = ((AotMachine) instance.getMachine()).compiledClass();
Copy link
Collaborator

@electrum electrum Oct 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With #565, this can become

var module = Parser.parse(wasmFile);
var result = AotCompiler.compileModule(module, name);
var compiled = result.classBytes();

It returns Map<String, byte[]> because we'll need to support multiple classes for method splitting and possibly other features. Any additional classes will be nested classes of the provided class name.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like an ideal API, just give me a little to review 565 👍 thanks a lot for the support!

Copy link
Collaborator

@electrum electrum Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the API to allow returning multiple classes, which is needed for inlining AotMethods, method splitting, etc. With that change, this code can become something like

for (Entry<String, byte[]> entry : result.classBytes()) {
    var binaryName = entry.getKey().replace('.', '/') + ".class"
    var targetFile = targetClassFolder.toPath().resolve();
    try {
        Files.write(targetFile, entry.getValue());
    } catch (IOException e) {
        throw new MojoExecutionException("Failed to write " + targetFile, e);
    }
}

Note that the configured class name must not be a nested name like x.y.Foo.Bar as it's not possible to differentiate this from an uppercase Foo package, and more importantly, generating code into a nested class is not possible (see this and this for an idea of why).

@andreaTP andreaTP force-pushed the aot-maven-plugin-rev2 branch from 617d65e to 7d6ba99 Compare October 12, 2024 11:43
var split = name.split("\\.");
var finalFolder = targetClassFolder.toPath();
var finalSourceFolder = targetSourceFolder.toPath();
for (int i = 0; i < (split.length - 1); i++) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code is a little messy, but should be easy to improve over it.

Copy link
Collaborator Author

@andreaTP andreaTP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated everything on top of the great latest improvement done by @electrum .
I go ahead and merge this, we can fix on top any leftover.

@andreaTP andreaTP merged commit f9ed8c6 into dylibso:main Oct 12, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants