|
| 1 | +/* |
| 2 | + * Copyright 2022 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 | +#include "ir/iteration.h" |
| 18 | +#include "ir/local-structural-dominance.h" |
| 19 | +#include "support/small_vector.h" |
| 20 | + |
| 21 | +namespace wasm { |
| 22 | + |
| 23 | +LocalStructuralDominance::LocalStructuralDominance(Function* func, |
| 24 | + Module& wasm, |
| 25 | + Mode mode) { |
| 26 | + if (!wasm.features.hasReferenceTypes()) { |
| 27 | + // No references, so nothing to look at. |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + bool hasRefVar = false; |
| 32 | + for (auto var : func->vars) { |
| 33 | + if (var.isRef()) { |
| 34 | + hasRefVar = true; |
| 35 | + break; |
| 36 | + } |
| 37 | + } |
| 38 | + if (!hasRefVar) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + if (mode == NonNullableOnly) { |
| 43 | + bool hasNonNullableVar = false; |
| 44 | + for (auto var : func->vars) { |
| 45 | + // Check if we have any non-nullable vars at all. |
| 46 | + if (var.isNonNullable()) { |
| 47 | + hasNonNullableVar = true; |
| 48 | + break; |
| 49 | + } |
| 50 | + } |
| 51 | + if (!hasNonNullableVar) { |
| 52 | + return; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + struct Scanner : public PostWalker<Scanner> { |
| 57 | + std::set<Index>& nonDominatingIndices; |
| 58 | + |
| 59 | + // The locals that have been set, and so at the current time, they |
| 60 | + // structurally dominate. |
| 61 | + std::vector<bool> localsSet; |
| 62 | + |
| 63 | + Scanner(Function* func, Mode mode, std::set<Index>& nonDominatingIndices) |
| 64 | + : nonDominatingIndices(nonDominatingIndices) { |
| 65 | + localsSet.resize(func->getNumLocals()); |
| 66 | + |
| 67 | + // Parameters always dominate. |
| 68 | + for (Index i = 0; i < func->getNumParams(); i++) { |
| 69 | + localsSet[i] = true; |
| 70 | + } |
| 71 | + |
| 72 | + for (Index i = func->getNumParams(); i < func->getNumLocals(); i++) { |
| 73 | + auto type = func->getLocalType(i); |
| 74 | + // Mark locals we don't need to care about as "set". We never do any |
| 75 | + // work for such a local. |
| 76 | + if (!type.isRef() || (mode == NonNullableOnly && type.isNullable())) { |
| 77 | + localsSet[i] = true; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Note that we do not need to start a scope for the function body. |
| 82 | + // Logically there is a scope there, but there is no code after it, so |
| 83 | + // there is nothing to clean up when that scope exits, so we may as well |
| 84 | + // not even create a scope. Just start walking the body now. |
| 85 | + walk(func->body); |
| 86 | + } |
| 87 | + |
| 88 | + using Locals = SmallVector<Index, 5>; |
| 89 | + |
| 90 | + // When we exit a control flow scope, we must undo the locals that it set. |
| 91 | + std::vector<Locals> cleanupStack; |
| 92 | + |
| 93 | + static void doBeginScope(Scanner* self, Expression** currp) { |
| 94 | + self->cleanupStack.emplace_back(); |
| 95 | + } |
| 96 | + |
| 97 | + static void doEndScope(Scanner* self, Expression** currp) { |
| 98 | + for (auto index : self->cleanupStack.back()) { |
| 99 | + assert(self->localsSet[index]); |
| 100 | + self->localsSet[index] = false; |
| 101 | + } |
| 102 | + self->cleanupStack.pop_back(); |
| 103 | + } |
| 104 | + |
| 105 | + static void doLocalSet(Scanner* self, Expression** currp) { |
| 106 | + auto index = (*currp)->cast<LocalSet>()->index; |
| 107 | + if (!self->localsSet[index]) { |
| 108 | + // This local is now set until the end of this scope. |
| 109 | + self->localsSet[index] = true; |
| 110 | + // If we are not in the topmost scope, note this for later cleanup. |
| 111 | + if (!self->cleanupStack.empty()) { |
| 112 | + self->cleanupStack.back().push_back(index); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + static void scan(Scanner* self, Expression** currp) { |
| 118 | + // Use a loop to avoid recursing on the last child - we can just go |
| 119 | + // straight into a loop iteration for it. |
| 120 | + while (1) { |
| 121 | + Expression* curr = *currp; |
| 122 | + |
| 123 | + switch (curr->_id) { |
| 124 | + case Expression::Id::InvalidId: |
| 125 | + WASM_UNREACHABLE("bad id"); |
| 126 | + |
| 127 | + // local.get can just be visited immediately, as it has no children. |
| 128 | + case Expression::Id::LocalGetId: { |
| 129 | + auto index = curr->cast<LocalGet>()->index; |
| 130 | + if (!self->localsSet[index]) { |
| 131 | + self->nonDominatingIndices.insert(index); |
| 132 | + } |
| 133 | + return; |
| 134 | + } |
| 135 | + case Expression::Id::LocalSetId: { |
| 136 | + auto* set = curr->cast<LocalSet>(); |
| 137 | + if (!self->localsSet[set->index]) { |
| 138 | + self->pushTask(doLocalSet, currp); |
| 139 | + } |
| 140 | + // Immediately continue in the loop. |
| 141 | + currp = &set->value; |
| 142 | + continue; |
| 143 | + } |
| 144 | + |
| 145 | + // Control flow structures. |
| 146 | + case Expression::Id::BlockId: { |
| 147 | + auto* block = curr->cast<Block>(); |
| 148 | + // Blocks with no name are never emitted in the binary format, so do |
| 149 | + // not create a scope for them. |
| 150 | + if (block->name.is()) { |
| 151 | + self->pushTask(Scanner::doEndScope, currp); |
| 152 | + } |
| 153 | + auto& list = block->list; |
| 154 | + for (int i = int(list.size()) - 1; i >= 0; i--) { |
| 155 | + self->pushTask(Scanner::scan, &list[i]); |
| 156 | + } |
| 157 | + if (block->name.is()) { |
| 158 | + // Just call the task immediately. |
| 159 | + doBeginScope(self, currp); |
| 160 | + } |
| 161 | + return; |
| 162 | + } |
| 163 | + case Expression::Id::IfId: { |
| 164 | + if (curr->cast<If>()->ifFalse) { |
| 165 | + self->pushTask(Scanner::doEndScope, currp); |
| 166 | + self->maybePushTask(Scanner::scan, &curr->cast<If>()->ifFalse); |
| 167 | + self->pushTask(Scanner::doBeginScope, currp); |
| 168 | + } |
| 169 | + self->pushTask(Scanner::doEndScope, currp); |
| 170 | + self->pushTask(Scanner::scan, &curr->cast<If>()->ifTrue); |
| 171 | + self->pushTask(Scanner::doBeginScope, currp); |
| 172 | + // Immediately continue in the loop. |
| 173 | + currp = &curr->cast<If>()->condition; |
| 174 | + continue; |
| 175 | + } |
| 176 | + case Expression::Id::LoopId: { |
| 177 | + self->pushTask(Scanner::doEndScope, currp); |
| 178 | + // Just call the task immediately. |
| 179 | + doBeginScope(self, currp); |
| 180 | + // Immediately continue in the loop. |
| 181 | + currp = &curr->cast<Loop>()->body; |
| 182 | + continue; |
| 183 | + } |
| 184 | + case Expression::Id::TryId: { |
| 185 | + auto& list = curr->cast<Try>()->catchBodies; |
| 186 | + for (int i = int(list.size()) - 1; i >= 0; i--) { |
| 187 | + self->pushTask(Scanner::doEndScope, currp); |
| 188 | + self->pushTask(Scanner::scan, &list[i]); |
| 189 | + self->pushTask(Scanner::doBeginScope, currp); |
| 190 | + } |
| 191 | + self->pushTask(Scanner::doEndScope, currp); |
| 192 | + // Just call the task immediately. |
| 193 | + doBeginScope(self, currp); |
| 194 | + // Immediately continue in the loop. |
| 195 | + currp = &curr->cast<Try>()->body; |
| 196 | + continue; |
| 197 | + } |
| 198 | + |
| 199 | + default: { |
| 200 | + // Control flow structures have been handled. This is an expression, |
| 201 | + // which we scan normally. |
| 202 | + assert(!Properties::isControlFlowStructure(curr)); |
| 203 | + PostWalker<Scanner>::scan(self, currp); |
| 204 | + return; |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + // Only local.set needs to be visited. |
| 211 | + void pushTask(TaskFunc func, Expression** currp) { |
| 212 | + // Visits to anything but a set can be ignored, so only very specific |
| 213 | + // tasks need to actually be pushed here. In particular, we don't want to |
| 214 | + // push tasks to call doVisit* when those callbacks do nothing. |
| 215 | + if (func == scan || func == doLocalSet || func == doBeginScope || |
| 216 | + func == doEndScope) { |
| 217 | + PostWalker<Scanner>::pushTask(func, currp); |
| 218 | + } |
| 219 | + } |
| 220 | + void maybePushTask(TaskFunc func, Expression** currp) { |
| 221 | + if (*currp) { |
| 222 | + pushTask(func, currp); |
| 223 | + } |
| 224 | + } |
| 225 | + }; |
| 226 | + |
| 227 | + Scanner(func, mode, nonDominatingIndices); |
| 228 | +} |
| 229 | + |
| 230 | +} // namespace wasm |
0 commit comments