diff options
author | Alon Zakai <azakai@google.com> | 2021-03-30 16:50:55 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-30 16:50:55 -0700 |
commit | a680b716efe08afd9920269253058a849d6c4286 (patch) | |
tree | 9268b2a903bc60a675c6db537ffa4a3c0cb75df6 /src | |
parent | 330f722a4f4ae5288b21954aefa496af5cbc71e7 (diff) | |
download | binaryen-a680b716efe08afd9920269253058a849d6c4286.tar.gz binaryen-a680b716efe08afd9920269253058a849d6c4286.tar.bz2 binaryen-a680b716efe08afd9920269253058a849d6c4286.zip |
SetGlobals pass (#3750)
This allows changing a global's value on the commandline in an easy way.
In some toolchains this is useful as the build can contain a global that
indicates something like a logging level, which this can customize.
Diffstat (limited to 'src')
-rw-r--r-- | src/passes/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/passes/SetGlobals.cpp | 63 | ||||
-rw-r--r-- | src/passes/pass.cpp | 3 | ||||
-rw-r--r-- | src/passes/passes.h | 1 |
4 files changed, 68 insertions, 0 deletions
diff --git a/src/passes/CMakeLists.txt b/src/passes/CMakeLists.txt index 906871b91..69fbfa672 100644 --- a/src/passes/CMakeLists.txt +++ b/src/passes/CMakeLists.txt @@ -58,6 +58,7 @@ set(passes_SOURCES PrintFeatures.cpp PrintFunctionMap.cpp RoundTrip.cpp + SetGlobals.cpp StackIR.cpp Strip.cpp StripTargetFeatures.cpp diff --git a/src/passes/SetGlobals.cpp b/src/passes/SetGlobals.cpp new file mode 100644 index 000000000..8a147665c --- /dev/null +++ b/src/passes/SetGlobals.cpp @@ -0,0 +1,63 @@ +/* + * Copyright 2021 WebAssembly Community Group participants + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Assigns values to specified globals. This can be useful to perform a minor +// customization of an existing wasm file. + +#include "pass.h" +#include "support/string.h" +#include "wasm-builder.h" +#include "wasm.h" + +namespace wasm { + +struct SetGlobals : public Pass { + void run(PassRunner* runner, Module* module) override { + Name input = runner->options.getArgument( + "set-globals", + "SetGlobals usage: wasm-opt --pass-arg=set-globals@x=y,z=w"); + + // The input is a set of X=Y pairs separated by commas. + String::Split pairs(input.str, ","); + for (auto& pair : pairs) { + String::Split nameAndValue(pair, "="); + auto name = nameAndValue[0]; + auto value = nameAndValue[1]; + auto* glob = module->getGlobalOrNull(name); + if (!glob) { + std::cerr << "warning: could not find global: " << name << '\n'; + } + // Parse the input. + Literal lit; + if (glob->type == Type::i32) { + lit = Literal(int32_t(stoi(value))); + } else if (glob->type == Type::i64) { + lit = Literal(int64_t(stoll(value))); + } else { + Fatal() << "global's type is not supported: " << name; + } + // The global now has a value, and is not imported. + glob->init = Builder(*module).makeConst(lit); + glob->module = glob->base = Name(); + } + } +}; + +// declare pass + +Pass* createSetGlobalsPass() { return new SetGlobals(); } + +} // namespace wasm diff --git a/src/passes/pass.cpp b/src/passes/pass.cpp index 72c415ed8..7790c3972 100644 --- a/src/passes/pass.cpp +++ b/src/passes/pass.cpp @@ -303,6 +303,9 @@ void PassRegistry::registerPasses() { registerPass("safe-heap", "instrument loads and stores to check for invalid behavior", createSafeHeapPass); + registerPass("set-globals", + "sets specified globals to specified values", + createSetGlobalsPass); registerPass("simplify-globals", "miscellaneous globals-related optimizations", createSimplifyGlobalsPass); diff --git a/src/passes/passes.h b/src/passes/passes.h index 93f986773..79d344f1a 100644 --- a/src/passes/passes.h +++ b/src/passes/passes.h @@ -102,6 +102,7 @@ Pass* createReReloopPass(); Pass* createRedundantSetEliminationPass(); Pass* createRoundTripPass(); Pass* createSafeHeapPass(); +Pass* createSetGlobalsPass(); Pass* createSimplifyLocalsPass(); Pass* createSimplifyGlobalsPass(); Pass* createSimplifyGlobalsOptimizingPass(); |