Commit graph

302 commits

Author SHA1 Message Date
Ali Mohammad Pur a60ecea16a LibWasm+LibWeb+test-wasm: Refcount Wasm::Module for function references
Prior to funcref, a partial chunk of an invalid module was never needed,
but funcref allows a partially instantiated module to modify imported
tables with references to its own functions, which means we need to keep
the second module alive while that function reference is present within
the imported table.
This was tested by the spectests, but very rarely caught as our GC does
not behave particularly predictably, making it so the offending module
remains in memory just long enough to let the tests pass.

This commit makes it so all function references keep their respective
modules alive.
2024-08-22 09:36:40 +02:00
Andreas Kling 2f3128333d LibWasm: Make BytecodeInterpreter overrides final
This gives us free devirtualization of some hot calls inside the
bytecode interpreter. Most notably the did_trap() checks.

Modest performance improvement on the https://figma.com/ landing page.
2024-08-20 12:01:43 +02:00
Diego Frias ed153a1720 LibWasm: Grow table type in table.grow 2024-08-18 23:35:02 +02:00
Diego Frias 4e7d3026d2 LibWasm+LibWeb: Allow tables to have externrefs in the JS API 2024-08-18 23:35:02 +02:00
Ali Mohammad Pur 0d05ab2ad0 LibWasm: Allow all Value::to<Integral>() calls
This brings back the old behaviour of Value::to<short>() (and other
similar calls), which WASI depends on.
To make sure all similar issues are caught in the future, this commit
also introduces an static assertion in Value::to().
2024-08-16 21:03:10 +02:00
Diego Frias c58665332e LibWasm: Ensure correct section ordering when parsing binary modules
There are (currently) no spec-tests ensuring that section ordering is
enforced, but it _is_ a part of the spec. A pull request to add this to
the specification testsuite has been opened at WebAssembly/spec#1775.
2024-08-10 10:39:10 +02:00
Ali Mohammad Pur d2ff92077b LibWasm: Use braces to initialize Reference::Foo classes
These have no declared constructor, so apple clang and clang clang seem
to disagree on some versions.
2024-08-07 23:39:46 +02:00
Diego Frias a58704296c LibWasm: Remove type information from Value
Gets fib(30) from 380ms to 340ms.
2024-08-07 01:10:20 +02:00
Diego Frias a2448308fd LibWasm: Directly remove from the stack when clearing a label
Theoretically, the previous "pop, then push" method should be faster,
but it's actually faster to just remove from the stack directly.
2024-08-07 01:10:20 +02:00
Diego Frias b73b17aab4 LibWasm: Inline bytecode interpreter instruction interpretation
Gets fib(30) from 480ms to 380ms.
2024-08-07 01:10:20 +02:00
Diego Frias a3b077c641 LibWasm: Split main interpreter stack into three
Instead of one stack to hold frames, labels, and values, there is now
three separate stacks. This speeds up fib(30) from 580ms to 480ms.
2024-08-07 01:10:20 +02:00
Diego Frias 15510fb42e LibWasm: Simplify downcasting in bytecode interpreter 2024-08-07 01:10:20 +02:00
Diego Frias 23cfee2205 LibWasm: Clean up module sections API
Remove `for_each_section_of_type` in favor of making the module's
sections defined as distinct fields. This means it is no longer possible
to have two of the same section (which is invalid in WebAssembly, for
anything other than custom sections).
2024-08-01 11:44:07 +02:00
Diego Frias 41dc58f702 LibWasm: Remove unused ParseError variant 2024-07-28 13:02:36 +02:00
Diego Frias 9800ef6047 LibWasm: Introduce TRY_READ for parser
Convenience macro akin to `TRY` for reading from a stream.
2024-07-28 13:02:36 +02:00
Diego Frias dc52998341 LibWasm: Remove Module::functions
`Module::functions` created clones of all of the functions in the
module. It provided a _slightly_ better API, but ended up costing around
40ms when instantiating spidermonkey.
2024-07-28 02:56:32 +02:00
Diego Frias fc57f5111d LibWasm: Remove needless and costly copies
Speeds up spidermonkey.wasm instantiation by around 60ms (240ms -> 180ms)
2024-07-27 08:19:51 +02:00
Diego Frias 2192c149e2 LibWasm: Stop using ConstrainedStream for function parsing
Speeds up spidermonkey.wasm instantiation by around 20ms (260ms -> 240ms)
2024-07-27 08:19:51 +02:00
Diego Frias 5cde327d46 LibWasm: Provide size hint when parsing instructions into a vector
Speeds up spidermonkey.wasm instantiation by around 20ms (280ms -> 260ms)
2024-07-27 08:19:51 +02:00
Diego Frias 2cfc1873c0 LibWasm: Flatten instructions in one continuous loop during parsing
Instead of multiple loops and multiple vectors, parse Wasm expressions
in a simple loop. This gets us from ~450ms to instantiate spidermonkey
to ~280ms.
2024-07-25 12:16:45 +02:00
Diego Frias 4e8376d07e LibWasm: Remove unused vector methods of the interpreter 2024-07-24 23:23:09 +02:00
Diego Frias 9cc3e7d32d LibWasm: Fix SIMD shuffle and swizzle
`swizzle` had the wrong operands, and the vector masking boolean logic
was incorrect in the internal `shuffle_or_0` implementation. `shuffle`
was previously implemented as a dynamic swizzle, when it uses an
immediate operand for lane indices in the spec.
2024-07-24 23:23:09 +02:00
Diego Frias d841742c35 LibWasm: Fix SIMD bit shift right
Set the sign in the vector's element type (even though it's a bit
redundant).
2024-07-24 23:23:09 +02:00
Diego Frias 4b9649282e LibWasm: Implement the rest of the SIMD conversions 2024-07-20 11:17:29 +02:00
Diego Frias 21c5084d23 LibWasm: Fix v128.any_true instruction 2024-07-20 11:17:29 +02:00
Diego Frias 616048c67e LibWasm: Implement integer conversion and narrowing SIMD instructions 2024-07-20 11:17:29 +02:00
Diego Frias 146646b597 LibWasm: Implement bitmask and float conversion instructions 2024-07-20 11:17:29 +02:00
Diego Frias ea67bc989f LibWasm: Remove some unnecessary memory checks
Also make `store_to_memory` take a `MemoryArgument` so that we no longer
have to make "synthetic instructions" in some scenarios.
2024-07-19 03:19:04 +02:00
Diego Frias 56ae6b3968 LibWasm: Remove some dead code 2024-07-19 03:19:04 +02:00
Ali Mohammad Pur 8cf0f36f7d LibWasm: Replace a hashtable with an RBTree to make instantiation faster
...by about 40%.
2024-07-18 14:54:50 +02:00
Ali Mohammad Pur f1abc36689 LibWasm/WASI: Make the premapped stdio files configurable 2024-07-18 14:54:50 +02:00
Ali Mohammad Pur 16dd8d4d3b LibWasm/WASI+wasm: Make WASI exit() "noreturn" instead of making it exit
This makes it so exit() traps with a known error; an embedder (wasm.cpp)
can simply match this format and handle the request accordingly.
2024-07-18 14:54:50 +02:00
Ali Mohammad Pur e08a7cb94d LibWasm/WASI: Add support for fd_seek() 2024-07-18 14:54:50 +02:00
Ali Mohammad Pur 708abf6bb1 LibWasm/WASI: Add support for fd_fdstat_get() 2024-07-18 14:54:50 +02:00
Ali Mohammad Pur e22408b8b4 LibWasm: Make import errors a bit more descriptive and helpful
Instead of "yeah something went wrong", show what went wrong.
2024-07-18 14:54:50 +02:00
Hendiadyoin1 144e822de2 LibWasm: Use shuffle_or_0 in for vector swizzles and shuffles
Otherwise we'd hit a VERIFY in AK::SIMD::shuffle() when that operand
contains an out-of-range value, the spec tests indicate that a swizzle
with an out-of-range index should return 0.

