README.txt 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //===-- README.txt - Notes for WebAssembly code gen -----------------------===//
  2. The object format emitted by the WebAssembly backed is documented in:
  3. * https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md
  4. The C ABI is described in:
  5. * https://github.com/WebAssembly/tool-conventions/blob/main/BasicCABI.md
  6. For more information on WebAssembly itself, see the home page:
  7. * https://webassembly.github.io/
  8. Emscripten provides a C/C++ compilation environment based on clang which
  9. includes standard libraries, tools, and packaging for producing WebAssembly
  10. applications that can run in browsers and other environments.
  11. wasi-sdk provides a more minimal C/C++ SDK based on clang, llvm and a libc based
  12. on musl, for producing WebAssemmbly applictions that use the WASI ABI.
  13. Rust provides WebAssembly support integrated into Cargo. There are two
  14. main options:
  15. - wasm32-unknown-unknown, which provides a relatively minimal environment
  16. that has an emphasis on being "native"
  17. - wasm32-unknown-emscripten, which uses Emscripten internally and
  18. provides standard C/C++ libraries, filesystem emulation, GL and SDL
  19. bindings
  20. For more information, see:
  21. * https://www.hellorust.com/
  22. The following documents contain some information on the semantics and binary
  23. encoding of WebAssembly itself:
  24. * https://github.com/WebAssembly/design/blob/main/Semantics.md
  25. * https://github.com/WebAssembly/design/blob/main/BinaryEncoding.md
  26. Some notes on ways that the generated code could be improved follow:
  27. //===---------------------------------------------------------------------===//
  28. Br, br_if, and br_table instructions can support having a value on the value
  29. stack across the jump (sometimes). We should (a) model this, and (b) extend
  30. the stackifier to utilize it.
  31. //===---------------------------------------------------------------------===//
  32. The min/max instructions aren't exactly a<b?a:b because of NaN and negative zero
  33. behavior. The ARM target has the same kind of min/max instructions and has
  34. implemented optimizations for them; we should do similar optimizations for
  35. WebAssembly.
  36. //===---------------------------------------------------------------------===//
  37. AArch64 runs SeparateConstOffsetFromGEPPass, followed by EarlyCSE and LICM.
  38. Would these be useful to run for WebAssembly too? Also, it has an option to
  39. run SimplifyCFG after running the AtomicExpand pass. Would this be useful for
  40. us too?
  41. //===---------------------------------------------------------------------===//
  42. Register stackification uses the VALUE_STACK physical register to impose
  43. ordering dependencies on instructions with stack operands. This is pessimistic;
  44. we should consider alternate ways to model stack dependencies.
  45. //===---------------------------------------------------------------------===//
  46. Lots of things could be done in WebAssemblyTargetTransformInfo.cpp. Similarly,
  47. there are numerous optimization-related hooks that can be overridden in
  48. WebAssemblyTargetLowering.
  49. //===---------------------------------------------------------------------===//
  50. Instead of the OptimizeReturned pass, which should consider preserving the
  51. "returned" attribute through to MachineInstrs and extending the
  52. MemIntrinsicResults pass to do this optimization on calls too. That would also
  53. let the WebAssemblyPeephole pass clean up dead defs for such calls, as it does
  54. for stores.
  55. //===---------------------------------------------------------------------===//
  56. Consider implementing optimizeSelect, optimizeCompareInstr, optimizeCondBranch,
  57. optimizeLoadInstr, and/or getMachineCombinerPatterns.
  58. //===---------------------------------------------------------------------===//
  59. Find a clean way to fix the problem which leads to the Shrink Wrapping pass
  60. being run after the WebAssembly PEI pass.
  61. //===---------------------------------------------------------------------===//
  62. When setting multiple local variables to the same constant, we currently get
  63. code like this:
  64. i32.const $4=, 0
  65. i32.const $3=, 0
  66. It could be done with a smaller encoding like this:
  67. i32.const $push5=, 0
  68. local.tee $push6=, $4=, $pop5
  69. local.copy $3=, $pop6
  70. //===---------------------------------------------------------------------===//
  71. WebAssembly registers are implicitly initialized to zero. Explicit zeroing is
  72. therefore often redundant and could be optimized away.
  73. //===---------------------------------------------------------------------===//
  74. Small indices may use smaller encodings than large indices.
  75. WebAssemblyRegColoring and/or WebAssemblyRegRenumbering should sort registers
  76. according to their usage frequency to maximize the usage of smaller encodings.
  77. //===---------------------------------------------------------------------===//
  78. Many cases of irreducible control flow could be transformed more optimally
  79. than via the transform in WebAssemblyFixIrreducibleControlFlow.cpp.
  80. It may also be worthwhile to do transforms before register coloring,
  81. particularly when duplicating code, to allow register coloring to be aware of
  82. the duplication.
  83. //===---------------------------------------------------------------------===//
  84. WebAssemblyRegStackify could use AliasAnalysis to reorder loads and stores more
  85. aggressively.
  86. //===---------------------------------------------------------------------===//
  87. WebAssemblyRegStackify is currently a greedy algorithm. This means that, for
  88. example, a binary operator will stackify with its user before its operands.
  89. However, if moving the binary operator to its user moves it to a place where
  90. its operands can't be moved to, it would be better to leave it in place, or
  91. perhaps move it up, so that it can stackify its operands. A binary operator
  92. has two operands and one result, so in such cases there could be a net win by
  93. preferring the operands.
  94. //===---------------------------------------------------------------------===//
  95. Instruction ordering has a significant influence on register stackification and
  96. coloring. Consider experimenting with the MachineScheduler (enable via
  97. enableMachineScheduler) and determine if it can be configured to schedule
  98. instructions advantageously for this purpose.
  99. //===---------------------------------------------------------------------===//
  100. WebAssemblyRegStackify currently assumes that the stack must be empty after
  101. an instruction with no return values, however wasm doesn't actually require
  102. this. WebAssemblyRegStackify could be extended, or possibly rewritten, to take
  103. full advantage of what WebAssembly permits.
  104. //===---------------------------------------------------------------------===//
  105. Add support for mergeable sections in the Wasm writer, such as for strings and
  106. floating-point constants.
  107. //===---------------------------------------------------------------------===//
  108. The function @dynamic_alloca_redzone in test/CodeGen/WebAssembly/userstack.ll
  109. ends up with a local.tee in its prolog which has an unused result, requiring
  110. an extra drop:
  111. global.get $push8=, 0
  112. local.tee $push9=, 1, $pop8
  113. drop $pop9
  114. [...]
  115. The prologue code initially thinks it needs an FP register, but later it
  116. turns out to be unneeded, so one could either approach this by being more
  117. clever about not inserting code for an FP in the first place, or optimizing
  118. away the copy later.
  119. //===---------------------------------------------------------------------===//