[WebAssembly] Use wasm-opt and LTO libraries when available.
authorDan Gohman <sunfish@mozilla.com>
Wed, 20 Nov 2019 23:25:43 +0000 (15:25 -0800)
committerDan Gohman <sunfish@mozilla.com>
Sat, 23 Nov 2019 06:16:28 +0000 (22:16 -0800)
When there's a wasm-opt in the PATH, run the it to optimize LLVM's
output. This fixes PR43796.

And, add an "llvm-lto" directory to the sysroot library search paths,
so that sysroots can provide LTO-enabled system libraries.

Differential Revision: https://reviews.llvm.org/D70500

clang/lib/Driver/ToolChains/WebAssembly.cpp
clang/test/Driver/wasm-toolchain-lto.c [new file with mode: 0644]

index ab64802..a2a9dff 100644 (file)
@@ -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,31 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
   CmdArgs.push_back(Output.getFilename());
 
   C.addCommand(std::make_unique<Command>(JA, *this, Linker, CmdArgs, Inputs));
+
+  // When optimizing, if wasm-opt is in the PATH, run wasm-opt.
+  if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
+    if (llvm::ErrorOr<std::string> WasmOptPath =
+           llvm::sys::findProgramByName("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();
+
+      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());
+        C.addCommand(std::make_unique<Command>(JA, *this, WasmOpt, CmdArgs, Inputs));
+      }
+    }
+  }
 }
 
 WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
@@ -109,6 +135,16 @@ WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
   } 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);
   }
 }
diff --git a/clang/test/Driver/wasm-toolchain-lto.c b/clang/test/Driver/wasm-toolchain-lto.c
new file mode 100644 (file)
index 0000000..2162073
--- /dev/null
@@ -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/