|
| 1 | +/* |
| 2 | + * Copyright 2017 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 | +// Spills values that might be pointers to the C stack. This allows |
| 19 | +// Boehm-style GC to see them properly. |
| 20 | +// |
| 21 | +// To reduce the overhead of the extra operations added here, you |
| 22 | +// should probably run optimizations after doing it. |
| 23 | +// TODO: add a dead store elimination pass, which would help here |
| 24 | +// |
| 25 | +// * There is currently no check that there is enough stack space. |
| 26 | +// |
| 27 | + |
| 28 | +#include "abi/stack.h" |
| 29 | +#include "cfg/liveness-traversal.h" |
| 30 | +#include "pass.h" |
| 31 | +#include "wasm-builder.h" |
| 32 | +#include "wasm.h" |
| 33 | + |
| 34 | +namespace wasm { |
| 35 | + |
| 36 | +struct SpillPointers |
| 37 | + : public WalkerPass<LivenessWalker<SpillPointers, Visitor<SpillPointers>>> { |
| 38 | + bool isFunctionParallel() override { return true; } |
| 39 | + |
| 40 | + Pass* create() override { return new SpillPointers; } |
| 41 | + |
| 42 | + // a mapping of the pointers to all the spillable things. We need to know |
| 43 | + // how to replace them, and as we spill we may modify them. This map |
| 44 | + // gives us, for an Expression** seen during the walk (and placed in the |
| 45 | + // basic block, which is what we iterate on for efficiency) => the |
| 46 | + // current actual pointer, which may have moded |
| 47 | + std::unordered_map<Expression**, Expression**> actualPointers; |
| 48 | + |
| 49 | + // note calls in basic blocks |
| 50 | + template<typename T> void visitSpillable(T* curr) { |
| 51 | + // if in unreachable code, ignore |
| 52 | + if (!currBasicBlock) { |
| 53 | + return; |
| 54 | + } |
| 55 | + auto* pointer = getCurrentPointer(); |
| 56 | + currBasicBlock->contents.actions.emplace_back(pointer); |
| 57 | + // starts out as correct, may change later |
| 58 | + actualPointers[pointer] = pointer; |
| 59 | + } |
| 60 | + |
| 61 | + void visitCall(Call* curr) { visitSpillable(curr); } |
| 62 | + void visitCallIndirect(CallIndirect* curr) { visitSpillable(curr); } |
| 63 | + |
| 64 | + // main entry point |
| 65 | + |
| 66 | + void doWalkFunction(Function* func) { |
| 67 | + super::doWalkFunction(func); |
| 68 | + spillPointers(); |
| 69 | + } |
| 70 | + |
| 71 | + // map pointers to their offset in the spill area |
| 72 | + typedef std::unordered_map<Index, Index> PointerMap; |
| 73 | + |
| 74 | + Type pointerType; |
| 75 | + |
| 76 | + void spillPointers() { |
| 77 | + pointerType = getModule()->memory.indexType; |
| 78 | + |
| 79 | + // we only care about possible pointers |
| 80 | + auto* func = getFunction(); |
| 81 | + PointerMap pointerMap; |
| 82 | + for (Index i = 0; i < func->getNumLocals(); i++) { |
| 83 | + if (func->getLocalType(i) == pointerType) { |
| 84 | + auto offset = pointerMap.size() * pointerType.getByteSize(); |
| 85 | + pointerMap[i] = offset; |
| 86 | + } |
| 87 | + } |
| 88 | + // find calls and spill around them |
| 89 | + bool spilled = false; |
| 90 | + Index spillLocal = -1; |
| 91 | + for (auto& curr : basicBlocks) { |
| 92 | + if (liveBlocks.count(curr.get()) == 0) { |
| 93 | + continue; // ignore dead blocks |
| 94 | + } |
| 95 | + auto& liveness = curr->contents; |
| 96 | + auto& actions = liveness.actions; |
| 97 | + Index lastCall = -1; |
| 98 | + for (Index i = 0; i < actions.size(); i++) { |
| 99 | + auto& action = liveness.actions[i]; |
| 100 | + if (action.isOther()) { |
| 101 | + lastCall = i; |
| 102 | + } |
| 103 | + } |
| 104 | + if (lastCall == Index(-1)) { |
| 105 | + continue; // nothing to see here |
| 106 | + } |
| 107 | + // scan through the block, spilling around the calls |
| 108 | + // TODO: we can filter on pointerMap everywhere |
| 109 | + SetOfLocals live = liveness.end; |
| 110 | + for (int i = int(actions.size()) - 1; i >= 0; i--) { |
| 111 | + auto& action = actions[i]; |
| 112 | + if (action.isGet()) { |
| 113 | + live.insert(action.index); |
| 114 | + } else if (action.isSet()) { |
| 115 | + live.erase(action.index); |
| 116 | + } else if (action.isOther()) { |
| 117 | + std::vector<Index> toSpill; |
| 118 | + for (auto index : live) { |
| 119 | + if (pointerMap.count(index) > 0) { |
| 120 | + toSpill.push_back(index); |
| 121 | + } |
| 122 | + } |
| 123 | + if (!toSpill.empty()) { |
| 124 | + // we now have a call + the information about which locals |
| 125 | + // should be spilled |
| 126 | + if (!spilled) { |
| 127 | + // prepare stack support: get a pointer to stack space big enough |
| 128 | + // for all our data |
| 129 | + spillLocal = Builder::addVar(func, pointerType); |
| 130 | + spilled = true; |
| 131 | + } |
| 132 | + // the origin was seen at walk, but the thing may have moved |
| 133 | + auto* pointer = actualPointers[action.origin]; |
| 134 | + spillPointersAroundCall( |
| 135 | + pointer, toSpill, spillLocal, pointerMap, func, getModule()); |
| 136 | + } |
| 137 | + } else { |
| 138 | + WASM_UNREACHABLE("unexpected action"); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + if (spilled) { |
| 143 | + // get the stack space, and set the local to it |
| 144 | + ABI::getStackSpace(spillLocal, |
| 145 | + func, |
| 146 | + pointerType.getByteSize() * pointerMap.size(), |
| 147 | + *getModule()); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + void spillPointersAroundCall(Expression** origin, |
| 152 | + std::vector<Index>& toSpill, |
| 153 | + Index spillLocal, |
| 154 | + PointerMap& pointerMap, |
| 155 | + Function* func, |
| 156 | + Module* module) { |
| 157 | + auto* call = *origin; |
| 158 | + if (call->type == Type::unreachable) { |
| 159 | + return; // the call is never reached anyhow, ignore |
| 160 | + } |
| 161 | + Builder builder(*module); |
| 162 | + auto* block = builder.makeBlock(); |
| 163 | + // move the operands into locals, as we must spill after they are executed |
| 164 | + auto handleOperand = [&](Expression*& operand) { |
| 165 | + auto temp = builder.addVar(func, operand->type); |
| 166 | + auto* set = builder.makeLocalSet(temp, operand); |
| 167 | + block->list.push_back(set); |
| 168 | + block->finalize(); |
| 169 | + if (actualPointers.count(&operand) > 0) { |
| 170 | + // this is something we track, and it's moving - update |
| 171 | + actualPointers[&operand] = &set->value; |
| 172 | + } |
| 173 | + operand = builder.makeLocalGet(temp, operand->type); |
| 174 | + }; |
| 175 | + if (call->is<Call>()) { |
| 176 | + for (auto*& operand : call->cast<Call>()->operands) { |
| 177 | + handleOperand(operand); |
| 178 | + } |
| 179 | + } else if (call->is<CallIndirect>()) { |
| 180 | + for (auto*& operand : call->cast<CallIndirect>()->operands) { |
| 181 | + handleOperand(operand); |
| 182 | + } |
| 183 | + handleOperand(call->cast<CallIndirect>()->target); |
| 184 | + } else { |
| 185 | + WASM_UNREACHABLE("unexpected expr"); |
| 186 | + } |
| 187 | + // add the spills |
| 188 | + for (auto index : toSpill) { |
| 189 | + block->list.push_back( |
| 190 | + builder.makeStore(pointerType.getByteSize(), |
| 191 | + pointerMap[index], |
| 192 | + pointerType.getByteSize(), |
| 193 | + builder.makeLocalGet(spillLocal, pointerType), |
| 194 | + builder.makeLocalGet(index, pointerType), |
| 195 | + pointerType)); |
| 196 | + } |
| 197 | + // add the (modified) call |
| 198 | + block->list.push_back(call); |
| 199 | + block->finalize(); |
| 200 | + *origin = block; |
| 201 | + } |
| 202 | +}; |
| 203 | + |
| 204 | +Pass* createSpillPointersPass() { return new SpillPointers(); } |
| 205 | + |
| 206 | +} // namespace wasm |
0 commit comments