Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Implementation of effects based on the Stack Switching proposal #1832

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions .github/actions/install-binaryen/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: Install Binaryen

inputs:
repository:
description: 'Repository name with owner. For example, actions/checkout'
default: WebAssembly/binaryen
ref:
description: >
The branch, tag or SHA to checkout. When checking out the repository that
triggered a workflow, this defaults to the reference or SHA for that
event. Otherwise, uses the default branch.
default: latest
build:
description: Whether we should build from source
default: false
runs:
using: composite
steps:
- name: Restore cached binaryen
if: ${{ inputs.build && inputs.build != 'false' }}
id: cache-binaryen
uses: actions/cache/restore@v4
with:
path: binaryen
key: ${{ runner.os }}-binaryen-${{ inputs.ref }}

- name: Checkout binaryen
if: ${{ inputs.build && inputs.build != 'false' && steps.cache-binaryen.outputs.cache-hit != 'true' }}
uses: actions/checkout@v4
with:
repository: ${{ inputs.repository }}
path: binaryen
submodules: true
ref: ${{ inputs.ref == 'latest' && 'main' || inputs.ref }}

- name: Install ninja (Linux)
if: ${{ inputs.build && inputs.build != 'false' && runner.os == 'Linux' && steps.cache-binaryen.outputs.cache-hit != 'true' }}
shell: bash
run: sudo apt-get install ninja-build

- name: Install ninja (MacOS)
if: ${{ inputs.build && inputs.build != 'false' && runner.os == 'macOS' && steps.cache-binaryen.outputs.cache-hit != 'true' }}
shell: bash
run: brew install ninja

- name: Build binaryen
if: ${{ inputs.build && inputs.build != 'false' && runner.os != 'Windows' && steps.cache-binaryen.outputs.cache-hit != 'true' }}
working-directory: ./binaryen
shell: bash
run: |
cmake -G Ninja .
ninja

- name: Install binaryen build dependencies (Windows)
if: ${{ inputs.build && inputs.build != 'false' && runner.os == 'Windows' && steps.cache-binaryen.outputs.cache-hit != 'true' }}
working-directory: ./binaryen
shell: bash
run: opam install conf-cmake conf-c++

- name: Build binaryen (Windows)
if: ${{ inputs.build && inputs.build != 'false' && runner.os == 'Windows' && steps.cache-binaryen.outputs.cache-hit != 'true' }}
working-directory: ./binaryen
shell: bash
run: |
opam exec -- cmake . -DBUILD_STATIC_LIB=ON -DBUILD_TESTS=off -DINSTALL_LIBS=off -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc
make -j 4

- name: Cache binaryen
if: ${{ inputs.build && inputs.build != 'false' && steps.cache-binaryen.outputs.cache-hit != 'true' }}
uses: actions/cache/save@v4
with:
path: binaryen
key: ${{ runner.os }}-binaryen-${{ inputs.ref }}

- name: Set binaryen's path
if: ${{ inputs.build && inputs.build != 'false' && runner.os != 'Windows' }}
shell: bash
run: echo "$GITHUB_WORKSPACE/binaryen/bin" >> $GITHUB_PATH

- name: Copy binaryen's tools (Windows)
if: ${{ inputs.build && inputs.build != 'false' && runner.os == 'Windows' }}
shell: bash
run: cp $GITHUB_WORKSPACE/binaryen/bin/wasm-{merge,opt}.exe _opam/bin

- name: Download Binaryen
if: ${{ ! inputs.build || inputs.build == 'false' }}
uses: Aandreba/setup-binaryen@v1.0.0
with:
token: ${{ github.token }}
version: ${{ inputs.ref }}
102 changes: 97 additions & 5 deletions .github/workflows/build-wasm_of_ocaml.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:
matrix:
os:
- ubuntu-latest
os-name:
- Ubuntu
ocaml-compiler:
- "4.14"
- "5.0"
Expand All @@ -27,30 +29,50 @@ jobs:
- false
all_jane_street_tests:
- false
wasi:
- false
include:
- os: macos-latest
os-name: MacOS
ocaml-compiler: "5.3"
separate_compilation: true
jane_street_tests: false
all_jane_street_tests: false
wasi: false
- os: windows-latest
os-name: Windows
ocaml-compiler: "5.2"
separate_compilation: true
jane_street_tests: true
all_jane_street_tests: true
wasi: false
- os: ubuntu-latest
os-name: Ubuntu
ocaml-compiler: "5.2"
separate_compilation: true
jane_street_tests: true
all_jane_street_tests: true
wasi: false
- os: ubuntu-latest
os-name: Ubuntu
ocaml-compiler: "5.2"
separate_compilation: false
jane_street_tests: true
all_jane_street_tests: false
wasi: false
- os: ubuntu-latest
os-name: Ubuntu
ocaml-compiler: "5.3"
separate_compilation: true
jane_street_tests: false
all_jane_street_tests: false
wasi: true

