Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012, 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 "bcc/Assert.h" |
Stephen Hines | fb81ec1 | 2015-05-18 20:04:23 -0700 | [diff] [blame] | 18 | #include "bcc/Config/Config.h" |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 19 | #include "bcc/Renderscript/RSTransforms.h" |
Stephen Hines | fb81ec1 | 2015-05-18 20:04:23 -0700 | [diff] [blame] | 20 | #include "bcc/Support/Log.h" |
| 21 | #include "bcinfo/MetadataExtractor.h" |
| 22 | #include "rsDefines.h" |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 23 | |
| 24 | #include <cstdlib> |
Stephen Hines | 0116d8b | 2012-12-12 20:48:15 -0800 | [diff] [blame] | 25 | #include <vector> |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 26 | |
Stephen Hines | b730e23 | 2013-01-09 15:31:36 -0800 | [diff] [blame] | 27 | #include <llvm/IR/DerivedTypes.h> |
| 28 | #include <llvm/IR/Function.h> |
| 29 | #include <llvm/IR/Instructions.h> |
| 30 | #include <llvm/IR/IRBuilder.h> |
| 31 | #include <llvm/IR/Module.h> |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 32 | #include <llvm/Pass.h> |
| 33 | #include <llvm/Support/raw_ostream.h> |
Stephen Hines | b730e23 | 2013-01-09 15:31:36 -0800 | [diff] [blame] | 34 | #include <llvm/IR/Type.h> |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 35 | |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 36 | using namespace bcc; |
| 37 | |
| 38 | namespace { |
| 39 | |
Stephen Hines | 2d201e5 | 2014-05-08 16:51:25 -0700 | [diff] [blame] | 40 | /* RSEmbedInfoPass - This pass operates on the entire module and embeds a |
| 41 | * string constaining relevant metadata directly as a global variable. |
| 42 | * This information does not need to be consistent across Android releases, |
| 43 | * because the standalone compiler + compatibility driver or system driver |
| 44 | * will be using the same format (i.e. bcc_compat + libRSSupport.so or |
| 45 | * bcc + libRSCpuRef are always paired together for installation). |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 46 | */ |
| 47 | class RSEmbedInfoPass : public llvm::ModulePass { |
| 48 | private: |
| 49 | static char ID; |
| 50 | |
| 51 | llvm::Module *M; |
| 52 | llvm::LLVMContext *C; |
| 53 | |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 54 | public: |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 55 | RSEmbedInfoPass() |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 56 | : ModulePass(ID), |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 57 | M(nullptr) { |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Stephen Hines | c754d49 | 2015-01-08 16:00:50 -0800 | [diff] [blame] | 60 | virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override { |
| 61 | AU.setPreservesAll(); |
| 62 | } |
| 63 | |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 64 | static std::string getRSInfoString(const llvm::Module *module) { |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 65 | std::string str; |
| 66 | llvm::raw_string_ostream s(str); |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 67 | bcinfo::MetadataExtractor me(module); |
| 68 | if (!me.extract()) { |
| 69 | bccAssert(false && "Could not extract RS metadata for module!"); |
| 70 | return std::string(""); |
| 71 | } |
| 72 | |
| 73 | size_t exportVarCount = me.getExportVarCount(); |
| 74 | size_t exportFuncCount = me.getExportFuncCount(); |
| 75 | size_t exportForEachCount = me.getExportForEachSignatureCount(); |
Matt Wala | 4e7a506 | 2015-07-30 16:27:51 -0700 | [diff] [blame^] | 76 | size_t exportReduceCount = me.getExportReduceCount(); |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 77 | size_t objectSlotCount = me.getObjectSlotCount(); |
Pirama Arumuga Nainar | a99ef64 | 2015-01-23 14:33:42 -0800 | [diff] [blame] | 78 | size_t pragmaCount = me.getPragmaCount(); |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 79 | const char **exportVarNameList = me.getExportVarNameList(); |
| 80 | const char **exportFuncNameList = me.getExportFuncNameList(); |
| 81 | const char **exportForEachNameList = me.getExportForEachNameList(); |
Matt Wala | 4e7a506 | 2015-07-30 16:27:51 -0700 | [diff] [blame^] | 82 | const char **exportReduceNameList = me.getExportReduceNameList(); |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 83 | const uint32_t *exportForEachSignatureList = |
| 84 | me.getExportForEachSignatureList(); |
| 85 | const uint32_t *objectSlotList = me.getObjectSlotList(); |
Pirama Arumuga Nainar | a99ef64 | 2015-01-23 14:33:42 -0800 | [diff] [blame] | 86 | const char **pragmaKeyList = me.getPragmaKeyList(); |
| 87 | const char **pragmaValueList = me.getPragmaValueList(); |
Pirama Arumuga Nainar | 9fe081b | 2015-01-27 14:09:19 -0800 | [diff] [blame] | 88 | bool isThreadable = me.isThreadable(); |
Pirama Arumuga Nainar | 51ee77b | 2015-02-19 16:33:27 -0800 | [diff] [blame] | 89 | const char *buildChecksum = me.getBuildChecksum(); |
| 90 | |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 91 | size_t i; |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 92 | |
| 93 | // We use a simple text format here that the compatibility library can |
| 94 | // easily parse. Each section starts out with its name followed by a count. |
| 95 | // The count denotes the number of lines to parse for that particular |
| 96 | // category. Variables and Functions merely put the appropriate identifier |
| 97 | // on the line, while ForEach kernels have the encoded int signature, |
| 98 | // followed by a hyphen followed by the identifier (function to look up). |
| 99 | // Object Slots are just listed as one integer per line. |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 100 | s << "exportVarCount: " << exportVarCount << "\n"; |
| 101 | for (i = 0; i < exportVarCount; ++i) { |
| 102 | s << exportVarNameList[i] << "\n"; |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 105 | s << "exportFuncCount: " << exportFuncCount << "\n"; |
| 106 | for (i = 0; i < exportFuncCount; ++i) { |
| 107 | s << exportFuncNameList[i] << "\n"; |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 110 | s << "exportForEachCount: " << exportForEachCount << "\n"; |
| 111 | for (i = 0; i < exportForEachCount; ++i) { |
| 112 | s << exportForEachSignatureList[i] << " - " |
| 113 | << exportForEachNameList[i] << "\n"; |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Matt Wala | 4e7a506 | 2015-07-30 16:27:51 -0700 | [diff] [blame^] | 116 | s << "exportReduceCount: " << exportReduceCount << "\n"; |
| 117 | for (i = 0; i < exportReduceCount; ++i) { |
| 118 | s << exportReduceNameList[i] << "\n"; |
| 119 | } |
| 120 | |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 121 | s << "objectSlotCount: " << objectSlotCount << "\n"; |
| 122 | for (i = 0; i < objectSlotCount; ++i) { |
| 123 | s << objectSlotList[i] << "\n"; |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Pirama Arumuga Nainar | a99ef64 | 2015-01-23 14:33:42 -0800 | [diff] [blame] | 126 | s << "pragmaCount: " << pragmaCount << "\n"; |
| 127 | for (i = 0; i < pragmaCount; ++i) { |
| 128 | s << pragmaKeyList[i] << " - " |
| 129 | << pragmaValueList[i] << "\n"; |
| 130 | } |
Pirama Arumuga Nainar | 9fe081b | 2015-01-27 14:09:19 -0800 | [diff] [blame] | 131 | s << "isThreadable: " << ((isThreadable) ? "yes" : "no") << "\n"; |
Pirama Arumuga Nainar | a99ef64 | 2015-01-23 14:33:42 -0800 | [diff] [blame] | 132 | |
Stephen Hines | e1c7d29 | 2015-04-15 13:20:00 -0700 | [diff] [blame] | 133 | if (buildChecksum != nullptr && buildChecksum[0]) { |
Pirama Arumuga Nainar | 51ee77b | 2015-02-19 16:33:27 -0800 | [diff] [blame] | 134 | s << "buildChecksum: " << buildChecksum << "\n"; |
| 135 | } |
| 136 | |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 137 | s.flush(); |
Stephen Hines | 2d201e5 | 2014-05-08 16:51:25 -0700 | [diff] [blame] | 138 | return str; |
| 139 | } |
| 140 | |
| 141 | virtual bool runOnModule(llvm::Module &M) { |
| 142 | this->M = &M; |
| 143 | C = &M.getContext(); |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 144 | |
| 145 | // Embed this as the global variable .rs.info so that it will be |
| 146 | // accessible from the shared object later. |
Stephen Hines | 2d201e5 | 2014-05-08 16:51:25 -0700 | [diff] [blame] | 147 | llvm::Constant *Init = llvm::ConstantDataArray::getString(*C, |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 148 | getRSInfoString(&M)); |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 149 | llvm::GlobalVariable *InfoGV = |
| 150 | new llvm::GlobalVariable(M, Init->getType(), true, |
| 151 | llvm::GlobalValue::ExternalLinkage, Init, |
Stephen Hines | fb81ec1 | 2015-05-18 20:04:23 -0700 | [diff] [blame] | 152 | kRsInfo); |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 153 | (void) InfoGV; |
| 154 | |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | virtual const char *getPassName() const { |
| 159 | return "Embed Renderscript Info"; |
| 160 | } |
| 161 | |
| 162 | }; // end RSEmbedInfoPass |
| 163 | |
| 164 | } // end anonymous namespace |
| 165 | |
| 166 | char RSEmbedInfoPass::ID = 0; |
| 167 | |
| 168 | namespace bcc { |
| 169 | |
| 170 | llvm::ModulePass * |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 171 | createRSEmbedInfoPass() { |
| 172 | return new RSEmbedInfoPass(); |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | } // end namespace bcc |