RSEmbedInfo.cpp revision c754d49ee856be620e041348a9f2b3d5610a5a26
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> 21#include <vector> 22 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> 28#include <llvm/Pass.h> 29#include <llvm/Support/raw_ostream.h> 30#include <llvm/IR/Type.h> 31 32#include "bcc/Config/Config.h" 33#include "bcc/Support/Log.h" 34#include "bcinfo/MetadataExtractor.h" 35 36using namespace bcc; 37 38namespace { 39 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). 46 */ 47class RSEmbedInfoPass : public llvm::ModulePass { 48private: 49 static char ID; 50 51 llvm::Module *M; 52 llvm::LLVMContext *C; 53 54public: 55 RSEmbedInfoPass() 56 : ModulePass(ID), 57 M(nullptr) { 58 } 59 60 virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override { 61 AU.setPreservesAll(); 62 } 63 64 static std::string getRSInfoString(const llvm::Module *module) { 65 std::string str; 66 llvm::raw_string_ostream s(str); 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(); 76 size_t objectSlotCount = me.getObjectSlotCount(); 77 const char **exportVarNameList = me.getExportVarNameList(); 78 const char **exportFuncNameList = me.getExportFuncNameList(); 79 const char **exportForEachNameList = me.getExportForEachNameList(); 80 const uint32_t *exportForEachSignatureList = 81 me.getExportForEachSignatureList(); 82 const uint32_t *objectSlotList = me.getObjectSlotList(); 83 size_t i; 84 85 // We use a simple text format here that the compatibility library can 86 // easily parse. Each section starts out with its name followed by a count. 87 // The count denotes the number of lines to parse for that particular 88 // category. Variables and Functions merely put the appropriate identifier 89 // on the line, while ForEach kernels have the encoded int signature, 90 // followed by a hyphen followed by the identifier (function to look up). 91 // Object Slots are just listed as one integer per line. 92 s << "exportVarCount: " << exportVarCount << "\n"; 93 for (i = 0; i < exportVarCount; ++i) { 94 s << exportVarNameList[i] << "\n"; 95 } 96 97 s << "exportFuncCount: " << exportFuncCount << "\n"; 98 for (i = 0; i < exportFuncCount; ++i) { 99 s << exportFuncNameList[i] << "\n"; 100 } 101 102 s << "exportForEachCount: " << exportForEachCount << "\n"; 103 for (i = 0; i < exportForEachCount; ++i) { 104 s << exportForEachSignatureList[i] << " - " 105 << exportForEachNameList[i] << "\n"; 106 } 107 108 s << "objectSlotCount: " << objectSlotCount << "\n"; 109 for (i = 0; i < objectSlotCount; ++i) { 110 s << objectSlotList[i] << "\n"; 111 } 112 113 s.flush(); 114 return str; 115 } 116 117 virtual bool runOnModule(llvm::Module &M) { 118 this->M = &M; 119 C = &M.getContext(); 120 121 // Embed this as the global variable .rs.info so that it will be 122 // accessible from the shared object later. 123 llvm::Constant *Init = llvm::ConstantDataArray::getString(*C, 124 getRSInfoString(&M)); 125 llvm::GlobalVariable *InfoGV = 126 new llvm::GlobalVariable(M, Init->getType(), true, 127 llvm::GlobalValue::ExternalLinkage, Init, 128 ".rs.info"); 129 (void) InfoGV; 130 131 return true; 132 } 133 134 virtual const char *getPassName() const { 135 return "Embed Renderscript Info"; 136 } 137 138}; // end RSEmbedInfoPass 139 140} // end anonymous namespace 141 142char RSEmbedInfoPass::ID = 0; 143 144namespace bcc { 145 146llvm::ModulePass * 147createRSEmbedInfoPass() { 148 return new RSEmbedInfoPass(); 149} 150 151} // end namespace bcc 152