(cherry picked from commit cd454a1e3d0bc8b3342ed39891c9b27409ecc829)
2024-07-17 09:56:13 -06:00
Ali Mohammad Pur 8c8310f0bd LibWasm: Make Absolute/Negate<SignedIntegral> explicitly work mod 2^N
Previously we relied on signed overflow, this commit makes the same
behaviour explicit (avoiding UB in the process).
2024-07-16 15:09:03 +02:00
Diego Frias 8a0ef17d9a LibWasm: Correctly validate v128_load*_lane instructions 2024-07-15 09:58:04 -06:00
Diego Frias f5326f1747 LibWasm: Implement rest of SIMD load/store instructions
Also implement `v128.any_true`.
2024-07-15 09:58:04 -06:00
Enver Balalic f0a306fe50 LibWasm: Implement most of iNxM SIMD operations
With this we pass an additional ~2100 tests.
We are left with 7106 WASM fails :).

There's still some test cases in the iNxM tests that fail with
this PR, but they are somewhat weird.

Co-authored-by: Diego Frias <styx5242@gmail.com>
2024-07-14 11:35:11 +02:00
Diego 4c7ef01b44 LibWasm: Fix sign issues in SIMD cmp ops 2024-07-13 14:38:10 +02:00
Diego Frias 13a8c2a79d LibWasm: Make SIMD float min/max operations binary ops
They previously acted like comparison operators, which was not correct.
2024-07-13 01:27:55 +02:00
Diego 3efb11f5d8 LibWasm: Validate stack correctly in v128_store*_lane instructions
Previously the validator put a `v128` on the stack, which is not what
the spec defines.
2024-07-13 00:04:02 +02:00
Diego 420a626554 LibWasm: Make memory.grow grow the memory's type
After a `memory.grow`, the type of the memory instance should be
updated so potential memory imports on the boundary are unlinkable.
2024-07-11 01:31:22 +02:00
Diego d07cf26894 LibWasm: Fix loop arity for single-type blocktypes
Single-type blocktypes previously gave loop labels an arity of 1, even
though they're shorthand for `[] -> [T]`.
2024-07-11 00:51:03 +02:00
Diego 625fbc8085 LibWasm: Implement SIMD bitwise operations 2024-07-11 00:50:08 +02:00
Diego e8fd8982f8 LibWasm: Give names to functions exported to JS via ref.func
https://webassembly.github.io/spec/js-api/index.html#name-of-the-webassembly-function
2024-07-10 00:37:18 +02:00
Diego afd8d90f32 LibWasm: Error when parsed section lengths are invalidated 2024-07-09 14:22:31 +02:00
Diego 5382fbb617 LibWasm: Remove Wasm::ValueType::Kind::Null* variants
As far as I know, they're not in the spec and don't serve any purposes
in the internals of LibWasm.
2024-07-09 14:22:00 +02:00
Diego 31c7e98a4a LibWasm: Fix comparisons between 0.0 and -0.0
According to the spec, -0.0 < 0.0.
2024-07-08 15:12:16 +02:00