|
| 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 | +#include <wasm.h> |
| 18 | +#include <pass.h> |
| 19 | +#include <wasm-builder.h> |
| 20 | + |
| 21 | +namespace wasm { |
| 22 | + |
| 23 | +// Adding segments adds overhead, this is a rough estimate |
| 24 | +const Index OVERHEAD = 8; |
| 25 | + |
| 26 | +struct MemoryPacking : public Pass { |
| 27 | + void run(PassRunner* runner, Module* module) override { |
| 28 | + if (!module->memory.exists) return; |
| 29 | + std::vector<Memory::Segment> packed; |
| 30 | + for (auto& segment : module->memory.segments) { |
| 31 | + // skip final zeros |
| 32 | + while (segment.data.size() > 0 && segment.data.back() == 0) { |
| 33 | + segment.data.pop_back(); |
| 34 | + } |
| 35 | + // we can only handle a constant offset for splitting |
| 36 | + if (auto* offset = segment.offset->dynCast<Const>()) { |
| 37 | + // Find runs of zeros, and split |
| 38 | + auto& data = segment.data; |
| 39 | + auto base = offset->value.geti32(); |
| 40 | + Index start = 0; |
| 41 | + // create new segments |
| 42 | + while (start < data.size()) { |
| 43 | + // skip initial zeros |
| 44 | + while (start < data.size() && data[start] == 0) { |
| 45 | + start++; |
| 46 | + } |
| 47 | + Index end = start; // end of data-containing part |
| 48 | + Index next = end; // after zeros we can skip. preserves next >= end |
| 49 | + while (next < data.size() && (next - end < OVERHEAD)) { |
| 50 | + if (data[end] != 0) { |
| 51 | + end++; |
| 52 | + next = end; // we can try to skip zeros from here |
| 53 | + } else { |
| 54 | + // end is on a zero, we are looking to skip |
| 55 | + if (data[next] != 0) { |
| 56 | + end = next; // we must extend the segment, including some zeros |
| 57 | + } else { |
| 58 | + next++; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + if (end != start) { |
| 63 | + packed.emplace_back(Builder(*module).makeConst(Literal(int32_t(base + start))), &data[start], end - start); |
| 64 | + } |
| 65 | + start = next; |
| 66 | + } |
| 67 | + } else { |
| 68 | + packed.push_back(segment); |
| 69 | + } |
| 70 | + } |
| 71 | + module->memory.segments.swap(packed); |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +Pass *createMemoryPackingPass() { |
| 76 | + return new MemoryPacking(); |
| 77 | +} |
| 78 | + |
| 79 | +} // namespace wasm |
| 80 | + |
0 commit comments