[clang] Add wasm-opt warning (PR #100321)

via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 29 07:56:21 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-driver

Author: Quentin Michaud (mh4ck-Thales)

<details>
<summary>Changes</summary>

Add a warning when `wasm-opt` is requested with a flag (#<!-- -->95208) but is not found in the path.

I'm using #<!-- -->77148 as reference on how to add a new warning but please tell me if you can help in implementing this. Also I'm not sure in which warning group include this, at first glance none seem relevant for this specific problem. 

Regarding https://github.com/llvm/llvm-project/pull/98373 another warning might be relevant if people target WASIp2+ and try to use wasm-opt explicitly. This can be grouped in one warning "wasm-opt not available in this context" or separated in 2 for clearer error messages. WDYT?

CC @<!-- -->sunfishcode @<!-- -->sbc100 @<!-- -->alexcrichton 

---
Full diff: https://github.com/llvm/llvm-project/pull/100321.diff


4 Files Affected:

- (modified) clang/include/clang/Basic/DiagnosticDriverKinds.td (+8) 
- (modified) clang/include/clang/Basic/DiagnosticGroups.td (+2) 
- (modified) clang/include/clang/Driver/Options.td (+1) 
- (modified) clang/lib/Driver/ToolChains/WebAssembly.cpp (+15) 


``````````diff
diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index ba90742fbdaabc..2a862edf8788c8 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -826,4 +826,12 @@ def err_drv_triple_version_invalid : Error<
 
 def warn_missing_include_dirs : Warning<
   "no such include directory: '%0'">, InGroup<MissingIncludeDirs>, DefaultIgnore;
+
+def warn_wasm_opt_not_found : Warning<
+  "wasm-opt was not found, some optimizations were not applied">,
+  InGroup<WebAssemblyOptimization>;
+def err_wasm_opt_not_found_with_flag : Error<
+  "wasm-opt was explicitly requested, but was not found">;
+def err_wasm_opt_requested_but_not_supported : Error<
+  "wasm-opt was explicitly requested, but is not supported with '%0'">;
 }
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td
index 28d315f63e5c47..fab11f47492db3 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -1520,6 +1520,8 @@ in addition with the pragmas or -fmax-tokens flag to get any warnings.
 
 def WebAssemblyExceptionSpec : DiagGroup<"wasm-exception-spec">;
 
+def WebAssemblyOptimization : DiagGroup<"wasm-opt">;
+
 def RTTI : DiagGroup<"rtti">;
 
 def OpenCLCoreFeaturesDiagGroup : DiagGroup<"pedantic-core-features">;
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 83cf753e824845..ba6e7ab9e11d06 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -8923,3 +8923,4 @@ def wasm_opt : Flag<["--"], "wasm-opt">,
   Group<m_Group>,
   HelpText<"Enable the wasm-opt optimizer (default)">,
   MarshallingInfoNegativeFlag<LangOpts<"NoWasmOpt">>;
+def Wwarn_wasm_opt_not_found : Flag<["-"], "Wwarn_wasm_opt_not_found">;
diff --git a/clang/lib/Driver/ToolChains/WebAssembly.cpp b/clang/lib/Driver/ToolChains/WebAssembly.cpp
index 9aacda5fd5702f..e9820ce1d35995 100644
--- a/clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ b/clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -9,6 +9,7 @@
 #include "WebAssembly.h"
 #include "CommonArgs.h"
 #include "Gnu.h"
+#include "clang/Basic/DiagnosticDriver.h"
 #include "clang/Basic/Version.h"
 #include "clang/Config/config.h"
 #include "clang/Driver/Compilation.h"
@@ -20,6 +21,7 @@
 #include "llvm/Support/Path.h"
 #include "llvm/Support/VirtualFileSystem.h"
 
+
 using namespace clang::driver;
 using namespace clang::driver::tools;
 using namespace clang::driver::toolchains;
@@ -172,6 +174,11 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
   bool RunWasmOpt = Args.hasFlag(options::OPT_wasm_opt,
                                  options::OPT_no_wasm_opt, WasmOptDefault);
 
+  if (TargetBuildsComponents(ToolChain.getTriple()) &&
+      Args.hasFlag(options::OPT_wasm_opt, options::OPT_no_wasm_opt, false)) {
+    ToolChain.getDriver().Diag(diag::err_wasm_opt_requested_but_not_supported)
+        << ToolChain.getTriple().str();
+  }
   // If wasm-opt is enabled and optimizations are happening look for the
   // `wasm-opt` program. If it's not found auto-disable it.
   std::string WasmOptPath;
@@ -180,6 +187,14 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
     if (WasmOptPath == "wasm-opt") {
       WasmOptPath = {};
     }
+    if (WasmOptPath.empty()) {
+      if (Args.hasFlag(options::OPT_wasm_opt, options::OPT_no_wasm_opt,
+                       false)) {
+        ToolChain.getDriver().Diag(diag::err_wasm_opt_not_found_with_flag);
+      } else {
+        ToolChain.getDriver().Diag(diag::warn_wasm_opt_not_found);
+      }
+    }
   }
 
   if (!WasmOptPath.empty()) {

``````````

</details>


https://github.com/llvm/llvm-project/pull/100321


More information about the cfe-commits mailing list