-
Notifications
You must be signed in to change notification settings - Fork 175
Large update to WASM Prism backend with Chicory #3902
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
Draft
headius
wants to merge
19
commits into
ruby:main
Choose a base branch
from
jruby:topic/javawasm_changes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+470
−277
Draft
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e0af0ef
Update group and artifact for publishing
headius 0d3a2f2
Add JRuby dependency for building parser API
headius 239e5ee
Move java_wasm output to src/main/resources
headius 742872c
Add basic bootstrapping instructions
headius 70c40c1
Upgrade to chicory 1.6.1
headius b1ec913
Merge remote-tracking branch 'ruby/main' into jruby-prism-wasm-updates
headius 098305d
Simplify setup and teardown
headius 1cbe798
Support AOT and interp WASM
headius 8e1aed5
Add test that parses JRuby's boot files
headius 3236f86
Fix the memory leak
andreaTP 1e17c1b
rem string free
andreaTP 010d03b
Merge pull request #1 from andreaTP/fix-memory-leak
headius d05601c
Cleanup and improvements
andreaTP 59b9326
Merge pull request #2 from andreaTP/more-cleanup
headius ed09e78
Merge pull request #1 from headius/jruby-prism-wasm-updates
headius ff9d363
Merge branch 'main' into topic/javawasm_changes
headius 94ee315
Update java-wasm to Java 21 minimum
headius 2235128
Move WASM parser to a JRuby package
headius 033392a
Rename Java package to org.ruby_lang.prism
headius 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
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
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 |
|---|---|---|
|
|
@@ -65,6 +65,7 @@ GEM | |
| PLATFORMS | ||
| java | ||
| ruby | ||
| universal-java | ||
|
|
||
| DEPENDENCIES | ||
| benchmark-ips | ||
|
|
||
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
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
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
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
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,23 @@ | ||
| This dir contains the chicory-prism artifact, a version of prism compiled to WASM and then AOT compiled to JVM bytecode by the Chicory project. | ||
|
|
||
| Generate the templated sources: | ||
|
|
||
| ``` | ||
| PRISM_EXCLUDE_PRETTYPRINT=1 PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS=1 PRISM_JAVA_BACKEND=jruby bundle exec rake templates | ||
| ``` | ||
|
|
||
| Compile to WASM using WASI SDK version 25: | ||
|
|
||
| ``` | ||
| make java-wasm WASI_SDK_PATH=.../wasi-sdk-25.0-arm64-macos | ||
| ``` | ||
|
|
||
| Build the AOT-compiled machine and wrapper library: | ||
|
|
||
| ``` | ||
| mvn -f java-wasm/pom.xml clean package | ||
| ``` | ||
|
|
||
| This should build the chicory-wasm jar file and pass some basic tests. | ||
|
|
||
| The jar will be under `java-wasm/target/chicory-prism-#####-SNAPSHOT.jar` or can be installed by using `install` instead of `pacakge` in the `mvn` command line above. |
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 @@ | ||
| profile.html |
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,6 @@ | ||
| #! /bin/bash | ||
| set -euxo pipefail | ||
|
|
||
| SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) | ||
|
|
||
| jbang --fresh --deps org.openjdk.jmh:jmh-generator-annprocess:1.36 --javaagent=ap-loader@jvm-profiling-tools/ap-loader=start,event=cpu,file=${SCRIPT_DIR}/profile.html ${SCRIPT_DIR}/test |
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,97 @@ | ||
| ///usr/bin/env jbang "$0" "$@" ; exit $? | ||
|
|
||
| //DEPS org.jruby:chicory-prism:0.0.1-SNAPSHOT | ||
| //DEPS org.jruby:jruby-complete:10.0.2.0 | ||
|
|
||
| import static java.lang.System.*; | ||
|
|
||
| import java.io.DataInputStream; | ||
| import java.io.InputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.EnumSet; | ||
|
|
||
| import org.jruby.Ruby; | ||
|
|
||
| import org.jruby.parser.prism.wasm.Prism; | ||
| import org.ruby_lang.prism.ParsingOptions; | ||
|
|
||
| public class test { | ||
|
|
||
| final static String[] JRUBY_BOOT_FILES = { | ||
| "jruby/java.rb", | ||
| "jruby/java/core_ext.rb", | ||
| "jruby/java/core_ext/object.rb", | ||
| "jruby/java/java_ext.rb", | ||
| "jruby/kernel.rb", | ||
| "jruby/kernel/signal.rb", | ||
| "jruby/kernel/kernel.rb", | ||
| "jruby/kernel/proc.rb", | ||
| "jruby/kernel/process.rb", | ||
| "jruby/kernel/enumerator.rb", | ||
| "jruby/kernel/enumerable.rb", | ||
| "jruby/kernel/io.rb", | ||
| "jruby/kernel/gc.rb", | ||
| "jruby/kernel/range.rb", | ||
| "jruby/kernel/file.rb", | ||
| "jruby/kernel/method.rb", | ||
| "jruby/kernel/thread.rb", | ||
| "jruby/kernel/integer.rb", | ||
| "jruby/kernel/time.rb", | ||
| "jruby/preludes.rb", | ||
| "jruby/kernel/prelude.rb", | ||
| "jruby/kernel/enc_prelude.rb", | ||
| "META-INF/jruby.home/lib/ruby/stdlib/unicode_normalize.rb", | ||
| "jruby/kernel/gem_prelude.rb", | ||
| "META-INF/jruby.home/lib/ruby/stdlib/rubygems.rb", | ||
| "META-INF/jruby.home/lib/ruby/stdlib/rbconfig.rb", | ||
| "jruby/kernel/rbconfig.rb", | ||
| "META-INF/jruby.home/lib/ruby/stdlib/rubygems/compatibility.rb", | ||
| "META-INF/jruby.home/lib/ruby/stdlib/rubygems/defaults.rb", | ||
| "META-INF/jruby.home/lib/ruby/stdlib/rubygems/deprecate.rb", | ||
| "META-INF/jruby.home/lib/ruby/stdlib/rubygems/errors.rb", | ||
| "META-INF/jruby.home/lib/ruby/stdlib/rubygems/target_rbconfig.rb", | ||
| "META-INF/jruby.home/lib/ruby/stdlib/rubygems/exceptions.rb", | ||
| "META-INF/jruby.home/lib/ruby/stdlib/rubygems/unknown_command_spell_checker.rb", | ||
| "META-INF/jruby.home/lib/ruby/stdlib/rubygems/specification.rb", | ||
| "META-INF/jruby.home/lib/ruby/stdlib/rubygems/basic_specification.rb", | ||
| "META-INF/jruby.home/lib/ruby/stdlib/rubygems/stub_specification.rb", | ||
| "META-INF/jruby.home/lib/ruby/stdlib/rubygems/platform.rb", | ||
| "META-INF/jruby.home/lib/ruby/stdlib/rubygems/specification_record.rb", | ||
| "META-INF/jruby.home/lib/ruby/stdlib/rubygems/util/list.rb", | ||
| "META-INF/jruby.home/lib/ruby/stdlib/rubygems/requirement.rb", | ||
| "META-INF/jruby.home/lib/ruby/stdlib/rubygems/version.rb", | ||
| }; | ||
|
|
||
| public static void main(String... args) throws Exception { | ||
| out.println("Starting"); | ||
| var count = 100; | ||
|
|
||
| var prism = new Prism(); | ||
| byte[] src = new byte[1024 * 1024]; | ||
|
|
||
| for (int i = 0; i < count; i++) { | ||
| for (var file : JRUBY_BOOT_FILES) { | ||
| byte[] options = ParsingOptions.serialize( | ||
| file.getBytes(StandardCharsets.UTF_8), | ||
| 1, | ||
| "UTF-8".getBytes(StandardCharsets.UTF_8), | ||
| false, | ||
| EnumSet.noneOf(ParsingOptions.CommandLine.class), | ||
| ParsingOptions.SyntaxVersion.LATEST, | ||
| false, | ||
| false, | ||
| false, | ||
| new byte[][][]{} | ||
| ); | ||
|
|
||
| try (InputStream fileIn = Ruby.getClassLoader().getResourceAsStream(file)) { | ||
| DataInputStream dis = new DataInputStream(fileIn); | ||
| int read = dis.read(src); | ||
| prism.serialize(options, src, read); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| out.println("End"); | ||
| } | ||
| } |
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
2 changes: 1 addition & 1 deletion
2
...ava-templates/org/prism/WasmResource.java → ...jruby/parser/prism/wasm/WasmResource.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
95 changes: 95 additions & 0 deletions
95
java-wasm/src/main/java/org/jruby/parser/prism/wasm/Prism.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,95 @@ | ||
| package org.jruby.parser.prism.wasm; | ||
|
|
||
| import com.dylibso.chicory.annotations.WasmModuleInterface; | ||
| import com.dylibso.chicory.runtime.ByteArrayMemory; | ||
| import com.dylibso.chicory.runtime.ImportValues; | ||
| import com.dylibso.chicory.runtime.Instance; | ||
| import com.dylibso.chicory.wasi.WasiOptions; | ||
| import com.dylibso.chicory.wasi.WasiPreview1; | ||
| import org.ruby_lang.prism.Loader; | ||
| import org.ruby_lang.prism.ParseResult; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| @WasmModuleInterface(WasmResource.absoluteFile) | ||
| public class Prism implements AutoCloseable { | ||
| private final WasiPreview1 wasi; | ||
| protected final Prism_ModuleExports exports; | ||
| private final Instance instance; | ||
|
|
||
| private int bufferPointer; | ||
| private int preSourcePointer; | ||
| private int preOptionsPointer; | ||
|
|
||
| private final int SOURCE_SIZE = 2 * 1024 * 1024; // 2 MiB | ||
| private final int PACKED_OPTIONS_BUFFER_SIZE = 1024; | ||
|
|
||
| public Prism() { | ||
| this(WasiOptions.builder().build()); | ||
| } | ||
|
|
||
| public Prism(WasiOptions wasiOpts) { | ||
| wasi = WasiPreview1.builder().withOptions(wasiOpts).build(); | ||
| instance = Instance.builder(PrismParser.load()) | ||
| .withMemoryFactory(ByteArrayMemory::new) | ||
| .withMachineFactory(PrismParser::create) | ||
| .withImportValues(ImportValues.builder().addFunction(wasi.toHostFunctions()).build()) | ||
| .build(); | ||
| exports = new Prism_ModuleExports(instance); | ||
|
|
||
| preOptionsPointer = exports.calloc(1, PACKED_OPTIONS_BUFFER_SIZE); | ||
| preSourcePointer = exports.calloc(1, SOURCE_SIZE); | ||
|
|
||
| bufferPointer = exports.calloc(exports.pmBufferSizeof(), 1); | ||
| exports.pmBufferInit(bufferPointer); | ||
| } | ||
|
|
||
| public byte[] serialize(byte[] packedOptions, byte[] sourceBytes, int sourceLength) { | ||
| int sourcePointer = 0; | ||
| boolean useDefaultSourcePointer = sourceLength + 1 > SOURCE_SIZE; | ||
| int optionsPointer = 0; | ||
| boolean useDefaultOptionsPointer = packedOptions.length > PACKED_OPTIONS_BUFFER_SIZE; | ||
| byte[] result; | ||
| try { | ||
| sourcePointer = (!useDefaultSourcePointer) ? | ||
| exports.calloc(1, sourceLength + 1) : preSourcePointer; | ||
| instance.memory().write(sourcePointer, sourceBytes, 0, sourceLength); | ||
| instance.memory().writeByte(sourcePointer + sourceLength, (byte) 0); | ||
|
|
||
| optionsPointer = (!useDefaultOptionsPointer) ? | ||
| exports.calloc(1, packedOptions.length) : preOptionsPointer; | ||
| instance.memory().write(optionsPointer, packedOptions); | ||
|
|
||
| exports.pmBufferClear(bufferPointer); | ||
|
|
||
| exports.pmSerializeParse( | ||
| bufferPointer, sourcePointer, sourceLength, optionsPointer); | ||
|
|
||
| result = instance.memory().readBytes( | ||
| exports.pmBufferValue(bufferPointer), | ||
| exports.pmBufferLength(bufferPointer)); | ||
| } finally { | ||
| if (!useDefaultSourcePointer) { | ||
| exports.free(sourcePointer); | ||
| } | ||
| if (!useDefaultOptionsPointer) { | ||
| exports.free(optionsPointer); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| public ParseResult serializeParse(byte[] packedOptions, String source) { | ||
| var sourceBytes = source.getBytes(StandardCharsets.ISO_8859_1); | ||
| byte[] result = serialize(packedOptions, sourceBytes, sourceBytes.length); | ||
| return Loader.load(result, sourceBytes); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| if (wasi != null) { | ||
| wasi.close(); | ||
| } | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it OK to bump files under
java/to 21 too in the Rakefile:prism/Rakefile
Lines 48 to 49 in 55948fe
I recall we had to downgrade to Java 8 (for JRuby 9 IIRC) in truffleruby/truffleruby@9acddea, is that still necessary?