[clang] 8128289 - [WebAssembly] Use wasm-opt and LTO libraries when available.

Dan Gohman via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 22 23:22:05 PST 2019


Author: Dan Gohman
Date: 2019-11-22T22:16:28-08:00
New Revision: 812828984c10857a4cd260eb638c52a4411f9143

URL: https://github.com/llvm/llvm-project/commit/812828984c10857a4cd260eb638c52a4411f9143
DIFF: https://github.com/llvm/llvm-project/commit/812828984c10857a4cd260eb638c52a4411f9143.diff

LOG: [WebAssembly] Use wasm-opt and LTO libraries when available.

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

Added: 
    clang/test/Driver/wasm-toolchain-lto.c

Modified: 
    clang/lib/Driver/ToolChains/WebAssembly.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Driver/ToolChains/WebAssembly.cpp b/clang/lib/Driver/ToolChains/WebAssembly.cpp
index ab648025f222..a2a9dff79e52 100644
--- a/clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ b/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,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
index 000000000000..216207316f3d
--- /dev/null
+++ b/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/


        


More information about the cfe-commits mailing list