Commit Graph

4512 Commits

Author SHA1 Message Date
Alex Crichton
62f8928bee x64: Add non-SSE4.1 lowerings of ceil/trunc/floor/nearest (#6224)
* x64: Add non-SSE4.1 lowerings of ceil/trunc/floor/nearest

This commit adds lowerings that work with SSE2 for CLIF `ceil`, `trunc`,
`floor`, and `nearest` instructions over vectors. To get these working
`insertlane` for float vectors was also implemented for non-SSE4.1
instructions as well.

Note that the goal of these lowerings is not speed but rather "it
works", so the decompose-to-call-libcalls logic for vector is probably
horrendously slow but should at least be correct.

* Skip new tests on riscv64

* Update cranelift/codegen/src/isa/x64/inst.isle

Co-authored-by: Andrew Brown <andrew.brown@intel.com>

---------

Co-authored-by: Andrew Brown <andrew.brown@intel.com>
2023-04-18 17:23:18 +00:00
T0b1-iOS
387db16d28 Remove unsigned variants of DataValue (#6218)
* remove unsigned variants of DataValue

* make value operation names more in-line with cranelift IR
2023-04-18 14:08:29 +00:00
Alex Crichton
7ebff82861 Optimize sign extension via shifts (#6220)
* Optimize sign extension via shifts

This commit adds egraph optimization patterns for left-shifting a value
and then right-shifting it as a form of sign extending its lower bits.
This matches the behavior of the WebAssembly `i32.extend8_s`
instruction, for example. Note that the lowering of that WebAssembly
instruction does not use shifts, but historical versions of LLVM that
didn't support the instruction, or versions with the instruction
disabled, will use shifts instead.

A second rule for reduction-of-extend being the same as the original
value was added to keep an existing shift-related test passing as well.

* Add reference assemblies for new opts
2023-04-17 18:48:08 +00:00
Alex Crichton
9a4bd7c6df x64: Begin to lift SSE 4.1 requirement for SIMD support (#6216)
* x64: Change `use_sse41` to a constructor

This refactors the existing `use_sse41` extractor to instead be a
`constructor` to use with `if-let`.

* x64: Gate the `pblendw` instruction on SSE4.1 being enabled

This specialization of `shuffle` isn't a base case so adding an `if-let`
here should be sufficient for gating this instruction properly on
enabled CPU features.

* x64: Gate `pmuldq` lowerings on SSE 4.1

The specialized rules using these instructions can fall back to the
standard lowerings for non-SSE 4.1 instructions.
2023-04-17 16:09:58 +00:00
kevaundray
85118c8c26 Add clippy suggestions (#6203)
* add clippy suggestions

* revert &/ref change

* Update cranelift/isle/isle/src/parser.rs

Co-authored-by: Jamey Sharp <jamey@minilop.net>

---------

Co-authored-by: Jamey Sharp <jamey@minilop.net>
2023-04-17 15:53:34 +00:00
Alex Crichton
91de5de049 Update wasm-tools crates (#6215)
While bringing in no major updates for Wasmtime I've taken this
opportunity to list myself for `cargo vet` with wildcard audits of this
family of crates. That means I shouldn't need to further add any more
entries in the future for updating these crates and additionally any
other organizations using these audits will automatically be able to
have audits for version that I publish.

While here I also ran `cargo vet prune` which was able to remove a
number of our exemptions.
2023-04-15 00:07:32 +00:00
Afonso Bordado
9e1ff9726c egraphs: Add bmask bit pattern optimization rule (#6196)
* egraphs: Add a bmask bit pattern optimization

* egraphs: Add more `ineg` rules

* egraphs: Add sshr rule

* egraphs: Simplify bmask rule

* egraphs: Add comutative version of bmask rule

* egraphs: Add more testcases

* egraphs: Cleanup rule comments

* egraphs: Add more `ineg` optimizations
2023-04-14 18:50:48 +00:00
Alex Crichton
2d25db047f x64: Lower SIMD requirement to SSE4.1 from SSE4.2 (#6206)
Cranelift only has one instruction SIMD which depends on SSE4.2 so this
commit adds a lowering rule for `pcmpgtq` which doesn't use SSE4.2 and
enables lowering the baseline requirement for SIMD support from SSE4.2
to SSE4.1.

The `has_sse42` setting is no longer enabled by default for Cranelift.
Additionally `enable_simd` no longer requires `has_sse42` on x64.
Finally the fuzz-generator for Wasmtime codegen settings now enables
flipping the `has_sse42` setting instead of unconditionally setting it
to `true`.

The specific lowering for `pcmpgtq` is copied from LLVM's lowering of
this instruction.
2023-04-14 17:24:43 +00:00
T0b1-iOS
3956a6aa0f remove unsigned_add_overflow_condition (#6199) 2023-04-13 14:30:44 +00:00
Karl Meakin
91e36f3449 Clarify the representation of icmp output (#6202)
* Clarify the representation of `icmp` output

* Reformat

* "ie" => "i.e."

* Update `fcmp` documentation as well
2023-04-12 20:05:44 +00:00
Karl Meakin
42528d82b8 Add multi_lane precondition to bitselect => {u,s}{min,max} rewrite (#6201) 2023-04-12 19:04:30 +00:00
T0b1-iOS
f684a5fbee remove iadd_cout and isub_bout (#6198) 2023-04-11 23:39:32 +00:00
Karl Meakin
c0166f78f9 ISLE: simplify select/bitselect when both choices are the same (#6141) 2023-04-11 22:41:19 +00:00
Karl Meakin
b9a58148cf ISLE: split algebraic.isle into several files (#6140)
* ISLE: split algebraic.isle into several files

* delete `algebraic.clif`

* Add `README.md`

* Remove old `algebraic.clif` tests

---------

Co-authored-by: Jamey Sharp <jsharp@fastly.com>
2023-04-11 21:39:18 +00:00
T0b1-iOS
569089e473 Add {u,s}{add,sub,mul}_overflow instructions (#5784)
* add `{u,s}{add,sub,mul}_overflow` with interpreter

* add `{u,s}{add,sub,mul}_overflow` for x64

* add `{u,s}{add,sub,mul}_overflow` for aarch64

* 128bit filetests for `{u,s}{add,sub,mul}_overflow`

* `{u,s}{add,sub,mul}_overflow` emit tests for x64

* `{u,s}{add,sub,mul}_overflow` emit tests for aarch64

* Initial review changes

* add `with_flags_extended` helper

* add `with_flags_chained` helper
2023-04-11 20:16:04 +00:00
Afonso Bordado
4c32dd7786 riscv64: Delete SelectIf instruction (#5888)
* riscv64: Delete `SelectIf` instruction

* riscv64: Fix typo in comment

Co-authored-by: Trevor Elliott <awesomelyawesome@gmail.com>

* riscv64: Improve `bmask` codegen

* riscv64: Use `lower_bmask` in `select_spectre_guard`

* riscv64: Use `lower_bmask` to extend values in `select_spectre_guard`

Co-authored-by: Trevor Elliott <awesomelyawesome@gmail.com>

---------

Co-authored-by: Trevor Elliott <awesomelyawesome@gmail.com>
2023-04-11 17:33:32 +00:00
Afonso Bordado
9acb649f17 cranelift-native: Detect RISC-V extensions using /proc/cpuinfo (#6192)
* cranelift-native: Move riscv to separate module

* cranelift-native: Read /proc/cpuinfo to parse RISC-V extensions

* ci: Add QEMU cpuinfo emulation patch

This patch emulates the /proc/cpuinfo interface for RISC-V. This
allows us to do feature detection for the RISC-V backend.

It has been queued for QEMU 8.1 so we should remove it as soon as
that is available.

* ci: Enable QEMU RISC-V extensions

* cranelift-native: Cleanup ISA string parsing

Co-Authored-By: Jamey Sharp <jsharp@fastly.com>

* cranelift-native: Rework `/proc/cpuinfo` parsing

Co-Authored-By: Jamey Sharp <jsharp@fastly.com>

---------

Co-authored-by: Jamey Sharp <jsharp@fastly.com>
2023-04-11 17:31:42 +00:00
kevaundray
f2393b8f27 Removes debug assertion that was related to issue 796 (#6175)
* fix typo: behaviour -> behavior

* remove debug assertion since 796 has been merged

* Update data_value.rs
2023-04-11 16:52:11 +00:00
bjorn3
0478ead3f8 Handle signature() for more libcalls (#6174)
* Handle signature() for more libcalls

This is necessary to be able to call them in the interpreter. All the
remaining libcalls which signature() doesn't handle are never used in
clif ir. Only in code compiled by a backend.

* Fix libcall declarations in cranelift-frontend

* Add function signatures

* Use correct pointer type instead of I64
2023-04-11 16:50:41 +00:00
bjorn3
52440f0fc8 Remove ImmutableRegisterState and replace {get,set}_value in State with current_frame{,_mut} (#6179)
* Remove ImmutableRegisterState

It was introduced for an SCCP optimization pass, but a simplified
version of this will likely use the egraph infrastructure instead.

* Replace {get,set}_value in State with current_frame{,_mut}

The outer Interpreter needs this anyway and only offering one way to
get locals simplifies things.

* Update comment
2023-04-11 12:15:22 +00:00
bjorn3
96a60aa26b Make cranelift-interpreter non-generic over value (#6178)
* Make cranelift-interpreter non-generic over value

Fixes #5793

* Review suggestion

Co-authored-by: Jamey Sharp <jamey@minilop.net>

* Fix fuzz target

* Update doc comments

---------

Co-authored-by: Jamey Sharp <jamey@minilop.net>
2023-04-11 11:13:29 +00:00
kevaundray
4053ae9e08 Minir typo/Grammar fixes (#6187)
* fix typo

* add test to check that Option<EntityRef> is twice as large as EntityRef

* grammar

* grammar

* reverse snakecase -- Not sure if folks want this type of change
2023-04-10 19:39:25 +00:00
Alex Crichton
435b6894d7 x64: Clarify and shrink up ModRM/SIB encoding (#6181)
I noticed recently that for the `ImmRegRegShift` addressing mode
Cranelift will unconditionally emit at least a 1-byte immediate for the
offset to be added to the register addition computation, even when the
offset is zero. In this case though the instruction encoding can be
slightly more compact and remove a byte. This commit started off by
applying this optimization, which resulted in the `*.clif` test changes
in this commit.

Further reading this code, however, I personally found it quite hard to
follow what was happening with all the various branches and ModRM/SIB
bits. I reviewed these encodings in the x64 architecture manual and
attempted to improve the logic for encoding here. The new version in
this commit is intended to be functionally equivalent to the prior
version where dropping a zero-offset from the `ImmRegRegShift` variant
is the only change.
2023-04-10 19:37:19 +00:00
Chris Fallin
8f1a7773a3 Revert "ISLE: rewrite loose inequalities to strict inequalities and strict inequalities to equalities (#6130)" (#6193)
This reverts commit 57e42d0c46.

Fixes #6185.
2023-04-10 18:43:15 +00:00
bjorn3
b9fb31e9a7 Re-export cranelift-control from cranelift-codegen (#6173)
This makes it easier to keep the versions of both in sync and avoids
having to specify another dependency for a single type.
2023-04-10 16:49:43 +00:00
Jamey Sharp
ac2bd1f305 cranelift: Rename a filetest with the wrong extension (#6190)
This test was committed with a `.isle` extension instead of `.clif`, so
it wasn't actually running in the test suite. Fortunately, it still
passes.
2023-04-10 16:27:42 +00:00
kevaundray
2d1dbb17af fix doc comment (#6183) 2023-04-10 14:23:49 +00:00
Chris Dickinson
a97e82c6e2 doc: fix StackSlot reference to FunctionBuilder (#6182)
`FunctionBuilder::create_stackslot` was split into `create_sized_stack_slot`
and `create_dynamic_stack_slot`. This updates the doc in the `StackBuilder`
docstring to refer to the new methods.

Fixes #5838.
2023-04-09 21:14:19 +00:00
Alexa VanHattum
71d3b638f3 Clarify instructions.rs documentation for ushr/ashr (narrow values) (#6186) 2023-04-09 20:01:49 +00:00
kevaundray
e3dbad9cc2 add result type assertion (#6184) 2023-04-09 19:55:15 +00:00
bjorn3
bada17beab Various cranelift interpreter improvements (#6176)
* Remove the validate_address State trait method

It isn't used anywhere

* Expose the inner Function of a Frame

This is necessary to create your own interpreter that reuses most of
cranelift-interpreter. For example to use a different State
implementation.

* Support the symbol_value and tls_value instructions in the interpreter
2023-04-07 15:22:13 +00:00
bjorn3
e1777710b1 Don't override declare_*_in_func in cranelift-jit (#6169)
Instead remove the colocated flag for hotplug mode in define_function.
This prevents issues if declare_*_in_func wasn't used due to eg the
function being from a previously serialized module and now deserialized
into JITModule.
2023-04-06 18:44:12 +00:00
Chris Fallin
230e2135d6 Cranelift: remove non-egraphs optimization pipeline and use_egraphs option. (#6167)
* Cranelift: remove non-egraphs optimization pipeline and `use_egraphs` option.

This PR removes the LICM, GVN, and preopt passes, and associated support
pieces, from `cranelift-codegen`. Not to worry, we still have
optimizations: the egraph framework subsumes all of these, and has been
on by default since #5181.

A few decision points:

- Filetests for the legacy LICM, GVN and simple_preopt were removed too.
  As we built optimizations in the egraph framework we wrote new tests
  for the equivalent functionality, and many of the old tests were
  testing specific behaviors in the old implementations that may not be
  relevant anymore. However if folks prefer I could take a different
  approach here and try to port over all of the tests.

- The corresponding filetest modes (commands) were deleted too. The
  `test alias_analysis` mode remains, but no longer invokes a separate
  GVN first (since there is no separate GVN that will not also do alias
  analysis) so the tests were tweaked slightly to work with that. The
  egrpah testsuite also covers alias analysis.

- The `divconst_magic_numbers` module is removed since it's unused
  without `simple_preopt`, though this is the one remaining optimization
  we still need to build in the egraphs framework, pending #5908. The
  magic numbers will live forever in git history so removing this in the
  meantime is not a major issue IMHO.

- The `use_egraphs` setting itself was removed at both the Cranelift and
  Wasmtime levels. It has been marked deprecated for a few releases now
  (Wasmtime 6.0, 7.0, upcoming 8.0, and corresponding Cranelift
  versions) so I think this is probably OK. As an alternative if anyone
  feels strongly, we could leave the setting and make it a no-op.

* Update test outputs for remaining test differences.
2023-04-06 18:11:03 +00:00
bjorn3
67c85b883e Remove the DataContext wrapper around DataDescription (#6170)
* Remove the DataContext wrapper around DataDescription

It doesn't have much of a purpose while making it harder to for example
rewrite the function and data object declarations within it as is
necessary for deserializing a serialized module.

* Derive Debug for DataDescription
2023-04-06 17:13:55 +00:00
bjorn3
e1812b611b Rename define_function to define_function_with_control_plane (#6165)
And add a define_function convenience function which uses a default
control plane.
2023-04-06 16:14:13 +00:00
Afonso Bordado
a9cda5af19 cranelift: Implement PartialEq in Function (#6157) 2023-04-05 22:33:10 +00:00
Remo Senekowitsch
7eb8914090 Chaos mode MVP: Skip branch optimization in MachBuffer (#6039)
* fuzz: Add chaos mode control plane

Co-authored-by: Falk Zwimpfer <24669719+FalkZ@users.noreply.github.com>
Co-authored-by: Moritz Waser <mzrw.dev@pm.me>

* fuzz: Skip branch optimization with chaos mode

Co-authored-by: Falk Zwimpfer <24669719+FalkZ@users.noreply.github.com>
Co-authored-by: Moritz Waser <mzrw.dev@pm.me>

* fuzz: Rename chaos engine -> control plane

Co-authored-by: Falk Zwimpfer <24669719+FalkZ@users.noreply.github.com>
Co-authored-by: Moritz Waser <mzrw.dev@pm.me>

* chaos mode: refactoring ControlPlane to be passed through the call stack by reference

Co-authored-by: Falk Zwimpfer <24669719+FalkZ@users.noreply.github.com>
Co-authored-by: Remo Senekowitsch <contact@remsle.dev>

* fuzz: annotate chaos todos

Co-authored-by: Falk Zwimpfer <24669719+FalkZ@users.noreply.github.com>
Co-authored-by: Moritz Waser <mzrw.dev@pm.me>

* fuzz: cleanup control plane

Co-authored-by: Falk Zwimpfer <24669719+FalkZ@users.noreply.github.com>
Co-authored-by: Moritz Waser <mzrw.dev@pm.me>

* fuzz: remove control plane from compiler context

Co-authored-by: Falk Zwimpfer <24669719+FalkZ@users.noreply.github.com>
Co-authored-by: Moritz Waser <mzrw.dev@pm.me>

* fuzz: move control plane into emit state

Co-authored-by: Falk Zwimpfer <24669719+FalkZ@users.noreply.github.com>
Co-authored-by: Moritz Waser <mzrw.dev@pm.me>

* fuzz: fix remaining compiler errors

Co-authored-by: Falk Zwimpfer <24669719+FalkZ@users.noreply.github.com>
Co-authored-by: Moritz Waser <mzrw.dev@pm.me>

* fix tests

* refactor emission state ctrl plane accessors

Co-authored-by: Falk Zwimpfer <24669719+FalkZ@users.noreply.github.com>
Co-authored-by: Moritz Waser <mzrw.dev@pm.me>

* centralize conditional compilation of chaos mode

Also cleanup a few straggling dependencies on cranelift-control
that aren't needed anymore.

Co-authored-by: Falk Zwimpfer <24669719+FalkZ@users.noreply.github.com>
Co-authored-by: Moritz Waser <mzrw.dev@pm.me>

* add cranelift-control to published crates

prtest:full

Co-authored-by: Falk Zwimpfer <24669719+FalkZ@users.noreply.github.com>
Co-authored-by: Moritz Waser <mzrw.dev@pm.me>

* add cranelift-control to public crates

Co-authored-by: Falk Zwimpfer <24669719+FalkZ@users.noreply.github.com>
Co-authored-by: Moritz Waser <mzrw.dev@pm.me>

---------

Co-authored-by: Falk Zwimpfer <24669719+FalkZ@users.noreply.github.com>
Co-authored-by: Moritz Waser <mzrw.dev@pm.me>
Co-authored-by: Remo Senekowitsch <contact@remsle.dev>
2023-04-05 19:28:46 +00:00
Afonso Bordado
064968b01d cranelift-interpreter: Propagate traps across calls (#6156)
* cranelift-interpreter: Propagate traps from call's

* cranelift-interpreter: Make `unwrap_return` only available in tests

This is a footgun for normal use in the interpreter (#6156) but it
still has uses in the tests, so enable it only there.
2023-04-05 19:09:48 +00:00
Alex Crichton
967543eb43 aarch64: Add more lowerings for the CLIF fma (#6150)
This commit adds new lowerings to the AArch64 backend of the
element-based `fmla` and `fmls` instructions. These instructions have
one of the multiplicands as an implicit broadcast of a single lane of
another register and can help remove `shuffle` or `dup` instructions
that would otherwise be used to implement them.
2023-04-05 17:22:55 +00:00
wasmtime-publish
bf741955f0 Bump Wasmtime to 9.0.0 (#6143)
Co-authored-by: Wasmtime Publish <wasmtime-publish@users.noreply.github.com>
2023-04-05 17:06:36 +00:00
Jamey Sharp
34c282ac2e ISLE: pattern type is always known (#6144)
While type-checking the AST for a pattern, ISLE was passing in an
`Option<TypeId>` for the expected result type of the pattern. However,
at every call we either passed `Some` type explicitly, or passed the
parent's expected type in a self-recursive call.

Therefore, by induction, `expected_ty` is never `None`. So this PR
unwraps the type everywhere. That in turn shows that a bunch of error
messages were unreachable, so this deletes a bunch of error-handling
code.

In addition, this function returned the type it computed for the
sub-pattern, but that information is already available in the
sub-pattern itself. Not only that but the type should always be equal to
`expected_ty`; when it isn't, we've reported a type error and are just
trying to check for more errors.

Most callers ignored the returned type but in some cases we used it to
try to avoid emitting useless error messages. I've preserved that
behavior for bind-patterns.

For and-patterns, the returned type looked like it was being used, but
because `expected_ty` was never `None`, the fallback of "fill in with
the sub-pattern's type" never fired. So I've deleted that fallback.

Finally, this reverts #4915 (9d99eff6f9)
which was introduced to flatten nested and-patterns, to simplify overlap
checking. However, the visitor trait used by trie_again effectively
flattens and-patterns anyway, so the current representation used for
overlap checking doesn't need this any more.
2023-04-05 16:22:31 +00:00
Alex Crichton
d45cbba83f Add egraph cprop optimizations for splat (#6148)
This commit adds constant-propagation optimizations for
`splat`-of-constant to produce a `vconst` node. This should help later
hoisting these constants out of loops if it shows up in wasm.
2023-04-05 16:10:45 +00:00
Jamey Sharp
81545c3a86 Revert "simple_gvn: recognize commutative operators (#6135)" (#6142)
This reverts commit c85bf27ff8.
2023-04-04 20:22:44 +00:00
Karl Meakin
57e42d0c46 ISLE: rewrite loose inequalities to strict inequalities and strict inequalities to equalities (#6130)
* ISLE: rewrite loose inequalities to strict inequalities

* Rewrite strict inequalities to equalities where possible
2023-04-04 17:42:19 +00:00
Jan-Justin van Tonder
c475735f5e cranelift-interpreter: Fix incorrect scalar_to_vector result (#6133)
* The `vectorizelanes` function performs a check to see whether there
is a single value provided in an array, and if so returns it as a
scalar.

While elsewhere in the interpreter this behaviour is relied
upon, it yields an incorrect result when attempting to convert a
scalar to a vector.

The original `vectorizelanes` remains untouched, however, an
unconditional variant `vectorizelanes_all` was added.

* A test was added under `filetests/runtests/issue5911.clif`.

Fixes #5911
2023-04-04 12:14:16 +00:00
Karl Meakin
c85bf27ff8 simple_gvn: recognize commutative operators (#6135)
* simple_gvn: recognize commutative operators

Normalize instructions with commutative opcodes by sorting the arguments. This
means instructions like `iadd v0, v1` and `iadd v1, v0` will be considered
identical by GVN and deduplicated.

* Remove `UsubSat` and `SsubSat` from `is_commutative`

They are not actually commutative

* Remove `TODO`s

* Move InstructionData normalization into helper fn

* Add normalization of commutative instructions in the epgrah implementation

* Handle reflexive icmp/fcmps in GVN

* Change formatting of `normalize_in_place`

* suggestions from code review
2023-04-04 00:25:05 +00:00
Karl Meakin
c8c224ead6 ISLE: move icmp rewrites to separate file. (#6120)
* ISLE: move `icmp` rewrites to separate file.

Move `icmp`-related rewrite rules from `algebraic.isle` to `icmp.isle`.
Also move `icmp`-related tests from `algebraic.clif` to `icmp.clif`.

* Put parameterized and unparameterized `icmp` tests in separate files

* Undo refactoring of (ir)reflexivity rewrites

* Fix `icmp-parameterised.clif`

* Undo formatting/comment changes
2023-03-31 17:40:31 +00:00
Yoni L
94f2ff0921 cranelift::codegen::Context::optimize(): reduce verbosity of "egraph stats" traces (#6122) 2023-03-30 00:46:14 +00:00
Alex Crichton
0b0ac3ff73 x64: Add AVX support for some more float-related instructions (#6092)
* x64: Add AVX encodings of `vcvt{ss2sd,sd2ss}`

Additionally update the instruction helpers to take an `XmmMem` argument
to allow load sinking into the instruction.

* x64: Add AVX encoding of `sqrts{s,d}`

* x64: Add AVX support for `rounds{s,d}`
2023-03-29 18:09:49 +00:00
Alex Crichton
afb417920d x64: Deduplicate fcmp emission logic (#6113)
* x64: Deduplicate fcmp emission logic

The `select`-of-`fcmp` lowering duplicated a good deal of `FloatCC`
lowering logic that was already done by `emit_fcmp`, so this commit
refactors these lowering rules to instead delegate to `emit_fcmp` and
then handle that result.

* Swap order of condition codes

Shouldn't affect the correctness of this operation and it's a bit more
natural to write the lowering rule this way.

* Swap the order of comparison operands

No need to swap `a b`, only the `x y` needs swapping.

* Fix x64 printing of `XmmCmove`
2023-03-29 16:24:25 +00:00