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 #ifndef RS2SPIRV_CONTEXT_H 18 #define RS2SPIRV_CONTEXT_H 19 20 #include "RSAllocationUtils.h" 21 #include "bcinfo/MetadataExtractor.h" 22 23 #include "llvm/ADT/SmallVector.h" 24 #include "llvm/ADT/StringMap.h" 25 #include "llvm/ADT/StringRef.h" 26 27 #include <stdint.h> 28 29 // Declare a friend relationship in a class with a test. Used rather that 30 // FRIEND_TEST to avoid globally importing gtest/gtest.h into the main 31 // RSoV header files. 32 #ifdef __HOST__ 33 #define RSOV_FRIEND_TEST(test_set_name, individual_test) \ 34 friend class test_set_name##_##individual_test##_Test 35 #else 36 #define RSOV_FRIEND_TEST(test_set_name, individual_test) 37 #endif // __HOST__ 38 39 namespace bcinfo { 40 class MetadataExtractor; 41 } 42 43 namespace llvm { 44 class Module; 45 } 46 47 namespace rs2spirv { 48 49 // A singleton that keeps state during the compilation from RS LLVM bitcode to 50 // SPIR-V, which provides quick lookup of metadata and shares information 51 // between the passes. 52 class Context { 53 RSOV_FRIEND_TEST(ContextTest, testInitialize); 54 55 public: 56 static Context &getInstance(); 57 58 Context(); 59 60 // Initialize the internal data struture such as the slot number lookup table, 61 // etc. 62 bool Initialize(std::unique_ptr<bcinfo::MetadataExtractor> ME); 63 64 // Returns the total number of exported variables getNumExportVar()65 uint32_t getNumExportVar() const { return mExportVarIndices.size(); } 66 67 // Adds the mapping from the slot number of an exported variable to the index 68 // of its field in the global buffer addExportVarIndex(uint32_t slot,uint32_t index)69 void addExportVarIndex(uint32_t slot, uint32_t index) { 70 mExportVarIndices[slot] = index; 71 } 72 73 // Adds the mapping from the name of an exported variable to the index of its 74 // field in the global buffer 75 void addExportVarIndex(const char *varName, uint32_t index); 76 77 // Given the slot number of an exported variable, returns the index of its 78 // field in the global buffer getExportVarIndex(uint32_t slot)79 uint32_t getExportVarIndex(uint32_t slot) const { 80 return mExportVarIndices[slot]; 81 } 82 setGlobalSize(uint64_t size)83 void setGlobalSize(uint64_t size) { 84 mGlobalSize = size; 85 } 86 getGlobalSize()87 uint64_t getGlobalSize() const { 88 return mGlobalSize; 89 } 90 91 // Returns the total number of foreach kernels getNumForEachKernel()92 uint32_t getNumForEachKernel() const { return mForEachNameToSlot.size(); } 93 94 // Checks if a name refers to a foreach kernel function isForEachKernel(llvm::StringRef name)95 bool isForEachKernel(llvm::StringRef name) const { 96 return mForEachNameToSlot.count(name) != 0; 97 } 98 getMetadata()99 const bcinfo::MetadataExtractor &getMetadata() const { return *mMetadata; } 100 getGlobalAllocs()101 llvm::SmallVectorImpl<RSAllocationInfo> &getGlobalAllocs() { 102 return mGlobalAllocs; 103 } 104 105 private: getSlotForExportVar(const char * varName)106 uint32_t getSlotForExportVar(const char *varName) { 107 const llvm::StringRef strVarName(varName); 108 auto it = mVarNameToSlot.find(strVarName); 109 if (it == mVarNameToSlot.end()) { 110 return std::numeric_limits<uint32_t>::max(); 111 } 112 return it->second; 113 } 114 115 bool mInitialized; 116 // RenderScript metadata embedded in the input LLVM Moduel 117 std::unique_ptr<bcinfo::MetadataExtractor> mMetadata; 118 // A map from exported variable names to their slot numbers 119 llvm::StringMap<uint32_t> mVarNameToSlot; 120 // The size of memory needed to store all global variables (static variables) 121 uint64_t mGlobalSize; 122 // A map from exported foreach kernel names to their slot numbers 123 llvm::StringMap<uint32_t> mForEachNameToSlot; 124 // These are the indices for each exported variable in the global buffer 125 llvm::SmallVector<uint32_t, 8> mExportVarIndices; 126 // For Global Allocations; carries global variable -> metadata offset 127 // mapping from an LLVM pass to a SPIRIT pass 128 llvm::SmallVector<RSAllocationInfo, 8> mGlobalAllocs; 129 }; 130 131 } // namespace rs2spirv 132 133 #endif // RS2SPIRV_CONTEXT_H 134