|
| 1 | +/* |
| 2 | + * Copyright 2016 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 | +// Merges locals when it is beneficial to do so. |
| 19 | +// |
| 20 | +// An obvious case is when locals are copied. In that case, two locals have the |
| 21 | +// same value in a range, and we can pick which of the two to use. For |
| 22 | +// example, in |
| 23 | +// |
| 24 | +// (if (result i32) |
| 25 | +// (tee_local $x |
| 26 | +// (get_local $y) |
| 27 | +// ) |
| 28 | +// (i32.const 100) |
| 29 | +// (get_local $x) |
| 30 | +// ) |
| 31 | +// |
| 32 | +// If that assignment of $y is never used again, everything is fine. But if |
| 33 | +// if is, then the live range of $y does not end in that get, and will |
| 34 | +// necessarily overlap with that of $x - making them appear to interfere |
| 35 | +// with each other in coalesce-locals, even though the value is identical. |
| 36 | +// |
| 37 | +// To fix that, we replace uses of $y with uses of $x. This extends $x's |
| 38 | +// live range and shrinks $y's live range. This tradeoff is not always good, |
| 39 | +// but $x and $y definitely overlap already, so trying to shrink the overlap |
| 40 | +// makes sense - if we remove the overlap entirely, we may be able to let |
| 41 | +// $x and $y be coalesced later. |
| 42 | +// |
| 43 | +// If we can remove only some of $y's uses, then we are definitely not |
| 44 | +// removing the overlap, and they do conflict. In that case, it's not clear |
| 45 | +// if this is beneficial or not, and we don't do it for now |
| 46 | +// TODO: investigate more |
| 47 | +// |
| 48 | + |
| 49 | +#include <wasm.h> |
| 50 | +#include <pass.h> |
| 51 | +#include <wasm-builder.h> |
| 52 | +#include <ir/local-graph.h> |
| 53 | + |
| 54 | +namespace wasm { |
| 55 | + |
| 56 | +struct MergeLocals : public WalkerPass<PostWalker<MergeLocals, UnifiedExpressionVisitor<MergeLocals>>> { |
| 57 | + bool isFunctionParallel() override { return true; } |
| 58 | + |
| 59 | + Pass* create() override { return new MergeLocals(); } |
| 60 | + |
| 61 | + void doWalkFunction(Function* func) { |
| 62 | + // first, instrument the graph by modifying each copy |
| 63 | + // (set_local $x |
| 64 | + // (get_local $y) |
| 65 | + // ) |
| 66 | + // to |
| 67 | + // (set_local $x |
| 68 | + // (tee_local $y |
| 69 | + // (get_local $y) |
| 70 | + // ) |
| 71 | + // ) |
| 72 | + // That is, we add a trivial assign of $y. This ensures we |
| 73 | + // have a new assignment of $y at the location of the copy, |
| 74 | + // which makes it easy for us to see if the value if $y |
| 75 | + // is still used after that point |
| 76 | + super::doWalkFunction(func); |
| 77 | + |
| 78 | + // optimize the copies, merging when we can, and removing |
| 79 | + // the trivial assigns we added temporarily |
| 80 | + optimizeCopies(); |
| 81 | + } |
| 82 | + |
| 83 | + std::vector<SetLocal*> copies; |
| 84 | + |
| 85 | + void visitSetLocal(SetLocal* curr) { |
| 86 | + if (auto* get = curr->value->dynCast<GetLocal>()) { |
| 87 | + if (get->index != curr->index) { |
| 88 | + Builder builder(*getModule()); |
| 89 | + auto* trivial = builder.makeTeeLocal(get->index, get); |
| 90 | + curr->value = trivial; |
| 91 | + copies.push_back(curr); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + void optimizeCopies() { |
| 97 | + if (copies.empty()) return; |
| 98 | + // compute all dependencies |
| 99 | + LocalGraph preGraph(getFunction(), getModule()); |
| 100 | + preGraph.computeInfluences(); |
| 101 | + // optimize each copy |
| 102 | + std::unordered_map<SetLocal*, SetLocal*> optimizedToCopy, optimizedToTrivial; |
| 103 | + for (auto* copy : copies) { |
| 104 | + auto* trivial = copy->value->cast<SetLocal>(); |
| 105 | + bool canOptimizeToCopy = false; |
| 106 | + auto& trivialInfluences = preGraph.setInfluences[trivial]; |
| 107 | + if (!trivialInfluences.empty()) { |
| 108 | + canOptimizeToCopy = true; |
| 109 | + for (auto* influencedGet : trivialInfluences) { |
| 110 | + // this get uses the trivial write, so it uses the value in the copy. |
| 111 | + // however, it may depend on other writes too, if there is a merge/phi, |
| 112 | + // and in that case we can't do anything |
| 113 | + assert(influencedGet->index == trivial->index); |
| 114 | + if (preGraph.getSetses[influencedGet].size() == 1) { |
| 115 | + // this is ok |
| 116 | + assert(*preGraph.getSetses[influencedGet].begin() == trivial); |
| 117 | + } else { |
| 118 | + canOptimizeToCopy = false; |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + if (canOptimizeToCopy) { |
| 124 | + // worth it for this copy, do it |
| 125 | + for (auto* influencedGet : trivialInfluences) { |
| 126 | + influencedGet->index = copy->index; |
| 127 | + } |
| 128 | + optimizedToCopy[copy] = trivial; |
| 129 | + } else { |
| 130 | + // alternatively, we can try to remove the conflict in the opposite way: given |
| 131 | + // (set_local $x |
| 132 | + // (get_local $y) |
| 133 | + // ) |
| 134 | + // we can look for uses of $x that could instead be uses of $y. this extends |
| 135 | + // $y's live range, but if it removes the conflict between $x and $y, it may be |
| 136 | + // worth it |
| 137 | + if (!trivialInfluences.empty()) { // if the trivial set we added has influences, it means $y lives on |
| 138 | + auto& copyInfluences = preGraph.setInfluences[copy]; |
| 139 | + if (!copyInfluences.empty()) { |
| 140 | + bool canOptimizeToTrivial = true; |
| 141 | + for (auto* influencedGet : copyInfluences) { |
| 142 | + // as above, avoid merges/phis |
| 143 | + assert(influencedGet->index == copy->index); |
| 144 | + if (preGraph.getSetses[influencedGet].size() == 1) { |
| 145 | + // this is ok |
| 146 | + assert(*preGraph.getSetses[influencedGet].begin() == copy); |
| 147 | + } else { |
| 148 | + canOptimizeToTrivial = false; |
| 149 | + break; |
| 150 | + } |
| 151 | + } |
| 152 | + if (canOptimizeToTrivial) { |
| 153 | + // worth it for this copy, do it |
| 154 | + for (auto* influencedGet : copyInfluences) { |
| 155 | + influencedGet->index = trivial->index; |
| 156 | + } |
| 157 | + optimizedToTrivial[copy] = trivial; |
| 158 | + // note that we don't |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + if (!optimizedToCopy.empty() || !optimizedToTrivial.empty()) { |
| 165 | + // finally, we need to verify that the changes work properly, that is, |
| 166 | + // they use the value from the right place (and are not affected by |
| 167 | + // another set of the index we changed to). |
| 168 | + // if one does not work, we need to undo all its siblings (don't extend |
| 169 | + // the live range unless we are definitely removing a conflict, same |
| 170 | + // logic as before). |
| 171 | + LocalGraph postGraph(getFunction(), getModule()); |
| 172 | + postGraph.computeInfluences(); |
| 173 | + for (auto& pair : optimizedToCopy) { |
| 174 | + auto* copy = pair.first; |
| 175 | + auto* trivial = pair.second; |
| 176 | + auto& trivialInfluences = preGraph.setInfluences[trivial]; |
| 177 | + for (auto* influencedGet : trivialInfluences) { |
| 178 | + // verify the set |
| 179 | + auto& sets = postGraph.getSetses[influencedGet]; |
| 180 | + if (sets.size() != 1 || *sets.begin() != copy) { |
| 181 | + // not good, undo all the changes for this copy |
| 182 | + for (auto* undo : trivialInfluences) { |
| 183 | + undo->index = trivial->index; |
| 184 | + } |
| 185 | + break; |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + for (auto& pair : optimizedToTrivial) { |
| 190 | + auto* copy = pair.first; |
| 191 | + auto* trivial = pair.second; |
| 192 | + auto& copyInfluences = preGraph.setInfluences[copy]; |
| 193 | + for (auto* influencedGet : copyInfluences) { |
| 194 | + // verify the set |
| 195 | + auto& sets = postGraph.getSetses[influencedGet]; |
| 196 | + if (sets.size() != 1 || *sets.begin() != trivial) { |
| 197 | + // not good, undo all the changes for this copy |
| 198 | + for (auto* undo : copyInfluences) { |
| 199 | + undo->index = copy->index; |
| 200 | + } |
| 201 | + break; |
| 202 | + } |
| 203 | + } |
| 204 | + // if this change was ok, we can probably remove the copy itself, |
| 205 | + // but we leave that for other passes |
| 206 | + } |
| 207 | + } |
| 208 | + // remove the trivial sets |
| 209 | + for (auto* copy : copies) { |
| 210 | + copy->value = copy->value->cast<SetLocal>()->value; |
| 211 | + } |
| 212 | + } |
| 213 | +}; |
| 214 | + |
| 215 | +Pass *createMergeLocalsPass() { |
| 216 | + return new MergeLocals(); |
| 217 | +} |
| 218 | + |
| 219 | +} // namespace wasm |
| 220 | + |
0 commit comments