Upgrade to Pro — share decks privately, control downloads, hide ads and more …

20161005-WASM

chikoski
October 05, 2016

 20161005-WASM

chikoski

October 05, 2016
Tweet

More Decks by chikoski

Other Decks in Technology

Transcript

  1. WebAssembly :
 a new virtual machine for the Web Mozilla

    Japan N. Shimizu ([email protected]) Emscripten Night!! @ DRECOM Cafe 2016/10/05
  2. Bytes asm.js String AST Parsing Validation Decode Bytes WASM MIR

    Execut able Validation Emission Compile asm.js is translated as WASM in its compilation pipeline (in SpiderMonkey)
  3. N. Shimizu / @chikoski • Mozilla Japan • Developer relation

    • Localizer: MDN, SUMO, Firefox for iOS • html5j Web platform / html5j Game • Programming language enthusiast • https://speakerdeck.com/chikoski/ • https://html5experts.jp/chikoski/
  4. Proudly non-profit, Mozilla makes products like Firefox with a mission

    to keep the power of the Web in the hands of users everywhere. Mozilla Mission (https://www.mozilla.org/en-US/mission/)
  5. Our mission is to promote openness, innovation & opportunity on

    the Web. Mozilla Mission(https://www.mozilla.org/en-US/mission/)
  6. String Executable AST Tokens IR Lexical analysis Parsing Semantic Analysis

    Optimization code generation Source code compilation (in general)
  7. asm.js • An extraordinarily optimizable, "low-level" subset of JavaScript •

    Numerical operations • Heap access using Array Buffer as linear memory space • Module system (import / export functions) • Type annotation and its formal validation • Foreign function interface (FFI) to JavaScript
  8. function Peano(stdlib, ffi, heap){ "use asm"; function zero(){ return 0;

    } function succ(a){ a = a | 0; return (a + 1) | 0; } return {zero: zero, succ: succ}; } asm.js module
  9. function Peano(stdlib, ffi, heap){ "use asm"; function zero(){ return 0;

    } function succ(a){ a = a | 0; return (a + 1) | 0; } return {zero: zero, succ: succ}; } Type annotations in asm.js
  10. function someModule(stdlib, ffi, heap){ var memo = new stdlib.Uint32Array(heap); //

    snip function f(n){ if(memo[n >> 2] | 0 != 0){ return memo[n >> 2] | 0; } memo[n >> 2] = doSometing(n) | 0; } Heap access in asm.js
  11. function Peano(){ use "asm"; // snip return {zero: zero, succ:

    succ}; } const module = Peano(); const zero = module.zero(); const one = module.succ(zero); asm.js module in JS
  12. % emsdk activate latest % source ${where emsdk installed}/emsdk_env.sh %

    emcc -o helloworld.js helloworld.c % node helloworld.js Hello world! % emcc -o index.html helloworld.c HTML / JS file generation from C / C++ code
  13. // call a C function defined as: int add(int a,

    int b); const add = Module.cwrap("add", "number", ["number, "number"]); const two = add(1, 1); const three = Module.ccall("add", "number", ["number", "number"], [1, 2]); Emscripten translated function call in JS
  14. Emscripten bridges the gap between Web and C/C++ • Standard

    C / C++ libraries • POSIX file system API • MemoryFS / IndexedDB FS / Special file JS emulation • Statically linked libraries • Inline JavaScript • asm.js function export / Object mapping with WebIDL
  15. WebAssembly • New web standard discussed in W3C community group

    • Size and load efficient and portable binary format • Safe and Near-native speed execution • Designed to execute within web browsers as well as non- browser embedding • Text representation (we can "view source" of WASMs)
  16. App no compression with gzip compression asm.js WASM asm.js WASM

    Hello World 301KiB 204KiB 71KiB 26KiB AngryBots 19MiB 6 MiB 4.1 MiB 3.0 MiB StandardAssets 25.7MiB 13.4 MiB 5.5 MiB 4.3 MiB UnityChan-CRS 21.3MiB 11.4 MiB 4.7 MiB 3.7 MiB Size efficiency: asm.js vs WASM
  17. WASM's most viable products (MVP) • Module : distributable, loadable

    and executable unit of code • Behaviors specified in terms of AST (abstract syntax tree) • Linear memory model • Binary format and its equivalent text format • Designed to be implemented both by web browsers and completely difference execution environments
  18. i32.mul i32.add set_local $a 1 i32.const 1 i32.const 2 i32.const

    (set_local $a (i32.add (i32.const 1) (i32.mul (i32.const 1) (i32.const 2)))) WAST: Text representation of WASM (in development)
  19. Type name Acceptable values i32 32bit integer i64 64bit integer

    f32 32bit floating point f64 64bit floating point Types in WASM
  20. ptr = malloc(100) 0 address_space_max (< 4GB) 100 byte i32.load

    i32.store WASM memory model: linear memory
  21. Operator Semantics i32.load8_s load 1 byte and extend it to

    signed i32 i32.load8_u load 1 byte and extend it to unsigned i32 i32.load16_s load 2 bytes and extend it to signed i32 i32.load16_u load 2 bytes and extend it to unsigned i32 i32.load load 4bytes i32 load operators
  22. Operator Semantics i32.store8 Store i32 value as i8 i32.store16 Store

    i32 value as i16 i32.store Store i32 value i32 store operators
  23. 0QFSBUPS 4FNBOUJDT grow_memory Grow memory size by given pages current_memory

    Return current memory size Memory page operators (1page = 64 KiB)
  24. Binary encoding • 8bit bytes / byte memory granularity /

    Little endian • 3 layers of compression: binary encoding / structural compression / compression with generic algorithm (e.g. gzip) • Preamble & sections • Preamble: magic & version number • Section: Import / export declaration, function signature list, set of function bodies, etc
  25. fetch("add.wasm").then(response => response.arrayBuffer(); ).then(buffer => { const codeByte = new

    UInt8Array(buffer); const module = Wasm.instantiateModlue(codeByte); alert(`1 + 2 = ${module.exports.add(1, 2)}`); }); WASM module instantiation
  26. Road map Soon after MVP • Threads • Shared memory

    • Dynamic Linking • zero-cost exceptions • fixed-width SIMDSoon after MVP Future versions • Finer-grained memory control • Large page support • More expressive control flow • GC / DOM integration • Linear memory > 4G • etc