WASM WASI the power of ONE All photographs












































- Slides: 44
WASM + WASI�� …the power of ONE All photographs in this deck © 2020 Jeremy Likness. All Rights Reserved. @Jeremy. Likness
LET’S TAKE A JOURNEY …
DEMO C#, IL, and native
In the beginning … const compute. Prime = () => { var last. Prime = -1; for (let i = 2; i < 50000; i+=1) { let is. Prime = true; for (let j = i-1; j >= 2; j-=1) { if (i % j == 0) { is. Prime = false; break; } if (is. Prime) { last. Prime = i; } } return last. Prime; }
Along came asm. js …
Along came asm. js … C/C++
Along came asm. js … C/C++ Clang
Along came asm. js … C/C+ + Clang LLVM
Along came asm. js … C/C++ Clang LLVM Emscripte n
Along came asm. js … C/C++ Clang LLVM Emscripte n Asm. js
primes. c int compute. Primes. Asm() { int last. Prime; int i; int j; int is. Prime; for (i = 2; i < 50000; i = i+1) { is. Prime = 1; for (j = i-1; j >= 2; j--) { if (i % j == 0) { is. Prime = -1; break; } } if (is. Prime == 1) { last. Prime = i; } } return last. Prime; }
BUILD ASM. JS Demo https: //github. com/Jeremy. Likness/blazor-wasm/tree/master/primes
primes. js function _compute. Primes. Asm() { var $0 = 0, $10 = 0, $11 = 0, $12 = 0, $13 = 0, $14 = 0, $15 = 0, $16 = 0, $17 = 0, $18 = 0, $19 = 0, $20 = 0, $21 = 0, $3 = 0, $4 = 0, $5 = 0, $6 = 0, $7 = 0; var $8 = 0, $9 = 0, label = 0, sp = 0; sp = STACKTOP; STACKTOP = STACKTOP + 16|0; if ((STACKTOP|0) >= (STACK_MAX|0)) abort. Stack. Overflow(16|0); $1 = 2; while(1) { $4 = $1; $5 = ($4|0)<(50000); if (!($5)) { break; } $3 = 1; $6 = $1; $7 = (($6) - 1)|0; $2 = $7; while(1) { $8 = $2; $9 = ($8|0)>=(2); if (!($9)) { break; } $10 = $1; $11 = $2; $12 = (($10|0) % ($11|0))&-1; $13 = ($12|0)==(0); if ($13) { label = 6; break; } $14 = $2; $15 = (($14) + -1)|0; $2 = $15; } if ((label|0) == 6) { label = 0; $3 = -1; } $16 = $3; $17 = ($16|0)==(1); if ($17) { $18 = $1; $0 = $18; } $19 = $1; $20 = (($19) + 1)|0; $1 = $20; } $21 = $0; STACKTOP = sp; return ($21|0); }
Java. Script vs. asm. js in Chrome Time to Compute (ms) 4000 3500 3000 2500 2000 1500 1000 500 0 Primes Java. Script asm. js
Then, in 2017 …
Binary A Quick Look Back… 1010 1101 0010 0000 1101 0000
1010 1101 0010 0000 1101 0000 $AD $20 $D 0 Hexadecim al Binary A Quick Look Back…
A Quick Look Back… LDA $D 020 Assembly $AD $20 $D 0 Hexadecimal Binary 1010 1101 0010 0000 1101 0000
A QUICK LOOK BACK…
A QUICK LOOK BACK…
primes. wasm. c int compute. Primes. Wasm() { int last. Prime; int i; int j; int is. Prime; for (i = 2; i < 50000; i = i+1) { is. Prime = 1; for (j = i-1; j >= 2; j--) { if (i % j == 0) { is. Prime = -1; break; } } if (is. Prime == 1) { last. Prime = i; } } return last. Prime; }
Wasm is Binary, this is a textual visualization (module (type $t 0 (func)) (type $t 1 (func (result i 32))) (func $__wasm_call_ctors (type $t 0)) (func $compute. Primes. Wasm (export "compute. Primes. Wasm") (type $t 1) (result i 32) (local $l 0 i 32) (local $l 1 i 32) (local $l 2 i 32) (local $l 3 i 32) (local $l 4 i 32) i 32. const 1 set_local $l 0 i 32. const 2 set_local $l 1 loop $L 0 get_local $l 0 set_local $l 2 block $B 1 block $B 2 loop $L 3 get_local $l 2 i 32. const 2 i 32. lt_s br_if $B 2 get_local $l 1 get_local $l 2 i 32. rem_s set_local $l 3 get_local $l 2 i 32. const -1 i 32. add set_local $l 2 get_local $l 3 br_if $L 3 end get_local $l 0 i 32. const 1 i 32. add set_local $l 0 get_local $l 1 i 32. const 1 i 32. add tee_local $l 1 i 32. const 50000 i 32. ne br_if $L 0 br $B 1 end get_local $l 1 set_local $l 4 get_local $l 0 i 32. const 1 i 32. add set_local $l 0 get_local $l 1 i 32. const 1 i 32. add tee_local $l 1 i 32. const 50000 i 32. ne br_if $L 0 end get_local $l 4) (table $T 0 1 1 anyfunc) (memory $memory (export "memory") 2) (global $g 0 (mut i 32) (i 32. const 66560)) (global $__heap_base (export "__heap_base") i 32 (i 32. const 66560)) (global $__data_end (export "__data_end") i 32 (i 32. const 1024)))
A QUICK LOOK AT THE BASICS Introducing Web. Assembly Studio: https: //webassembly. studio/
Stack-Based Programming
Stack-Based Programming get_local $lhs 2
Stack-Based Programming 4 2 get_local $rhs
Stack-Based Programming 4 6 2 i 32. add
Wasm vs. asm. js Byte code (no precompilation/script parsing) Compact delivery (smaller file size) Wasm-specific instruction set (not constrained by Java. Script)
THE CATCH
WEBASSEMBLY IS A BLACK BOX It can only communicate via “imports” and “exports”
CHAOS TREES Demo
THEN ALONG CAME A SPIDER…
Expanding Wasm …. NET Core. NET Standard. NET Runtime
Expanding Wasm … Web. Assembly Conceptual Machine Browser Implementation
Expanding Wasm … WASI Conceptual Operating System Wasmer, Wasmtime, Browser
BUILD A WASI MODULE Demo
(module (type $t 0 (func (param i 32) (result i 32))) (type $t 1 (func)) (import "wasi_unstable" "fd_write" (func $fd_write (type $t 0))) (func $main (type $t 1) i 32. const 0 i 32. const 8 i 32. store i 32. const 4 i 32. const 22 i 32. store i 32. const 1 i 32. const 0 i 32. const 1 i 32. const 30 call $fd_write drop) (memory $memory 1) (export "memory" (memory 0)) (export "_start" (func $main)) (data (i 32. const 8) "My first WASI module! a"))
RUN WASI IN THE BROWSER Demo https: //webassembly. sh, wasm and qr 2 text
RUN WASI ON LINUX Demo
Lend you a hand? How about an ARM? https: //medium. com/wasmer/running-webassembly-on-arm-7 d 365 ed 0 e 50 c
Why Web. Assembly? Byte code (no precompilation/script parsing) Compact delivery (smaller file size) Wasm-specific instruction set (not constrained by Java. Script)
Why WASI? Near-native speeds Operating system support Truly portable byte code Run everywhere
https: //blog. jeremylikness. com/ @Jeremy. Likness@microsoft. com https: //blog. jeremylikness. com/tags/webassembly https: //blog. jeremylikness. com/blog/explore-wasi-from-your-browser/ https: //webassembly. studio https: //webassembly. sh