-
Notifications
You must be signed in to change notification settings - Fork 59
Aot pre-compiled machine #556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <parent> | ||
| <groupId>com.dylibso.chicory</groupId> | ||
| <artifactId>chicory</artifactId> | ||
| <version>999-SNAPSHOT</version> | ||
| <relativePath>../pom.xml</relativePath> | ||
| </parent> | ||
| <artifactId>aot-maven-plugin</artifactId> | ||
| <packaging>maven-plugin</packaging> | ||
| <name>Chicory - AOT Maven Plugin</name> | ||
| <description>A Maven Plugin to generate a package for running WASM</description> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.dylibso.chicory</groupId> | ||
| <artifactId>aot</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.dylibso.chicory</groupId> | ||
| <artifactId>runtime</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.dylibso.chicory</groupId> | ||
| <artifactId>wasm</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.github.javaparser</groupId> | ||
| <artifactId>javaparser-core</artifactId> | ||
| <version>${javaparser.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.maven</groupId> | ||
| <artifactId>maven-core</artifactId> | ||
| <version>${maven-plugin-api.version}</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.maven</groupId> | ||
| <artifactId>maven-model</artifactId> | ||
| <version>${maven-plugin-api.version}</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.maven</groupId> | ||
| <artifactId>maven-plugin-api</artifactId> | ||
| <version>${maven-plugin-api.version}</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.maven.plugin-tools</groupId> | ||
| <artifactId>maven-plugin-annotations</artifactId> | ||
| <version>${maven-plugin-annotations.version}</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-plugin-plugin</artifactId> | ||
| <configuration> | ||
| <goalPrefix>chicory</goalPrefix> | ||
| </configuration> | ||
| <executions> | ||
| <execution> | ||
| <id>default-descriptor</id> | ||
| <phase>process-classes</phase> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
134 changes: 134 additions & 0 deletions
134
aot-maven-plugin/src/main/java/com/dylibso/chicory/maven/aot/AotGenMojo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| package com.dylibso.chicory.maven.aot; | ||
|
|
||
| import static com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType; | ||
| import static com.github.javaparser.StaticJavaParser.parseType; | ||
|
|
||
| import com.dylibso.chicory.aot.AotCompiler; | ||
| import com.dylibso.chicory.runtime.Machine; | ||
| import com.dylibso.chicory.wasm.Parser; | ||
| import com.github.javaparser.ast.CompilationUnit; | ||
| import com.github.javaparser.ast.Modifier; | ||
| import com.github.javaparser.ast.NodeList; | ||
| import com.github.javaparser.ast.expr.NameExpr; | ||
| import com.github.javaparser.ast.expr.ObjectCreationExpr; | ||
| import com.github.javaparser.ast.stmt.ReturnStmt; | ||
| import com.github.javaparser.utils.SourceRoot; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.util.Map; | ||
| import org.apache.maven.model.Resource; | ||
| import org.apache.maven.plugin.AbstractMojo; | ||
| import org.apache.maven.plugin.MojoExecutionException; | ||
| import org.apache.maven.plugins.annotations.LifecyclePhase; | ||
| import org.apache.maven.plugins.annotations.Mojo; | ||
| import org.apache.maven.plugins.annotations.Parameter; | ||
| import org.apache.maven.project.MavenProject; | ||
|
|
||
| /** | ||
| * This plugin generates an invokable library from the compiled Wasm | ||
| */ | ||
| @Mojo(name = "wasm-aot-gen", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true) | ||
| public class AotGenMojo extends AbstractMojo { | ||
|
|
||
| /** | ||
| * the wasm module to be used | ||
| */ | ||
| @Parameter(required = true) | ||
| private File wasmFile; | ||
|
|
||
| /** | ||
| * the name to be used by the generated class | ||
| */ | ||
| @Parameter(required = true) | ||
| private String name; | ||
|
|
||
| /** | ||
| * the target folder to generate classes | ||
| */ | ||
| @Parameter( | ||
| required = true, | ||
| defaultValue = "${project.basedir}/target/generated-resources/chicory-aot") | ||
| private File targetClassFolder; | ||
|
|
||
| /** | ||
| * the target source folder to generate the Machine implementation | ||
| */ | ||
| @Parameter( | ||
| required = true, | ||
| defaultValue = "${project.basedir}/target/generated-sources/chicory-aot") | ||
| private File targetSourceFolder; | ||
|
|
||
| /** | ||
| * The current Maven project. | ||
| */ | ||
| @Parameter(property = "project", required = true, readonly = true) | ||
| private MavenProject project; | ||
|
|
||
| @Override | ||
| @SuppressWarnings({"StringSplitter", "deprecation"}) | ||
| public void execute() throws MojoExecutionException { | ||
| var module = Parser.parse(wasmFile); | ||
| var result = AotCompiler.compileModule(module, name); | ||
| var split = name.split("\\."); | ||
| var finalFolder = targetClassFolder.toPath(); | ||
| var finalSourceFolder = targetSourceFolder.toPath(); | ||
| for (int i = 0; i < (split.length - 1); i++) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| finalFolder = finalFolder.resolve(split[i]); | ||
| finalSourceFolder = finalSourceFolder.resolve(split[i]); | ||
| } | ||
| finalFolder.toFile().mkdirs(); | ||
| var packageName = split[0]; | ||
| for (int i = 1; i < (split.length - 1); i++) { | ||
| packageName += "." + split[i]; | ||
| } | ||
|
|
||
| // Generate static Machine implementation | ||
| finalSourceFolder.toFile().mkdirs(); | ||
| final SourceRoot dest = new SourceRoot(finalSourceFolder); | ||
|
|
||
| var machineName = split[split.length - 1] + "MachineFactory"; | ||
|
|
||
| var cu = new CompilationUnit(packageName); | ||
| cu.setPackageDeclaration(packageName); | ||
| cu.setStorage(finalSourceFolder.resolve(machineName + ".java")); | ||
|
|
||
| cu.addImport("com.dylibso.chicory.runtime.Instance"); | ||
| cu.addImport("com.dylibso.chicory.runtime.Machine"); | ||
|
|
||
| var clazz = cu.addClass(machineName, Modifier.Keyword.PUBLIC, Modifier.Keyword.FINAL); | ||
|
|
||
| var constr = clazz.addConstructor(Modifier.Keyword.PRIVATE); | ||
| constr.createBody(); | ||
|
|
||
| var method = clazz.addMethod("create", Modifier.Keyword.PUBLIC, Modifier.Keyword.STATIC); | ||
| method.addParameter(parseType("Instance"), "instance"); | ||
| method.setType(Machine.class); | ||
| var methodBody = method.createBody(); | ||
|
|
||
| var constructorInvocation = | ||
| new ObjectCreationExpr( | ||
| null, | ||
| parseClassOrInterfaceType(name), | ||
| NodeList.nodeList(new NameExpr("instance"))); | ||
| methodBody.addStatement(new ReturnStmt(constructorInvocation)); | ||
|
|
||
| dest.add(cu); | ||
| dest.saveAll(); | ||
|
|
||
| for (Map.Entry<String, byte[]> entry : result.classBytes().entrySet()) { | ||
| var binaryName = entry.getKey().replace('.', '/') + ".class"; | ||
| var targetFile = targetClassFolder.toPath().resolve(binaryName); | ||
| try { | ||
| Files.write(targetFile, entry.getValue()); | ||
| } catch (IOException e) { | ||
| throw new MojoExecutionException("Failed to write " + targetFile, e); | ||
| } | ||
| } | ||
|
|
||
| Resource resource = new Resource(); | ||
| resource.setDirectory(targetClassFolder.getPath()); | ||
| project.addResource(resource); | ||
| project.addCompileSourceRoot(targetSourceFolder.getPath()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <parent> | ||
| <groupId>com.dylibso.chicory</groupId> | ||
| <artifactId>chicory</artifactId> | ||
| <version>999-SNAPSHOT</version> | ||
| </parent> | ||
| <artifactId>machine-tests</artifactId> | ||
| <packaging>jar</packaging> | ||
|
|
||
| <name>Chicory - Machines tests</name> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.dylibso.chicory</groupId> | ||
| <artifactId>runtime</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.dylibso.chicory</groupId> | ||
| <artifactId>wasm</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.dylibso.chicory</groupId> | ||
| <artifactId>aot</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.dylibso.chicory</groupId> | ||
| <artifactId>log</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.dylibso.chicory</groupId> | ||
| <artifactId>wasi</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.dylibso.chicory</groupId> | ||
| <artifactId>wasm-corpus</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.jimfs</groupId> | ||
| <artifactId>jimfs</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-api</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>com.dylibso.chicory</groupId> | ||
| <artifactId>aot-maven-plugin</artifactId> | ||
| <executions> | ||
| <execution> | ||
| <id>compile-quickjs</id> | ||
| <goals> | ||
| <goal>wasm-aot-gen</goal> | ||
| </goals> | ||
| <configuration> | ||
| <name>com.dylibso.chicory.testing.gen.QuickJS</name> | ||
| <wasmFile>${project.basedir}/../wasm-corpus/src/main/resources/compiled/quickjs-provider.javy-dynamic.wasm</wasmFile> | ||
| </configuration> | ||
| </execution> | ||
| <execution> | ||
| <id>compile-dynamic-js</id> | ||
| <goals> | ||
| <goal>wasm-aot-gen</goal> | ||
| </goals> | ||
| <configuration> | ||
| <name>com.dylibso.chicory.testing.gen.DynamicHelloJS</name> | ||
| <wasmFile>${project.basedir}/../wasm-corpus/src/main/resources/compiled/hello-world.js.javy-dynamic.wasm</wasmFile> | ||
| </configuration> | ||
| </execution> | ||
| <execution> | ||
| <id>wat2wasm</id> | ||
| <goals> | ||
| <goal>wasm-aot-gen</goal> | ||
| </goals> | ||
| <configuration> | ||
| <name>com.dylibso.chicory.wabt.Wat2Wasm</name> | ||
| <wasmFile>${project.basedir}/../wabt/src/main/resources/wat2wasm</wasmFile> | ||
| </configuration> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.