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" |
| 18 | #include "bcc/Renderscript/RSTransforms.h" |
| 19 | |
| 20 | #include <cstdlib> |
Stephen Hines | 0116d8b | 2012-12-12 20:48:15 -0800 | [diff] [blame] | 21 | #include <vector> |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 22 | |
Stephen Hines | b730e23 | 2013-01-09 15:31:36 -0800 | [diff] [blame] | 23 | #include <llvm/IR/DerivedTypes.h> |
| 24 | #include <llvm/IR/Function.h> |
| 25 | #include <llvm/IR/Instructions.h> |
| 26 | #include <llvm/IR/IRBuilder.h> |
| 27 | #include <llvm/IR/Module.h> |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 28 | #include <llvm/Pass.h> |
| 29 | #include <llvm/Support/raw_ostream.h> |
Stephen Hines | b730e23 | 2013-01-09 15:31:36 -0800 | [diff] [blame] | 30 | #include <llvm/IR/Type.h> |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 31 | |
| 32 | #include "bcc/Config/Config.h" |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 33 | #include "bcc/Support/Log.h" |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 34 | #include "bcinfo/MetadataExtractor.h" |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 35 | |
| 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 | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 60 | static std::string getRSInfoString(const llvm::Module *module) { |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 61 | std::string str; |
| 62 | llvm::raw_string_ostream s(str); |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 63 | bcinfo::MetadataExtractor me(module); |
| 64 | if (!me.extract()) { |
| 65 | bccAssert(false && "Could not extract RS metadata for module!"); |
| 66 | return std::string(""); |
| 67 | } |
| 68 | |
| 69 | size_t exportVarCount = me.getExportVarCount(); |
| 70 | size_t exportFuncCount = me.getExportFuncCount(); |
| 71 | size_t exportForEachCount = me.getExportForEachSignatureCount(); |
| 72 | size_t objectSlotCount = me.getObjectSlotCount(); |
| 73 | const char **exportVarNameList = me.getExportVarNameList(); |
| 74 | const char **exportFuncNameList = me.getExportFuncNameList(); |
| 75 | const char **exportForEachNameList = me.getExportForEachNameList(); |
| 76 | const uint32_t *exportForEachSignatureList = |
| 77 | me.getExportForEachSignatureList(); |
| 78 | const uint32_t *objectSlotList = me.getObjectSlotList(); |
| 79 | size_t i; |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 80 | |
| 81 | // We use a simple text format here that the compatibility library can |
| 82 | // easily parse. Each section starts out with its name followed by a count. |
| 83 | // The count denotes the number of lines to parse for that particular |
| 84 | // category. Variables and Functions merely put the appropriate identifier |
| 85 | // on the line, while ForEach kernels have the encoded int signature, |
| 86 | // followed by a hyphen followed by the identifier (function to look up). |
| 87 | // Object Slots are just listed as one integer per line. |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 88 | s << "exportVarCount: " << exportVarCount << "\n"; |
| 89 | for (i = 0; i < exportVarCount; ++i) { |
| 90 | s << exportVarNameList[i] << "\n"; |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 93 | s << "exportFuncCount: " << exportFuncCount << "\n"; |
| 94 | for (i = 0; i < exportFuncCount; ++i) { |
| 95 | s << exportFuncNameList[i] << "\n"; |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 98 | s << "exportForEachCount: " << exportForEachCount << "\n"; |
| 99 | for (i = 0; i < exportForEachCount; ++i) { |
| 100 | s << exportForEachSignatureList[i] << " - " |
| 101 | << exportForEachNameList[i] << "\n"; |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 104 | s << "objectSlotCount: " << objectSlotCount << "\n"; |
| 105 | for (i = 0; i < objectSlotCount; ++i) { |
| 106 | s << objectSlotList[i] << "\n"; |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | s.flush(); |
Stephen Hines | 2d201e5 | 2014-05-08 16:51:25 -0700 | [diff] [blame] | 110 | return str; |
| 111 | } |
| 112 | |
| 113 | virtual bool runOnModule(llvm::Module &M) { |
| 114 | this->M = &M; |
| 115 | C = &M.getContext(); |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 116 | |
| 117 | // Embed this as the global variable .rs.info so that it will be |
| 118 | // accessible from the shared object later. |
Stephen Hines | 2d201e5 | 2014-05-08 16:51:25 -0700 | [diff] [blame] | 119 | llvm::Constant *Init = llvm::ConstantDataArray::getString(*C, |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 120 | getRSInfoString(&M)); |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 121 | llvm::GlobalVariable *InfoGV = |
| 122 | new llvm::GlobalVariable(M, Init->getType(), true, |
| 123 | llvm::GlobalValue::ExternalLinkage, Init, |
| 124 | ".rs.info"); |
| 125 | (void) InfoGV; |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | virtual const char *getPassName() const { |
| 131 | return "Embed Renderscript Info"; |
| 132 | } |
| 133 | |
| 134 | }; // end RSEmbedInfoPass |
| 135 | |
| 136 | } // end anonymous namespace |
| 137 | |
| 138 | char RSEmbedInfoPass::ID = 0; |
| 139 | |
| 140 | namespace bcc { |
| 141 | |
| 142 | llvm::ModulePass * |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 143 | createRSEmbedInfoPass() { |
| 144 | return new RSEmbedInfoPass(); |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | } // end namespace bcc |