blob: 95fc27b9e65afc8804cd6644de85d09f4e856c68 [file] [log] [blame]
Stephen Hines932bc6e2011-07-27 16:26:26 -07001/*
Stephen Hinescc366e52012-02-21 17:22:04 -08002 * Copyright 2011-2012, The Android Open Source Project
Stephen Hines932bc6e2011-07-27 16:26:26 -07003 *
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#ifndef __ANDROID_BCINFO_METADATAEXTRACTOR_H__
18#define __ANDROID_BCINFO_METADATAEXTRACTOR_H__
19
20#include <cstddef>
Pirama Arumuga Nainar8e908932016-03-06 23:05:45 -080021#include <memory>
22
Stephen Hines932bc6e2011-07-27 16:26:26 -070023#include <stdint.h>
24
25namespace llvm {
Chris Wailesa6681822014-07-11 15:15:46 -070026 class Function;
Stephen Hines569986d2012-03-09 19:58:45 -080027 class Module;
Stephen Hines932bc6e2011-07-27 16:26:26 -070028 class NamedMDNode;
29}
30
31namespace bcinfo {
32
Stephen Hinese1fd8042012-03-27 11:03:46 -070033enum RSFloatPrecision {
34 RS_FP_Full = 0,
35 RS_FP_Relaxed = 1,
Stephen Hinese1fd8042012-03-27 11:03:46 -070036};
37
David Gross33cda5c2015-01-30 11:41:19 -080038enum MetadataSignatureBitval {
39 MD_SIG_None = 0,
40 MD_SIG_In = 0x000001,
41 MD_SIG_Out = 0x000002,
42 MD_SIG_Usr = 0x000004,
43 MD_SIG_X = 0x000008,
44 MD_SIG_Y = 0x000010,
45 MD_SIG_Kernel = 0x000020,
46 MD_SIG_Z = 0x000040,
47 MD_SIG_Ctxt = 0x000080,
48};
49
Stephen Hines932bc6e2011-07-27 16:26:26 -070050class MetadataExtractor {
David Gross79e1a052016-01-11 14:42:51 -080051 public:
David Grossa48ea362016-06-02 14:46:55 -070052 struct Reduce {
53 // These strings are owned by the Reduce instance, and deleted upon its destruction.
David Gross79e1a052016-01-11 14:42:51 -080054 // They are assumed to have been allocated by "new []" and hence are deleted by "delete []".
55 const char *mReduceName;
56 const char *mInitializerName;
57 const char *mAccumulatorName;
58 const char *mCombinerName;
59 const char *mOutConverterName;
60 const char *mHalterName;
61
62 uint32_t mSignature; // of accumulator function
63 uint32_t mInputCount; // of accumulator function (and of kernel itself)
64 uint32_t mAccumulatorDataSize; // in bytes
65
David Grossa48ea362016-06-02 14:46:55 -070066 Reduce() :
David Gross79e1a052016-01-11 14:42:51 -080067 mReduceName(nullptr),
68 mInitializerName(nullptr), mAccumulatorName(nullptr), mCombinerName(nullptr),
69 mOutConverterName(nullptr), mHalterName(nullptr),
70 mSignature(0), mInputCount(0), mAccumulatorDataSize(0) {
71 }
David Grossa48ea362016-06-02 14:46:55 -070072 ~Reduce() {
David Gross79e1a052016-01-11 14:42:51 -080073 delete [] mReduceName;
74 delete [] mInitializerName;
75 delete [] mAccumulatorName;
76 delete [] mCombinerName;
77 delete [] mOutConverterName;
78 delete [] mHalterName;
79 }
80
David Grossa48ea362016-06-02 14:46:55 -070081 Reduce(const Reduce &) = delete;
82 void operator=(const Reduce &) = delete;
David Gross79e1a052016-01-11 14:42:51 -080083 };
84
Stephen Hines932bc6e2011-07-27 16:26:26 -070085 private:
Stephen Hines569986d2012-03-09 19:58:45 -080086 const llvm::Module *mModule;
Stephen Hines932bc6e2011-07-27 16:26:26 -070087 const char *mBitcode;
88 size_t mBitcodeSize;
89
90 size_t mExportVarCount;
91 size_t mExportFuncCount;
Stephen Hines33f8fe22011-08-17 12:56:22 -070092 size_t mExportForEachSignatureCount;
Matt Wala1895ac12015-07-16 15:34:29 -070093 size_t mExportReduceCount;
Stephen Hines569986d2012-03-09 19:58:45 -080094 const char **mExportVarNameList;
95 const char **mExportFuncNameList;
Stephen Hinescc366e52012-02-21 17:22:04 -080096 const char **mExportForEachNameList;
Stephen Hines33f8fe22011-08-17 12:56:22 -070097 const uint32_t *mExportForEachSignatureList;
Chris Wailesa6681822014-07-11 15:15:46 -070098 const uint32_t *mExportForEachInputCountList;
David Grossa48ea362016-06-02 14:46:55 -070099 const Reduce *mExportReduceList;
Chris Wailesa6681822014-07-11 15:15:46 -0700100
Stephen Hines932bc6e2011-07-27 16:26:26 -0700101 size_t mPragmaCount;
102 const char **mPragmaKeyList;
103 const char **mPragmaValueList;
104
105 size_t mObjectSlotCount;
106 const uint32_t *mObjectSlotList;
107
Stephen Hines09479232015-04-02 08:43:41 -0700108 uint32_t mTargetAPI;
Stephen Hinesb67c9e72012-03-22 11:02:48 -0700109 uint32_t mCompilerVersion;
Daniel Malea094881f2011-12-14 17:39:16 -0500110 uint32_t mOptimizationLevel;
111
Stephen Hinese1fd8042012-03-27 11:03:46 -0700112 enum RSFloatPrecision mRSFloatPrecision;
113
Pirama Arumuga Nainar9fe081b2015-01-27 14:09:19 -0800114 // Flag to mark that script is threadable. True by default.
115 bool mIsThreadable;
116
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -0800117 const char *mBuildChecksum;
118
Dean De Leofff398d2015-11-25 13:00:31 +0000119 bool mHasDebugInfo;
120
Stephen Hines932bc6e2011-07-27 16:26:26 -0700121 // Helper functions for extraction
Stephen Hinescc366e52012-02-21 17:22:04 -0800122 bool populateForEachMetadata(const llvm::NamedMDNode *Names,
123 const llvm::NamedMDNode *Signatures);
David Grossa48ea362016-06-02 14:46:55 -0700124 bool populateReduceMetadata(const llvm::NamedMDNode *ReduceMetadata);
Stephen Hines932bc6e2011-07-27 16:26:26 -0700125 bool populateObjectSlotMetadata(const llvm::NamedMDNode *ObjectSlotMetadata);
126 void populatePragmaMetadata(const llvm::NamedMDNode *PragmaMetadata);
Pirama Arumuga Nainar9fe081b2015-01-27 14:09:19 -0800127 void readThreadableFlag(const llvm::NamedMDNode *ThreadableMetadata);
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -0800128 void readBuildChecksumMetadata(const llvm::NamedMDNode *ChecksumMetadata);
Stephen Hines932bc6e2011-07-27 16:26:26 -0700129
Chris Wailesa6681822014-07-11 15:15:46 -0700130 uint32_t calculateNumInputs(const llvm::Function *Function,
131 uint32_t Signature);
132
Stephen Hines932bc6e2011-07-27 16:26:26 -0700133 public:
134 /**
135 * Reads metadata from \p bitcode.
136 *
137 * \param bitcode - input bitcode string.
138 * \param bitcodeSize - length of \p bitcode string (in bytes).
139 */
140 MetadataExtractor(const char *bitcode, size_t bitcodeSize);
141
Stephen Hines569986d2012-03-09 19:58:45 -0800142 /**
143 * Reads metadata from \p module.
144 *
145 * \param module - input module.
146 */
147 MetadataExtractor(const llvm::Module *module);
148
Stephen Hines932bc6e2011-07-27 16:26:26 -0700149 ~MetadataExtractor();
150
151 /**
152 * Extract the actual metadata from the supplied bitcode.
153 *
154 * \return true on success and false if an error occurred.
155 */
156 bool extract();
157
158 /**
Stephen Hines09479232015-04-02 08:43:41 -0700159 * \return target API level of this bitcode.
160 *
161 * The target API is used during the SDK compilation to provide proper
162 * visibility of the RenderScript runtime API functions.
163 */
164 uint32_t getTargetAPI() const {
165 return mTargetAPI;
166 }
167
168 /**
Stephen Hines932bc6e2011-07-27 16:26:26 -0700169 * \return number of exported global variables (slots) in this script/module.
170 */
171 size_t getExportVarCount() const {
172 return mExportVarCount;
173 }
174
175 /**
Stephen Hines569986d2012-03-09 19:58:45 -0800176 * \return array of exported variable names.
177 */
178 const char **getExportVarNameList() const {
179 return mExportVarNameList;
180 }
181
182 /**
Stephen Hines932bc6e2011-07-27 16:26:26 -0700183 * \return number of exported global functions (slots) in this script/module.
184 */
185 size_t getExportFuncCount() const {
186 return mExportFuncCount;
187 }
188
189 /**
Stephen Hines569986d2012-03-09 19:58:45 -0800190 * \return array of exported function names.
191 */
192 const char **getExportFuncNameList() const {
193 return mExportFuncNameList;
194 }
195
196 /**
Stephen Hines33f8fe22011-08-17 12:56:22 -0700197 * \return number of exported ForEach functions in this script/module.
198 */
199 size_t getExportForEachSignatureCount() const {
200 return mExportForEachSignatureCount;
201 }
202
203 /**
Stephen Hines569986d2012-03-09 19:58:45 -0800204 * \return array of exported ForEach function signatures.
Stephen Hines33f8fe22011-08-17 12:56:22 -0700205 */
206 const uint32_t *getExportForEachSignatureList() const {
207 return mExportForEachSignatureList;
208 }
209
210 /**
Stephen Hines569986d2012-03-09 19:58:45 -0800211 * \return array of exported ForEach function names.
Stephen Hinescc366e52012-02-21 17:22:04 -0800212 */
213 const char **getExportForEachNameList() const {
214 return mExportForEachNameList;
215 }
216
217 /**
Chris Wailesa6681822014-07-11 15:15:46 -0700218 * \return array of input parameter counts.
219 */
220 const uint32_t *getExportForEachInputCountList() const {
221 return mExportForEachInputCountList;
222 }
223
224 /**
David Grossa48ea362016-06-02 14:46:55 -0700225 * \return number of exported general reduce kernels (slots) in this script/module.
Matt Wala1895ac12015-07-16 15:34:29 -0700226 */
227 size_t getExportReduceCount() const {
228 return mExportReduceCount;
229 }
230
231 /**
David Gross79e1a052016-01-11 14:42:51 -0800232 * \return array of exported general reduce kernel descriptions.
233 */
David Grossa48ea362016-06-02 14:46:55 -0700234 const Reduce *getExportReduceList() const {
235 return mExportReduceList;
David Gross79e1a052016-01-11 14:42:51 -0800236 }
237
238 /**
Stephen Hines932bc6e2011-07-27 16:26:26 -0700239 * \return number of pragmas contained in pragmaKeyList and pragmaValueList.
240 */
241 size_t getPragmaCount() const {
242 return mPragmaCount;
243 }
244
245 /**
246 * \return pragma keys (the name for the pragma).
247 */
248 const char **getPragmaKeyList() const {
249 return mPragmaKeyList;
250 }
251
252 /**
253 * \return pragma values (contents corresponding to a particular pragma key).
254 */
255 const char **getPragmaValueList() const {
256 return mPragmaValueList;
257 }
258
259 /**
260 * \return number of object slots contained in objectSlotList.
261 */
262 size_t getObjectSlotCount() const {
263 return mObjectSlotCount;
264 }
265
266 /**
267 * \return array of object slot numbers that must be cleaned up by driver
268 * on script teardown.
269 */
270 const uint32_t *getObjectSlotList() const {
271 return mObjectSlotList;
272 }
Daniel Malea094881f2011-12-14 17:39:16 -0500273
Stephen Hinesb67c9e72012-03-22 11:02:48 -0700274 /**
275 * \return compiler version that generated this bitcode.
276 */
277 uint32_t getCompilerVersion() const {
278 return mCompilerVersion;
279 }
280
281 /**
282 * \return compiler optimization level for this bitcode.
283 */
Daniel Malea094881f2011-12-14 17:39:16 -0500284 uint32_t getOptimizationLevel() const {
285 return mOptimizationLevel;
286 }
Stephen Hinese1fd8042012-03-27 11:03:46 -0700287
288 /**
289 * \return minimal floating point precision that the script requires.
290 */
291 enum RSFloatPrecision getRSFloatPrecision() const {
292 return mRSFloatPrecision;
293 }
Stephen Hinesd8817752013-08-02 17:56:51 -0700294
295 /**
296 * \return whether or not this ForEach function signature has an "In"
297 * parameter.
298 *
299 * \param sig - ForEach function signature to check.
300 */
301 static bool hasForEachSignatureIn(uint32_t sig) {
David Gross33cda5c2015-01-30 11:41:19 -0800302 return sig & MD_SIG_In;
Stephen Hinesd8817752013-08-02 17:56:51 -0700303 }
304
305 /**
306 * \return whether or not this ForEach function signature has an "Out"
307 * parameter.
308 *
309 * \param sig - ForEach function signature to check.
310 */
311 static bool hasForEachSignatureOut(uint32_t sig) {
David Gross33cda5c2015-01-30 11:41:19 -0800312 return sig & MD_SIG_Out;
Stephen Hinesd8817752013-08-02 17:56:51 -0700313 }
314
315 /**
316 * \return whether or not this ForEach function signature has a "UsrData"
317 * parameter.
318 *
319 * \param sig - ForEach function signature to check.
320 */
321 static bool hasForEachSignatureUsrData(uint32_t sig) {
David Gross33cda5c2015-01-30 11:41:19 -0800322 return sig & MD_SIG_Usr;
Stephen Hinesd8817752013-08-02 17:56:51 -0700323 }
324
325 /**
326 * \return whether or not this ForEach function signature has an "X"
327 * parameter.
328 *
329 * \param sig - ForEach function signature to check.
330 */
331 static bool hasForEachSignatureX(uint32_t sig) {
David Gross33cda5c2015-01-30 11:41:19 -0800332 return sig & MD_SIG_X;
Stephen Hinesd8817752013-08-02 17:56:51 -0700333 }
334
335 /**
336 * \return whether or not this ForEach function signature has a "Y"
337 * parameter.
338 *
339 * \param sig - ForEach function signature to check.
340 */
341 static bool hasForEachSignatureY(uint32_t sig) {
David Gross33cda5c2015-01-30 11:41:19 -0800342 return sig & MD_SIG_Y;
Stephen Hinesd8817752013-08-02 17:56:51 -0700343 }
344
345 /**
346 * \return whether or not this ForEach function signature is a
347 * pass-by-value "Kernel".
348 *
349 * \param sig - ForEach function signature to check.
350 */
351 static bool hasForEachSignatureKernel(uint32_t sig) {
David Gross33cda5c2015-01-30 11:41:19 -0800352 return sig & MD_SIG_Kernel;
353 }
354
355 /**
356 * \return whether or not this ForEach function signature has a "Z"
357 * parameter.
358 *
359 * \param sig - ForEach function signature to check.
360 */
361 static bool hasForEachSignatureZ(uint32_t sig) {
362 return sig & MD_SIG_Z;
363 }
364
365 /**
366 * \return whether or not this ForEach function signature has a "Ctxt"
367 * parameter.
368 *
369 * \param sig - ForEach function signature to check.
370 */
371 static bool hasForEachSignatureCtxt(uint32_t sig) {
372 return sig & MD_SIG_Ctxt;
Stephen Hinesd8817752013-08-02 17:56:51 -0700373 }
Pirama Arumuga Nainar9fe081b2015-01-27 14:09:19 -0800374
Pirama Arumuga Nainar7de5fde2015-01-27 14:09:19 -0800375 /**
376 * \return whether "Kernels" in this script can be processed
377 * by multiple threads
378 */
379
380 bool isThreadable() const {
Pirama Arumuga Nainar9fe081b2015-01-27 14:09:19 -0800381 return mIsThreadable;
382 }
383
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -0800384 /**
385 * \return the build checksum extracted from the LLVM metadata
386 */
387 const char *getBuildChecksum() const {
388 return mBuildChecksum;
389 }
Dean De Leofff398d2015-11-25 13:00:31 +0000390
391 /**
392 * \return whether the module contains debug metadata
393 */
394 bool hasDebugInfo() const {
395 return mHasDebugInfo;
396 }
Stephen Hines932bc6e2011-07-27 16:26:26 -0700397};
398
399} // namespace bcinfo
400
401#endif // __ANDROID_BCINFO_METADATAEXTRACTOR_H__