sbc100 created this revision.
Herald added subscribers: pmatos, asb, wingo, ecnelises, sunfish, 
jgravelle-google, dschuff.
Herald added a project: All.
sbc100 requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, aheejin.
Herald added a project: clang.

This flag causes wasm-ld to preserve features section even in the face
of `--strip-all`.  This is useful for both clang (which can run
wasm-opt) after linking, and emcc (which performs a bunch of post-link
work).

Fixes: https://github.com/llvm/llvm-project/issues/60613
Fixes: https://github.com/llvm/llvm-project/issues/55781


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149917

Files:
  clang/lib/Driver/ToolChains/WebAssembly.cpp
  lld/wasm/Config.h
  lld/wasm/Driver.cpp
  lld/wasm/Options.td
  lld/wasm/SyntheticSections.h

Index: lld/wasm/SyntheticSections.h
===================================================================
--- lld/wasm/SyntheticSections.h
+++ lld/wasm/SyntheticSections.h
@@ -408,7 +408,8 @@
   TargetFeaturesSection()
       : SyntheticSection(llvm::wasm::WASM_SEC_CUSTOM, "target_features") {}
   bool isNeeded() const override {
-    return !config->stripAll && features.size() > 0;
+    return features.size() > 0 &&
+           (!config->stripAll || config->preserveFeatures);
   }
   void writeBody() override;
 
Index: lld/wasm/Options.td
===================================================================
--- lld/wasm/Options.td
+++ lld/wasm/Options.td
@@ -132,6 +132,8 @@
 
 def strip_debug: F<"strip-debug">, HelpText<"Strip debugging information">;
 
+def preserve_features: F<"preserve-features">, HelpText<"Preserve features section even when --strip-all is given.  This is useful for compiler drivers such as clang or emcc that depend on the features section for post-link processing.">;
+
 defm threads
     : Eq<"threads", "Number of threads. '1' disables multi-threading. By "
                     "default all available hardware threads are used">;
Index: lld/wasm/Driver.cpp
===================================================================
--- lld/wasm/Driver.cpp
+++ lld/wasm/Driver.cpp
@@ -476,6 +476,7 @@
       args.hasFlag(OPT_merge_data_segments, OPT_no_merge_data_segments,
                    !config->relocatable);
   config->pie = args.hasFlag(OPT_pie, OPT_no_pie, false);
+  config->preserveFeatures = args.hasArg(OPT_preserve_features);
   config->printGcSections =
       args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false);
   config->saveTemps = args.hasArg(OPT_save_temps);
Index: lld/wasm/Config.h
===================================================================
--- lld/wasm/Config.h
+++ lld/wasm/Config.h
@@ -57,6 +57,7 @@
   std::optional<bool> is64;
   bool mergeDataSegments;
   bool pie;
+  bool preserveFeatures;
   bool printGcSections;
   bool relocatable;
   bool saveTemps;
Index: clang/lib/Driver/ToolChains/WebAssembly.cpp
===================================================================
--- clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -126,14 +126,25 @@
   CmdArgs.push_back("-o");
   CmdArgs.push_back(Output.getFilename());
 
+  // When optimizing, if wasm-opt is available, run it.
+  std::string WasmOptPath;
+  if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
+    WasmOptPath = ToolChain.GetProgramPath("wasm-opt");
+    if (WasmOptPath == "wasm-opt") {
+      WasmOptPath = {};
+    }
+  }
+
+  if (!WasmOptPath.empty()) {
+    CmdArgs.push_back("--preserve-features");
+  }
+
   C.addCommand(std::make_unique<Command>(JA, *this,
                                          ResponseFileSupport::AtFileCurCP(),
                                          Linker, CmdArgs, Inputs, Output));
 
-  // When optimizing, if wasm-opt is available, run it.
   if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
-    auto WasmOptPath = ToolChain.GetProgramPath("wasm-opt");
-    if (WasmOptPath != "wasm-opt") {
+    if (!WasmOptPath.empty()) {
       StringRef OOpt = "s";
       if (A->getOption().matches(options::OPT_O4) ||
           A->getOption().matches(options::OPT_Ofast))
@@ -145,13 +156,13 @@
 
       if (OOpt != "0") {
         const char *WasmOpt = Args.MakeArgString(WasmOptPath);
-        ArgStringList CmdArgs;
-        CmdArgs.push_back(Output.getFilename());
-        CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
-        CmdArgs.push_back("-o");
-        CmdArgs.push_back(Output.getFilename());
+        ArgStringList OptArgs;
+        OptArgs.push_back(Output.getFilename());
+        OptArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
+        OptArgs.push_back("-o");
+        OptArgs.push_back(Output.getFilename());
         C.addCommand(std::make_unique<Command>(
-            JA, *this, ResponseFileSupport::AtFileCurCP(), WasmOpt, CmdArgs,
+            JA, *this, ResponseFileSupport::AtFileCurCP(), WasmOpt, OptArgs,
             Inputs, Output));
       }
     }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to