Skip to content

Add flag to opt out of wasm-opt #95208

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

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang/include/clang/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -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">>;
72 changes: 37 additions & 35 deletions clang/lib/Driver/ToolChains/WebAssembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
}
Expand Down
Loading