Commit graph

22 commits

Author SHA1 Message Date
Ali Mohammad Pur
31da9ab4e8 LibWasm: Take memory_fill arguments in the right order
This makes ruffle.rs work again :^)
2025-10-04 11:17:08 +02:00
Ali Mohammad Pur
353febfab6 LibWasm: Remove confusing newline after a few TAILCALLs 2025-10-04 11:17:08 +02:00
Ali Mohammad Pur
2397ae4af5 LibWasm: Use [[gnu::musttail]] on new-enough GCC versions
This is supported starting GCC 15.
The warning -Wmaybe-musttail-local-addr complained about &value possibly
escaping (it cannot, but gcc is being pessimistic about
store_to_memory), so a little rearrangement of that function was
necessary.
2025-10-01 23:47:29 +02:00
Ali Mohammad Pur
02b3c4f8a9 LibWasm: Utilise direct threading if/when possible
~50% performance improvement on coremark.
2025-10-01 23:47:29 +02:00
Ali Mohammad Pur
cf30d61d8b LibWasm: Use a faster way to detect live registers
Instead of doing a naive O(n^2) liveness detection loop, use a bitmap
for values allocated to registers.
This cuts down validating time from 20% to 1.4% of runtime on the same
game as last commit.
2025-10-01 23:47:29 +02:00
Pavel Shliak
a125bc97c4 LibWasm: Fix memory.fill ignoring memory index and unsafe bounds check
Previously, the memory.fill instruction always wrote to memory 0,
ignoring the selected memory index. This caused incorrect behavior
in multi-memory modules (e.g. filling mem0 instead of mem1).
Additionally, the bounds check used `destination_offset + count`
without overflow checking, which could wrap and bypass validation.

This patch:
- Passes `args.memory_index` into store_to_memory, so the correct
  memory is filled.
- Uses Checked<u32> for destination_offset + count, consistent
  with memory.copy and memory.init, to prevent overflow.

Minimal repro:

    (module
      (memory $m0 1)
      (memory $m1 1)

      (func (export "go") (result i32)
        ;; Fill mem1[0] with 0xAA
        i32.const 0
        i32.const 170
        i32.const 1
        memory.fill (memory 1)

        ;; Return (mem1[0] << 8) | mem0[0]
        i32.const 0
        i32.load8_u (memory 1)
        i32.const 8
        i32.shl
        i32.const 0
        i32.load8_u (memory 0)
        i32.or
      )
    )

Before fix: returns 170 (0x00AA).
After fix:  returns 43520 (0xAA00).
2025-09-06 08:51:11 +02:00
Ali Mohammad Pur
22448b0c35 LibWasm: Move the interpreter IP out of the configuration object
This, along with moving the sources and destination out of the config
object, makes it so we don't have to double-deref to get to them on each
instruction, leading to a ~15% perf improvement on dispatch.
2025-08-26 15:20:33 +02:00
Ali Mohammad Pur
f7bdc596b4 LibWasm: Avoid allocations for the label stack as much as possible
Namely, find an upper bound at validation time so we can allocate the
space when entering the frame.

Also drop labels at once instead of popping them off one at a time now
that we're using a Vector.
2025-08-26 15:20:33 +02:00
Ali Mohammad Pur
6732e1cdc3 LibWasm: Don't clobber registers on (most) calls
This still passes the values on the stack, but registers are now allowed
to cross a call boundary.
This is a very significant (>50%) improvement on the small call
microbenchmarks on my machine.
2025-08-26 15:20:33 +02:00
ayeteadoe
78a08bac82 LibWasm: Fix Windows build 2025-08-09 16:41:37 -06:00
Ali Mohammad Pur
b97ad99014 LibWasm: Remove unnecessary C-style casts
Or replace them with static-cast when necessary.
2025-08-08 12:54:06 +02:00
Ali Mohammad Pur
33cd5ae08c LibWasm: Fuse some very common instruction combos into specialised ops
Largely combinations of i32.const and local.get.
This shaves off at most single-digit% number of instructions from
dispatch, which translates to at most ~10% reduced dispatch time.

Across most benchmarks, this gains around ~5% perf increase.
2025-08-08 12:54:06 +02:00
Ali Mohammad Pur
0e5ecef848 LibWasm: Try really hard to avoid touching the value stack
This commit adds a register allocator, with 8 available "register"
slots.
In testing with various random blobs, this moves anywhere from 30% to
74% of value accesses into predefined slots, and is about a ~20% perf
increase end-to-end.

To actually make this usable, a few structural changes were also made:
- we no longer do one instruction per interpret call
- trapping is an (unlikely) exit condition
- the label and frame stacks are replaced with linked lists with a huge
  node cache size, as we only need to touch the last element and
  push/pop is very frequent.
2025-08-08 12:54:06 +02:00
Ali Mohammad Pur
bd7c188b86 LibWasm: Avoid memory copy in read_value<T>() if possible
If the address is already aligned properly, just read a T from it;
otherwise copy it to a local aligned array. This was a bottleneck on
memory-heavy benchmarks.
2025-08-08 12:54:06 +02:00
Ali Mohammad Pur
83995ada1f LibWasm: Quit early in memory_fill if store_to_memory traps
We shouldn't try to run the next 'instruction' if we trap.
Unbreaks the memory-fill test in wpt.
2025-05-22 07:35:58 +01:00
Ali Mohammad Pur
bfc1ebb2d4 LibWasm: Disable spammy 'memory access oob' debug prints 2025-05-22 07:35:58 +01:00
Ali Mohammad Pur
d79d5b70a5 LibWasm: Validate indirect calls at runtime
This is required by the spec, and also unbreaks all of the call-indirect
wpt tests.
2025-05-22 07:35:58 +01:00
Ali Mohammad Pur
39b637a446 LibWasm+LibWeb: Throw a js stack-overflow error if wasm stack overflows
Follows the spec.
2025-05-22 07:35:58 +01:00
Tim Ledbetter
58ffc56c38 LibWas: Unbreak compilation with WASM_TRACE_DEBUG enabled 2025-05-19 10:20:40 +01:00
Timothy Flynn
7280ed6312 Meta: Enforce newlines around namespaces
This has come up several times during code review, so let's just enforce
it using a new clang-format 20 option.
2025-05-14 02:01:59 -06:00
Ali Mohammad Pur
51bab5b186 LibWasm: Make traps hold on to externally-managed data
...instead of specially handling JS::Completion.
This makes it possible for LibWeb/LibJS to have full control over how
these things are made, stored, and visited (whenever).

Fixes an issue where we couldn't roundtrip a JS exception through Wasm.
2025-04-22 08:43:46 -06:00
Timothy Flynn
93712b24bf Everywhere: Hoist the Libraries folder to the top-level 2024-11-10 12:50:45 +01:00
Renamed from Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.cpp (Browse further)