• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
36 using namespace bcc;
37 
38 namespace {
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  */
47 class RSEmbedInfoPass : public llvm::ModulePass {
48 private:
49   static char ID;
50 
51   llvm::Module *M;
52   llvm::LLVMContext *C;
53 
54 public:
RSEmbedInfoPass()55   RSEmbedInfoPass()
56       : ModulePass(ID),
57         M(NULL) {
58   }
59 
getRSInfoString(const llvm::Module * module)60   static std::string getRSInfoString(const llvm::Module *module) {
61     std::string str;
62     llvm::raw_string_ostream s(str);
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;
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.
88     s << "exportVarCount: " << exportVarCount << "\n";
89     for (i = 0; i < exportVarCount; ++i) {
90       s << exportVarNameList[i] << "\n";
91     }
92 
93     s << "exportFuncCount: " << exportFuncCount << "\n";
94     for (i = 0; i < exportFuncCount; ++i) {
95       s << exportFuncNameList[i] << "\n";
96     }
97 
98     s << "exportForEachCount: " << exportForEachCount << "\n";
99     for (i = 0; i < exportForEachCount; ++i) {
100       s << exportForEachSignatureList[i] << " - "
101         << exportForEachNameList[i] << "\n";
102     }
103 
104     s << "objectSlotCount: " << objectSlotCount << "\n";
105     for (i = 0; i < objectSlotCount; ++i) {
106       s << objectSlotList[i] << "\n";
107     }
108 
109     s.flush();
110     return str;
111   }
112 
runOnModule(llvm::Module & M)113   virtual bool runOnModule(llvm::Module &M) {
114     this->M = &M;
115     C = &M.getContext();
116 
117     // Embed this as the global variable .rs.info so that it will be
118     // accessible from the shared object later.
119     llvm::Constant *Init = llvm::ConstantDataArray::getString(*C,
120                                                               getRSInfoString(&M));
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 
getPassName() const130   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 *
createRSEmbedInfoPass()143 createRSEmbedInfoPass() {
144   return new RSEmbedInfoPass();
145 }
146 
147 }  // end namespace bcc
148