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

Use PATH instead of WASM_OPT to find wasm-opt.


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,29 @@
   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();
+
+      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 +133,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);
   }
 }


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,29 @@
   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();
+
+      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 +133,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);
   }
 }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to