|
| 1 | +/* |
| 2 | + * Copyright 2026 WebAssembly Community Group participants |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// |
| 18 | +// Sorts functions by structural similarity. This groups mutually-compressible |
| 19 | +// instruction sequences together, maximizing subsequent compression ratio |
| 20 | +// (e.g., Gzip/Brotli). |
| 21 | +// |
| 22 | + |
| 23 | +#include <algorithm> |
| 24 | +#include <memory> |
| 25 | +#include <vector> |
| 26 | + |
| 27 | +#include "ir/utils.h" |
| 28 | +#include "pass.h" |
| 29 | +#include "support/threads.h" |
| 30 | +#include "wasm.h" |
| 31 | + |
| 32 | +namespace wasm { |
| 33 | + |
| 34 | +// Post-order traversal visitor to extract instruction sequence |
| 35 | +struct OpcodeSequenceBuilder |
| 36 | + : public PostWalker<OpcodeSequenceBuilder, |
| 37 | + UnifiedExpressionVisitor<OpcodeSequenceBuilder>> { |
| 38 | + std::vector<uint32_t> sequence; |
| 39 | + const size_t max_len = 512; |
| 40 | + |
| 41 | + void visitExpression(Expression* curr) { |
| 42 | + if (sequence.size() >= max_len) { |
| 43 | + return; |
| 44 | + } |
| 45 | + // Append the core expression ID |
| 46 | + sequence.push_back(curr->_id); |
| 47 | + |
| 48 | + // Capture important immediate type/operator information |
| 49 | + // TODO: There's probably more data that would be useful to capture. |
| 50 | + if (auto* unary = curr->dynCast<Unary>()) { |
| 51 | + sequence.push_back(unary->op); |
| 52 | + } else if (auto* binary = curr->dynCast<Binary>()) { |
| 53 | + sequence.push_back(binary->op); |
| 54 | + } else if (auto* load = curr->dynCast<Load>()) { |
| 55 | + sequence.push_back(load->bytes); |
| 56 | + sequence.push_back(load->offset); |
| 57 | + } else if (auto* store = curr->dynCast<Store>()) { |
| 58 | + sequence.push_back(store->bytes); |
| 59 | + sequence.push_back(store->offset); |
| 60 | + } else if (auto* localGet = curr->dynCast<LocalGet>()) { |
| 61 | + sequence.push_back(localGet->type.getID()); |
| 62 | + } else if (auto* localSet = curr->dynCast<LocalSet>()) { |
| 63 | + sequence.push_back(localSet->type.getID()); |
| 64 | + } else if (auto* const_ = curr->dynCast<Const>()) { |
| 65 | + sequence.push_back(const_->type.getID()); |
| 66 | + } |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +struct ReorderFunctionsBySimilarity : public Pass { |
| 71 | + bool requiresNonNullableLocalFixups() override { return false; } |
| 72 | + |
| 73 | + void run(Module* module) override { |
| 74 | + // If the number of defined functions is small, similarity-based reordering |
| 75 | + // does not help and can regress size due to breaking natural call |
| 76 | + // proximity. |
| 77 | + size_t numDefined = 0; |
| 78 | + for (const auto& func : module->functions) { |
| 79 | + if (!func->imported()) { |
| 80 | + numDefined++; |
| 81 | + } |
| 82 | + } |
| 83 | + size_t minFunctions = 150; |
| 84 | + auto arg = getArgumentOrDefault("reorder-functions-by-similarity", ""); |
| 85 | + if (!arg.empty()) { |
| 86 | + minFunctions = std::stoul(arg); |
| 87 | + } |
| 88 | + if (numDefined < minFunctions) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + // 1. Separate imported and defined functions |
| 93 | + std::vector<std::unique_ptr<Function>> importedFuncs; |
| 94 | + std::vector<std::unique_ptr<Function>> definedFuncs; |
| 95 | + |
| 96 | + for (auto& func : module->functions) { |
| 97 | + if (func->imported()) { |
| 98 | + importedFuncs.push_back(std::move(func)); |
| 99 | + } else { |
| 100 | + definedFuncs.push_back(std::move(func)); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // 2. Build keys for defined functions in parallel |
| 105 | + struct FunctionSortKey { |
| 106 | + std::unique_ptr<Function> func; |
| 107 | + std::string typeStr; |
| 108 | + std::vector<std::string> varsStrs; |
| 109 | + std::vector<uint32_t> opcodeSequence; |
| 110 | + size_t originalIndex; |
| 111 | + |
| 112 | + bool operator<(const FunctionSortKey& other) const { |
| 113 | + if (typeStr != other.typeStr) { |
| 114 | + return typeStr < other.typeStr; |
| 115 | + } |
| 116 | + if (varsStrs != other.varsStrs) { |
| 117 | + return varsStrs < other.varsStrs; |
| 118 | + } |
| 119 | + if (opcodeSequence != other.opcodeSequence) { |
| 120 | + return opcodeSequence < other.opcodeSequence; |
| 121 | + } |
| 122 | + return originalIndex < other.originalIndex; |
| 123 | + } |
| 124 | + }; |
| 125 | + |
| 126 | + size_t numThreads = ThreadPool::get()->size(); |
| 127 | + std::vector<std::function<ThreadWorkState()>> doWorkers; |
| 128 | + std::atomic<size_t> nextFunction(0); |
| 129 | + size_t numFunctions = definedFuncs.size(); |
| 130 | + |
| 131 | + std::vector<FunctionSortKey> keys(numFunctions); |
| 132 | + |
| 133 | + for (size_t i = 0; i < numThreads; i++) { |
| 134 | + doWorkers.push_back([&]() { |
| 135 | + while (true) { |
| 136 | + auto index = nextFunction.fetch_add(1); |
| 137 | + if (index >= numFunctions) { |
| 138 | + return ThreadWorkState::Finished; |
| 139 | + } |
| 140 | + auto& func = definedFuncs[index]; |
| 141 | + |
| 142 | + FunctionSortKey key; |
| 143 | + key.typeStr = func->type.toString(); |
| 144 | + |
| 145 | + key.varsStrs.reserve(func->vars.size()); |
| 146 | + for (auto var : func->vars) { |
| 147 | + key.varsStrs.push_back(var.toString()); |
| 148 | + } |
| 149 | + |
| 150 | + OpcodeSequenceBuilder builder; |
| 151 | + builder.walk(func->body); |
| 152 | + key.opcodeSequence = std::move(builder.sequence); |
| 153 | + |
| 154 | + key.originalIndex = index; |
| 155 | + key.func = std::move(func); |
| 156 | + keys[index] = std::move(key); |
| 157 | + } |
| 158 | + }); |
| 159 | + } |
| 160 | + ThreadPool::get()->work(doWorkers); |
| 161 | + |
| 162 | + // 3. Sort defined functions by the similarity heuristic |
| 163 | + std::sort(keys.begin(), keys.end()); |
| 164 | + |
| 165 | + // 4. Re-assemble module->functions vector |
| 166 | + module->functions.clear(); |
| 167 | + module->functions.reserve(importedFuncs.size() + keys.size()); |
| 168 | + |
| 169 | + for (auto& func : importedFuncs) { |
| 170 | + module->functions.push_back(std::move(func)); |
| 171 | + } |
| 172 | + for (auto& key : keys) { |
| 173 | + module->functions.push_back(std::move(key.func)); |
| 174 | + } |
| 175 | + } |
| 176 | +}; |
| 177 | + |
| 178 | +Pass* createReorderFunctionsBySimilarityPass() { |
| 179 | + return new ReorderFunctionsBySimilarity(); |
| 180 | +} |
| 181 | + |
| 182 | +} // namespace wasm |
0 commit comments