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/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
23
24 #include <limits>
25
26 #define DEBUG_TYPE "rs2spirv-context"
27
28 namespace rs2spirv {
29
getInstance()30 Context &Context::getInstance() {
31 static Context c;
32 return c;
33 }
34
Context()35 Context::Context() : mInitialized(false) {}
36
Initialize(std::unique_ptr<bcinfo::MetadataExtractor> ME)37 bool Context::Initialize(std::unique_ptr<bcinfo::MetadataExtractor> ME) {
38 if (mInitialized) {
39 return true;
40 }
41
42 mMetadata = std::move(ME);
43
44 if (!mMetadata->extract()) {
45 llvm::errs() << "cannot extract metadata\n";
46 return false;
47 }
48
49 const char **varNames = mMetadata->getExportVarNameList();
50 size_t varCount = mMetadata->getExportVarCount();
51 mExportVarIndices.resize(varCount);
52
53 // Builds the lookup table from a variable name to its slot number
54 for (size_t slot = 0; slot < varCount; slot++) {
55 std::string varName(varNames[slot]);
56 mVarNameToSlot.insert(std::make_pair(varName, (uint32_t)slot));
57 }
58
59 const size_t kernelCount = mMetadata->getExportForEachSignatureCount();
60 const char **kernelNames = mMetadata->getExportForEachNameList();
61 for (size_t slot = 0; slot < kernelCount; slot++) {
62 mForEachNameToSlot.insert(std::make_pair(kernelNames[slot], slot));
63 }
64
65 mInitialized = true;
66
67 return true;
68 }
69
addExportVarIndex(const char * varName,uint32_t index)70 void Context::addExportVarIndex(const char *varName, uint32_t index) {
71 DEBUG(llvm::dbgs() << varName << " index=" << index << '\n');
72 const uint32_t slot = getSlotForExportVar(varName);
73 if (slot == std::numeric_limits<uint32_t>::max()) {
74 return;
75 }
76 addExportVarIndex(slot, index);
77 }
78
79
80 } // namespace rs2spirv
81