News for hackers. Stuff that matters.
Re:Wasm3 – A high performance WebAssembly interpreter in C by ridiculous_fish on Monday January 13, 2020 @12:12AM (#22031998)
This is pretty exciting if real:

> Bytecode/opcodes are translated into more efficient "operations" during a compilation pass, generating pages of meta-machine code

WASM compiled to a novel bytecode format aimed at efficient interpretation.

> Commonly occurring sequences of operations can can also be optimized into a "fused" operation.

Peephole optimizations producing fused opcodes, makes sense.

> In M3/Wasm, the stack machine model is translated into a more direct and efficient "register file" approach

WASM translated to register-based bytecode. That's awesome!

> Since operations all have a standardized signature and arguments are tail-call passed through to the next, the M3 "virtual" machine registers end up mapping directly to real CPU registers.

This is some black magic, if it works!

Re:Wasm3 – A high performance WebAssembly interpreter in C by vnorilo on Monday January 13, 2020 @04:50AM (#22033120)
Sure this is neat stuff, but I don't think any of it is novel. Bochs is a good source for some bytecode vm performance wizardry [1], even if the bytecode in question is the x86 ISA.

Regardless, kudos to the authors and nice to see a fast wasm interpreter done well.

1: http://www.emulators.com/docs/nx25_nostradamus.htm

Re:Wasm3 – A high performance WebAssembly interpreter in C by saagarjha on Monday January 13, 2020 @06:26AM (#22033494)
Yeah, threaded interpretation is nothing new. Notably, Forth compilers have often used it; the iSH x86 emulator (https://github.com/tbodt/ish) is a more recent example of this technique.
Re:Wasm3 – A high performance WebAssembly interpreter in C by vshymanskyy on Monday January 13, 2020 @07:53AM (#22033980)
It's not only the "threaded code" approach, that makes Wasm3 so fast. In fact, Intel's WAMR also utilizes this method, yet is 30 times slower..
Re:Wasm3 – A high performance WebAssembly interpreter in C by kevingadd on Monday January 13, 2020 @04:33AM (#22033061)
IR getting converted into an interpreter-oriented bytecode is pretty common. Mono does it for its interpreter and IIRC, Spidermonkey has historically done that as well. I'm not sure if V8 has ever interpreted from an IR (maybe now?) but you could view their original 'baseline JIT' model as converting into an interpreter-focused IR, where the IR just happened to be extremely unoptimized x86 assembly.

Translating the stack machine into registers was always a core part of the model but it's interesting to me that even interpreters are doing it. The necessity of doing coloring to assign registers efficiently is kind of unfortunate, I feel like the WASM compiler would have been the right place to do this offline.

Re:Wasm3 – A high performance WebAssembly interpreter in C by jashmatthews on Monday January 13, 2020 @05:05AM (#22033188)
> The necessity of doing coloring to assign registers efficiently is kind of unfortunate

Register based VMs like Lua don't do this. The register allocation is incredibly simple https://github.com/LuaJIT/LuaJIT/blob/v2.1/src/lj_parse.c#L3...

Re:Wasm3 – A high performance WebAssembly interpreter in C by tom_mellior on Monday January 13, 2020 @05:47AM (#22033345)
But that's an allocator to virtual registers that don't try to correspond to (a valid number of) physical CPU registers. Sure it's easy to allocate to a large number of registers. It's harder to do it to a small number, like the project discussed here seems to claim to do.
Re:Wasm3 – A high performance WebAssembly interpreter in C by jashmatthews on Monday January 13, 2020 @10:59AM (#22035541)
I could be completely wrong but it looks like what this does is more like stack-caching. One operand, if it's the result of the last instruction, should already be in "virtual r0" which is already in a register. For the other operand of the opcode, it's an infinite set of "registers" at an offset from the stack pointer: https://github.com/wasm3/wasm3/tree/b1462d450ca367e39e4b2eb4...
Re:Wasm3 – A high performance WebAssembly interpreter in C by tom_mellior on Monday January 13, 2020 @06:02PM (#22039723)
You're right. This doesn't use a register machine in the usual sense of the word. It's kind of the software analog to accumulator machines.
Re:Wasm3 – A high performance WebAssembly interpreter in C by saagarjha on Monday January 13, 2020 @06:22AM (#22033481)
> The necessity of doing coloring to assign registers efficiently is kind of unfortunate

You don't have to do register allocation with coloring; it's just that most implementations do.

Re:Wasm3 – A high performance WebAssembly interpreter in C by uasm on Monday January 13, 2020 @05:48AM (#22033347)
> "WASM translated to register-based bytecode. That's awesome!"

If the hardware executing this code is "stack-based" (or, does not offer enough general purpose registers to accomodate the funtion call) - this will need to be converted back to a stack-based function call (either at runtime, or beforehand). Wouldn't this intermediate WASM-to-register-based-bytecode translation be redundant then?

Re:Wasm3 – A high performance WebAssembly interpreter in C by tom_mellior on Monday January 13, 2020 @08:49AM (#22034400)
You seem to be asking something like "if we always hit the algorithm's slow path, isn't the algorithm slow?". The answer is "yes, but we will (hopefully) almost never hit the slow path". On x64-64 you will typically be able to pass 6 integer/pointer values and 8 floating-point values in registers. That should be enough for most function calls in real-world code.
Re:Wasm3 – A high performance WebAssembly interpreter in C by tyingq on Monday January 13, 2020 @06:22AM (#22033480)
I don't know of any current physical stack machine CPUs.
Re:Wasm3 – A high performance WebAssembly interpreter in C by uasm on Monday January 13, 2020 @08:29AM (#22034259)
Stacks are used extensively across the x86 family [0]

[0] - https://en.wikipedia.org/wiki/X86_calling_conventions

Re:Wasm3 – A high performance WebAssembly interpreter in C by tyingq on Monday January 13, 2020 @08:45AM (#22034376)
"Has a stack" isn't the same as "Has a stack based ISA".
Re:Wasm3 – A high performance WebAssembly interpreter in C by colejohnson66 on Monday January 13, 2020 @06:10PM (#22039796)
To expand: a Forth machine or similar would be a stack based ISA. Using a stack is a different matters; Pretty much every ISA uses stacks.
Re:Wasm3 – A high performance WebAssembly interpreter in C by jeltz on Monday January 13, 2020 @09:57AM (#22034941)
No, stack machine CPUs are pretty obscure things, especially today. See the link below for some examples. All or virtually all modern commercial CPUs are register based.

https://en.wikipedia.org/wiki/Stack_machine#Commercial_stack...

Re:Wasm3 – A high performance WebAssembly interpreter in C by AlEinstein on Monday January 13, 2020 @06:24AM (#22033489)
What proportion of machines running web assembly are stack-based, would you say?

I would guess negligible.