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