[PATCH] D70500: [WebAssembly] Enable use of wasm-opt and LTO-enabled system libraries

Dan Gohman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 20 13:19:07 PST 2019


sunfish updated this revision to Diff 230322.
sunfish added a comment.

In D70500#1754012 <https://reviews.llvm.org/D70500#1754012>, @sbc100 wrote:

> On I just remember why this is probably a bad idea.   llvm bitcode is not designed to be stable,  unlike object files, so its probably not a good idea to encourage the distributing of bitcode files in SDKs and such.


That's a good point. I think we can address this by including the LLVM revision in the path --I've now updated the patch to do this.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D70500/new/

https://reviews.llvm.org/D70500

Files:
  clang/lib/Driver/ToolChains/WebAssembly.cpp
  clang/test/Driver/wasm-toolchain-lto.c


Index: clang/test/Driver/wasm-toolchain-lto.c
===================================================================
--- /dev/null
+++ clang/test/Driver/wasm-toolchain-lto.c
@@ -0,0 +1,6 @@
+// A basic C link command-line with optimization with known OS and LTO enabled.
+
+// RUN: %clang -### -O2 -flto -no-canonical-prefixes -target wasm32-wasi --sysroot=/foo %s 2>&1 \
+// RUN:   | FileCheck -check-prefix=LINK_OPT_KNOWN %s
+// LINK_OPT_KNOWN: clang{{.*}}" "-cc1" {{.*}} "-o" "[[temp:[^"]*]]"
+// LINK_OPT_KNOWN: wasm-ld{{.*}}" "-L/foo/lib/wasm32-wasi/llvm-lto/
Index: clang/lib/Driver/ToolChains/WebAssembly.cpp
===================================================================
--- clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -8,6 +8,7 @@
 
 #include "WebAssembly.h"
 #include "CommonArgs.h"
+#include "clang/Basic/Version.h"
 #include "clang/Config/config.h"
 #include "clang/Driver/Compilation.h"
 #include "clang/Driver/Driver.h"
@@ -90,6 +91,28 @@
   CmdArgs.push_back(Output.getFilename());
 
   C.addCommand(llvm::make_unique<Command>(JA, *this, Linker, CmdArgs, Inputs));
+
+  // When optimizing, if WASM_OPT is set, run wasm-opt.
+  if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
+    if (const char *WasmOptPath = getenv("WASM_OPT")) {
+      StringRef OOpt = "s";
+      if (A->getOption().matches(options::OPT_O4) ||
+          A->getOption().matches(options::OPT_Ofast))
+        OOpt = "4";
+      else if (A->getOption().matches(options::OPT_O0))
+        OOpt = "0";
+      else if (A->getOption().matches(options::OPT_O))
+        OOpt = A->getValue();
+
+      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());
+      C.addCommand(llvm::make_unique<Command>(JA, *this, WasmOpt, CmdArgs, Inputs));
+    }
+  }
 }
 
 WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
@@ -109,6 +132,16 @@
   } else {
     const std::string MultiarchTriple =
         getMultiarchTriple(getDriver(), Triple, getDriver().SysRoot);
+    if (D.isUsingLTO()) {
+      auto LLVMRevision = getLLVMRevision();
+      if (!LLVMRevision.empty()) {
+        // For LTO, enable use of lto-enabled sysroot libraries too, if available.
+        // Note that the directory is keyed to the LLVM revision, as LLVM's
+        // bitcode format is not stable.
+        getFilePaths().push_back(getDriver().SysRoot + "/lib/" + MultiarchTriple +
+                                 "/llvm-lto/" + LLVMRevision);
+      }
+    }
     getFilePaths().push_back(getDriver().SysRoot + "/lib/" + MultiarchTriple);
   }
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D70500.230322.patch
Type: text/x-patch
Size: 2807 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191120/ee9a7554/attachment.bin>


More information about the cfe-commits mailing list