Commit Graph

96 Commits

Author SHA1 Message Date
Andrew Brown
ce67ea5d58 Factor out common translations for SIMD comparisons 2019-11-05 16:42:34 -08:00
Andrew Brown
7715c5e07a Translate WASM integer greater-than to CLIF 2019-11-05 16:42:34 -08:00
Andrew Brown
f053595748 Translate WASM iTxN.gt_s to CLIF 2019-11-05 16:42:34 -08:00
Andrew Brown
3a94e8d46a Translate WASM iTxN.ne to CLIF 2019-11-05 16:42:34 -08:00
Andrew Brown
f8c3dfeb17 Translate WASM iTxN.eq to CLIF 2019-11-05 16:42:34 -08:00
Peter Huene
9f506692c2 Fix clippy warnings.
This commit fixes the current set of (stable) clippy warnings in the repo.
2019-10-24 17:20:12 -07:00
Andrew Brown
77035b44a6 Translate WASM all_true to CLIF 2019-10-22 11:01:05 -07:00
Andrew Brown
65e18df12f Translate WASM any_true to CLIF 2019-10-22 11:01:05 -07:00
Andrew Brown
135f9eb4a6 Translate WASM bitselect to CLIF 2019-10-17 15:49:29 -07:00
Andrew Brown
19a980363e Translate WASM shr to CLIF sshr and ushr
As with shift left, the spec requires that the shift count is computed modulo the lane width (see https://github.com/WebAssembly/simd/blob/master/proposals/simd/SIMD.md#left-shift-by-scalar).
2019-10-15 15:51:50 -07:00
Andrew Brown
808885ce56 Translate WASM shl to CLIF ishl
Note how, according to the spec (see https://github.com/WebAssembly/simd/blob/master/proposals/simd/SIMD.md#left-shift-by-scalar), the shift count is computed modulo the lane width. E.g., a shift count of 17 on an i16x8 should not result in all zeroes as it does with Cranelift's `ishl` and x86's `PSSLW`--it should shift once to the left.
2019-10-15 15:51:50 -07:00
Andrew Brown
67733bd2fc Use ConstantData exclusively for inserting data into the constant pool
Previously we allowed anything that could be converted into ConstantData (e.g. a Vec).
2019-10-15 15:19:00 -07:00
Andrew Brown
a03f905d08 Replace V128Imm functionality with ConstantData
This moves most original uses of V128Imm (e.g. in parsing) to ConstantData and shifts the unit tests from V128Imm to ConstantData.
2019-10-15 15:19:00 -07:00
Nick Fitzgerald
bae0257fc3 cranelift-wasm: Fix reachability tracking for if .. else .. end
We weren't previously keeping track of quite the right information for whether
an `if .. else .. end`'s following block was reachable or not. It should be
reachable if the head is reachable and either the consequent or alternative end
reachable (and therefore fall through to the following block) or do an early
`br_if` to it.

This commit rejiggers `ControlStackFrame::If` to keep track of reachability at
the end of the consequent (we don't need to keep track of it at the end of the
alternative, since that is simply `state.reachable`) and adds Wasm tests for
every reachability situation we can encounter with `if .. else .. end`.

Fixes #1132
2019-10-15 10:37:59 -07:00
Nick Fitzgerald
ca53090f1b cranelift-wasm: Create ModuleTranslationState and polish API a little (#1111)
* cranelift-wasm: replace `WasmTypesMap` with `ModuleTranslationState`

The `ModuleTranslationState` contains information decoded from the Wasm module
that must be referenced during each Wasm function's translation.

This is only for data that is maintained by `cranelift-wasm` itself, as opposed
to being maintained by the embedder. Data that is maintained by the embedder is
represented with `ModuleEnvironment`.

A `ModuleTranslationState` is returned by `translate_module`, and can then be
used when translating functions from that module.

* cranelift-wasm: rename `TranslationState` to `FuncTranslationState`

To disambiguate a bit with the new `ModuleTranslationState`.

* cranelift-wasm: Reorganize the internal `state` module into submodules

One module for the `ModuleTranslationState` and another for the
`FuncTranslationState`.

* cranelift-wasm: replace `FuncTranslator` with methods on `ModuleTranslationState`

`FuncTranslator` was two methods that always took ownership of `self`, so it
didn't really make sense as an object as opposed to two different functions, or
in this case methods on the object that actually persists for a longer time.

I think this improves ergonomics nicely.

Before:

```rust
let module_translation = translate_module(...)?;
for body in func_bodies {
    let mut translator = FuncTranslator::new();
    translator.translate(body, ...)?;
}
```

After:

```rust
let module_translation = translate_module(...)?;
for body in func_bodies {
    module_translation.translate_func(body, ...)?;
}
```

Note that this commit does not remove `FuncTranslator`. It still exists, but is
just a wrapper over the `ModuleTranslationState` methods, and it is marked
deprecated, so that downstream users get a heads up. This should make the
transition easier.

* Revert "cranelift-wasm: replace `FuncTranslator` with methods on `ModuleTranslationState`"

This reverts commit 075f9ae933bcaae39348b61287c8f78a4009340d.
2019-10-11 12:37:17 -07:00
Andrew Brown
b19f804ed5 Convert WASM logical operators to CLIF 2019-10-11 11:05:24 -07:00
Nick Fitzgerald
913d26841a cranelift-wasm: Jump to the destination block at end of consequent
This commit fixes a bug where at the end of an `if..else..end`'s consequent
block, we would sometimes erroneously jump to the `else` block instead of to the
following destination block. Not good!
2019-10-02 17:33:56 -07:00
Nick Fitzgerald
10be3e4ba8 cranelift-wasm: support multi-value Wasm (#1049)
This commit introduces initial support for multi-value Wasm. Wasm blocks and
calls can now take and return an arbitrary number of values.

The encoding for multi-value blocks means that we need to keep the contents of
the "Types" section around when translating function bodies. To do this, we
introduce a `WasmTypesMap` type that maps the type indices to their parameters
and returns, construct it when parsing the "Types" section, and shepherd it
through a bunch of functions and methods when translating function bodies.
2019-10-02 12:40:35 -07:00
Andrew Brown
90c49a2f7c Add saturating subtraction with a SIMD encoding
This includes the new instructions `ssub_sat` and `usub_sat` and only encodes the i8x16 and i16x8 types; these are what is needed for implementing the SIMD spec (see https://github.com/WebAssembly/simd/blob/master/proposals/simd/SIMD.md#saturating-integer-subtraction).
2019-09-30 13:54:30 -07:00
Andrew Brown
21144068d4 Add saturating addition with a SIMD encoding
This includes the new instructions `sadd_sat` and `uadd_sat` and only encodes the i8x16 and i16x8 types; these are what is needed for implementing the SIMD spec (see https://github.com/WebAssembly/simd/blob/master/proposals/simd/SIMD.md#saturating-integer-addition).
2019-09-30 13:54:30 -07:00
Andrew Brown
630cb3ee62 Add x86 encoding for SIMD imul
Only i16x8 and i32x4 are encoded in this commit mainly because i8x16 and i64x2 do not have simple encodings in x86. i64x2 is not required by the SIMD spec and there is discussion (https://github.com/WebAssembly/simd/pull/98#issuecomment-530092217) about removing i8x16.
2019-09-30 13:54:30 -07:00
Andrew Brown
ba393afd4d Add x86 legalization for SIMD ineg 2019-09-30 13:54:30 -07:00
Andrew Brown
ca1df499a0 Add x86 encoding for isub 2019-09-30 13:54:30 -07:00
Andrew Brown
e45ef24d3b Convert SIMD load and store to their respective CLIF instructions 2019-09-27 12:48:30 -07:00
Anthony Ramine
eeb3159fe9 Make wasm_unsupported be composeable 2019-09-23 10:36:03 +02:00
Andrew Brown
766cf8ddfd Add x86 implemention for SIMD iadd 2019-09-19 12:04:14 -07:00
Andrew Brown
af1499ce99 Add x86 implementation of shuffle 2019-09-19 10:53:40 -07:00
Erin Power
5426e42a27 Revert "Remove FunctionBuilderContext from API, and change FunctionBuilder API"
This reverts commit 39e638af99dbe6537bc935bfb1a74669b62877b3.
2019-09-17 08:58:46 +02:00
Andrew Brown
f1363168a9 Translate the sign-extended and zero-extended versions of extract_lane 2019-09-10 10:45:12 -07:00
julian-seward1
63367d205c Fix #975 (Structure of cranelift-wasm/src/translation_utils.rs causes many pointless heap allocations) (#1010)
This patch restricts the `Err(..)` return from `blocktype_to_type` to be
`Err(..)` only in the case where it really is an error to continue.  The three
use points of `blocktype_to_type` are changed to check for an `Err(..)` rather
than silently ignoring it.  There are also cosmetic changes to `type_to_type`
and `tabletype_to_type`.

When compiling wasm_lua_binarytrees, this reduces the number of blocks
allocated by CL by 1.9%.  Instruction count falls by 0.1%.

Details:

* `type_to_type` and `tabletype_to_type`:

   - Added the function name in the failure message

   - No functional change for non-error cases

   - Push the `Ok(..)` to expression leaves, where it really applies.  This
     corrects the misleading impression that, in the case of an unsupported
     type, the function returns `Ok` wrapped around whatever
     `wasm_unsupported` returns.  It doesn't do that, but it certainly reads
     like that.  This assumes that the LLVM backend will do tail merging, so
     the generated code will be unchanged.

* `blocktype_to_type`:

  - Change return type from `WasmResult<ir::Type>` to `WasmResult<Option<ir::Type>>`

  - Manually inline the call to `type_to_type`, to make this function easier
    to read.

  - For the non-error case: map `TypeOrFuncType::Type(Type::EmptyBlockType)`
    to `Ok(None)` rather than `Err(..)`, since that's what all the call sites
    expect - For the error cases, add the function name in the failure
    messages

* cranelift-wasm/src/code_translator.rs

  - For the three uses of `blocktype_to_type`, use `?` to detect failures and
    drop out immediately, meaning that the code will no longer silently ignore
    errors.
2019-09-10 15:06:10 +02:00
Aaron Power
8fd1128990 Remove FunctionBuilderContext from API, and change FunctionBuilder API 2019-09-07 14:43:07 -07:00
Andrew Brown
c595acfd0d Convert constants added by v128.const to the appropriate type before use
By default, constants added by SIMD's v128.const will be typed as I8x16 in CLIF. This type must be changed to the appropriate vector type before use to satisfy cranelift's type checking. To do this, we track what SSA values are created by v128.const and convert them with a raw_bitcast immediately before use in the currently implemented SIMD instructions.
2019-08-26 16:12:06 -07:00
Andrew Brown
cb041407c1 Translate existing WASM SIMD operations to CLIF 2019-08-26 16:12:06 -07:00
Dan Gohman
0c2c597852 Update to latest versions of term, capstone, wabt, goblin, wasmparser. 2019-08-20 14:18:14 -07:00
Carmen Kwan
19257f80c1 Add reference types R32 and R64
-Add resumable_trap, safepoint, isnull, and null instructions
-Add Stackmap struct and StackmapSink trait

Co-authored-by: Mir Ahmed <mirahmed753@gmail.com>
Co-authored-by: Dan Gohman <sunfish@mozilla.com>
2019-08-16 11:35:16 -07:00
Adam C. Foltzer
73670aab43 Return a WasmResult from ModuleEnvironment methods (#886)
* [wasm] return a WasmResult from `declare_table_elements`

This method in particular needs to accommodate failure because any table index other than zero is
currently invalid.

* [wasm] additional failure handling improvements

- Adds `WasmResult<()>` as the return type for most of the `ModuleEnvironment` methods that
previously returned nothing.

- Replaces some panics with `WasmError::Unsupported` now that the methods can return a result.

- Adds a `wasm_unsupported!()` macro for early returns with a formatted unsupported message.
2019-08-07 13:23:32 -07:00
Sean Stangl
be36cc6538 Generate basic blocks for wasm if..then..else. 2019-07-25 11:04:02 -06:00
Andy Wortman
b7a9d65458 cranelift-wasm hooks to instrument wasm operators (#861)
* cranelift-wasm hooks to instrument wasm operators
2019-07-25 09:36:17 -07:00
Sean Stangl
926e4c8bf8 Generate basic blocks for Wasm br_if. 2019-07-23 07:58:57 -06:00
Benjamin Bouvier
f6ac165ff6 [wasm] Don't panic when seeing unexpected types but properly fail instead; 2019-07-03 14:46:23 +02:00
Dan Gohman
f163050c9a Update to wasmparser 0.32.1. 2019-07-02 11:59:06 -07:00
Dan Gohman
ccd77c1d0b Update to wasmparser 0.31.0 and goblin 0.0.22. 2019-06-28 15:23:54 +02:00
Yury Delendik
8f95c51730 Reconstruct locations of the original source variable 2019-05-09 00:35:44 -07:00
Benjamin Bouvier
02e114cf3d [wasm] Make FuncEnvironment functions fallible (fixes #752); 2019-04-30 13:58:18 +02:00
Dan Gohman
6b85df0168 Update to wasmparser 0.29.2. 2019-03-26 09:06:41 -07:00
lazypassion
747ad3c4c5 moved crates in lib/ to src/, renamed crates, modified some files' text (#660)
moved crates in lib/ to src/, renamed crates, modified some files' text (#660)
2019-01-28 15:56:54 -08:00