blob: 06fe5479d5b7f16371dab21206c4206f138c27ca [file] [log] [blame]
Stephen Hines86a0b792012-11-06 20:04:47 -08001/*
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 Hines0116d8b2012-12-12 20:48:15 -080021#include <vector>
Stephen Hines86a0b792012-11-06 20:04:47 -080022
Stephen Hinesb730e232013-01-09 15:31:36 -080023#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 Hines86a0b792012-11-06 20:04:47 -080028#include <llvm/Pass.h>
29#include <llvm/Support/raw_ostream.h>
Stephen Hinesb730e232013-01-09 15:31:36 -080030#include <llvm/IR/Type.h>
Stephen Hines86a0b792012-11-06 20:04:47 -080031
32#include "bcc/Config/Config.h"
Stephen Hines86a0b792012-11-06 20:04:47 -080033#include "bcc/Support/Log.h"
Stephen Hines1253c192014-05-08 18:18:01 -070034#include "bcinfo/MetadataExtractor.h"
Stephen Hines86a0b792012-11-06 20:04:47 -080035
36using namespace bcc;
37
38namespace {
39
Stephen Hines2d201e52014-05-08 16:51:25 -070040/* 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 Hines86a0b792012-11-06 20:04:47 -080046 */
47class RSEmbedInfoPass : public llvm::ModulePass {
48private:
49 static char ID;
50
51 llvm::Module *M;
52 llvm::LLVMContext *C;
53
Stephen Hines86a0b792012-11-06 20:04:47 -080054public:
Stephen Hines1253c192014-05-08 18:18:01 -070055 RSEmbedInfoPass()
Stephen Hines86a0b792012-11-06 20:04:47 -080056 : ModulePass(ID),
Chris Wailes900c6c12014-08-13 15:40:00 -070057 M(nullptr) {
Stephen Hines86a0b792012-11-06 20:04:47 -080058 }
59
Stephen Hines1253c192014-05-08 18:18:01 -070060 static std::string getRSInfoString(const llvm::Module *module) {
Stephen Hines86a0b792012-11-06 20:04:47 -080061 std::string str;
62 llvm::raw_string_ostream s(str);
Stephen Hines1253c192014-05-08 18:18:01 -070063 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 Hines86a0b792012-11-06 20:04:47 -080080
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 Hines1253c192014-05-08 18:18:01 -070088 s << "exportVarCount: " << exportVarCount << "\n";
89 for (i = 0; i < exportVarCount; ++i) {
90 s << exportVarNameList[i] << "\n";
Stephen Hines86a0b792012-11-06 20:04:47 -080091 }
92
Stephen Hines1253c192014-05-08 18:18:01 -070093 s << "exportFuncCount: " << exportFuncCount << "\n";
94 for (i = 0; i < exportFuncCount; ++i) {
95 s << exportFuncNameList[i] << "\n";
Stephen Hines86a0b792012-11-06 20:04:47 -080096 }
97
Stephen Hines1253c192014-05-08 18:18:01 -070098 s << "exportForEachCount: " << exportForEachCount << "\n";
99 for (i = 0; i < exportForEachCount; ++i) {
100 s << exportForEachSignatureList[i] << " - "
101 << exportForEachNameList[i] << "\n";
Stephen Hines86a0b792012-11-06 20:04:47 -0800102 }
103
Stephen Hines1253c192014-05-08 18:18:01 -0700104 s << "objectSlotCount: " << objectSlotCount << "\n";
105 for (i = 0; i < objectSlotCount; ++i) {
106 s << objectSlotList[i] << "\n";
Stephen Hines86a0b792012-11-06 20:04:47 -0800107 }
108
109 s.flush();
Stephen Hines2d201e52014-05-08 16:51:25 -0700110 return str;
111 }
112
113 virtual bool runOnModule(llvm::Module &M) {
114 this->M = &M;
115 C = &M.getContext();
Stephen Hines86a0b792012-11-06 20:04:47 -0800116
117 // Embed this as the global variable .rs.info so that it will be
118 // accessible from the shared object later.
Stephen Hines2d201e52014-05-08 16:51:25 -0700119 llvm::Constant *Init = llvm::ConstantDataArray::getString(*C,
Stephen Hines1253c192014-05-08 18:18:01 -0700120 getRSInfoString(&M));
Stephen Hines86a0b792012-11-06 20:04:47 -0800121 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
138char RSEmbedInfoPass::ID = 0;
139
140namespace bcc {
141
142llvm::ModulePass *
Stephen Hines1253c192014-05-08 18:18:01 -0700143createRSEmbedInfoPass() {
144 return new RSEmbedInfoPass();
Stephen Hines86a0b792012-11-06 20:04:47 -0800145}
146
147} // end namespace bcc