diff options
author | Alon Zakai <azakai@google.com> | 2021-11-19 16:57:31 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-19 16:57:31 -0800 |
commit | 01eccfff22e0dbe806f077eec26ed43f4a85e656 (patch) | |
tree | cbf0538c0ee0bd8f813f0a634ae02820e0425132 /src | |
parent | d532e3a4b02375fe85a2e81be336a181e6bdc10b (diff) | |
download | binaryen-01eccfff22e0dbe806f077eec26ed43f4a85e656.tar.gz binaryen-01eccfff22e0dbe806f077eec26ed43f4a85e656.tar.bz2 binaryen-01eccfff22e0dbe806f077eec26ed43f4a85e656.zip |
[Wasm GC] Signature Refining pass (#4326)
This is fairly short and simple after the recent refactorings. This basically
just finds all uses of each signature/function type, and then sees if it
receives more specific types as params. It then rewrites the types if so.
This just handles arguments so far, and not return types.
This differs from DeadArgumentElimination's refineArguments() in that
that pass modifies each function by itself, changing the type of the
function as needed. That is only valid if the type is not observable, that
is, if the function is called indirectly then DAE ignores it. This pass will
work on the types themselves, so it considers all functions sharing a
type as a whole, and when it upgrades that type it ends up affecting them
all.
This finds optimization opportunities on 4% of the total signature
types in j2wasm. Those lead to some benefits in later opts, but the
effect is not huge.
Diffstat (limited to 'src')
-rw-r--r-- | src/passes/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/passes/SignatureRefining.cpp | 207 | ||||
-rw-r--r-- | src/passes/pass.cpp | 4 | ||||
-rw-r--r-- | src/passes/passes.h | 1 |
4 files changed, 213 insertions, 0 deletions
diff --git a/src/passes/CMakeLists.txt b/src/passes/CMakeLists.txt index 3313637ba..c3f42d41f 100644 --- a/src/passes/CMakeLists.txt +++ b/src/passes/CMakeLists.txt @@ -68,6 +68,7 @@ set(passes_SOURCES RoundTrip.cpp SetGlobals.cpp StackIR.cpp + SignatureRefining.cpp Strip.cpp StripTargetFeatures.cpp RedundantSetElimination.cpp diff --git a/src/passes/SignatureRefining.cpp b/src/passes/SignatureRefining.cpp new file mode 100644 index 000000000..8e8ecfbe2 --- /dev/null +++ b/src/passes/SignatureRefining.cpp @@ -0,0 +1,207 @@ +/* + * 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. + */ + +// +// Apply more specific subtypes to signature/function types where possible. +// +// This differs from DeadArgumentElimination's refineArgumentTypes() etc. in +// that DAE will modify the type of a function. It can only do that if the +// function's type is not observable, which means it is not taken by reference. +// On the other hand, this pass will modify the signature types themselves, +// which means it can optimize functions whose reference is taken, and it does +// so while considering all users of the type (across all functions sharing that +// type, and all call_refs using it). +// +// TODO: optimize results too and not just params. +// + +#include "ir/find_all.h" +#include "ir/lubs.h" +#include "ir/module-utils.h" +#include "ir/type-updating.h" +#include "pass.h" +#include "wasm-type.h" +#include "wasm.h" + +using namespace std; + +namespace wasm { + +namespace { + +struct SignatureRefining : public Pass { + // Maps each heap type to the possible refinement of the types in their + // signatures. We will fill this during analysis and then use it while doing + // an update of the types. If a type has no improvement that we can find, it + // will not appear in this map. + std::unordered_map<HeapType, Signature> newSignatures; + + void run(PassRunner* runner, Module* module) override { + if (getTypeSystem() != TypeSystem::Nominal) { + Fatal() << "SignatureRefining requires nominal typing"; + } + + if (!module->tables.empty()) { + // When there are tables we must also take their types into account, which + // would require us to take call_indirect, element segments, etc. into + // account. For now, do nothing if there are tables. + // TODO + return; + } + + // First, find all the calls and call_refs. + + struct CallInfo { + std::vector<Call*> calls; + std::vector<CallRef*> callRefs; + }; + + ModuleUtils::ParallelFunctionAnalysis<CallInfo> analysis( + *module, [&](Function* func, CallInfo& info) { + if (func->imported()) { + return; + } + info.calls = std::move(FindAll<Call>(func->body).list); + info.callRefs = std::move(FindAll<CallRef>(func->body).list); + }); + + // A map of types to the calls and call_refs that use that type. + std::unordered_map<HeapType, CallInfo> allCallsTo; + + // Combine all the information we gathered into that map. + for (auto& [func, info] : analysis.map) { + // For direct calls, add each call to the type of the function being + // called. + for (auto* call : info.calls) { + allCallsTo[module->getFunction(call->target)->type].calls.push_back( + call); + } + + // For indirect calls, add each call_ref to the type the call_ref uses. + for (auto* callRef : info.callRefs) { + auto calledType = callRef->target->type; + if (calledType != Type::unreachable) { + allCallsTo[calledType.getHeapType()].callRefs.push_back(callRef); + } + } + } + + // Compute optimal LUBs. + std::unordered_set<HeapType> seen; + for (auto& func : module->functions) { + auto type = func->type; + if (!seen.insert(type).second) { + continue; + } + + auto sig = type.getSignature(); + + auto numParams = sig.params.size(); + std::vector<LUBFinder> paramLUBs(numParams); + + auto updateLUBs = [&](const ExpressionList& operands) { + for (Index i = 0; i < numParams; i++) { + paramLUBs[i].noteUpdatableExpression(operands[i]); + } + }; + + auto& callsTo = allCallsTo[type]; + for (auto* call : callsTo.calls) { + updateLUBs(call->operands); + } + for (auto* callRef : callsTo.callRefs) { + updateLUBs(callRef->operands); + } + + // Find the final LUBs, and see if we found an improvement. + std::vector<Type> newParamsTypes; + for (auto& lub : paramLUBs) { + if (!lub.noted()) { + break; + } + newParamsTypes.push_back(lub.getBestPossible()); + } + if (newParamsTypes.size() < numParams) { + // We did not have type information to calculate a LUB (no calls, or + // some param is always unreachable), so there is nothing we can improve + // here. Other passes might remove the type entirely. + continue; + } + auto newParams = Type(newParamsTypes); + if (newParams != func->getParams()) { + // We found an improvement! + newSignatures[type] = Signature(newParams, Type::none); + for (auto& lub : paramLUBs) { + lub.updateNulls(); + } + } + } + + if (newSignatures.empty()) { + // We found nothing to optimize. + return; + } + + // Update function contents for their new parameter types. + struct CodeUpdater : public WalkerPass<PostWalker<CodeUpdater>> { + bool isFunctionParallel() override { return true; } + + SignatureRefining& parent; + Module& wasm; + + CodeUpdater(SignatureRefining& parent, Module& wasm) + : parent(parent), wasm(wasm) {} + + CodeUpdater* create() override { return new CodeUpdater(parent, wasm); } + + void doWalkFunction(Function* func) { + auto iter = parent.newSignatures.find(func->type); + if (iter != parent.newSignatures.end()) { + std::vector<Type> newParamsTypes; + for (auto param : iter->second.params) { + newParamsTypes.push_back(param); + } + TypeUpdating::updateParamTypes(func, newParamsTypes, wasm); + } + } + }; + CodeUpdater(*this, *module).run(runner, module); + + // Rewrite the types. + class TypeRewriter : public GlobalTypeRewriter { + SignatureRefining& parent; + + public: + TypeRewriter(Module& wasm, SignatureRefining& parent) + : GlobalTypeRewriter(wasm), parent(parent) {} + + void modifySignature(HeapType oldSignatureType, Signature& sig) override { + auto iter = parent.newSignatures.find(oldSignatureType); + if (iter != parent.newSignatures.end()) { + sig.params = getTempType(iter->second.params); + } + } + }; + + TypeRewriter(*module, *this).update(); + } +}; + +} // anonymous namespace + +Pass* createSignatureRefiningPass() { return new SignatureRefining(); } + +} // namespace wasm diff --git a/src/passes/pass.cpp b/src/passes/pass.cpp index d20a5edcd..92206deff 100644 --- a/src/passes/pass.cpp +++ b/src/passes/pass.cpp @@ -342,6 +342,9 @@ void PassRegistry::registerPasses() { registerPass("set-globals", "sets specified globals to specified values", createSetGlobalsPass); + registerPass("signature-refining", + "apply more specific subtypes to signature types where possible", + createSignatureRefiningPass); registerPass("simplify-globals", "miscellaneous globals-related optimizations", createSimplifyGlobalsPass); @@ -530,6 +533,7 @@ void PassRunner::addDefaultGlobalOptimizationPrePasses() { if (wasm->features.hasGC() && getTypeSystem() == TypeSystem::Nominal && options.optimizeLevel >= 2) { addIfNoDWARFIssues("type-refining"); + addIfNoDWARFIssues("signature-refining"); addIfNoDWARFIssues("global-refining"); // Global type optimization can remove fields that are not needed, which can // remove ref.funcs that were once assigned to vtables but are no longer diff --git a/src/passes/passes.h b/src/passes/passes.h index 5a67b1d82..8a2c4cacc 100644 --- a/src/passes/passes.h +++ b/src/passes/passes.h @@ -112,6 +112,7 @@ Pass* createRedundantSetEliminationPass(); Pass* createRoundTripPass(); Pass* createSafeHeapPass(); Pass* createSetGlobalsPass(); +Pass* createSignatureRefiningPass(); Pass* createSimplifyLocalsPass(); Pass* createSimplifyGlobalsPass(); Pass* createSimplifyGlobalsOptimizingPass(); |