blob: cc86accde2bde2966f924efa496ac17cb0e35647 [file] [log] [blame]
Yang Nieb654872017-02-21 16:40:01 -08001/*
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
I-Jui (Ray) Sungc2f647a2017-03-03 15:50:09 -080020#include "RSAllocationUtils.h"
Yang Nieb654872017-02-21 16:40:01 -080021#include "bcinfo/MetadataExtractor.h"
22
23#include "llvm/ADT/StringMap.h"
24#include "llvm/ADT/StringRef.h"
25
Yang Nieb654872017-02-21 16:40:01 -080026#include <limits>
I-Jui (Ray) Sungbbf0b0e2017-03-27 15:33:22 -070027#include <stdint.h>
Yang Nieb654872017-02-21 16:40:01 -080028#include <vector>
29
30// Declare a friend relationship in a class with a test. Used rather that
31// FRIEND_TEST to avoid globally importing gtest/gtest.h into the main
32// RSoV header files.
33#ifdef __HOST__
I-Jui (Ray) Sungbbf0b0e2017-03-27 15:33:22 -070034#define RSOV_FRIEND_TEST(test_set_name, individual_test) \
35 friend class test_set_name##_##individual_test##_Test
Yang Nieb654872017-02-21 16:40:01 -080036#else
37#define RSOV_FRIEND_TEST(test_set_name, individual_test)
I-Jui (Ray) Sungbbf0b0e2017-03-27 15:33:22 -070038#endif // __HOST__
Yang Nieb654872017-02-21 16:40:01 -080039
40namespace bcinfo {
41class MetadataExtractor;
42}
43
44namespace llvm {
45class Module;
46}
47
48namespace rs2spirv {
49
50// A singleton that keeps state during the compilation from RS LLVM bitcode to
51// SPIR-V, which provides quick lookup of metadata and shares information
52// between the passes.
53class Context {
54 RSOV_FRIEND_TEST(ContextTest, testInitialize);
55
56public:
57 static Context &getInstance();
58
59 Context();
60
61 // Initialize the internal data struture such as the slot number lookup table,
62 // etc.
I-Jui (Ray) Sungbbf0b0e2017-03-27 15:33:22 -070063 bool Initialize(std::unique_ptr<bcinfo::MetadataExtractor> ME);
Yang Nieb654872017-02-21 16:40:01 -080064
65 // Returns the total number of exported variables
I-Jui (Ray) Sungbbf0b0e2017-03-27 15:33:22 -070066 uint32_t getNumExportVar() const { return mExportVarIndices.size(); }
Yang Nieb654872017-02-21 16:40:01 -080067
68 // Adds the mapping from the slot number of an exported variable to the index
69 // of its field in the global buffer
70 void addExportVarIndex(uint32_t slot, uint32_t index) {
71 mExportVarIndices[slot] = index;
72 }
73
74 // Adds the mapping from the name of an exported variable to the index of its
75 // field in the global buffer
76 void addExportVarIndex(const char *varName, uint32_t index) {
77 const uint32_t slot = getSlotForExportVar(varName);
78 if (slot == std::numeric_limits<uint32_t>::max()) {
79 assert(0 && "Invalid name for an exported variable");
80 return;
81 }
82 addExportVarIndex(slot, index);
83 }
84
85 // Given the slot number of an exported variable, returns the index of its
86 // field in the global buffer
87 uint32_t getExportVarIndex(uint32_t slot) const {
88 return mExportVarIndices[slot];
89 }
90
91 // Returns the total number of foreach kernels
I-Jui (Ray) Sungbbf0b0e2017-03-27 15:33:22 -070092 uint32_t getNumForEachKernel() const { return mForEachNameToSlot.size(); }
Yang Nieb654872017-02-21 16:40:01 -080093
94 // Checks if a name refers to a foreach kernel function
95 bool isForEachKernel(llvm::StringRef name) const {
96 return mForEachNameToSlot.count(name) != 0;
97 }
98
I-Jui (Ray) Sungbbf0b0e2017-03-27 15:33:22 -070099 const bcinfo::MetadataExtractor &getMetadata() const { return *mMetadata; }
Yang Nieb654872017-02-21 16:40:01 -0800100
I-Jui (Ray) Sungc2f647a2017-03-03 15:50:09 -0800101 llvm::SmallVectorImpl<RSAllocationInfo> &getGlobalAllocs() {
102 return mGlobalAllocs;
103 }
104
Yang Nieb654872017-02-21 16:40:01 -0800105private:
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 // A map from exported foreach kernel names to their slot numbers
121 llvm::StringMap<uint32_t> mForEachNameToSlot;
122 // These are the indices for each exported variable in the global buffer
123 std::vector<uint32_t> mExportVarIndices;
I-Jui (Ray) Sungc2f647a2017-03-03 15:50:09 -0800124 // For Global Allocations; carries global variable -> metadata offset
125 // mapping from an LLVM pass to a SPIRIT pass
126 llvm::SmallVector<RSAllocationInfo, 8> mGlobalAllocs;
Yang Nieb654872017-02-21 16:40:01 -0800127};
128
129} // namespace rs2spirv
130
131#endif // RS2SPIRV_CONTEXT_H