forked from kripken/talks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emwasm.html
406 lines (364 loc) · 16.7 KB
/
emwasm.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>WebAssembly Toolchain Stories</title>
<meta name="description" content="Big Web App? Compile It!">
<meta name="author" content="Alon Zakai">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/default.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write( '<link rel="stylesheet" href="css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
</script>
<style type="text/css">
table {
background-color: #fff !important;
color: #000 !important;
padding: 0.5em !important;
border: 2px solid black !important;
box-shadow: 0.2em 0.2em 0.2em rgba(0,0,0,0.9) !important;
margin-top: 0.5em !important;
margin-bottom: 0.5em !important;
}
td {
vertical-align: middle !important;
text-align: center !important;
}
strong {
color: #fc5;
}
</style>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1><br>WebAssembly<br>Toolchain Stories</h1>
<br>
<h3><div style="color: #f85">Alon Zakai</div></h3>
<h3><div style="color: #fc5">December 2016</div></h3>
</section>
<section>
<p>Let's see how we got here</p>
</section>
<section>
<h2>2010</h2>
<p><img src="i-have-no-idea-what-im-doing-dog.jpg" style="border: 1px solid black; box-shadow: 0.2em 0.2em 0.2em rgba(0,0,0,0.9);"></img></p>
<p>"Can <strong>C++</strong> be compiled to <strong>JavaScript</strong> so it runs on the web?"</p>
<p>→ the <a href="http://emscripten.org/">Emscripten</a> project</p>
</section>
<section>
<p>Earliest prototypes were <a href="http://mozakai.blogspot.com/2010/07/experiments-with-static-javascript-as.html">very hackish Python</a></p>
<br>
<div class="fragment">
<p>A more serious approach: <strong>Emscripten's JS compiler</strong></p>
<br>
<center><table><tr>
<td style="color: #c81">LLVM IR text</td>
<td>→</td>
<td>JS compiler</td>
<td>→</td>
<td style="color: #c81">JS</td>
</tr></table></center>
<br>
<p>Had an internal IR, did some optimization (<a href="http://mozakai.blogspot.com/2012/05/reloop-all-blocks.html">relooper</a>, etc.)</p>
</div>
</section>
<section>
<h2>2011 - an important year</h2>
<br>
<p><table class="fragment"><tr><td><img src="Daenerys3.jpg"></img></td><td><b>Game of Thrones</b> premiers</td></tr></table></p>
<br>
<p class="fragment">Also, Emscripten adds <strong>JS optimizer</strong></p>
</section>
<section>
<p><strong>JS Optimizer</strong> optimizes compiler's JS output</p>
<br>
<center><table><tr>
<td style="color: #c81">LLVM IR</td>
<td>→</td>
<td>JS compiler</td>
<td>→</td>
<td style="background-color: #f73"><b style="padding: 0.1em">JS optimizer</b></td>
<td>→</td>
<td style="color: #c81">JS</td>
</tr></table></center>
<p><small>(what changed is in <b style="background-color: #f73; color: #000; padding: 0.1em">bold</b>)</small></p>
<br>
<p>Peephole optimizations, later more (registerize in 2012, etc.)</p>
</section>
<section>
<h2>2013</h2>
<br>
<p><a href="https://en.wikipedia.org/wiki/Asm.js">asm.js</a> is now a thing, less need for radical experimentation</p>
<br>
<p>→ <a href="https://github.com/kripken/emscripten-fastcomp">fastcomp</a>: LLVM asm.js backend, replaces the JS compiler</p>
<br>
<center><table><tr>
<td style="color: #c81">LLVM IR</td>
<td>→</td>
<td style="background-color: #f73"><b style="padding: 0.1em">asm.js backend</b></td>
<td>→</td>
<td>JS optimizer</td>
<td>→</td>
<td style="color: #c81">JS</td>
</tr></table></center>
</section>
<section>
<h2>2014</h2>
<br>
<p>Rewrote the JS optimizer in C++</p>
<br>
<center><table><tr>
<td style="color: #c81">LLVM IR</td>
<td>→</td>
<td>asm.js backend</td>
<td>→</td>
<td>JS optimizer <b style="background-color: #f73; padding: 0.1em">(C++)<b></td>
<td>→</td>
<td style="color: #c81">JS</td>
</tr></table></center>
<br>
<p class="fragment">And that is what our asm.js toolchain looks like today</p>
</section>
<section>
<h2>2015</h2>
<p><a href="http://webassembly.org/">WebAssembly (wasm)</a> is coming, we need ways to compile to it</p>
<br>
<p>Two parallel efforts are begun:</p>
<hr>
<p class="fragment"><strong>1.</strong> <a href="http://lists.llvm.org/pipermail/llvm-dev/2015-June/086850.html">LLVM WebAssembly backend</a>, from scratch, not limited by asm.js (which lacks proper i64s, etc.)</p>
<p class="fragment"><strong>2.</strong> <a href="https://github.com/WebAssembly/binaryen/blob/master/src/asm2wasm.h">asm2wasm</a> which translates asm.js into WebAssembly, an <a href="http://kripken.github.io/talks/wasm.html">easy way</a> to get something working</p>
<hr>
</section>
<section>
<p>Indeed, we quickly got asm2wasm working:</p>
<br>
<center><table><tr>
<td style="color: #c81">LLVM IR</td>
<td>→</td>
<td>asm.js backend</td>
<td>→</td>
<td>JS optimizer</td>
<td>→</td>
<td style="background-color: #f73"><b style="padding: 0.1em">asm2wasm</b></td>
<td>→</td>
<td style="color: #c81">wasm</td>
</tr></table></center>
</section>
<section>
<p>asm2wasm is part of the <a href="https://github.com/WebAssembly/binaryen/">Binaryen</a> project, which has a <a href="http://kripken.github.io/talks/binaryen.html">WebAssembly optimizer</a></p>
<br>
<p>The optimizer can run standalone, but also asm2wasm can run it in parallel, leading to</p>
<br>
<center><table><tr>
<td style="color: #c81">LLVM IR</td>
<td>→</td>
<td>asm.js backend</td>
<td>→</td>
<td>JS optimizer</td>
<td>→</td>
<td>asm2wasm <b style="background-color: #f73; padding: 0.1em">& wasm optimizer</b></td>
<td>→</td>
<td style="color: #c81">wasm</td>
</tr></table></center>
</section>
<section>
<h2>2016 - another important year</h2>
<p><center><table class="fragment"><tr><td style="color: #f21; background-color: black; padding: 1.5em 2em 1.5em 2em"><b>SPOILER<br>IMAGE</b></td><td style="padding: 0.5em"></td><td><b>Game of Thrones</b> goes<br>well beyond the books</td></tr></table></center></p>
<br>
<div class="fragment">
<p>Also, wasm optimizer good enough we don't need JS one:</p>
<center><table><tr>
<td style="color: #c81">LLVM IR</td>
<td>→</td>
<td>asm.js backend</td>
<td>→</td>
<td style="background-color: #f73; color: #400; text-decoration: line-through">JS optimizer</td>
<td>→</td>
<td>asm2wasm<br>& wasm optimizer</td>
<td>→</td>
<td style="color: #c81">wasm</td>
</tr></table></center>
</div>
</section>
<section>
<p>Which brings us to this:</p>
<br>
<center><table><tr>
<td style="color: #c81">LLVM IR</td>
<td>→</td>
<td>asm.js backend</td>
<td>→</td>
<td>asm2wasm<br>& wasm optimizer</td>
<td>→</td>
<td style="color: #c81">wasm</td>
</tr></table></center>
<br>
<p class="fragment"><strong>Note</strong>: we don't need to emit proper JS or asm.js anymore...</p>
</section>
<section>
<p><strong>wasm-only</strong> mode: Adds special intrinsics to the asm.js backend's output, for <strong>i64s</strong> and other things asm.js couldn't handle well</p>
<br>
<pre><code contenteditable>
function truncate(x) {
x = i64(x); // declare param as i64
return i64_trunc(x) | 0; // truncate to low 32 bits
}
</code></pre>
<br>
<p>→ <strong>asm2wasm no longer limited by asm.js</strong>, just like WebAssembly backend</p>
</section>
<section>
<p><strong>Current state of asm2wasm toolchain:</strong></p>
<br>
<center><table><tr>
<td style="color: #c81">LLVM IR</td>
<td>→</td>
<td><b style="background-color: #f73; padding: 0.1em">"asm.js"</b> backend</td>
<td>→</td>
<td>asm2wasm<br>& wasm optimizer</td>
<td>→</td>
<td style="color: #c81">wasm</td>
</tr></table></center>
<br>
<p>Between the backend and asm2wasm we use that asm.js<strong style="font-style: italic">-ish</strong> language we just saw (which has yet to be named - ideas?)</p>
</section>
<section>
<p>Summary of asm2wasm stories:</p>
<center><table>
<tr><td><b>phase</b></td><td style="padding-left: 1.1em"></td><td style="text-align: center"><b>description</b></td></tr>
<tr><td colspan=4><hr></td></tr>
<tr><td>asm.js</td><td></td><td>
<small><table style="border: 0 !important; box-shadow: none !important"><tr>
<td>asm.js backend</td>
<td>→</td>
<td>JS optimizer</td>
</tr></table></small>
</td></tr>
<tr><td>+ asm2wasm</td><td></td><td>
<small><table style="border: 0 !important; box-shadow: none !important"><tr>
<td>asm.js backend</td>
<td>→</td>
<td>JS optimizer</td>
<td>→</td>
<td style="background-color: #f73"><b style="padding: 0.1em">asm2wasm</b></td>
</tr></table></small>
</td></tr>
<tr><td>+ wasm opts</td><td></td><td>
<small><table style="border: 0 !important; box-shadow: none !important"><tr>
<td>asm.js backend</td>
<td>→</td>
<td>JS optimizer</td>
<td>→</td>
<td>asm2wasm <b style="padding: 0.1em; background-color: #f73">& wasm opts</b></td>
</tr></table></small>
</td></tr>
<tr><td>– js opts</td><td></td><td>
<small><table style="border: 0 !important; box-shadow: none !important"><tr>
<td>asm.js backend</td>
<td>→</td>
<td>asm2wasm & wasm opts</td>
</tr></table></small>
</td></tr>
<tr><td>wasm-only</td><td></td><td>
<small><table style="border: 0 !important; box-shadow: none !important"><tr>
<td><b style="padding: 0.1em; background-color: #f73">"asm.js"</b> backend</td>
<td>→</td>
<td>asm2wasm & wasm opts</td>
</tr></table></small>
</td></tr>
</table></center>
<p class="fragment">Now lets see some <strong>data</strong>!</p>
</section>
<section>
<p><strong>Box2D</strong> code size measurements</h2>
<center><table>
<tr><td><b>phase</b></td><td style="text-align: center"><b>gzipped size (K)</b></td><td></td></tr>
<tr><td colspan=4><hr></td></tr>
<tr><td>asm.js</td><td>55.3</td><td></td></tr>
<tr><td>+ asm2wasm</td><td>50.0</td><td>(a)</td></tr>
<tr><td>+ wasm opts</td><td>45.2</td><td></td></tr>
<tr><td>– js opts</td><td>44.1</td><td></td></tr>
<tr><td>wasm-only</td><td>43.1</td><td>(b)</td></tr>
</table></center>
<p><small>(lower is better)</small></p>
<p><b>(a)</b> going to wasm is a <strong>10%</strong> improvement</p>
<p><b>(b)</b> the rest add a further <strong>14%</strong></p>
</section>
<section>
<p><strong>Box2D</strong> speed measurements</h2>
<center><table>
<tr><td><b>phase</b></td><td style="text-align: center"><b>time (seconds)</b></td><td></td></tr>
<tr><td colspan=4><hr></td></tr>
<tr><td>asm.js</td><td>5.97</td><td></td></tr>
<tr><td>+ asm2wasm</td><td>5.66</td><td>(a)</td></tr>
<tr><td>+ wasm opts</td><td>5.28</td><td></td></tr>
<tr><td>– js opts</td><td>5.24</td><td>(b)</td></tr>
<tr><td>wasm-only</td><td>5.24</td><td></td></tr>
</table></center>
<p><small>(lower is better)</small></p>
<p><b>(a)</b> going to wasm is a <strong>5%</strong> improvement</p>
<p><b>(b)</b> the rest add a further <strong>7%</strong></p>
<br>
<p class="fragment">Hmm, wasm-only doesn't help much here...</p>
</section>
<section>
<p>An <strong>i64</strong>-specific benchmark, <strong>corrections-i64</strong>:</p>
<center><table>
<tr><td><b>phase</b></td><td style="text-align: center"><b>time (seconds)</b></td><td></td></tr>
<tr><td colspan=4><hr></td></tr>
<tr><td>asm.js</td><td>28.46</td><td></td></tr>
<tr><td>+ asm2wasm</td><td>13.21</td><td>(a)</td></tr>
<tr><td>+ wasm opts</td><td>12.99</td><td></td></tr>
<tr><td>– js opts</td><td>12.88</td><td></td></tr>
<tr><td>wasm-only</td><td>6.88</td><td>(b)</td></tr>
</table></center>
<p><small>(lower is better)</small></p>
<p><b>(a)</b> asm2wasm uses i64s in runtime, ~<strong>2X</strong> faster</p>
<p><b>(b)</b> <strong>wasm-only</strong> uses i64s throughout, for a further ~<strong>2X</strong></p>
</section>
<section>
<h2>Conclusion</h2>
<hr>
<p><strong>asm2wasm</strong> started as a hack, but grew into a pretty effective approach</p>
<br>
<p class="fragment">Good we have <strong>both</strong> the asm2wasm and WebAssembly backend options</p>
<br>
<p class="fragment">Still <strong>learning and exploring</strong> in this space</p>
<hr>
<p style="color: #fff"><center><table class="fragment"><tr><td style="padding-left: 1em; padding-right: 1em"><b>Thank you for listening.</b></td></tr></table><center></p>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
theme: 'default', //Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'fade', // default/cube/page/concave/zoom/linear/fade/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
// { src: 'plugin/remotes/remotes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>