Commit Graph

8795 Commits

Author SHA1 Message Date
Damian Heaton
dd23a21b9b Implement Swizzle and Splat for interpreter (#3268)
* Implement `Swizzle` and `Splat` for interpreter

Implemented for the Cranelift interpreter:
- `Swizzle` to shuffle an `i8x16` SIMD vector based
on the indices specified in another vector of the same size.
- `Splat` to create a SIMD vector with all lanes having the same value.

Copyright (c) 2021, Arm Limited

* Fix old x86 backend failing test

Copyright (c) 2021, Arm Limited

* Represent i16x8 and above as hex

Copyright (c) 2021, Arm Limited
2021-09-07 09:53:49 -07:00
Afonso Bordado
63e9a81deb Implement vany_true and vall_true instructions in interpreter (#3304)
* cranelift: Implement ZeroExtend for a bunch of types in interpreter

* cranelift: Implement VConst on interpreter

* cranelift: Implement VallTrue on interpreter

* cranelift: Implement VanyTrue on interpreter

* cranelift: Mark `v{all,any}_true` tests as machinst only

* cranelift: Disable `vany_true` tests on aarch64

The `b64x2` case produces an illegal instruction. See #3305
2021-09-07 09:50:39 -07:00
Alex Crichton
c73673559b Avoid vector allocations in wasm->host calls (#3294)
This commit improves the runtime support for wasm-to-host invocations
for functions created with `Func::new` or `wasmtime_func_new` in the C
API. Previously a `Vec` (sometimes a `SmallVec`) would be dynamically
allocated on each host call to store the arguments that are coming from
wasm and going to the host. In the case of the `wasmtime` crate we need
to decode the `u128`-stored values, and in the case of the C API we need
to decode the `Val` into the C API's `wasmtime_val_t`.

The technique used in this commit is to store a singular `Vec<T>` inside
the "store", be it the literal `Store<T>` or within the `T` in the case
of the C API, which can be reused across wasm->host calls. This means
that we're unlikely to actually perform dynamic memory allocation and
instead we should hit a faster path where the `Vec` always has enough
capacity.

Note that this is just a mild improvement for `Func::new`-based
functions. It's still the case that `Func::wrap` is much faster, but
unfortunately the C API doesn't have access to `Func::wrap`, so the main
motivation here is accelerating the C API.
2021-09-03 15:14:21 -05:00
Chris Fallin
0473e1990a Merge pull request #3299 from afonso360/fuzzer-brtable
Cranelift CLIF Fuzzer add jump tables and `br_table`
2021-09-03 12:43:55 -07:00
Chris Fallin
3db8043a02 Merge pull request #3300 from cfallin/cranelift-mtg-20210906
Cranelift agenda for Mon Sep 6: cancel meeting (empty agenda + US holiday).
2021-09-03 12:01:03 -07:00
Chris Fallin
ab7157088f Cranelift agenda for Mon Sep 6: cancel meeting (empty agenda + US holiday). 2021-09-03 11:58:54 -07:00
Alex Crichton
ca3947911e Refactor the internals of Store<T> (#3291)
* Refactor the internals of `Store<T>`

This commit is an overdue refactoring and renaming of some internals of
the `Store` type in Wasmtime. The actual implementation of `Store<T>`
has evolved from the original implementation to the point where some of
the aspects of how things are structured no longer makes sense. There's
also always been a lot of unnecessary gymnastics when trying to get
access to various store pieces depending on where you are in `wasmtime`.

This refactoring aims to simplify all this and make the internals much
easier to read/write. The following changes were made:

* The `StoreOpaque<'_>` type is deleted, along with the `opaque()`
  method.

* The `StoreInnermost` type was renamed to `StoreOpaque`.
  `StoreOpaque<'_>` is dead. Long live `StoreOpaque`. This renaming
  and a few small tweaks means that this type now suffices for all
  consumers.

* The `AsContextMut` and `AsContext` traits are now implemented for
  `StoreInner<T>`.

These changes, while subtly small, help clean up a lot of the internals
of `wasmtime`. There's a lot less verbose `&mut
store.as_context_mut().opaque()` now. Additionally many methods can
simply start with `let store = store.as_context_mut().0;` and use things
internally. One of the nicer aspects of using references directly is
that the compiler automatically reborrows references as necessary
meaning there's lots of less manual reborrowing.

The main motivation for this change was actually somewhat roundabout
where I found that when `StoreOpaque<'_>` was being captured in closures
and iterators it's 3 pointers wide which is a lot of data to move
around. Now things capture over `&mut StoreOpaque` which is just one
nice and small pointer to move around. In any case though I've long
wanted to revisit the design of these internals to improve the
ergonomics. It's not expected that this change alone will really have
all that much impact on the performance of `wasmtime`.

Finally a doc comment was added to `store.rs` to try to explain all the
`Store`-related types since there are a nontrivial amount.

* Rustfmt
2021-09-03 13:55:18 -05:00
Alex Crichton
50ce19a4a4 Remove an indirect function call in Func::new (#3293)
This commit optimizes the runtime execution of `Func::new` by removing
an indirect function call that happens whenever a host function is
called. This indirection was generally done to prevent monomoprhizing a
lot into consumer code but the few extra functions this makes
monomorphic are fairly small, and in general wasm->host call performance
is pretty important.

While not a massive win this is expected to improve codegen, especially
because with the indirect call removed the compiler should now be able
to prove more often when a `Func::new` closure doesn't panic or return
an error.
2021-09-03 13:40:51 -05:00
Afonso Bordado
81d5781e6c cranelift: CLIF fuzzer generate jump tables and br_table 2021-09-03 19:10:49 +01:00
Afonso Bordado
cbfae6f336 cranelift: CLIF fuzzer refactor param count generation 2021-09-03 19:10:49 +01:00
Chris Fallin
36b7e81979 Merge pull request #3094 from afonso360/fuzzer-branching
Cranelift CLIF Fuzzer generate blocks and branches
2021-09-03 10:30:14 -07:00
Chris Fallin
ecd795f736 Merge pull request #3290 from dheaton-arm/implement-ssatarith
Implement `SaddSat` and `SsubSat` for the Cranelift interpreter
2021-09-03 09:48:34 -07:00
Alex Crichton
c33700087d Align order of wasm types/values across Wasmtime (#3292)
Wasmtime has a few representations of `Val` and `ValType` across the
internal crates, the `wasmtime` crate, and the C API. These were
previously sometimes mentioned in different orders which means that
converting between the two took a little extra code than before. This
commit is a micro-optimization to align the types across the various
places we define these to help reduce the codegen burden when converting
between these types.

This is not expected to have a major impact on performance, rather it's
a small cleanup which should be easy-ish to preserve I've noticed while
staring at assembly.
2021-09-03 11:43:56 -05:00
Chris Fallin
e3ccff0249 Merge pull request #3283 from dheaton-arm/implement-umulhi
Implement `Umulhi` for the interpreter
2021-09-03 09:29:21 -07:00
dheaton-arm
8f057e0482 Implement SaddSat and SsubSat for the interpreter
Implemented `SaddSat` and `SsubSat` to add and subtract signed vector
values, saturating at the type boundaries rather than overflowing.

Changed the parser to allow signed `i8` immediates in vectors as part of
this work; fixes #3276.

Copyright (c) 2021, Arm Limited.
2021-09-03 11:35:39 +01:00
dheaton-arm
562947c678 Fix CI tests + rename tests
- Fixed CI tests for AArch64 and old x86.
- Rename `simd-umulhi.clif` to `umulhi.clif`.
- Rename `simd-umulhi-aarch64.clif` to `simd-umulhi.clif`.

Copyright (c) 2021, Arm Limited.
2021-09-03 10:37:24 +01:00
Nick Fitzgerald
dd71acd7e3 Merge pull request #3281 from alexcrichton/small-opts
Some small optimizations for calling wasm functions
2021-09-02 15:06:42 -07:00
Pat Hickey
fa15adfdd0 Merge pull request #3271 from bytecodealliance/pch/flexible_ser_module_versioning
More flexible versioning for module serialization
2021-09-02 12:51:03 -07:00
Chris Fallin
d6a77898ba Merge pull request #3272 from dheaton-arm/implement-iaddpairwise
Implement `IaddPairwise` for the interpreter
2021-09-02 10:52:47 -07:00
Chris Fallin
ebbb7600e1 Merge pull request #3286 from cfallin/fix-meeting-notes-typo
Fix meeting-notes typo (thanks @bjorn3).
2021-09-02 10:01:30 -07:00
Chris Fallin
d9595ad143 Fix meeting-notes typo (thanks @bjorn3). 2021-09-02 10:00:43 -07:00
Chris Fallin
6e05b646a3 Merge pull request #3282 from afonso360/x64-fix-brtables
cranelift: Fix `br_table` for `i64` types in x64 backend.
2021-09-02 09:58:42 -07:00
Chris Fallin
2389a4ea00 Merge pull request #3274 from bnjbvr/fix-m1
A round of Mac M1 fixes
2021-09-02 09:49:01 -07:00
Chris Fallin
000a97f4ff Merge pull request #3279 from dheaton-arm/implement-insertlane
Implement `Insertlane` for the Cranelift interpreter
2021-09-02 09:44:59 -07:00
Chris Fallin
3fed24ff4c Merge pull request #3285 from cfallin/wasmtime-mtg-notes-20210902
Meeting notes from wasmtime meeting on 2021-09-02.
2021-09-02 09:39:06 -07:00
Chris Fallin
b99a81cedd Meeting notes from wasmtime meeting on 2021-09-02. 2021-09-02 09:38:06 -07:00
Pat Hickey
63b7120a00 fix the tests 2021-09-02 09:31:21 -07:00
Afonso Bordado
20913a7473 cranelift: Enable compiling br_tables for types larger than i32 2021-09-02 16:26:23 +01:00
Alex Crichton
37b9fc5333 Fix async build 2021-09-02 07:38:29 -07:00
Afonso Bordado
f9ada24bcf cranelift: Fix br_table for i64 inputs
We still only support a maximum of u32::MAX entries, however we no
longer crash when compiling 64 bit indexes.

Fixes #3100
2021-09-02 15:31:48 +01:00
Alex Crichton
6b5e21d80e Inline some trivial store accessors
These were showing up in some profiles, but they're trivial functions,
so `#[inline]` them.
2021-09-02 07:26:10 -07:00
Alex Crichton
230159efa7 Inline some type conversions for ()
The `()` type accidentally wasn't getting its trivial type conversions
inlined because it doesn't actually have any type parameters. This
commit adds `#[inline]` to the relevant functions to ensure that these
get inlined across crates.
2021-09-02 07:26:07 -07:00
Alex Crichton
c8f55ed688 Optimize codegen slightly calling wasm functions
Currently wasm-calls work with `Result<T, Trap>` internally but `Trap`
is an enum defined in `wasmtime-runtime` which is actually quite large.
Since traps are supposed to be rare this commit changes these functions
to return a `Box<Trap>` which is un-boxed later up in the `wasmtime`
crate within a `#[cold]` function.
2021-09-02 07:26:03 -07:00
Benjamin Bouvier
0cf9b2d5e6 Remove cargo check for old rustc versions
Firefox has updated its toolchain to something much more recent.
2021-09-02 16:06:02 +02:00
Alex Crichton
25755ff23a Discuss precompiled modules at today's meeting (#3280)
A new agenda item!
2021-09-02 09:03:54 -05:00
dheaton-arm
16b6a404e4 Implement Umulhi for the interpreter
Implemented `Umulhi` for the Cranelift interpreter, performing unsigned
integer multiplication and producing the high half of a double-length
result.

Fixed `ExtractUpper` conversion behaviour as part of this change, which
was extracting from a 128-bit value regardless of the size of the
original value.

Copyright (c) 2021, Arm Limited.
2021-09-02 13:11:41 +01:00
Benjamin Bouvier
85ec11acb9 Aarch64: always generate the CFA directive indicating no pointer signing 2021-09-02 09:16:34 +02:00
Benjamin Bouvier
fb94b81538 Use 16K code pages on Mac M1
Fixes #3278.
2021-09-02 09:16:34 +02:00
Benjamin Bouvier
f871e8cf8f Correctly set the address of FP when unwinding from within fibers on aarch64
Fixes #3256.
2021-09-02 08:58:03 +02:00
Pat Hickey
f46f58ecc2 replace Config::deserialize_check_wasmtime_version with Config::module_version
which is more expressive than the former.

Instead of just configuring Module::deserialize to ignore version
information, we can configure Module::serialize to emit a custom version
string, and Module::deserialize to check for that string. A new enum
ModuleVersionStrategy is declared, and
Config::deserialize_check_wasmtime_version:bool is replaced with
Config::module_version:ModuleVersionStrategy.
2021-09-01 17:12:15 -07:00
Afonso Bordado
f0f2efba26 cranelift: CLIF Fuzzer generate brz/brnz/bricmp instructions 2021-09-01 19:44:11 +01:00
Afonso Bordado
f4bd7d17a3 cranelift: CLIF Fuzzer generate multiple blocks 2021-09-01 19:44:05 +01:00
Afonso Bordado
fb1201cb60 cranelift: CLIF Fuzzer limit instructions executed in interpreter 2021-09-01 19:43:58 +01:00
Alex Crichton
1532516a36 Use relative call instructions between wasm functions (#3275)
* Use relative `call` instructions between wasm functions

This commit is a relatively major change to the way that Wasmtime
generates code for Wasm modules and how functions call each other.
Prior to this commit all function calls between functions, even if they
were defined in the same module, were done indirectly through a
register. To implement this the backend would emit an absolute 8-byte
relocation near all function calls, load that address into a register,
and then call it. While this technique is simple to implement and easy
to get right, it has two primary downsides associated with it:

* Function calls are always indirect which means they are more difficult
  to predict, resulting in worse performance.

* Generating a relocation-per-function call requires expensive
  relocation resolution at module-load time, which can be a large
  contributing factor to how long it takes to load a precompiled module.

To fix these issues, while also somewhat compromising on the previously
simple implementation technique, this commit switches wasm calls within
a module to using the `colocated` flag enabled in Cranelift-speak, which
basically means that a relative call instruction is used with a
relocation that's resolved relative to the pc of the call instruction
itself.

When switching the `colocated` flag to `true` this commit is also then
able to move much of the relocation resolution from `wasmtime_jit::link`
into `wasmtime_cranelift::obj` during object-construction time. This
frontloads all relocation work which means that there's actually no
relocations related to function calls in the final image, solving both
of our points above.

The main gotcha in implementing this technique is that there are
hardware limitations to relative function calls which mean we can't
simply blindly use them. AArch64, for example, can only go +/- 64 MB
from the `bl` instruction to the target, which means that if the
function we're calling is a greater distance away then we would fail to
resolve that relocation. On x86_64 the limits are +/- 2GB which are much
larger, but theoretically still feasible to hit. Consequently the main
increase in implementation complexity is fixing this issue.

This issue is actually already present in Cranelift itself, and is
internally one of the invariants handled by the `MachBuffer` type. When
generating a function relative jumps between basic blocks have similar
restrictions. This commit adds new methods for the `MachBackend` trait
and updates the implementation of `MachBuffer` to account for all these
new branches. Specifically the changes to `MachBuffer` are:

* For AAarch64 the `LabelUse::Branch26` value now supports veneers, and
  AArch64 calls use this to resolve relocations.

* The `emit_island` function has been rewritten internally to handle
  some cases which previously didn't come up before, such as:

  * When emitting an island the deadline is now recalculated, where
    previously it was always set to infinitely in the future. This was ok
    prior since only a `Branch19` supported veneers and once it was
    promoted no veneers were supported, so without multiple layers of
    promotion the lack of a new deadline was ok.

  * When emitting an island all pending fixups had veneers forced if
    their branch target wasn't known yet. This was generally ok for
    19-bit fixups since the only kind getting a veneer was a 19-bit
    fixup, but with mixed kinds it's a bit odd to force veneers for a
    26-bit fixup just because a nearby 19-bit fixup needed a veneer.
    Instead fixups are now re-enqueued unless they're known to be
    out-of-bounds. This may run the risk of generating more islands for
    19-bit branches but it should also reduce the number of islands for
    between-function calls.

  * Otherwise the internal logic was tweaked to ideally be a bit more
    simple, but that's a pretty subjective criteria in compilers...

I've added some simple testing of this for now. A synthetic compiler
option was create to simply add padded 0s between functions and test
cases implement various forms of calls that at least need veneers. A
test is also included for x86_64, but it is unfortunately pretty slow
because it requires generating 2GB of output. I'm hoping for now it's
not too bad, but we can disable the test if it's prohibitive and
otherwise just comment the necessary portions to be sure to run the
ignored test if these parts of the code have changed.

The final end-result of this commit is that for a large module I'm
working with the number of relocations dropped to zero, meaning that
nothing actually needs to be done to the text section when it's loaded
into memory (yay!). I haven't run final benchmarks yet but this is the
last remaining source of significant slowdown when loading modules,
after I land a number of other PRs both active and ones that I only have
locally for now.

* Fix arm32

* Review comments
2021-09-01 13:27:38 -05:00
Chris Fallin
91410aaddf Merge pull request #3234 from dheaton-arm/implement-isubb
Implement `IsubBin`, `IsubBout`, and `IsubBorrow`for Cranelift interpreter
2021-09-01 11:25:43 -07:00
Chris Fallin
2a63979151 Merge pull request #3258 from afonso360/ssa-dce
cranelift: Prevent infinite loops in ssa frontend with unreachable code.
2021-09-01 11:19:40 -07:00
Aaron Turner
191051b644 Docs: Created the Wasmtime Markdown Parser Example (#3193)
* Finished the Markdown Parser Example for Wasmtime

* Made requested changes

* Tiny change to explanation of `--dir` CLI arg

* Add `bash` annotations to shell script code blocks

* Trying to fix the markdown example bug

* Figured out rustdoc, and what needed to be done

* Made requested changes

Co-authored-by: Till Schneidereit <till@tillschneidereit.net>
2021-09-01 12:25:36 -05:00
dheaton-arm
d956d349d8 Implement Insertlane for the Cranelift interpreter
Implemented `Insertlane` to insert a value in the lane specified by the
immediate value, overwriting the existing value in that lane.

Added `TernaryImm8` support for the `imm_value` function.

Copyright (c) 2021, Arm Limited.
2021-09-01 16:21:27 +01:00
dheaton-arm
4cdb2d3dac Merge vector iterators into chain
Copyrght (c) 2021, Arm Limited
2021-09-01 15:55:35 +01:00
Afonso Bordado
f9f5ae59a6 cranelift: Merge interpreter tests with runtests (#3252)
Almost all the tests in the interpreter are already in the runtests
folder so that we can reuse them for the backends. The distinction
between interpreter tests and runtests is no longer very clear, since
they should both support the same clif code, and produce the same results.

We only have two test files:

* `add.clif` tests the add and jump instruction, both of which are
already covered in other test files, so we remove that file.

* `fibonacci.clif` does a recursive call which is currently not supported
in the filetest environment, so we keep this test interpreter only for now.
2021-09-01 06:42:02 -07:00