-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Fiber switching for WebAssembly #13107
Open
lbguilherme
wants to merge
29
commits into
crystal-lang:master
Choose a base branch
from
lbguilherme:feat/wasm32-fibers
base: master
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.
+279
−24
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
75c09ea
feat: Fiber switching for WebAssembly
lbguilherme 310e67d
Merge branch 'master' into feat/wasm32-fibers
lbguilherme ae4746d
fix: run tests with a fresh compiler
lbguilherme f78eafd
feat: in release mode optimize for size, not for speed
lbguilherme 11ba5a7
fix: typos
lbguilherme 595e55f
chore: small refactor on wasm-opt command
lbguilherme cd104f3
fix: wasm32 ci
lbguilherme be05e33
fix: avoid stripping debug info if we are in debug mode
lbguilherme 006c6db
fix: no need for sudo
lbguilherme 0837426
fix ci
lbguilherme 48af90b
fix: correctly use @debug enum
lbguilherme ac8f5a5
Update src/compiler/crystal/compiler.cr
lbguilherme f623c44
style
lbguilherme b7f2a96
fix: don't skip asyncify transform on imports
lbguilherme e72092b
fix ci making fresh compiler
lbguilherme 559230b
Update src/fiber/context/wasm32.cr
lbguilherme 3d0515f
ci: use a more up-to-date version of binaryen
lbguilherme 72a7daa
Merge branch 'feat/wasm32-fibers' of github.com:lbguilherme/crystal i…
lbguilherme 5efd984
ci: install curl
lbguilherme 5a57893
fix: can't compress relocations while also preserving debug info
lbguilherme 3d53b7f
fix: no need to enable all wasm features. everything we need is alrea…
lbguilherme 9996e02
ci: build spec suite in release mode
lbguilherme 8082fb1
fix: --all-features is in fact required because we need bulk memory o…
lbguilherme 0e3268d
Merge branch 'master' into feat/wasm32-fibers
lbguilherme 90ce2be
Merge remote-tracking branch 'upstream/master' into feat/wasm32-fibers
lbguilherme c339f44
Merge branch 'master' into feat/wasm32-fibers
beta-ziliani 417c7b1
enable pcre2 and update wasmtime
lbguilherme 281547a
feat: refactor Asyncify module
lbguilherme 189466c
fix: place the main stack before global data and increase its size
lbguilherme 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 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 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 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,106 @@ | ||
{% skip_file unless flag?(:wasm32) %} | ||
|
||
# :nodoc: | ||
@[Link(wasm_import_module: "asyncify")] | ||
lib LibAsyncify | ||
struct Data | ||
current_location : Void* | ||
end_location : Void* | ||
end | ||
|
||
fun start_unwind(data : Data*) | ||
fun stop_unwind | ||
fun start_rewind(data : Data*) | ||
fun stop_rewind | ||
end | ||
|
||
# :nodoc: | ||
module Crystal::Asyncify | ||
enum State | ||
Normal | ||
Unwinding | ||
Rewinding | ||
end | ||
|
||
@@state = State::Normal | ||
class_getter! main_func, current_func | ||
|
||
# Reads the stack pointer global. | ||
def self.stack_pointer | ||
stack_pointer = uninitialized Void* | ||
asm(" | ||
.globaltype __stack_pointer, i32 | ||
global.get __stack_pointer | ||
local.set $0 | ||
" : "=r"(stack_pointer)) | ||
|
||
stack_pointer | ||
end | ||
|
||
# Sets the stack pointer global. Use this in conjuction with unwinding the stack. | ||
def self.stack_pointer=(stack_pointer : Void*) | ||
asm(" | ||
.globaltype __stack_pointer, i32 | ||
local.get $0 | ||
global.set __stack_pointer | ||
" :: "r"(stack_pointer)) | ||
end | ||
|
||
# Wraps the entrypoint to capture and stop stack unwindings and trigger a rewind | ||
# into the right point. | ||
@[NoInline] | ||
def self.wrap_main(&block) | ||
@@main_func = block | ||
@@current_func = block | ||
block.call | ||
|
||
until @@state.normal? | ||
@@state = State::Normal | ||
LibAsyncify.stop_unwind | ||
|
||
if before_rewind = @@before_rewind | ||
before_rewind.call | ||
end | ||
|
||
if rewind_data = @@rewind_data | ||
@@state = State::Rewinding | ||
LibAsyncify.start_rewind(rewind_data) | ||
end | ||
|
||
func = @@rewind_func.not_nil! | ||
@@current_func = func | ||
func.call | ||
end | ||
end | ||
|
||
# Performs a stack unwind. All stack local variables will be stored in the `unwind_data` buffer. | ||
# If a `rewind_data` buffer is provided, the stack will be rewinded into that position after unwinding. | ||
# `rewind_func` controls the execution target to invoke after unwinding. It can be a new function, the | ||
# main function or the currently executing function. Finally, the `before_rewind` callback can be used | ||
# to specify some action to do after unwinding and before rewinding. | ||
def self.unwind( | ||
*, | ||
unwind_data : LibAsyncify::Data*, | ||
rewind_data : LibAsyncify::Data*?, | ||
rewind_func : Proc(Void), | ||
before_rewind : Proc(Void)? = nil | ||
) | ||
@@rewind_data = rewind_data | ||
@@rewind_func = rewind_func | ||
@@before_rewind = before_rewind | ||
|
||
real_unwind(unwind_data) | ||
end | ||
|
||
@[NoInline] | ||
private def self.real_unwind(unwind_data : LibAsyncify::Data*) | ||
if @@state.rewinding? | ||
@@state = State::Normal | ||
LibAsyncify.stop_rewind | ||
return | ||
end | ||
|
||
@@state = State::Unwinding | ||
LibAsyncify.start_unwind(unwind_data) | ||
end | ||
end |
This file contains 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 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 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 |
---|---|---|
|
@@ -141,7 +141,7 @@ class Fiber | |
end | ||
|
||
# :nodoc: | ||
def run | ||
def run : Nil | ||
GC.unlock_read | ||
@proc.call | ||
rescue ex | ||
|
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.
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.
I'm wondering if we should reuse
Fiber::StackPool::STACK_SIZE
or perhaps introduce a more generic property for this. It would also be great to allow configuration of the stack size at compile time.Perhaps a generic
CRYSTAL_STACK_SIZE
environment variable could be useful for this? It would populate this value here andFiber::StackPool::STACK_SIZE
and have 8MB as default value.Just thinking ahead, this can be implemented outside this PR. (It's probably better to extract it).
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.
I like having a
CRYSTAL_STACK_SIZE
environment variable! But let's do that on another PR.