|
| 1 | +/* |
| 2 | + * Copyright 2025 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 | +// Reorder private types within a single large recursion group to minimize the |
| 19 | +// cumulative size of type indices throughout the module. |
| 20 | +// |
| 21 | + |
| 22 | +#include "ir/module-utils.h" |
| 23 | +#include "ir/type-updating.h" |
| 24 | +#include "pass.h" |
| 25 | +#include "support/insert_ordered.h" |
| 26 | +#include "support/topological_sort.h" |
| 27 | +#include "wasm-type.h" |
| 28 | +#include "wasm.h" |
| 29 | +namespace wasm { |
| 30 | + |
| 31 | +namespace { |
| 32 | + |
| 33 | +struct ReorderingTypeRewriter : GlobalTypeRewriter { |
| 34 | + using InfoMap = InsertOrderedMap<HeapType, ModuleUtils::HeapTypeInfo>; |
| 35 | + |
| 36 | + InfoMap& typeInfo; |
| 37 | + |
| 38 | + // Use a simpler cost calculation so the effects can be seen with smaller test |
| 39 | + // cases. |
| 40 | + bool forTesting; |
| 41 | + |
| 42 | + // Try sorting with several exponential factors applied to the weight |
| 43 | + // contribution from successors, then pick the best result. |
| 44 | + static constexpr float minFactor = 0.0; |
| 45 | + static constexpr float maxFactor = 1.0; |
| 46 | + static constexpr Index numFactors = 21; |
| 47 | + |
| 48 | + ReorderingTypeRewriter(Module& wasm, InfoMap& typeInfo, bool forTesting) |
| 49 | + : GlobalTypeRewriter(wasm), typeInfo(typeInfo), forTesting(forTesting) {} |
| 50 | + |
| 51 | + std::vector<HeapType> getSortedTypes(PredecessorGraph preds) override { |
| 52 | + auto numTypes = preds.size(); |
| 53 | + std::unordered_map<HeapType, Index> indices; |
| 54 | + for (auto& [type, _] : preds) { |
| 55 | + indices[type] = indices.size(); |
| 56 | + } |
| 57 | + // We will use raw type indices in the various sorts. Extract an index-only |
| 58 | + // successor graph and a map from type index to use count. |
| 59 | + TopologicalSort::Graph succs(numTypes); |
| 60 | + std::vector<Index> counts; |
| 61 | + counts.reserve(numTypes); |
| 62 | + for (auto& [type, currPreds] : preds) { |
| 63 | + auto it = typeInfo.find(type); |
| 64 | + assert(it != typeInfo.end()); |
| 65 | + counts.push_back(it->second.useCount); |
| 66 | + for (auto pred : currPreds) { |
| 67 | + succs[indices.at(pred)].push_back(indices.at(type)); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // A successors-first order used to propagate weights from successors to |
| 72 | + // predecessors. |
| 73 | + auto succsFirst = TopologicalSort::sort(succs); |
| 74 | + std::reverse(succsFirst.begin(), succsFirst.end()); |
| 75 | + |
| 76 | + // Try each factor in turn, keeping the best results. |
| 77 | + std::vector<Index> bestSort; |
| 78 | + Index bestCost = 0; |
| 79 | + for (Index factorIndex = 0; factorIndex < numFactors; ++factorIndex) { |
| 80 | + float factor = getFactor(factorIndex); |
| 81 | + |
| 82 | + // Accumulate weights. Start with the use counts for each type, then add |
| 83 | + // the adjusted weight for each successor to the weights of its |
| 84 | + // predecessors. |
| 85 | + std::vector<float> weights(numTypes); |
| 86 | + for (Index i = 0; i < numTypes; ++i) { |
| 87 | + weights[i] = counts[i]; |
| 88 | + } |
| 89 | + for (Index pred : succsFirst) { |
| 90 | + for (Index succ : succs[pred]) { |
| 91 | + weights[pred] += weights[succ] * factor; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + auto sort = TopologicalSort::minSort( |
| 96 | + succs, [&](Index a, Index b) { return weights[a] > weights[b]; }); |
| 97 | + auto cost = getCost(sort, counts); |
| 98 | + if (factorIndex == 0 || cost < bestCost) { |
| 99 | + bestSort = std::move(sort); |
| 100 | + bestCost = cost; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Translate the best sort from indices back to types. |
| 105 | + std::vector<HeapType> result; |
| 106 | + result.reserve(numTypes); |
| 107 | + for (Index i = 0; i < numTypes; ++i) { |
| 108 | + result.push_back(preds[bestSort[i]].first); |
| 109 | + } |
| 110 | + return result; |
| 111 | + } |
| 112 | + |
| 113 | + float getFactor(Index i) { |
| 114 | + return minFactor + (maxFactor - minFactor) * i / (numFactors - 1); |
| 115 | + } |
| 116 | + |
| 117 | + Index getCost(const std::vector<Index>& order, |
| 118 | + const std::vector<Index> counts) { |
| 119 | + // Model the number of usable bits in an LEB byte, but make it much smaller |
| 120 | + // for testing. |
| 121 | + Index bitsPerByte = forTesting ? 1 : 7; |
| 122 | + Index indicesPerByte = 1u << bitsPerByte; |
| 123 | + Index cost = 0; |
| 124 | + Index numBytes = 1; |
| 125 | + Index maxIndex = indicesPerByte; |
| 126 | + for (Index i = 0; i < order.size(); ++i) { |
| 127 | + if (i == maxIndex) { |
| 128 | + ++numBytes; |
| 129 | + maxIndex *= indicesPerByte; |
| 130 | + } |
| 131 | + cost += numBytes * counts[order[i]]; |
| 132 | + } |
| 133 | + return cost; |
| 134 | + } |
| 135 | +}; |
| 136 | + |
| 137 | +struct ReorderTypes : Pass { |
| 138 | + bool forTesting = false; |
| 139 | + |
| 140 | + ReorderTypes(bool forTesting = false) : forTesting(forTesting) {} |
| 141 | + |
| 142 | + void run(Module* module) override { |
| 143 | + if (!module->features.hasGC()) { |
| 144 | + // This pass only does anything with GC types. |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + // See note in RemoveUnusedTypes. |
| 149 | + if (!getPassOptions().closedWorld) { |
| 150 | + Fatal() << "ReorderTypes requires --closed-world"; |
| 151 | + } |
| 152 | + |
| 153 | + // Collect the use counts for each type. |
| 154 | + auto typeInfo = ModuleUtils::collectHeapTypeInfo( |
| 155 | + *module, ModuleUtils::TypeInclusion::BinaryTypes); |
| 156 | + |
| 157 | + ReorderingTypeRewriter(*module, typeInfo, forTesting).update(); |
| 158 | + } |
| 159 | +}; |
| 160 | + |
| 161 | +} // anonymous namespace |
| 162 | + |
| 163 | +Pass* createReorderTypesPass() { return new ReorderTypes(); } |
| 164 | +Pass* createReorderTypesForTestingPass() { return new ReorderTypes(true); } |
| 165 | + |
| 166 | +} // namespace wasm |
0 commit comments