|
| 1 | +/** |
| 2 | + * Copyright (c) 2017-present, Facebook, Inc. |
| 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 | +#include "glow/Backends/BackendUtils.h" |
| 17 | +#include "glow/IR/Instrs.h" |
| 18 | + |
| 19 | +using namespace glow; |
| 20 | +using llvm::cast; |
| 21 | +using llvm::isa; |
| 22 | + |
| 23 | +uint8_t *glow::collectConstants( |
| 24 | + const IRFunction *F, uint64_t constantMaxSize, |
| 25 | + const std::unordered_map<std::string, runtime::RuntimeSymbolInfo> |
| 26 | + &symbolTable) { |
| 27 | + |
| 28 | + // At compile time condense constants to a single block of memory. |
| 29 | + // This allows the graph to go away after compile time. |
| 30 | + uint8_t *baseConstantWeightVarsStore = |
| 31 | + (uint8_t *)alignedAlloc(constantMaxSize, TensorAlignment); |
| 32 | + for (auto &v : F->getGraph()->getParent()->getConstants()) { |
| 33 | + assert(isa<WeightVar>(F->getWeightForNode(v))); |
| 34 | + auto *w = cast<WeightVar>(F->getWeightForNode(v)); |
| 35 | + auto payload = v->getPayload().getUnsafePtr(); |
| 36 | + auto numBytes = w->getSizeInBytes(); |
| 37 | + auto it = symbolTable.find(std::string(w->getName())); |
| 38 | + assert(it != symbolTable.end() && "Symbol not found."); |
| 39 | + auto addr = it->second.offset; |
| 40 | + // Copy weight to offset. |
| 41 | + memcpy(baseConstantWeightVarsStore + addr, payload, numBytes); |
| 42 | + } |
| 43 | + return baseConstantWeightVarsStore; |
| 44 | +} |
| 45 | + |
| 46 | +/// Helper function, gets offset of \p v from \p symbolTable. |
| 47 | +size_t glow::getValueOffset( |
| 48 | + Value *v, const std::unordered_map<std::string, runtime::RuntimeSymbolInfo> |
| 49 | + &symbolTable) { |
| 50 | + auto it = symbolTable.find(std::string(v->getName())); |
| 51 | + assert(it != symbolTable.end() && "Symbol not found."); |
| 52 | + return it->second.offset; |
| 53 | +} |
0 commit comments