blob: 0acea3d98d73bfd3c70a8954447f5ff37568fcd5 [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
Jean-Luc Brouilleta2dd52f2017-02-16 20:57:26 -080017#include "Assert.h"
18#include "Log.h"
19#include "RSTransforms.h"
20#include "RSUtils.h"
Stephen Hinesfb81ec12015-05-18 20:04:23 -070021#include "rsDefines.h"
Stephen Hines86a0b792012-11-06 20:04:47 -080022
Jean-Luc Brouilleta2dd52f2017-02-16 20:57:26 -080023#include "bcc/Config.h"
24#include "bcinfo/MetadataExtractor.h"
25
Stephen Hines86a0b792012-11-06 20:04:47 -080026#include <cstdlib>
Stephen Hines0116d8b2012-12-12 20:48:15 -080027#include <vector>
Stephen Hines86a0b792012-11-06 20:04:47 -080028
Stephen Hinesb730e232013-01-09 15:31:36 -080029#include <llvm/IR/DerivedTypes.h>
30#include <llvm/IR/Function.h>
31#include <llvm/IR/Instructions.h>
32#include <llvm/IR/IRBuilder.h>
33#include <llvm/IR/Module.h>
Stephen Hines86a0b792012-11-06 20:04:47 -080034#include <llvm/Pass.h>
35#include <llvm/Support/raw_ostream.h>
Stephen Hinesb730e232013-01-09 15:31:36 -080036#include <llvm/IR/Type.h>
Stephen Hines86a0b792012-11-06 20:04:47 -080037
Stephen Hines86a0b792012-11-06 20:04:47 -080038using namespace bcc;
39
40namespace {
41
Stephen Hines2d201e52014-05-08 16:51:25 -070042/* RSEmbedInfoPass - This pass operates on the entire module and embeds a
43 * string constaining relevant metadata directly as a global variable.
44 * This information does not need to be consistent across Android releases,
45 * because the standalone compiler + compatibility driver or system driver
46 * will be using the same format (i.e. bcc_compat + libRSSupport.so or
47 * bcc + libRSCpuRef are always paired together for installation).
Stephen Hines86a0b792012-11-06 20:04:47 -080048 */
49class RSEmbedInfoPass : public llvm::ModulePass {
50private:
51 static char ID;
52
53 llvm::Module *M;
54 llvm::LLVMContext *C;
55
Stephen Hines86a0b792012-11-06 20:04:47 -080056public:
Stephen Hines1253c192014-05-08 18:18:01 -070057 RSEmbedInfoPass()
Stephen Hines86a0b792012-11-06 20:04:47 -080058 : ModulePass(ID),
Chris Wailes900c6c12014-08-13 15:40:00 -070059 M(nullptr) {
Stephen Hines86a0b792012-11-06 20:04:47 -080060 }
61
Stephen Hinesc754d492015-01-08 16:00:50 -080062 virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
63 AU.setPreservesAll();
64 }
65
Stephen Hines1253c192014-05-08 18:18:01 -070066 static std::string getRSInfoString(const llvm::Module *module) {
Stephen Hines86a0b792012-11-06 20:04:47 -080067 std::string str;
68 llvm::raw_string_ostream s(str);
Stephen Hines1253c192014-05-08 18:18:01 -070069 bcinfo::MetadataExtractor me(module);
70 if (!me.extract()) {
71 bccAssert(false && "Could not extract RS metadata for module!");
72 return std::string("");
73 }
74
75 size_t exportVarCount = me.getExportVarCount();
76 size_t exportFuncCount = me.getExportFuncCount();
77 size_t exportForEachCount = me.getExportForEachSignatureCount();
Matt Wala4e7a5062015-07-30 16:27:51 -070078 size_t exportReduceCount = me.getExportReduceCount();
Stephen Hines1253c192014-05-08 18:18:01 -070079 size_t objectSlotCount = me.getObjectSlotCount();
Pirama Arumuga Nainara99ef642015-01-23 14:33:42 -080080 size_t pragmaCount = me.getPragmaCount();
Stephen Hines1253c192014-05-08 18:18:01 -070081 const char **exportVarNameList = me.getExportVarNameList();
82 const char **exportFuncNameList = me.getExportFuncNameList();
83 const char **exportForEachNameList = me.getExportForEachNameList();
84 const uint32_t *exportForEachSignatureList =
85 me.getExportForEachSignatureList();
David Grossa48ea362016-06-02 14:46:55 -070086 const bcinfo::MetadataExtractor::Reduce *exportReduceList =
87 me.getExportReduceList();
Stephen Hines1253c192014-05-08 18:18:01 -070088 const uint32_t *objectSlotList = me.getObjectSlotList();
Pirama Arumuga Nainara99ef642015-01-23 14:33:42 -080089 const char **pragmaKeyList = me.getPragmaKeyList();
90 const char **pragmaValueList = me.getPragmaValueList();
Pirama Arumuga Nainar9fe081b2015-01-27 14:09:19 -080091 bool isThreadable = me.isThreadable();
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -080092 const char *buildChecksum = me.getBuildChecksum();
93
Stephen Hines1253c192014-05-08 18:18:01 -070094 size_t i;
Stephen Hines86a0b792012-11-06 20:04:47 -080095
David Gross79e1a052016-01-11 14:42:51 -080096 // We use a simple text format here that the compatibility library
97 // can easily parse. Each section starts out with its name
98 // followed by a count. The count denotes the number of lines to
David Grossa48ea362016-06-02 14:46:55 -070099 // parse for that particular category. Variables and Functions
100 // merely put the appropriate identifier on the line. ForEach
101 // kernels have the encoded int signature, followed by a hyphen
102 // followed by the identifier (function to look up). General
103 // reduce kernels have the encoded int signature, followed by a
104 // hyphen followed by the accumulator data size, followed by a
105 // hyphen followed by the identifier (reduction name); and then
106 // for each possible constituent function, a hyphen followed by
107 // the identifier (function name) -- in the case where the
108 // function is omitted, "." is used in place of the identifier.
109 // Object Slots are just listed as one integer per line.
David Gross79e1a052016-01-11 14:42:51 -0800110
Stephen Hines1253c192014-05-08 18:18:01 -0700111 s << "exportVarCount: " << exportVarCount << "\n";
112 for (i = 0; i < exportVarCount; ++i) {
113 s << exportVarNameList[i] << "\n";
Stephen Hines86a0b792012-11-06 20:04:47 -0800114 }
115
Stephen Hines1253c192014-05-08 18:18:01 -0700116 s << "exportFuncCount: " << exportFuncCount << "\n";
117 for (i = 0; i < exportFuncCount; ++i) {
118 s << exportFuncNameList[i] << "\n";
Stephen Hines86a0b792012-11-06 20:04:47 -0800119 }
120
Stephen Hines1253c192014-05-08 18:18:01 -0700121 s << "exportForEachCount: " << exportForEachCount << "\n";
122 for (i = 0; i < exportForEachCount; ++i) {
123 s << exportForEachSignatureList[i] << " - "
124 << exportForEachNameList[i] << "\n";
Stephen Hines86a0b792012-11-06 20:04:47 -0800125 }
126
Matt Wala4e7a5062015-07-30 16:27:51 -0700127 s << "exportReduceCount: " << exportReduceCount << "\n";
David Grossa48ea362016-06-02 14:46:55 -0700128 auto reduceFnName = [](const char *Name) { return Name ? Name : "."; };
Matt Wala4e7a5062015-07-30 16:27:51 -0700129 for (i = 0; i < exportReduceCount; ++i) {
David Grossa48ea362016-06-02 14:46:55 -0700130 const bcinfo::MetadataExtractor::Reduce &reduce = exportReduceList[i];
131 s << reduce.mSignature << " - "
132 << reduce.mAccumulatorDataSize << " - "
133 << reduce.mReduceName << " - "
134 << reduceFnName(reduce.mInitializerName) << " - "
135 << reduceFnName(reduce.mAccumulatorName) << " - "
136 << ((reduce.mCombinerName != nullptr)
137 ? reduce.mCombinerName
138 : nameReduceCombinerFromAccumulator(reduce.mAccumulatorName)) << " - "
139 << reduceFnName(reduce.mOutConverterName) << " - "
140 << reduceFnName(reduce.mHalterName)
David Gross79e1a052016-01-11 14:42:51 -0800141 << "\n";
142 }
143
Stephen Hines1253c192014-05-08 18:18:01 -0700144 s << "objectSlotCount: " << objectSlotCount << "\n";
145 for (i = 0; i < objectSlotCount; ++i) {
146 s << objectSlotList[i] << "\n";
Stephen Hines86a0b792012-11-06 20:04:47 -0800147 }
148
Pirama Arumuga Nainara99ef642015-01-23 14:33:42 -0800149 s << "pragmaCount: " << pragmaCount << "\n";
150 for (i = 0; i < pragmaCount; ++i) {
151 s << pragmaKeyList[i] << " - "
152 << pragmaValueList[i] << "\n";
153 }
Pirama Arumuga Nainar9fe081b2015-01-27 14:09:19 -0800154 s << "isThreadable: " << ((isThreadable) ? "yes" : "no") << "\n";
Pirama Arumuga Nainara99ef642015-01-23 14:33:42 -0800155
Stephen Hinese1c7d292015-04-15 13:20:00 -0700156 if (buildChecksum != nullptr && buildChecksum[0]) {
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -0800157 s << "buildChecksum: " << buildChecksum << "\n";
158 }
159
Stephen Hines86a0b792012-11-06 20:04:47 -0800160 s.flush();
Stephen Hines2d201e52014-05-08 16:51:25 -0700161 return str;
162 }
163
164 virtual bool runOnModule(llvm::Module &M) {
165 this->M = &M;
166 C = &M.getContext();
Stephen Hines86a0b792012-11-06 20:04:47 -0800167
168 // Embed this as the global variable .rs.info so that it will be
169 // accessible from the shared object later.
Stephen Hines2d201e52014-05-08 16:51:25 -0700170 llvm::Constant *Init = llvm::ConstantDataArray::getString(*C,
Stephen Hines1253c192014-05-08 18:18:01 -0700171 getRSInfoString(&M));
Stephen Hines86a0b792012-11-06 20:04:47 -0800172 llvm::GlobalVariable *InfoGV =
173 new llvm::GlobalVariable(M, Init->getType(), true,
174 llvm::GlobalValue::ExternalLinkage, Init,
Stephen Hinesfb81ec12015-05-18 20:04:23 -0700175 kRsInfo);
Stephen Hines86a0b792012-11-06 20:04:47 -0800176 (void) InfoGV;
177
178 return true;
179 }
180
181 virtual const char *getPassName() const {
182 return "Embed Renderscript Info";
183 }
184
185}; // end RSEmbedInfoPass
186
187} // end anonymous namespace
188
189char RSEmbedInfoPass::ID = 0;
190
191namespace bcc {
192
193llvm::ModulePass *
Stephen Hines1253c192014-05-08 18:18:01 -0700194createRSEmbedInfoPass() {
195 return new RSEmbedInfoPass();
Stephen Hines86a0b792012-11-06 20:04:47 -0800196}
197
198} // end namespace bcc