Path: blob/main/contrib/llvm-project/llvm/lib/Target/WebAssembly/README.txt
35269 views
//===-- README.txt - Notes for WebAssembly code gen -----------------------===//12The object format emitted by the WebAssembly backed is documented in:34* https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md56The C ABI is described in:78* https://github.com/WebAssembly/tool-conventions/blob/main/BasicCABI.md910For more information on WebAssembly itself, see the home page:1112* https://webassembly.github.io/1314Emscripten provides a C/C++ compilation environment based on clang which15includes standard libraries, tools, and packaging for producing WebAssembly16applications that can run in browsers and other environments.1718wasi-sdk provides a more minimal C/C++ SDK based on clang, llvm and a libc based19on musl, for producing WebAssembly applications that use the WASI ABI.2021Rust provides WebAssembly support integrated into Cargo. There are two22main options:23- wasm32-unknown-unknown, which provides a relatively minimal environment24that has an emphasis on being "native"25- wasm32-unknown-emscripten, which uses Emscripten internally and26provides standard C/C++ libraries, filesystem emulation, GL and SDL27bindings28For more information, see:29* https://www.hellorust.com/3031The following documents contain some information on the semantics and binary32encoding of WebAssembly itself:33* https://github.com/WebAssembly/design/blob/main/Semantics.md34* https://github.com/WebAssembly/design/blob/main/BinaryEncoding.md3536Some notes on ways that the generated code could be improved follow:3738//===---------------------------------------------------------------------===//3940Br, br_if, and br_table instructions can support having a value on the value41stack across the jump (sometimes). We should (a) model this, and (b) extend42the stackifier to utilize it.4344//===---------------------------------------------------------------------===//4546The min/max instructions aren't exactly a<b?a:b because of NaN and negative zero47behavior. The ARM target has the same kind of min/max instructions and has48implemented optimizations for them; we should do similar optimizations for49WebAssembly.5051//===---------------------------------------------------------------------===//5253AArch64 runs SeparateConstOffsetFromGEPPass, followed by EarlyCSE and LICM.54Would these be useful to run for WebAssembly too? Also, it has an option to55run SimplifyCFG after running the AtomicExpand pass. Would this be useful for56us too?5758//===---------------------------------------------------------------------===//5960Register stackification uses the VALUE_STACK physical register to impose61ordering dependencies on instructions with stack operands. This is pessimistic;62we should consider alternate ways to model stack dependencies.6364//===---------------------------------------------------------------------===//6566Lots of things could be done in WebAssemblyTargetTransformInfo.cpp. Similarly,67there are numerous optimization-related hooks that can be overridden in68WebAssemblyTargetLowering.6970//===---------------------------------------------------------------------===//7172Instead of the OptimizeReturned pass, which should consider preserving the73"returned" attribute through to MachineInstrs and extending the74MemIntrinsicResults pass to do this optimization on calls too. That would also75let the WebAssemblyPeephole pass clean up dead defs for such calls, as it does76for stores.7778//===---------------------------------------------------------------------===//7980Consider implementing optimizeSelect, optimizeCompareInstr, optimizeCondBranch,81optimizeLoadInstr, and/or getMachineCombinerPatterns.8283//===---------------------------------------------------------------------===//8485Find a clean way to fix the problem which leads to the Shrink Wrapping pass86being run after the WebAssembly PEI pass.8788//===---------------------------------------------------------------------===//8990When setting multiple local variables to the same constant, we currently get91code like this:9293i32.const $4=, 094i32.const $3=, 09596It could be done with a smaller encoding like this:9798i32.const $push5=, 099local.tee $push6=, $4=, $pop5100local.copy $3=, $pop6101102//===---------------------------------------------------------------------===//103104WebAssembly registers are implicitly initialized to zero. Explicit zeroing is105therefore often redundant and could be optimized away.106107//===---------------------------------------------------------------------===//108109Small indices may use smaller encodings than large indices.110WebAssemblyRegColoring and/or WebAssemblyRegRenumbering should sort registers111according to their usage frequency to maximize the usage of smaller encodings.112113//===---------------------------------------------------------------------===//114115Many cases of irreducible control flow could be transformed more optimally116than via the transform in WebAssemblyFixIrreducibleControlFlow.cpp.117118It may also be worthwhile to do transforms before register coloring,119particularly when duplicating code, to allow register coloring to be aware of120the duplication.121122//===---------------------------------------------------------------------===//123124WebAssemblyRegStackify could use AliasAnalysis to reorder loads and stores more125aggressively.126127//===---------------------------------------------------------------------===//128129WebAssemblyRegStackify is currently a greedy algorithm. This means that, for130example, a binary operator will stackify with its user before its operands.131However, if moving the binary operator to its user moves it to a place where132its operands can't be moved to, it would be better to leave it in place, or133perhaps move it up, so that it can stackify its operands. A binary operator134has two operands and one result, so in such cases there could be a net win by135preferring the operands.136137//===---------------------------------------------------------------------===//138139Instruction ordering has a significant influence on register stackification and140coloring. Consider experimenting with the MachineScheduler (enable via141enableMachineScheduler) and determine if it can be configured to schedule142instructions advantageously for this purpose.143144//===---------------------------------------------------------------------===//145146WebAssemblyRegStackify currently assumes that the stack must be empty after147an instruction with no return values, however wasm doesn't actually require148this. WebAssemblyRegStackify could be extended, or possibly rewritten, to take149full advantage of what WebAssembly permits.150151//===---------------------------------------------------------------------===//152153Add support for mergeable sections in the Wasm writer, such as for strings and154floating-point constants.155156//===---------------------------------------------------------------------===//157158The function @dynamic_alloca_redzone in test/CodeGen/WebAssembly/userstack.ll159ends up with a local.tee in its prolog which has an unused result, requiring160an extra drop:161162global.get $push8=, 0163local.tee $push9=, 1, $pop8164drop $pop9165[...]166167The prologue code initially thinks it needs an FP register, but later it168turns out to be unneeded, so one could either approach this by being more169clever about not inserting code for an FP in the first place, or optimizing170away the copy later.171172//===---------------------------------------------------------------------===//173174175