runs-on: ${{ matrix.os }}

name:
${{ matrix.wasi && 'WASI / ' || '' }}${{ (! matrix.separate_compilation) && 'Whole program / ' || ''}}${{ matrix.ocaml-compiler }} / ${{ matrix.os-name }}${{ matrix.all_jane_street_tests && ' / Jane Street tests' || ''}}

steps:
- name: Set git to use LF
if: ${{ matrix.os == 'windows-latest' && matrix.ocaml-compiler < 5.2 }}
Expand All @@ -77,15 +99,63 @@ jobs:
with:
node-version: latest

- name: Set-up Rust toolchain
if: matrix.wasi
uses: actions-rust-lang/setup-rust-toolchain@v1

- name: Checkout Wasmtime
if: matrix.wasi
uses: actions/checkout@v4
with:
repository: bytecodealliance/wasmtime
path: wasmtime
submodules: true

- name: Build Wasmtime
if: matrix.wasi
working-directory: ./wasmtime
run: |
cargo build
echo `pwd`/target/debug >> "$GITHUB_PATH"

- name: Checkout Virgil
if: matrix.wasi
uses: actions/checkout@v4
with:
repository: titzer/virgil
path: virgil

- name: Build Virgil
if: matrix.wasi
working-directory: ./virgil
run: |
export PATH=$PATH:`pwd`/bin
echo `pwd`/bin >> "$GITHUB_PATH"
make

- name: Checkout Wizard engine
if: matrix.wasi
uses: actions/checkout@v4
with:
repository: titzer/wizard-engine
path: wizard-engine

- name: Build Wizard engine
if: matrix.wasi
working-directory: ./wizard-engine
run: |
make -j 4
echo `pwd`/bin >> "$GITHUB_PATH"

- name: Set-up OCaml ${{ matrix.ocaml-compiler }}
uses: ocaml/setup-ocaml@v3
with:
ocaml-compiler: ${{ matrix.ocaml-compiler }}

- name: Set-up Binaryen
uses: Aandreba/setup-binaryen@v1.0.0
- name: Install Binaryen
uses: ./wasm_of_ocaml/.github/actions/install-binaryen
with:
token: ${{ github.token }}
build: true

- name: Pin faked binaryen-bin package
# It's faster to use a cached version
Expand Down Expand Up @@ -129,7 +199,7 @@ jobs:
opam install . -t

- name: Run tests
if: ${{ matrix.separate_compilation }}
if: ${{ matrix.separate_compilation && ! matrix.wasi }}
working-directory: ./wasm_of_ocaml
run: opam exec -- dune build @runtest-wasm

Expand All @@ -138,11 +208,33 @@ jobs:
# See https://github.com/libuv/libuv/issues/3622

- name: Run tests with CPS effects
if: ${{ matrix.ocaml-compiler >= '5.' && matrix.separate_compilation }}
if: ${{ matrix.ocaml-compiler >= '5.' && matrix.separate_compilation && ! matrix.wasi }}
continue-on-error: ${{ matrix.os == 'windows-latest' }}
working-directory: ./wasm_of_ocaml
run: opam exec -- dune build @runtest-wasm --profile with-effects

- name: Run tests (WASI runtime - node)
if: ${{ false }}
working-directory: ./wasm_of_ocaml
run: opam exec -- dune build @runtest-wasm --profile wasi

- name: Run tests (WASI runtime - Wizard engine)
if: ${{ matrix.wasi }}
working-directory: ./wasm_of_ocaml
env:
WASM_ENGINE: wizard-fast
run: opam exec -- dune build @runtest-wasm --profile wasi

- name: Run tests (WASI runtime - wasmtime)
if: ${{ false }}
working-directory: ./wasm_of_ocaml
env:
WASM_ENGINE: wasmtime
WASI_FLAGS: --enable trap-on-exception
RUST_BACKTRACE: 0
continue-on-error: true
run: opam exec -- dune build @runtest-wasm --profile wasi

- name: Run Base tests
if: matrix.all_jane_street_tests
continue-on-error: ${{ matrix.os == 'windows-latest' }}
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ jobs:
- run: opam install conf-pkg-config conf-mingw-w64-g++-i686 conf-mingw-w64-g++-x86_64
if: runner.os == 'Windows'

- name: Set-up Binaryen
uses: Aandreba/setup-binaryen@v1.0.0
- name: Install Binaryen
uses: ./.github/actions/install-binaryen
with:
token: ${{ github.token }}
build: true

