RSEmbedInfo.cpp revision a99ef646b808e99cf870a8663170a1162851a42f
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    size_t pragmaCount = me.getPragmaCount();
78    const char **exportVarNameList = me.getExportVarNameList();
79    const char **exportFuncNameList = me.getExportFuncNameList();
80    const char **exportForEachNameList = me.getExportForEachNameList();
81    const uint32_t *exportForEachSignatureList =
82        me.getExportForEachSignatureList();
83    const uint32_t *objectSlotList = me.getObjectSlotList();
84    const char **pragmaKeyList = me.getPragmaKeyList();
85    const char **pragmaValueList = me.getPragmaValueList();
86    size_t i;
87
88    // We use a simple text format here that the compatibility library can
89    // easily parse. Each section starts out with its name followed by a count.
90    // The count denotes the number of lines to parse for that particular
91    // category. Variables and Functions merely put the appropriate identifier
92    // on the line, while ForEach kernels have the encoded int signature,
93    // followed by a hyphen followed by the identifier (function to look up).
94    // Object Slots are just listed as one integer per line.
95    s << "exportVarCount: " << exportVarCount << "\n";
96    for (i = 0; i < exportVarCount; ++i) {
97      s << exportVarNameList[i] << "\n";
98    }
99
100    s << "exportFuncCount: " << exportFuncCount << "\n";
101    for (i = 0; i < exportFuncCount; ++i) {
102      s << exportFuncNameList[i] << "\n";
103    }
104
105    s << "exportForEachCount: " << exportForEachCount << "\n";
106    for (i = 0; i < exportForEachCount; ++i) {
107      s << exportForEachSignatureList[i] << " - "
108        << exportForEachNameList[i] << "\n";
109    }
110
111    s << "objectSlotCount: " << objectSlotCount << "\n";
112    for (i = 0; i < objectSlotCount; ++i) {
113      s << objectSlotList[i] << "\n";
114    }
115
116    s << "pragmaCount: " << pragmaCount << "\n";
117    for (i = 0; i < pragmaCount; ++i) {
118      s << pragmaKeyList[i] << " - "
119        << pragmaValueList[i] << "\n";
120    }
121
122    s.flush();
123    return str;
124  }
125
126  virtual bool runOnModule(llvm::Module &M) {
127    this->M = &M;
128    C = &M.getContext();
129
130    // Embed this as the global variable .rs.info so that it will be
131    // accessible from the shared object later.
132    llvm::Constant *Init = llvm::ConstantDataArray::getString(*C,
133                                                              getRSInfoString(&M));
134    llvm::GlobalVariable *InfoGV =
135        new llvm::GlobalVariable(M, Init->getType(), true,
136                                 llvm::GlobalValue::ExternalLinkage, Init,
137                                 ".rs.info");
138    (void) InfoGV;
139
140    return true;
141  }
142
143  virtual const char *getPassName() const {
144    return "Embed Renderscript Info";
145  }
146
147};  // end RSEmbedInfoPass
148
149}  // end anonymous namespace
150
151char RSEmbedInfoPass::ID = 0;
152
153namespace bcc {
154
155llvm::ModulePass *
156createRSEmbedInfoPass() {
157  return new RSEmbedInfoPass();
158}
159
160}  // end namespace bcc
161