Skip to content

Commit 962d7ac

Browse files
authored
Add flag to opt out of wasm-opt (#95208)
This PR fixes #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.
1 parent 431213c commit 962d7ac

File tree

3 files changed

+49
-35
lines changed

3 files changed

+49
-35
lines changed

clang/include/clang/Basic/LangOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,10 @@ class LangOptions : public LangOptionsBase {
575575
// implementation on real-world examples.
576576
std::string OpenACCMacroOverride;
577577

578+
// Indicates if the wasm-opt binary must be ignored in the case of a
579+
// WebAssembly target.
580+
bool NoWasmOpt = false;
581+
578582
LangOptions();
579583

580584
/// Set language defaults for the given input language and

clang/include/clang/Driver/Options.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8770,3 +8770,11 @@ def spirv : DXCFlag<"spirv">,
87708770
def fspv_target_env_EQ : Joined<["-"], "fspv-target-env=">, Group<dxc_Group>,
87718771
HelpText<"Specify the target environment">,
87728772
Values<"vulkan1.2, vulkan1.3">;
8773+
def no_wasm_opt : Flag<["--"], "no-wasm-opt">,
8774+
Group<m_Group>,
8775+
HelpText<"Disable the wasm-opt optimizer">,
8776+
MarshallingInfoFlag<LangOpts<"NoWasmOpt">>;
8777+
def wasm_opt : Flag<["--"], "wasm-opt">,
8778+
Group<m_Group>,
8779+
HelpText<"Enable the wasm-opt optimizer (default)">,
8780+
MarshallingInfoNegativeFlag<LangOpts<"NoWasmOpt">>;

clang/lib/Driver/ToolChains/WebAssembly.cpp

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -158,44 +158,46 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
158158
CmdArgs.push_back("-o");
159159
CmdArgs.push_back(Output.getFilename());
160160

161-
// When optimizing, if wasm-opt is available, run it.
162-
std::string WasmOptPath;
163-
if (Args.getLastArg(options::OPT_O_Group)) {
164-
WasmOptPath = ToolChain.GetProgramPath("wasm-opt");
165-
if (WasmOptPath == "wasm-opt") {
166-
WasmOptPath = {};
161+
if (Args.hasFlag(options::OPT_wasm_opt, options::OPT_no_wasm_opt, true)) {
162+
// When optimizing, if wasm-opt is available, run it.
163+
std::string WasmOptPath;
164+
if (Args.getLastArg(options::OPT_O_Group)) {
165+
WasmOptPath = ToolChain.GetProgramPath("wasm-opt");
166+
if (WasmOptPath == "wasm-opt") {
167+
WasmOptPath = {};
168+
}
167169
}
168-
}
169-
170-
if (!WasmOptPath.empty()) {
171-
CmdArgs.push_back("--keep-section=target_features");
172-
}
173170

174-
C.addCommand(std::make_unique<Command>(JA, *this,
175-
ResponseFileSupport::AtFileCurCP(),
176-
Linker, CmdArgs, Inputs, Output));
177-
178-
if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
179171
if (!WasmOptPath.empty()) {
180-
StringRef OOpt = "s";
181-
if (A->getOption().matches(options::OPT_O4) ||
182-
A->getOption().matches(options::OPT_Ofast))
183-
OOpt = "4";
184-
else if (A->getOption().matches(options::OPT_O0))
185-
OOpt = "0";
186-
else if (A->getOption().matches(options::OPT_O))
187-
OOpt = A->getValue();
188-
189-
if (OOpt != "0") {
190-
const char *WasmOpt = Args.MakeArgString(WasmOptPath);
191-
ArgStringList OptArgs;
192-
OptArgs.push_back(Output.getFilename());
193-
OptArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
194-
OptArgs.push_back("-o");
195-
OptArgs.push_back(Output.getFilename());
196-
C.addCommand(std::make_unique<Command>(
197-
JA, *this, ResponseFileSupport::AtFileCurCP(), WasmOpt, OptArgs,
198-
Inputs, Output));
172+
CmdArgs.push_back("--keep-section=target_features");
173+
}
174+
175+
C.addCommand(std::make_unique<Command>(JA, *this,
176+
ResponseFileSupport::AtFileCurCP(),
177+
Linker, CmdArgs, Inputs, Output));
178+
179+
if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
180+
if (!WasmOptPath.empty()) {
181+
StringRef OOpt = "s";
182+
if (A->getOption().matches(options::OPT_O4) ||
183+
A->getOption().matches(options::OPT_Ofast))
184+
OOpt = "4";
185+
else if (A->getOption().matches(options::OPT_O0))
186+
OOpt = "0";
187+
else if (A->getOption().matches(options::OPT_O))
188+
OOpt = A->getValue();
189+
190+
if (OOpt != "0") {
191+
const char *WasmOpt = Args.MakeArgString(WasmOptPath);
192+
ArgStringList OptArgs;
193+
OptArgs.push_back(Output.getFilename());
194+
OptArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
195+
OptArgs.push_back("-o");
196+
OptArgs.push_back(Output.getFilename());
197+
C.addCommand(std::make_unique<Command>(
198+
JA, *this, ResponseFileSupport::AtFileCurCP(), WasmOpt, OptArgs,
199+
Inputs, Output));
200+
}
199201
}
200202
}
201203
}

0 commit comments

Comments
 (0)