1/* 2 * Copyright 2017, The Android Open Source Project 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 "Context.h" 18 19#include "llvm/ADT/Triple.h" 20#include "llvm/IR/Module.h" 21#include "llvm/Support/raw_ostream.h" 22 23namespace rs2spirv { 24 25Context &Context::getInstance() { 26 static Context c; 27 return c; 28} 29 30Context::Context() : mInitialized(false) {} 31 32bool Context::Initialize(std::unique_ptr<bcinfo::MetadataExtractor> ME) { 33 if (mInitialized) { 34 return true; 35 } 36 37 mMetadata = std::move(ME); 38 39 if (!mMetadata->extract()) { 40 llvm::errs() << "cannot extract metadata\n"; 41 return false; 42 } 43 44 const char **varNames = mMetadata->getExportVarNameList(); 45 size_t varCount = mMetadata->getExportVarCount(); 46 mExportVarIndices.resize(varCount); 47 48 // Builds the lookup table from a variable name to its slot number 49 for (size_t slot = 0; slot < varCount; slot++) { 50 std::string varName(varNames[slot]); 51 mVarNameToSlot.insert(std::make_pair(varName, (uint32_t)slot)); 52 } 53 54 const size_t kernelCount = mMetadata->getExportForEachSignatureCount(); 55 const char **kernelNames = mMetadata->getExportForEachNameList(); 56 for (size_t slot = 0; slot < kernelCount; slot++) { 57 mForEachNameToSlot.insert(std::make_pair(kernelNames[slot], slot)); 58 } 59 60 mInitialized = true; 61 62 return true; 63} 64 65} // namespace rs2spirv 66