-
Notifications
You must be signed in to change notification settings - Fork 12k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add flag to opt out of wasm-opt #95208
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang @llvm/pr-subscribers-clang-driver Author: Quentin Michaud (mh4ck-Thales) ChangesThis PR fixes #55781 by adding the I think that adding a warning when no flag or the CC @sunfishcode that proposed in the associated issue to review this patch. Full diff: https://github.com/llvm/llvm-project/pull/95208.diff 3 Files Affected:
diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h
index 75e88afbd9705..91f1c2f2e6239 100644
--- a/clang/include/clang/Basic/LangOptions.h
+++ b/clang/include/clang/Basic/LangOptions.h
@@ -575,6 +575,10 @@ class LangOptions : public LangOptionsBase {
// implementation on real-world examples.
std::string OpenACCMacroOverride;
+ // Indicates if the wasm-opt binary must be ignored in the case of a
+ // WebAssembly target.
+ bool NoWasmOpt = false;
+
LangOptions();
/// Set language defaults for the given input language and
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index d44faa55c456f..22a400c9707b1 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -8740,3 +8740,11 @@ def spirv : DXCFlag<"spirv">,
def fspv_target_env_EQ : Joined<["-"], "fspv-target-env=">, Group<dxc_Group>,
HelpText<"Specify the target environment">,
Values<"vulkan1.2, vulkan1.3">;
+def no_wasm_opt : Flag<["--"], "no-wasm-opt">,
+ Group<m_Group>,
+ HelpText<"Disable the wasm-opt optimizer">,
+ MarshallingInfoFlag<LangOpts<"NoWasmOpt">>;
+def wasm_opt : Flag<["--"], "wasm-opt">,
+ Group<m_Group>,
+ HelpText<"Enable the wasm-opt optimizer (default)">,
+ MarshallingInfoNegativeFlag<LangOpts<"NoWasmOpt">>;
diff --git a/clang/lib/Driver/ToolChains/WebAssembly.cpp b/clang/lib/Driver/ToolChains/WebAssembly.cpp
index 5b763df9b3329..60bd97e0ee987 100644
--- a/clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ b/clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -158,44 +158,46 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.push_back("-o");
CmdArgs.push_back(Output.getFilename());
- // When optimizing, if wasm-opt is available, run it.
- std::string WasmOptPath;
- if (Args.getLastArg(options::OPT_O_Group)) {
- WasmOptPath = ToolChain.GetProgramPath("wasm-opt");
- if (WasmOptPath == "wasm-opt") {
- WasmOptPath = {};
+ if (Args.hasFlag(options::OPT_wasm_opt, options::OPT_no_wasm_opt, true)) {
+ // When optimizing, if wasm-opt is available, run it.
+ std::string WasmOptPath;
+ if (Args.getLastArg(options::OPT_O_Group)) {
+ WasmOptPath = ToolChain.GetProgramPath("wasm-opt");
+ if (WasmOptPath == "wasm-opt") {
+ WasmOptPath = {};
+ }
}
- }
-
- if (!WasmOptPath.empty()) {
- CmdArgs.push_back("--keep-section=target_features");
- }
- C.addCommand(std::make_unique<Command>(JA, *this,
- ResponseFileSupport::AtFileCurCP(),
- Linker, CmdArgs, Inputs, Output));
-
- if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
if (!WasmOptPath.empty()) {
- 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 OptArgs;
- OptArgs.push_back(Output.getFilename());
- OptArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
- OptArgs.push_back("-o");
- OptArgs.push_back(Output.getFilename());
- C.addCommand(std::make_unique<Command>(
- JA, *this, ResponseFileSupport::AtFileCurCP(), WasmOpt, OptArgs,
- Inputs, Output));
+ CmdArgs.push_back("--keep-section=target_features");
+ }
+
+ C.addCommand(std::make_unique<Command>(JA, *this,
+ ResponseFileSupport::AtFileCurCP(),
+ Linker, CmdArgs, Inputs, Output));
+
+ if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
+ if (!WasmOptPath.empty()) {
+ 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 OptArgs;
+ OptArgs.push_back(Output.getFilename());
+ OptArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
+ OptArgs.push_back("-o");
+ OptArgs.push_back(Output.getFilename());
+ C.addCommand(std::make_unique<Command>(
+ JA, *this, ResponseFileSupport::AtFileCurCP(), WasmOpt, OptArgs,
+ Inputs, Output));
+ }
}
}
}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me.
Any opinion on whether to implement a warning or not ? |
@mh4ck-Thales Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
I wonder if there is some why to avoiding adding this new option globally like this and instead limit it in scope the wasm target in some way? |
@mh4ck-Thales A warning could be useful in some situations, but I also don't know how to navigate the situation with existing users using @sbc100 I'm open to ideas for how to limit the scope to wasm in some way. |
This PR fixes llvm#55781 by adding the `--no-wasm-opt` and `--wasm-opt` flags in clang to disable/enable the `wasm-opt` optimizations. The default is to enable `wasm-opt` as before in order to not break existing workflows. I think that adding a warning when no flag or the `--wasm-opt` flag is given but `wasm-opt` wasn't found in the path may be relevant here. It allows people using `wasm-opt` to be aware of if it have been used on their produced binary or not. The only downside I see to this is that people already using the toolchain with the `-O` and `-Werror` flags but without `wasm-opt` in the path will see their toolchain break (with an easy fix: either adding `--no-wasm-opt` or add `wasm-opt` to the path). I haven't implemented this here because I haven't figured out how to add such a warning, and I don't know if this warning should be added here or in another PR. CC @sunfishcode that proposed in the associated issue to review this patch.
I don't know who needs to hear this, but I got bitten by I was trying to compile zlib-ng for WASIp2 and it's CMake platform detection was passing Because in When that happens during CMake detetection, that problem is not surfaced and you are looking at
Until now, I was naive user of CMake.
Should I open new issue for this ? Which of it and where ? |
I think should never not be running wasm-opt when the linker is wasm-component-ld. Thats not going to work, at least not any time soon. How to integrate wasm-opt into wasm-component-ld is a good question. I imagine @alexcrichton has some ideas in that area. |
Ah yes that's correct, for |
I've opened #98373 to avoid running |
@sbc100 I tried to limit the scope of this new option to Wasm using the corresponding group in @sunfishcode Is there a kind of deprecation process in LLVM, when we can warn of breaking changes ? Otherwise not sure how to handle it. |
@mh4ck-Thales If I read the release schedule right, this flag hasn't made it into a release yet, so we still have time to make changes without having to deprecate anything. |
@sunfishcode from the release schedule these changes should be picked up to |
I would also love to see #98373 merged. Thank you. |
@mh4ck-Thales Yes; even if we don't get it in before the release branch is created, I think it'd be reasonable to backport a patch to the release branch. |
This commit adds a check that disables `wasm-opt` for the `wasm32-wasip2` target because `wasm-opt` doesn't support components at this time. This also fixes a minor issue from #95208 where if `wasm-opt` was disabled then the linker wouldn't run at all.
This commit adds a check that disables `wasm-opt` for the `wasm32-wasip2` target because `wasm-opt` doesn't support components at this time. This also fixes a minor issue from llvm#95208 where if `wasm-opt` was disabled then the linker wouldn't run at all.
Summary: This commit adds a check that disables `wasm-opt` for the `wasm32-wasip2` target because `wasm-opt` doesn't support components at this time. This also fixes a minor issue from #95208 where if `wasm-opt` was disabled then the linker wouldn't run at all. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251310
This PR fixes #55781 by adding the
--no-wasm-opt
and--wasm-opt
flags in clang to disable/enable thewasm-opt
optimizations. The default is to enablewasm-opt
as before in order to not break existing workflows.I think that adding a warning when no flag or the
--wasm-opt
flag is given butwasm-opt
wasn't found in the path may be relevant here. It allows people usingwasm-opt
to be aware of if it have been used on their produced binary or not. The only downside I see to this is that people already using the toolchain with the-O
and-Werror
flags but withoutwasm-opt
in the path will see their toolchain break (with an easy fix: either adding--no-wasm-opt
or addwasm-opt
to the path). I haven't implemented this here because I haven't figured out how to add such a warning, and I don't know if this warning should be added here or in another PR.CC @sunfishcode that proposed in the associated issue to review this patch.