b43807a835fc878e5eefcb8b4a18aff62d7f4540.patch 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. From b43807a835fc878e5eefcb8b4a18aff62d7f4540 Mon Sep 17 00:00:00 2001
  2. From: Alon Zakai <azakai@google.com>
  3. Date: Wed, 19 Aug 2020 14:55:46 -0700
  4. Subject: [PATCH] wasm-emscripten-finalize: Make EM_ASM modifications optional
  5. (#3044)
  6. wasm-emscripten-finalize renames EM_ASM calls to have the signature in
  7. the name. This isn't actually useful - emscripten doesn't benefit from that. I
  8. think it was optimized in fastcomp, and in upstream we copied the general
  9. form but not the optimizations, and then EM_JS came along which is
  10. easier to optimize anyhow.
  11. This PR makes those changes optional: when not doing them, it just
  12. leaves the calls as they are. Emscripten will need some changes to
  13. handle that, but those are simple.
  14. For convenience this adds a flag to "minimize wasm changes". The idea
  15. is that this flag avoids needing a double-roll or other inconvenience
  16. as the changes need to happen in tandem on the emscripten side.
  17. The same flag can be reused for later changes similar to this one.
  18. When they are all done we can remove the flag. (Note how the code
  19. ifdefed by the flag can be removed once we no longer need the old
  20. way of doing things - that is, the new approach is simpler on the
  21. binaryen side).
  22. See #3043
  23. ---
  24. src/tools/wasm-emscripten-finalize.cpp | 11 +++++++
  25. src/wasm-emscripten.h | 4 +++
  26. src/wasm/wasm-emscripten.cpp | 40 +++++++++++++++++---------
  27. 3 files changed, 42 insertions(+), 13 deletions(-)
  28. diff --git a/src/tools/wasm-emscripten-finalize.cpp b/src/tools/wasm-emscripten-finalize.cpp
  29. index ce1da4c9e1..960c6870cf 100644
  30. --- a/src/tools/wasm-emscripten-finalize.cpp
  31. +++ b/src/tools/wasm-emscripten-finalize.cpp
  32. @@ -56,6 +56,7 @@ int main(int argc, const char* argv[]) {
  33. bool checkStackOverflow = false;
  34. uint64_t globalBase = INVALID_BASE;
  35. bool standaloneWasm = false;
  36. + bool minimizeWasmChanges = false;
  37. ToolOptions options("wasm-emscripten-finalize",
  38. "Performs Emscripten-specific transforms on .wasm files");
  39. @@ -163,6 +164,15 @@ int main(int argc, const char* argv[]) {
  40. [&standaloneWasm](Options* o, const std::string&) {
  41. standaloneWasm = true;
  42. })
  43. + .add("--minimize-wasm-changes",
  44. + "",
  45. + "Modify the wasm as little as possible. This is useful during "
  46. + "development as we reduce the number of changes to the wasm, as it "
  47. + "lets emscripten control how much modifications to do.",
  48. + Options::Arguments::Zero,
  49. + [&minimizeWasmChanges](Options* o, const std::string&) {
  50. + minimizeWasmChanges = true;
  51. + })
  52. .add_positional("INFILE",
  53. Options::Arguments::One,
  54. [&infile](Options* o, const std::string& argument) {
  55. @@ -222,6 +232,7 @@ int main(int argc, const char* argv[]) {
  56. EmscriptenGlueGenerator generator(wasm);
  57. generator.setStandalone(standaloneWasm);
  58. generator.setSideModule(sideModule);
  59. + generator.setMinimizeWasmChanges(minimizeWasmChanges);
  60. generator.fixInvokeFunctionNames();
  61. diff --git a/src/wasm-emscripten.h b/src/wasm-emscripten.h
  62. index 395c76a160..fe8b8ed36a 100644
  63. --- a/src/wasm-emscripten.h
  64. +++ b/src/wasm-emscripten.h
  65. @@ -35,6 +35,9 @@ class EmscriptenGlueGenerator {
  66. void setStandalone(bool standalone_) { standalone = standalone_; }
  67. void setSideModule(bool sideModule_) { sideModule = sideModule_; }
  68. + void setMinimizeWasmChanges(bool minimizeWasmChanges_) {
  69. + minimizeWasmChanges = minimizeWasmChanges_;
  70. + }
  71. Function* generateMemoryGrowthFunction();
  72. Function* generateAssignGOTEntriesFunction();
  73. @@ -71,6 +74,7 @@ class EmscriptenGlueGenerator {
  74. bool useStackPointerGlobal;
  75. bool standalone;
  76. bool sideModule;
  77. + bool minimizeWasmChanges;
  78. // Used by generateDynCallThunk to track all the dynCall functions created
  79. // so far.
  80. std::unordered_set<Signature> sigs;
  81. diff --git a/src/wasm/wasm-emscripten.cpp b/src/wasm/wasm-emscripten.cpp
  82. index e4664e645c..f4da0e6e7a 100644
  83. --- a/src/wasm/wasm-emscripten.cpp
  84. +++ b/src/wasm/wasm-emscripten.cpp
  85. @@ -320,6 +320,7 @@ std::string proxyingSuffix(Proxying proxy) {
  86. struct AsmConstWalker : public LinearExecutionWalker<AsmConstWalker> {
  87. Module& wasm;
  88. + bool minimizeWasmChanges;
  89. std::vector<Address> segmentOffsets; // segment index => address offset
  90. struct AsmConst {
  91. @@ -334,8 +335,9 @@ struct AsmConstWalker : public LinearExecutionWalker<AsmConstWalker> {
  92. // last sets in the current basic block, per index
  93. std::map<Index, LocalSet*> sets;
  94. - AsmConstWalker(Module& _wasm)
  95. - : wasm(_wasm), segmentOffsets(getSegmentOffsets(wasm)) {}
  96. + AsmConstWalker(Module& _wasm, bool minimizeWasmChanges)
  97. + : wasm(_wasm), minimizeWasmChanges(minimizeWasmChanges),
  98. + segmentOffsets(getSegmentOffsets(wasm)) {}
  99. void noteNonLinear(Expression* curr);
  100. @@ -426,7 +428,9 @@ void AsmConstWalker::visitCall(Call* curr) {
  101. int32_t address = value->value.geti32();
  102. auto code = codeForConstAddr(wasm, segmentOffsets, address);
  103. auto& asmConst = createAsmConst(address, code, sig, importName);
  104. - fixupName(curr->target, baseSig, asmConst.proxy);
  105. + if (!minimizeWasmChanges) {
  106. + fixupName(curr->target, baseSig, asmConst.proxy);
  107. + }
  108. }
  109. Proxying AsmConstWalker::proxyType(Name name) {
  110. @@ -439,6 +443,9 @@ Proxying AsmConstWalker::proxyType(Name name) {
  111. }
  112. void AsmConstWalker::visitTable(Table* curr) {
  113. + if (minimizeWasmChanges) {
  114. + return;
  115. + }
  116. for (auto& segment : curr->segments) {
  117. for (auto& name : segment.data) {
  118. auto* func = wasm.getFunction(name);
  119. @@ -515,24 +522,30 @@ void AsmConstWalker::addImports() {
  120. }
  121. }
  122. -AsmConstWalker fixEmAsmConstsAndReturnWalker(Module& wasm) {
  123. +static AsmConstWalker fixEmAsmConstsAndReturnWalker(Module& wasm,
  124. + bool minimizeWasmChanges) {
  125. // Collect imports to remove
  126. // This would find our generated functions if we ran it later
  127. std::vector<Name> toRemove;
  128. - for (auto& import : wasm.functions) {
  129. - if (import->imported() && import->base.hasSubstring(EM_ASM_PREFIX)) {
  130. - toRemove.push_back(import->name);
  131. + if (!minimizeWasmChanges) {
  132. + for (auto& import : wasm.functions) {
  133. + if (import->imported() && import->base.hasSubstring(EM_ASM_PREFIX)) {
  134. + toRemove.push_back(import->name);
  135. + }
  136. }
  137. }
  138. // Walk the module, generate _sig versions of EM_ASM functions
  139. - AsmConstWalker walker(wasm);
  140. + AsmConstWalker walker(wasm, minimizeWasmChanges);
  141. walker.process();
  142. - // Remove the base functions that we didn't generate
  143. - for (auto importName : toRemove) {
  144. - wasm.removeFunction(importName);
  145. + if (!minimizeWasmChanges) {
  146. + // Remove the base functions that we didn't generate
  147. + for (auto importName : toRemove) {
  148. + wasm.removeFunction(importName);
  149. + }
  150. }
  151. +
  152. return walker;
  153. }
  154. @@ -754,7 +767,8 @@ std::string EmscriptenGlueGenerator::generateEmscriptenMetadata(
  155. std::stringstream meta;
  156. meta << "{\n";
  157. - AsmConstWalker emAsmWalker = fixEmAsmConstsAndReturnWalker(wasm);
  158. + AsmConstWalker emAsmWalker =
  159. + fixEmAsmConstsAndReturnWalker(wasm, minimizeWasmChanges);
  160. // print
  161. commaFirst = true;
  162. @@ -810,7 +824,7 @@ std::string EmscriptenGlueGenerator::generateEmscriptenMetadata(
  163. commaFirst = true;
  164. ModuleUtils::iterImportedFunctions(wasm, [&](Function* import) {
  165. if (emJsWalker.codeByName.count(import->base.str) == 0 &&
  166. - !import->base.startsWith(EM_ASM_PREFIX.str) &&
  167. + (minimizeWasmChanges || !import->base.startsWith(EM_ASM_PREFIX.str)) &&
  168. !import->base.startsWith("invoke_")) {
  169. if (declares.insert(import->base.str).second) {
  170. meta << nextElement() << '"' << import->base.str << '"';