- name: Install faked binaryen-bin package
# It's faster to use a cached version
Expand Down
61 changes: 53 additions & 8 deletions compiler/bin-wasm_of_ocaml/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,39 @@ let with_runtime_files ~runtime_wasm_files f =
in
Wat_preprocess.with_preprocessed_files ~variables:[] ~inputs f

let build_runtime ~runtime_file =
(* Keep this variables in sync with gen/gen.ml *)
let variables =
[ "wasi", Config.Flag.wasi (); "trap-on-exception", Config.Flag.trap_on_exception () ]
in
match
List.find_opt Runtime_files.precompiled_runtimes ~f:(fun (flags, _) ->
assert (
List.length flags = List.length variables
&& List.for_all2 ~f:(fun (k, _) (k', _) -> String.equal k k') flags variables);
Poly.equal flags variables)
with
| Some (_, contents) -> Fs.write_file ~name:runtime_file ~contents
| None ->
let inputs =
List.map
~f:(fun (module_name, contents) ->
{ Wat_preprocess.module_name
; file = module_name ^ ".wat"
; source = Contents contents
})
(if Config.Flag.wasi ()
then ("libc", Runtime_files.wasi_libc) :: Runtime_files.wat_files
else Runtime_files.wat_files)
in
Runtime.build
~link_options:[ "-g" ]
~opt_options:[ "-g"; "-O2" ]
~variables:
(List.map ~f:(fun (k, v) : (_ * Wat_preprocess.value) -> k, Bool v) variables)
~inputs
~output_file:runtime_file

let link_and_optimize
~profile
~sourcemap_root
Expand All @@ -99,7 +132,7 @@ let link_and_optimize
let enable_source_maps = Option.is_some opt_sourcemap_file in
Fs.with_intermediate_file (Filename.temp_file "runtime" ".wasm")
@@ fun runtime_file ->
Fs.write_file ~name:runtime_file ~contents:Runtime_files.wasm_runtime;
build_runtime ~runtime_file;
Fs.with_intermediate_file (Filename.temp_file "wasm-merged" ".wasm")
@@ fun temp_file ->
opt_with
Expand All @@ -125,7 +158,10 @@ let link_and_optimize
@@ fun opt_temp_sourcemap' ->
let primitives =
Binaryen.dead_code_elimination
~dependencies:Runtime_files.dependencies
~dependencies:
(if Config.Flag.wasi ()
then Runtime_files.wasi_dependencies
else Runtime_files.dependencies)
~opt_input_sourcemap:opt_temp_sourcemap
~opt_output_sourcemap:opt_temp_sourcemap'
~input_file:temp_file
Expand All @@ -145,7 +181,7 @@ let link_and_optimize

let link_runtime ~profile runtime_wasm_files output_file =
if List.is_empty runtime_wasm_files
then Fs.write_file ~name:output_file ~contents:Runtime_files.wasm_runtime
then build_runtime ~runtime_file:output_file
else
Fs.with_intermediate_file (Filename.temp_file "extra_runtime" ".wasm")
@@ fun extra_runtime ->
Expand All @@ -167,7 +203,7 @@ let link_runtime ~profile runtime_wasm_files output_file =
();
Fs.with_intermediate_file (Filename.temp_file "runtime" ".wasm")
@@ fun runtime_file ->
Fs.write_file ~name:runtime_file ~contents:Runtime_files.wasm_runtime;
build_runtime ~runtime_file;
Binaryen.link
~opt_output_sourcemap:None
~inputs:
Expand Down Expand Up @@ -245,7 +281,13 @@ let build_js_runtime ~primitives ?runtime_arguments () =
| _ -> assert false
in
let init_fun =
match Parse_js.parse (Parse_js.Lexer.of_string Runtime_files.js_runtime) with
match
Parse_js.parse
(Parse_js.Lexer.of_string
(if Config.Flag.wasi ()
then Runtime_files.js_wasi_launcher
else Runtime_files.js_launcher))
with
| [ (Expression_statement f, _) ] -> f
| _ -> assert false
in
Expand Down Expand Up @@ -478,9 +520,12 @@ let run
tmp_wasm_file
in
let wasm_name =
Printf.sprintf
"code-%s"
(String.sub (Digest.to_hex (Digest.file tmp_wasm_file)) ~pos:0 ~len:20)
if Config.Flag.wasi ()
then "code"
else
Printf.sprintf
"code-%s"
(String.sub (Digest.to_hex (Digest.file tmp_wasm_file)) ~pos:0 ~len:20)
in
let tmp_wasm_file' = Filename.concat tmp_dir (wasm_name ^ ".wasm") in
Sys.rename tmp_wasm_file tmp_wasm_file';
Expand Down
Loading
Loading