Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 1 | //===-- README.txt - Notes for WebAssembly code gen -----------------------===// |
| 2 | |
Dan Gohman | 68a423b | 2016-10-26 17:44:09 +0000 | [diff] [blame] | 3 | This WebAssembly backend is presently under development. |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 4 | |
Dan Gohman | 68a423b | 2016-10-26 17:44:09 +0000 | [diff] [blame] | 5 | Currently the easiest way to use it is through Emscripten, which provides a |
| 6 | compilation environment that includes standard libraries, tools, and packaging |
| 7 | for producing WebAssembly applications that can run in browsers and other |
| 8 | environments. For more information, see the Emscripten documentation in |
| 9 | general, and this page in particular: |
| 10 | * https://github.com/kripken/emscripten/wiki/New-WebAssembly-Backend |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 11 | |
Dan Gohman | 68a423b | 2016-10-26 17:44:09 +0000 | [diff] [blame] | 12 | Other ways of using this backend, such as via a standalone "clang", are also |
| 13 | under development, though they are not generally usable yet. |
| 14 | |
| 15 | For more information on WebAssembly itself, see the home page: |
| 16 | * https://webassembly.github.io/ |
| 17 | |
| 18 | The following documents contain some information on the semantics and binary |
| 19 | encoding of WebAssembly itself: |
Dan Gohman | 5d3391f | 2016-10-24 20:35:17 +0000 | [diff] [blame] | 20 | * https://github.com/WebAssembly/design/blob/master/Semantics.md |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 21 | * https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md |
| 22 | |
JF Bastien | f05f6fd | 2015-12-05 19:36:33 +0000 | [diff] [blame] | 23 | The backend is built, tested and archived on the following waterfall: |
JF Bastien | 4383a34 | 2016-01-22 04:21:49 +0000 | [diff] [blame] | 24 | https://wasm-stat.us |
JF Bastien | f05f6fd | 2015-12-05 19:36:33 +0000 | [diff] [blame] | 25 | |
Dan Gohman | 68a423b | 2016-10-26 17:44:09 +0000 | [diff] [blame] | 26 | The backend's bringup is done in part by using the GCC torture test suite, since |
| 27 | it doesn't require C library support. Current known failures are in |
JF Bastien | f05f6fd | 2015-12-05 19:36:33 +0000 | [diff] [blame] | 28 | known_gcc_test_failures.txt, all other tests should pass. The waterfall will |
| 29 | turn red if not. Once most of these pass, further testing will use LLVM's own |
JF Bastien | c2b3048 | 2015-12-09 13:29:32 +0000 | [diff] [blame] | 30 | test suite. The tests can be run locally using: |
JF Bastien | 4383a34 | 2016-01-22 04:21:49 +0000 | [diff] [blame] | 31 | https://github.com/WebAssembly/waterfall/blob/master/src/compile_torture_tests.py |
JF Bastien | f05f6fd | 2015-12-05 19:36:33 +0000 | [diff] [blame] | 32 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 33 | //===---------------------------------------------------------------------===// |
Dan Gohman | dfa81d8 | 2015-11-20 03:08:27 +0000 | [diff] [blame] | 34 | |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 35 | Br, br_if, and br_table instructions can support having a value on the value |
| 36 | stack across the jump (sometimes). We should (a) model this, and (b) extend |
| 37 | the stackifier to utilize it. |
Dan Gohman | dfa81d8 | 2015-11-20 03:08:27 +0000 | [diff] [blame] | 38 | |
| 39 | //===---------------------------------------------------------------------===// |
Dan Gohman | 753abf8 | 2015-12-06 19:29:54 +0000 | [diff] [blame] | 40 | |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 41 | The min/max instructions aren't exactly a<b?a:b because of NaN and negative zero |
Dan Gohman | 753abf8 | 2015-12-06 19:29:54 +0000 | [diff] [blame] | 42 | behavior. The ARM target has the same kind of min/max instructions and has |
| 43 | implemented optimizations for them; we should do similar optimizations for |
| 44 | WebAssembly. |
| 45 | |
| 46 | //===---------------------------------------------------------------------===// |
| 47 | |
| 48 | AArch64 runs SeparateConstOffsetFromGEPPass, followed by EarlyCSE and LICM. |
| 49 | Would these be useful to run for WebAssembly too? Also, it has an option to |
| 50 | run SimplifyCFG after running the AtomicExpand pass. Would this be useful for |
| 51 | us too? |
| 52 | |
| 53 | //===---------------------------------------------------------------------===// |
| 54 | |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 55 | Register stackification uses the VALUE_STACK physical register to impose |
Dan Gohman | 753abf8 | 2015-12-06 19:29:54 +0000 | [diff] [blame] | 56 | ordering dependencies on instructions with stack operands. This is pessimistic; |
| 57 | we should consider alternate ways to model stack dependencies. |
| 58 | |
| 59 | //===---------------------------------------------------------------------===// |
| 60 | |
| 61 | Lots of things could be done in WebAssemblyTargetTransformInfo.cpp. Similarly, |
| 62 | there are numerous optimization-related hooks that can be overridden in |
| 63 | WebAssemblyTargetLowering. |
| 64 | |
| 65 | //===---------------------------------------------------------------------===// |
| 66 | |
| 67 | Instead of the OptimizeReturned pass, which should consider preserving the |
| 68 | "returned" attribute through to MachineInstrs and extending the StoreResults |
| 69 | pass to do this optimization on calls too. That would also let the |
| 70 | WebAssemblyPeephole pass clean up dead defs for such calls, as it does for |
| 71 | stores. |
| 72 | |
| 73 | //===---------------------------------------------------------------------===// |
| 74 | |
Dan Gohman | 7f86ca1 | 2016-01-16 00:20:03 +0000 | [diff] [blame] | 75 | Consider implementing optimizeSelect, optimizeCompareInstr, optimizeCondBranch, |
| 76 | optimizeLoadInstr, and/or getMachineCombinerPatterns. |
| 77 | |
| 78 | //===---------------------------------------------------------------------===// |
| 79 | |
| 80 | Find a clean way to fix the problem which leads to the Shrink Wrapping pass |
| 81 | being run after the WebAssembly PEI pass. |
| 82 | |
| 83 | //===---------------------------------------------------------------------===// |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 84 | |
| 85 | When setting multiple local variables to the same constant, we currently get |
| 86 | code like this: |
| 87 | |
| 88 | i32.const $4=, 0 |
| 89 | i32.const $3=, 0 |
| 90 | |
| 91 | It could be done with a smaller encoding like this: |
| 92 | |
| 93 | i32.const $push5=, 0 |
| 94 | tee_local $push6=, $4=, $pop5 |
| 95 | copy_local $3=, $pop6 |
| 96 | |
| 97 | //===---------------------------------------------------------------------===// |
Dan Gohman | 4918702 | 2016-02-08 03:42:36 +0000 | [diff] [blame] | 98 | |
| 99 | WebAssembly registers are implicitly initialized to zero. Explicit zeroing is |
| 100 | therefore often redundant and could be optimized away. |
| 101 | |
| 102 | //===---------------------------------------------------------------------===// |
Dan Gohman | 87e368b | 2016-02-19 19:22:44 +0000 | [diff] [blame] | 103 | |
| 104 | Small indices may use smaller encodings than large indices. |
Dan Gohman | 3a71ccb | 2016-02-20 23:11:14 +0000 | [diff] [blame] | 105 | WebAssemblyRegColoring and/or WebAssemblyRegRenumbering should sort registers |
| 106 | according to their usage frequency to maximize the usage of smaller encodings. |
Dan Gohman | 87e368b | 2016-02-19 19:22:44 +0000 | [diff] [blame] | 107 | |
| 108 | //===---------------------------------------------------------------------===// |
Dan Gohman | e6b8136 | 2016-03-04 20:09:57 +0000 | [diff] [blame] | 109 | |
Dan Gohman | ddfa1a6 | 2016-03-09 04:17:36 +0000 | [diff] [blame] | 110 | Many cases of irreducible control flow could be transformed more optimally |
| 111 | than via the transform in WebAssemblyFixIrreducibleControlFlow.cpp. |
| 112 | |
| 113 | It may also be worthwhile to do transforms before register coloring, |
| 114 | particularly when duplicating code, to allow register coloring to be aware of |
| 115 | the duplication. |
| 116 | |
| 117 | //===---------------------------------------------------------------------===// |
Dan Gohman | 6627e5f | 2016-05-16 18:51:03 +0000 | [diff] [blame] | 118 | |
| 119 | WebAssemblyRegStackify could use AliasAnalysis to reorder loads and stores more |
| 120 | aggressively. |
| 121 | |
| 122 | //===---------------------------------------------------------------------===// |
| 123 | |
| 124 | WebAssemblyRegStackify is currently a greedy algorithm. This means that, for |
| 125 | example, a binary operator will stackify with its user before its operands. |
| 126 | However, if moving the binary operator to its user moves it to a place where |
| 127 | its operands can't be moved to, it would be better to leave it in place, or |
| 128 | perhaps move it up, so that it can stackify its operands. A binary operator |
| 129 | has two operands and one result, so in such cases there could be a net win by |
| 130 | prefering the operands. |
| 131 | |
| 132 | //===---------------------------------------------------------------------===// |
Dan Gohman | e045f67 | 2016-05-18 20:19:02 +0000 | [diff] [blame] | 133 | |
| 134 | Instruction ordering has a significant influence on register stackification and |
| 135 | coloring. Consider experimenting with the MachineScheduler (enable via |
| 136 | enableMachineScheduler) and determine if it can be configured to schedule |
| 137 | instructions advantageously for this purpose. |
| 138 | |
| 139 | //===---------------------------------------------------------------------===// |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 140 | |
| 141 | WebAssembly is now officially a stack machine, rather than an AST, and this |
| 142 | comes with additional opportunities for WebAssemblyRegStackify. Specifically, |
| 143 | the stack doesn't need to be empty after an instruction with no return values. |
| 144 | WebAssemblyRegStackify could be extended, or possibly rewritten, to take |
| 145 | advantage of the new opportunities. |
| 146 | |
| 147 | //===---------------------------------------------------------------------===// |