blob: 78d94e55929ac9be46633ddfdb19e71621013c64 [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#include "bcinfo/MetadataExtractor.h"
18
Stephen Hinesb67c9e72012-03-22 11:02:48 -070019#include "bcinfo/BitcodeWrapper.h"
Stephen Hinesfb81ec12015-05-18 20:04:23 -070020#include "rsDefines.h"
Stephen Hinesb67c9e72012-03-22 11:02:48 -070021
Stephen Hines932bc6e2011-07-27 16:26:26 -070022#define LOG_TAG "bcinfo"
Mark Salyzynd5c69012017-01-10 14:27:14 -080023#include <log/log.h>
Stephen Hines932bc6e2011-07-27 16:26:26 -070024
David Grosscf5afcb2017-03-20 15:23:26 -070025#include "Assert.h"
26
Stephen Hines932bc6e2011-07-27 16:26:26 -070027#include "llvm/Bitcode/ReaderWriter.h"
Stephen Hinesb730e232013-01-09 15:31:36 -080028#include "llvm/IR/Constants.h"
29#include "llvm/IR/LLVMContext.h"
30#include "llvm/IR/Module.h"
Chris Wailesa6681822014-07-11 15:15:46 -070031#include "llvm/IR/Function.h"
Stephen Hines932bc6e2011-07-27 16:26:26 -070032#include "llvm/Support/MemoryBuffer.h"
33
Miao Wang110b1c12017-03-03 12:20:24 -080034#ifdef __ANDROID__
35#include "Properties.h"
36#endif
37
Stephen Hines932bc6e2011-07-27 16:26:26 -070038#include <cstdlib>
39
40namespace bcinfo {
41
Stephen Hines1bd9f622015-03-18 14:53:10 -070042namespace {
43
Stephen Hinese1c7d292015-04-15 13:20:00 -070044llvm::StringRef getStringOperand(const llvm::Metadata *node) {
45 if (auto *mds = llvm::dyn_cast_or_null<const llvm::MDString>(node)) {
Stephen Hines1bd9f622015-03-18 14:53:10 -070046 return mds->getString();
47 }
48 return llvm::StringRef();
49}
Stephen Hinese1c7d292015-04-15 13:20:00 -070050
51bool extractUIntFromMetadataString(uint32_t *value,
52 const llvm::Metadata *m) {
53 llvm::StringRef SigString = getStringOperand(m);
54 if (SigString != "") {
55 if (!SigString.getAsInteger(10, *value)) {
56 return true;
57 }
58 }
59 return false;
60}
61
Matt Wala1895ac12015-07-16 15:34:29 -070062const char *createStringFromValue(llvm::Metadata *m) {
63 auto ref = getStringOperand(m);
64 char *c = new char[ref.size() + 1];
65 memcpy(c, ref.data(), ref.size());
66 c[ref.size()] = '\0';
67 return c;
Stephen Hines1bd9f622015-03-18 14:53:10 -070068}
69
David Gross79e1a052016-01-11 14:42:51 -080070const char *createStringFromOptionalValue(llvm::MDNode *n, unsigned opndNum) {
71 llvm::Metadata *opnd;
72 if (opndNum >= n->getNumOperands() || !(opnd = n->getOperand(opndNum)))
73 return nullptr;
74 return createStringFromValue(opnd);
75}
76
Matt Wala1895ac12015-07-16 15:34:29 -070077// Collect metadata from NamedMDNodes that contain a list of names
78// (strings).
79//
80// Inputs:
81//
82// NamedMetadata - An LLVM metadata node, each of whose operands have
83// a string as their first entry
84//
85// NameList - A reference that will hold an allocated array of strings
86//
87// Count - A reference that will hold the length of the allocated
88// array of strings
89//
90// Return value:
91//
92// Return true on success, false on error.
93//
94// Upon success, the function sets NameList to an array of strings
95// corresponding the names found in the metadata. The function sets
96// Count to the number of entries in NameList.
97//
98// An error occurs if one of the metadata operands doesn't have a
99// first entry.
100bool populateNameMetadata(const llvm::NamedMDNode *NameMetadata,
101 const char **&NameList, size_t &Count) {
102 if (!NameMetadata) {
103 NameList = nullptr;
104 Count = 0;
105 return true;
106 }
107
108 Count = NameMetadata->getNumOperands();
109 if (!Count) {
110 NameList = nullptr;
111 return true;
112 }
113
114 NameList = new const char *[Count];
115
116 for (size_t i = 0; i < Count; i++) {
117 llvm::MDNode *Name = NameMetadata->getOperand(i);
118 if (Name && Name->getNumOperands() > 0) {
119 NameList[i] = createStringFromValue(Name->getOperand(0));
120 } else {
121 ALOGE("Metadata operand does not contain a name string");
122 for (size_t AllocatedIndex = 0; AllocatedIndex < i; AllocatedIndex++) {
123 delete [] NameList[AllocatedIndex];
124 }
125 delete [] NameList;
126 NameList = nullptr;
127 Count = 0;
128
129 return false;
130 }
131 }
132
133 return true;
134}
135
136} // end anonymous namespace
137
Stephen Hines932bc6e2011-07-27 16:26:26 -0700138// Name of metadata node where pragma info resides (should be synced with
139// slang.cpp)
140static const llvm::StringRef PragmaMetadataName = "#pragma";
141
142// Name of metadata node where exported variable names reside (should be
143// synced with slang_rs_metadata.h)
144static const llvm::StringRef ExportVarMetadataName = "#rs_export_var";
145
146// Name of metadata node where exported function names reside (should be
147// synced with slang_rs_metadata.h)
148static const llvm::StringRef ExportFuncMetadataName = "#rs_export_func";
149
Stephen Hinescc366e52012-02-21 17:22:04 -0800150// Name of metadata node where exported ForEach name information resides
151// (should be synced with slang_rs_metadata.h)
152static const llvm::StringRef ExportForEachNameMetadataName =
153 "#rs_export_foreach_name";
154
Stephen Hines33f8fe22011-08-17 12:56:22 -0700155// Name of metadata node where exported ForEach signature information resides
156// (should be synced with slang_rs_metadata.h)
157static const llvm::StringRef ExportForEachMetadataName = "#rs_export_foreach";
158
David Gross79e1a052016-01-11 14:42:51 -0800159// Name of metadata node where exported general reduce information resides
160// (should be synced with slang_rs_metadata.h)
David Grossa48ea362016-06-02 14:46:55 -0700161static const llvm::StringRef ExportReduceMetadataName = "#rs_export_reduce";
David Gross79e1a052016-01-11 14:42:51 -0800162
Stephen Hines932bc6e2011-07-27 16:26:26 -0700163// Name of metadata node where RS object slot info resides (should be
164// synced with slang_rs_metadata.h)
165static const llvm::StringRef ObjectSlotMetadataName = "#rs_object_slots";
166
Pirama Arumuga Nainar9fe081b2015-01-27 14:09:19 -0800167static const llvm::StringRef ThreadableMetadataName = "#rs_is_threadable";
Stephen Hines932bc6e2011-07-27 16:26:26 -0700168
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -0800169// Name of metadata node where the checksum for this build is stored. (should
170// be synced with libbcc/lib/Core/Source.cpp)
171static const llvm::StringRef ChecksumMetadataName = "#rs_build_checksum";
172
Dean De Leofff398d2015-11-25 13:00:31 +0000173// Name of metadata node which contains a list of compile units that have debug
174// metadata. If this is null then there is no debug metadata in the compile
175// unit.
176static const llvm::StringRef DebugInfoMetadataName = "llvm.dbg.cu";
177
David Grosscf5afcb2017-03-20 15:23:26 -0700178const char MetadataExtractor::kWrapperMetadataName[] = "#rs_wrapper";
179
Stephen Hines932bc6e2011-07-27 16:26:26 -0700180MetadataExtractor::MetadataExtractor(const char *bitcode, size_t bitcodeSize)
Chris Wailes900c6c12014-08-13 15:40:00 -0700181 : mModule(nullptr), mBitcode(bitcode), mBitcodeSize(bitcodeSize),
Stephen Hines569986d2012-03-09 19:58:45 -0800182 mExportVarCount(0), mExportFuncCount(0), mExportForEachSignatureCount(0),
David Grossa48ea362016-06-02 14:46:55 -0700183 mExportReduceCount(0), mExportVarNameList(nullptr),
Matt Wala1895ac12015-07-16 15:34:29 -0700184 mExportFuncNameList(nullptr), mExportForEachNameList(nullptr),
185 mExportForEachSignatureList(nullptr),
David Grossa48ea362016-06-02 14:46:55 -0700186 mExportForEachInputCountList(nullptr),
187 mExportReduceList(nullptr),
Matt Wala1895ac12015-07-16 15:34:29 -0700188 mPragmaCount(0), mPragmaKeyList(nullptr), mPragmaValueList(nullptr),
189 mObjectSlotCount(0), mObjectSlotList(nullptr),
190 mRSFloatPrecision(RS_FP_Full), mIsThreadable(true),
Dean De Leofff398d2015-11-25 13:00:31 +0000191 mBuildChecksum(nullptr), mHasDebugInfo(false) {
Stephen Hinesb67c9e72012-03-22 11:02:48 -0700192 BitcodeWrapper wrapper(bitcode, bitcodeSize);
193 mCompilerVersion = wrapper.getCompilerVersion();
194 mOptimizationLevel = wrapper.getOptimizationLevel();
Stephen Hines569986d2012-03-09 19:58:45 -0800195}
196
Stephen Hines569986d2012-03-09 19:58:45 -0800197MetadataExtractor::MetadataExtractor(const llvm::Module *module)
Matt Wala1895ac12015-07-16 15:34:29 -0700198 : mModule(module), mBitcode(nullptr), mBitcodeSize(0),
199 mExportVarCount(0), mExportFuncCount(0), mExportForEachSignatureCount(0),
David Grossa48ea362016-06-02 14:46:55 -0700200 mExportReduceCount(0), mExportVarNameList(nullptr),
Matt Wala1895ac12015-07-16 15:34:29 -0700201 mExportFuncNameList(nullptr), mExportForEachNameList(nullptr),
202 mExportForEachSignatureList(nullptr),
David Grossa48ea362016-06-02 14:46:55 -0700203 mExportForEachInputCountList(nullptr),
204 mExportReduceList(nullptr),
Matt Wala1895ac12015-07-16 15:34:29 -0700205 mPragmaCount(0), mPragmaKeyList(nullptr), mPragmaValueList(nullptr),
206 mObjectSlotCount(0), mObjectSlotList(nullptr),
207 mRSFloatPrecision(RS_FP_Full), mIsThreadable(true),
208 mBuildChecksum(nullptr) {
David Grosscf5afcb2017-03-20 15:23:26 -0700209 const llvm::NamedMDNode *const wrapperMDNode = module->getNamedMetadata(kWrapperMetadataName);
210 bccAssert((wrapperMDNode != nullptr) && (wrapperMDNode->getNumOperands() == 1));
211 const llvm::MDNode *const wrapperMDTuple = wrapperMDNode->getOperand(0);
212
213 bool success = true;
214 success &= extractUIntFromMetadataString(&mCompilerVersion, wrapperMDTuple->getOperand(0));
215 success &= extractUIntFromMetadataString(&mOptimizationLevel, wrapperMDTuple->getOperand(1));
216 bccAssert(success);
Stephen Hines932bc6e2011-07-27 16:26:26 -0700217}
218
219
220MetadataExtractor::~MetadataExtractor() {
Stephen Hines569986d2012-03-09 19:58:45 -0800221 if (mExportVarNameList) {
222 for (size_t i = 0; i < mExportVarCount; i++) {
223 delete [] mExportVarNameList[i];
Chris Wailes900c6c12014-08-13 15:40:00 -0700224 mExportVarNameList[i] = nullptr;
Stephen Hines569986d2012-03-09 19:58:45 -0800225 }
226 }
227 delete [] mExportVarNameList;
Chris Wailes900c6c12014-08-13 15:40:00 -0700228 mExportVarNameList = nullptr;
Stephen Hines569986d2012-03-09 19:58:45 -0800229
230 if (mExportFuncNameList) {
231 for (size_t i = 0; i < mExportFuncCount; i++) {
232 delete [] mExportFuncNameList[i];
Chris Wailes900c6c12014-08-13 15:40:00 -0700233 mExportFuncNameList[i] = nullptr;
Stephen Hines569986d2012-03-09 19:58:45 -0800234 }
235 }
236 delete [] mExportFuncNameList;
Chris Wailes900c6c12014-08-13 15:40:00 -0700237 mExportFuncNameList = nullptr;
Stephen Hines569986d2012-03-09 19:58:45 -0800238
Stephen Hinescc366e52012-02-21 17:22:04 -0800239 if (mExportForEachNameList) {
240 for (size_t i = 0; i < mExportForEachSignatureCount; i++) {
241 delete [] mExportForEachNameList[i];
Chris Wailes900c6c12014-08-13 15:40:00 -0700242 mExportForEachNameList[i] = nullptr;
Stephen Hinescc366e52012-02-21 17:22:04 -0800243 }
244 }
245 delete [] mExportForEachNameList;
Chris Wailes900c6c12014-08-13 15:40:00 -0700246 mExportForEachNameList = nullptr;
Stephen Hinescc366e52012-02-21 17:22:04 -0800247
Stephen Hines33f8fe22011-08-17 12:56:22 -0700248 delete [] mExportForEachSignatureList;
Chris Wailes900c6c12014-08-13 15:40:00 -0700249 mExportForEachSignatureList = nullptr;
Stephen Hines33f8fe22011-08-17 12:56:22 -0700250
David Gross79e1a052016-01-11 14:42:51 -0800251 delete [] mExportForEachInputCountList;
252 mExportForEachInputCountList = nullptr;
253
David Grossa48ea362016-06-02 14:46:55 -0700254 delete [] mExportReduceList;
255 mExportReduceList = nullptr;
David Gross79e1a052016-01-11 14:42:51 -0800256
Stephen Hinescc366e52012-02-21 17:22:04 -0800257 for (size_t i = 0; i < mPragmaCount; i++) {
258 if (mPragmaKeyList) {
259 delete [] mPragmaKeyList[i];
Chris Wailes900c6c12014-08-13 15:40:00 -0700260 mPragmaKeyList[i] = nullptr;
Stephen Hinescc366e52012-02-21 17:22:04 -0800261 }
262 if (mPragmaValueList) {
263 delete [] mPragmaValueList[i];
Chris Wailes900c6c12014-08-13 15:40:00 -0700264 mPragmaValueList[i] = nullptr;
Stephen Hines932bc6e2011-07-27 16:26:26 -0700265 }
266 }
267 delete [] mPragmaKeyList;
Chris Wailes900c6c12014-08-13 15:40:00 -0700268 mPragmaKeyList = nullptr;
Stephen Hines932bc6e2011-07-27 16:26:26 -0700269 delete [] mPragmaValueList;
Chris Wailes900c6c12014-08-13 15:40:00 -0700270 mPragmaValueList = nullptr;
Stephen Hines932bc6e2011-07-27 16:26:26 -0700271
272 delete [] mObjectSlotList;
Chris Wailes900c6c12014-08-13 15:40:00 -0700273 mObjectSlotList = nullptr;
Stephen Hines932bc6e2011-07-27 16:26:26 -0700274
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -0800275 delete [] mBuildChecksum;
276
Stephen Hines932bc6e2011-07-27 16:26:26 -0700277 return;
278}
279
280
281bool MetadataExtractor::populateObjectSlotMetadata(
282 const llvm::NamedMDNode *ObjectSlotMetadata) {
283 if (!ObjectSlotMetadata) {
284 return true;
285 }
286
287 mObjectSlotCount = ObjectSlotMetadata->getNumOperands();
288
289 if (!mObjectSlotCount) {
290 return true;
291 }
292
293 uint32_t *TmpSlotList = new uint32_t[mObjectSlotCount];
294 memset(TmpSlotList, 0, mObjectSlotCount * sizeof(*TmpSlotList));
295
296 for (size_t i = 0; i < mObjectSlotCount; i++) {
297 llvm::MDNode *ObjectSlot = ObjectSlotMetadata->getOperand(i);
Chris Wailes900c6c12014-08-13 15:40:00 -0700298 if (ObjectSlot != nullptr && ObjectSlot->getNumOperands() == 1) {
Stephen Hinese1c7d292015-04-15 13:20:00 -0700299 if (!extractUIntFromMetadataString(&TmpSlotList[i], ObjectSlot->getOperand(0))) {
300 ALOGE("Non-integer object slot value");
301 return false;
Stephen Hines932bc6e2011-07-27 16:26:26 -0700302 }
Stephen Hinese1c7d292015-04-15 13:20:00 -0700303 } else {
304 ALOGE("Corrupt object slot information");
305 return false;
Stephen Hines932bc6e2011-07-27 16:26:26 -0700306 }
307 }
308
309 mObjectSlotList = TmpSlotList;
310
311 return true;
312}
313
314
Stephen Hines932bc6e2011-07-27 16:26:26 -0700315void MetadataExtractor::populatePragmaMetadata(
316 const llvm::NamedMDNode *PragmaMetadata) {
317 if (!PragmaMetadata) {
318 return;
319 }
320
321 mPragmaCount = PragmaMetadata->getNumOperands();
322 if (!mPragmaCount) {
323 return;
324 }
325
Stephen Hines569986d2012-03-09 19:58:45 -0800326 const char **TmpKeyList = new const char*[mPragmaCount];
327 const char **TmpValueList = new const char*[mPragmaCount];
Stephen Hines932bc6e2011-07-27 16:26:26 -0700328
329 for (size_t i = 0; i < mPragmaCount; i++) {
330 llvm::MDNode *Pragma = PragmaMetadata->getOperand(i);
Chris Wailes900c6c12014-08-13 15:40:00 -0700331 if (Pragma != nullptr && Pragma->getNumOperands() == 2) {
Stephen Hines1bd9f622015-03-18 14:53:10 -0700332 llvm::Metadata *PragmaKeyMDS = Pragma->getOperand(0);
Stephen Hines569986d2012-03-09 19:58:45 -0800333 TmpKeyList[i] = createStringFromValue(PragmaKeyMDS);
Stephen Hines1bd9f622015-03-18 14:53:10 -0700334 llvm::Metadata *PragmaValueMDS = Pragma->getOperand(1);
Stephen Hines569986d2012-03-09 19:58:45 -0800335 TmpValueList[i] = createStringFromValue(PragmaValueMDS);
Stephen Hines932bc6e2011-07-27 16:26:26 -0700336 }
337 }
338
Stephen Hines569986d2012-03-09 19:58:45 -0800339 mPragmaKeyList = TmpKeyList;
340 mPragmaValueList = TmpValueList;
341
Stephen Hinese1fd8042012-03-27 11:03:46 -0700342 // Check to see if we have any FP precision-related pragmas.
343 std::string Relaxed("rs_fp_relaxed");
344 std::string Imprecise("rs_fp_imprecise");
Stephen Hines32c56ec2012-06-01 15:26:30 -0700345 std::string Full("rs_fp_full");
Stephen Hinese1fd8042012-03-27 11:03:46 -0700346 bool RelaxedPragmaSeen = false;
Jean-Luc Brouillet5f2e6b92014-07-08 11:10:03 -0700347 bool FullPragmaSeen = false;
Stephen Hinese1fd8042012-03-27 11:03:46 -0700348 for (size_t i = 0; i < mPragmaCount; i++) {
349 if (!Relaxed.compare(mPragmaKeyList[i])) {
Stephen Hinese1fd8042012-03-27 11:03:46 -0700350 RelaxedPragmaSeen = true;
351 } else if (!Imprecise.compare(mPragmaKeyList[i])) {
Jean-Luc Brouillet5f2e6b92014-07-08 11:10:03 -0700352 ALOGW("rs_fp_imprecise is deprecated. Assuming rs_fp_relaxed instead.");
353 RelaxedPragmaSeen = true;
354 } else if (!Full.compare(mPragmaKeyList[i])) {
355 FullPragmaSeen = true;
Stephen Hinese1fd8042012-03-27 11:03:46 -0700356 }
357 }
358
Jean-Luc Brouillet5f2e6b92014-07-08 11:10:03 -0700359 if (RelaxedPragmaSeen && FullPragmaSeen) {
360 ALOGE("Full and relaxed precision specified at the same time!");
Stephen Hinese1fd8042012-03-27 11:03:46 -0700361 }
Jean-Luc Brouillet5f2e6b92014-07-08 11:10:03 -0700362 mRSFloatPrecision = RelaxedPragmaSeen ? RS_FP_Relaxed : RS_FP_Full;
Stephen Hinese1fd8042012-03-27 11:03:46 -0700363
Elliott Hughesfe0de782015-08-12 15:09:33 -0700364#ifdef __ANDROID__
Jean-Luc Brouillet5f2e6b92014-07-08 11:10:03 -0700365 // Provide an override for precsiion via adb shell setprop
Stephen Hines32c56ec2012-06-01 15:26:30 -0700366 // adb shell setprop debug.rs.precision rs_fp_full
367 // adb shell setprop debug.rs.precision rs_fp_relaxed
368 // adb shell setprop debug.rs.precision rs_fp_imprecise
Miao Wang110b1c12017-03-03 12:20:24 -0800369 char PrecisionPropBuf[PROP_VALUE_MAX];
Stephen Hines32c56ec2012-06-01 15:26:30 -0700370 const std::string PrecisionPropName("debug.rs.precision");
371 property_get("debug.rs.precision", PrecisionPropBuf, "");
372 if (PrecisionPropBuf[0]) {
373 if (!Relaxed.compare(PrecisionPropBuf)) {
Jean-Luc Brouillet5f2e6b92014-07-08 11:10:03 -0700374 ALOGI("Switching to RS FP relaxed mode via setprop");
Stephen Hines32c56ec2012-06-01 15:26:30 -0700375 mRSFloatPrecision = RS_FP_Relaxed;
376 } else if (!Imprecise.compare(PrecisionPropBuf)) {
Chris Wailes900c6c12014-08-13 15:40:00 -0700377 ALOGW("Switching to RS FP relaxed mode via setprop. rs_fp_imprecise was "
378 "specified but is deprecated ");
Jean-Luc Brouillet5f2e6b92014-07-08 11:10:03 -0700379 mRSFloatPrecision = RS_FP_Relaxed;
Stephen Hines32c56ec2012-06-01 15:26:30 -0700380 } else if (!Full.compare(PrecisionPropBuf)) {
Jean-Luc Brouillet5f2e6b92014-07-08 11:10:03 -0700381 ALOGI("Switching to RS FP full mode via setprop");
Stephen Hines32c56ec2012-06-01 15:26:30 -0700382 mRSFloatPrecision = RS_FP_Full;
Jean-Luc Brouillet5f2e6b92014-07-08 11:10:03 -0700383 } else {
384 ALOGE("Unrecognized debug.rs.precision %s", PrecisionPropBuf);
Stephen Hines32c56ec2012-06-01 15:26:30 -0700385 }
386 }
Nick Kralevich85d95712013-05-22 13:49:01 -0700387#endif
Stephen Hines932bc6e2011-07-27 16:26:26 -0700388}
389
Chris Wailesa6681822014-07-11 15:15:46 -0700390uint32_t MetadataExtractor::calculateNumInputs(const llvm::Function *Function,
391 uint32_t Signature) {
392
393 if (hasForEachSignatureIn(Signature)) {
394 uint32_t OtherCount = 0;
395
396 OtherCount += hasForEachSignatureUsrData(Signature);
397 OtherCount += hasForEachSignatureX(Signature);
398 OtherCount += hasForEachSignatureY(Signature);
David Gross33cda5c2015-01-30 11:41:19 -0800399 OtherCount += hasForEachSignatureZ(Signature);
400 OtherCount += hasForEachSignatureCtxt(Signature);
Chris Wailesa6681822014-07-11 15:15:46 -0700401 OtherCount += hasForEachSignatureOut(Signature) &&
402 Function->getReturnType()->isVoidTy();
403
404 return Function->arg_size() - OtherCount;
405
406 } else {
407 return 0;
408 }
409}
410
411
Stephen Hines33f8fe22011-08-17 12:56:22 -0700412bool MetadataExtractor::populateForEachMetadata(
Stephen Hinescc366e52012-02-21 17:22:04 -0800413 const llvm::NamedMDNode *Names,
414 const llvm::NamedMDNode *Signatures) {
Stephen Hines11de13e2013-06-07 11:41:18 -0700415 if (!Names && !Signatures && mCompilerVersion == 0) {
Stephen Hines0231bb02011-10-10 15:30:25 -0700416 // Handle legacy case for pre-ICS bitcode that doesn't contain a metadata
417 // section for ForEach. We generate a full signature for a "root" function
418 // which means that we need to set the bottom 5 bits in the mask.
419 mExportForEachSignatureCount = 1;
Stephen Hinescc366e52012-02-21 17:22:04 -0800420 char **TmpNameList = new char*[mExportForEachSignatureCount];
Stephen Hinesfb81ec12015-05-18 20:04:23 -0700421 size_t RootLen = strlen(kRoot) + 1;
422 TmpNameList[0] = new char[RootLen];
423 strncpy(TmpNameList[0], kRoot, RootLen);
Stephen Hinescc366e52012-02-21 17:22:04 -0800424
Stephen Hines0231bb02011-10-10 15:30:25 -0700425 uint32_t *TmpSigList = new uint32_t[mExportForEachSignatureCount];
426 TmpSigList[0] = 0x1f;
Stephen Hines569986d2012-03-09 19:58:45 -0800427
Stephen Hinescc366e52012-02-21 17:22:04 -0800428 mExportForEachNameList = (const char**)TmpNameList;
Stephen Hines0231bb02011-10-10 15:30:25 -0700429 mExportForEachSignatureList = TmpSigList;
Stephen Hines33f8fe22011-08-17 12:56:22 -0700430 return true;
431 }
432
Stephen Hines109be672012-06-06 16:01:59 -0700433 if (Signatures) {
434 mExportForEachSignatureCount = Signatures->getNumOperands();
435 if (!mExportForEachSignatureCount) {
436 return true;
437 }
438 } else {
439 mExportForEachSignatureCount = 0;
Chris Wailes900c6c12014-08-13 15:40:00 -0700440 mExportForEachSignatureList = nullptr;
Stephen Hines33f8fe22011-08-17 12:56:22 -0700441 return true;
442 }
443
444 uint32_t *TmpSigList = new uint32_t[mExportForEachSignatureCount];
Stephen Hines569986d2012-03-09 19:58:45 -0800445 const char **TmpNameList = new const char*[mExportForEachSignatureCount];
Chris Wailesa6681822014-07-11 15:15:46 -0700446 uint32_t *TmpInputCountList = new uint32_t[mExportForEachSignatureCount];
Stephen Hines33f8fe22011-08-17 12:56:22 -0700447
448 for (size_t i = 0; i < mExportForEachSignatureCount; i++) {
Stephen Hinescc366e52012-02-21 17:22:04 -0800449 llvm::MDNode *SigNode = Signatures->getOperand(i);
Chris Wailes900c6c12014-08-13 15:40:00 -0700450 if (SigNode != nullptr && SigNode->getNumOperands() == 1) {
Stephen Hinese1c7d292015-04-15 13:20:00 -0700451 if (!extractUIntFromMetadataString(&TmpSigList[i], SigNode->getOperand(0))) {
452 ALOGE("Non-integer signature value");
453 return false;
Stephen Hines33f8fe22011-08-17 12:56:22 -0700454 }
Stephen Hinese1c7d292015-04-15 13:20:00 -0700455 } else {
456 ALOGE("Corrupt signature information");
457 return false;
Stephen Hines33f8fe22011-08-17 12:56:22 -0700458 }
459 }
460
Stephen Hines109be672012-06-06 16:01:59 -0700461 if (Names) {
462 for (size_t i = 0; i < mExportForEachSignatureCount; i++) {
463 llvm::MDNode *Name = Names->getOperand(i);
Chris Wailes900c6c12014-08-13 15:40:00 -0700464 if (Name != nullptr && Name->getNumOperands() == 1) {
Stephen Hines109be672012-06-06 16:01:59 -0700465 TmpNameList[i] = createStringFromValue(Name->getOperand(0));
Chris Wailesa6681822014-07-11 15:15:46 -0700466
David Gross8d7fe842016-02-18 14:00:32 -0800467 // Note that looking up the function by name can fail: One of
468 // the uses of MetadataExtractor is as part of the
469 // RSEmbedInfoPass, which bcc_compat runs sufficiently late in
470 // the phase order that RSKernelExpandPass has already run and
471 // the original (UNexpanded) kernel function (TmpNameList[i])
472 // may have been deleted as having no references (if it has
473 // been inlined into the expanded kernel function and is
474 // otherwise unreferenced).
Chris Wailes900c6c12014-08-13 15:40:00 -0700475 llvm::Function *Func =
476 mModule->getFunction(llvm::StringRef(TmpNameList[i]));
Chris Wailesa6681822014-07-11 15:15:46 -0700477
Chris Wailes900c6c12014-08-13 15:40:00 -0700478 TmpInputCountList[i] = (Func != nullptr) ?
Chris Wailesa6681822014-07-11 15:15:46 -0700479 calculateNumInputs(Func, TmpSigList[i]) : 0;
Stephen Hines109be672012-06-06 16:01:59 -0700480 }
Stephen Hinescc366e52012-02-21 17:22:04 -0800481 }
Stephen Hines109be672012-06-06 16:01:59 -0700482 } else {
483 if (mExportForEachSignatureCount != 1) {
Stephen Hines3e4cac42012-06-07 01:57:15 -0700484 ALOGE("mExportForEachSignatureCount = %zu, but should be 1",
Stephen Hines109be672012-06-06 16:01:59 -0700485 mExportForEachSignatureCount);
486 }
487 char *RootName = new char[5];
488 strncpy(RootName, "root", 5);
489 TmpNameList[0] = RootName;
Stephen Hinescc366e52012-02-21 17:22:04 -0800490 }
491
Stephen Hines569986d2012-03-09 19:58:45 -0800492 mExportForEachNameList = TmpNameList;
493 mExportForEachSignatureList = TmpSigList;
Chris Wailesa6681822014-07-11 15:15:46 -0700494 mExportForEachInputCountList = TmpInputCountList;
Stephen Hines569986d2012-03-09 19:58:45 -0800495
Stephen Hines33f8fe22011-08-17 12:56:22 -0700496 return true;
497}
498
499
David Grossa48ea362016-06-02 14:46:55 -0700500bool MetadataExtractor::populateReduceMetadata(const llvm::NamedMDNode *ReduceMetadata) {
501 mExportReduceCount = 0;
502 mExportReduceList = nullptr;
David Gross79e1a052016-01-11 14:42:51 -0800503
David Grossa48ea362016-06-02 14:46:55 -0700504 if (!ReduceMetadata || !(mExportReduceCount = ReduceMetadata->getNumOperands()))
David Gross79e1a052016-01-11 14:42:51 -0800505 return true;
506
Luis Lozanod389fdd2016-10-18 17:18:56 -0700507 std::unique_ptr<Reduce[]> TmpReduceList(new Reduce[mExportReduceCount]);
David Gross79e1a052016-01-11 14:42:51 -0800508
David Grossa48ea362016-06-02 14:46:55 -0700509 for (size_t i = 0; i < mExportReduceCount; i++) {
510 llvm::MDNode *Node = ReduceMetadata->getOperand(i);
David Gross79e1a052016-01-11 14:42:51 -0800511 if (!Node || Node->getNumOperands() < 3) {
512 ALOGE("Missing reduce metadata");
513 return false;
514 }
515
David Grossa48ea362016-06-02 14:46:55 -0700516 TmpReduceList[i].mReduceName = createStringFromValue(Node->getOperand(0));
David Gross79e1a052016-01-11 14:42:51 -0800517
David Grossa48ea362016-06-02 14:46:55 -0700518 if (!extractUIntFromMetadataString(&TmpReduceList[i].mAccumulatorDataSize,
David Gross79e1a052016-01-11 14:42:51 -0800519 Node->getOperand(1))) {
520 ALOGE("Non-integer accumulator data size value in reduce metadata");
521 return false;
522 }
523
524 llvm::MDNode *AccumulatorNode = llvm::dyn_cast<llvm::MDNode>(Node->getOperand(2));
525 if (!AccumulatorNode || AccumulatorNode->getNumOperands() != 2) {
526 ALOGE("Malformed accumulator node in reduce metadata");
527 return false;
528 }
David Grossa48ea362016-06-02 14:46:55 -0700529 TmpReduceList[i].mAccumulatorName = createStringFromValue(AccumulatorNode->getOperand(0));
530 if (!extractUIntFromMetadataString(&TmpReduceList[i].mSignature,
David Gross79e1a052016-01-11 14:42:51 -0800531 AccumulatorNode->getOperand(1))) {
532 ALOGE("Non-integer signature value in reduce metadata");
533 return false;
534 }
David Gross8d7fe842016-02-18 14:00:32 -0800535 // Note that looking up the function by name can fail: One of the
536 // uses of MetadataExtractor is as part of the RSEmbedInfoPass,
537 // which bcc_compat runs sufficiently late in the phase order that
538 // RSKernelExpandPass has already run and the original
539 // (UNexpanded) accumulator function (mAccumulatorName) may have
540 // been deleted as having no references (if it has been inlined
541 // into the expanded accumulator function and is otherwise
542 // unreferenced).
David Gross79e1a052016-01-11 14:42:51 -0800543 llvm::Function *Func =
David Grossa48ea362016-06-02 14:46:55 -0700544 mModule->getFunction(llvm::StringRef(TmpReduceList[i].mAccumulatorName));
David Gross79e1a052016-01-11 14:42:51 -0800545 // Why calculateNumInputs() - 1? The "-1" is because we don't
546 // want to treat the accumulator argument as an input.
David Grossa48ea362016-06-02 14:46:55 -0700547 TmpReduceList[i].mInputCount = (Func ? calculateNumInputs(Func, TmpReduceList[i].mSignature) - 1 : 0);
David Gross79e1a052016-01-11 14:42:51 -0800548
David Grossa48ea362016-06-02 14:46:55 -0700549 TmpReduceList[i].mInitializerName = createStringFromOptionalValue(Node, 3);
550 TmpReduceList[i].mCombinerName = createStringFromOptionalValue(Node, 4);
551 TmpReduceList[i].mOutConverterName = createStringFromOptionalValue(Node, 5);
552 TmpReduceList[i].mHalterName = createStringFromOptionalValue(Node, 6);
David Gross79e1a052016-01-11 14:42:51 -0800553 }
554
Luis Lozanod389fdd2016-10-18 17:18:56 -0700555 mExportReduceList = TmpReduceList.release();
David Gross79e1a052016-01-11 14:42:51 -0800556 return true;
557}
558
Pirama Arumuga Nainar9fe081b2015-01-27 14:09:19 -0800559void MetadataExtractor::readThreadableFlag(
560 const llvm::NamedMDNode *ThreadableMetadata) {
561
562 // Scripts are threadable by default. If we read a valid metadata value for
563 // 'ThreadableMetadataName' and it is set to 'no', we mark script as non
564 // threadable. All other exception paths retain the default value.
565
566 mIsThreadable = true;
567 if (ThreadableMetadata == nullptr)
568 return;
569
570 llvm::MDNode *mdNode = ThreadableMetadata->getOperand(0);
571 if (mdNode == nullptr)
572 return;
573
Stephen Hines1bd9f622015-03-18 14:53:10 -0700574 llvm::Metadata *mdValue = mdNode->getOperand(0);
Pirama Arumuga Nainar9fe081b2015-01-27 14:09:19 -0800575 if (mdValue == nullptr)
576 return;
577
Stephen Hinese1c7d292015-04-15 13:20:00 -0700578 if (getStringOperand(mdValue) == "no")
Pirama Arumuga Nainar9fe081b2015-01-27 14:09:19 -0800579 mIsThreadable = false;
Pirama Arumuga Nainar9fe081b2015-01-27 14:09:19 -0800580}
581
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -0800582void MetadataExtractor::readBuildChecksumMetadata(
583 const llvm::NamedMDNode *ChecksumMetadata) {
584
585 if (ChecksumMetadata == nullptr)
586 return;
587
Stephen Hines1bd9f622015-03-18 14:53:10 -0700588 llvm::MDNode *mdNode = ChecksumMetadata->getOperand(0);
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -0800589 if (mdNode == nullptr)
590 return;
591
Stephen Hines1bd9f622015-03-18 14:53:10 -0700592 llvm::Metadata *mdValue = mdNode->getOperand(0);
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -0800593 if (mdValue == nullptr)
594 return;
595
596 mBuildChecksum = createStringFromValue(mdValue);
597}
Pirama Arumuga Nainar9fe081b2015-01-27 14:09:19 -0800598
Stephen Hines932bc6e2011-07-27 16:26:26 -0700599bool MetadataExtractor::extract() {
Stephen Hines569986d2012-03-09 19:58:45 -0800600 if (!(mBitcode && mBitcodeSize) && !mModule) {
601 ALOGE("Invalid/empty bitcode/module");
Stephen Hines932bc6e2011-07-27 16:26:26 -0700602 return false;
603 }
604
Stephen Hinesd0993af2014-07-15 16:49:25 -0700605 std::unique_ptr<llvm::LLVMContext> mContext;
Pirama Arumuga Nainar8e908932016-03-06 23:05:45 -0800606 bool shouldNullModule = false;
Stephen Hines932bc6e2011-07-27 16:26:26 -0700607
Stephen Hines569986d2012-03-09 19:58:45 -0800608 if (!mModule) {
609 mContext.reset(new llvm::LLVMContext());
Stephen Hinesd0993af2014-07-15 16:49:25 -0700610 std::unique_ptr<llvm::MemoryBuffer> MEM(
Stephen Hines569986d2012-03-09 19:58:45 -0800611 llvm::MemoryBuffer::getMemBuffer(
Stephen Hinese708ffe2012-05-03 15:18:49 -0700612 llvm::StringRef(mBitcode, mBitcodeSize), "", false));
Stephen Hines569986d2012-03-09 19:58:45 -0800613 std::string error;
614
Pirama Arumuga Nainar8e908932016-03-06 23:05:45 -0800615 llvm::ErrorOr<std::unique_ptr<llvm::Module> > errval =
616 llvm::parseBitcodeFile(MEM.get()->getMemBufferRef(), *mContext);
Stephen Hinesd0993af2014-07-15 16:49:25 -0700617 if (std::error_code ec = errval.getError()) {
Tim Murrayc2074ca2014-04-08 15:39:08 -0700618 ALOGE("Could not parse bitcode file");
619 ALOGE("%s", ec.message().c_str());
620 return false;
Stephen Hines569986d2012-03-09 19:58:45 -0800621 }
Pirama Arumuga Nainar8e908932016-03-06 23:05:45 -0800622
623 mModule = errval.get().release();
624 shouldNullModule = true;
Stephen Hines932bc6e2011-07-27 16:26:26 -0700625 }
626
627 const llvm::NamedMDNode *ExportVarMetadata =
Stephen Hines569986d2012-03-09 19:58:45 -0800628 mModule->getNamedMetadata(ExportVarMetadataName);
Stephen Hines932bc6e2011-07-27 16:26:26 -0700629 const llvm::NamedMDNode *ExportFuncMetadata =
Stephen Hines569986d2012-03-09 19:58:45 -0800630 mModule->getNamedMetadata(ExportFuncMetadataName);
Stephen Hinescc366e52012-02-21 17:22:04 -0800631 const llvm::NamedMDNode *ExportForEachNameMetadata =
Stephen Hines569986d2012-03-09 19:58:45 -0800632 mModule->getNamedMetadata(ExportForEachNameMetadataName);
Stephen Hines33f8fe22011-08-17 12:56:22 -0700633 const llvm::NamedMDNode *ExportForEachMetadata =
Stephen Hines569986d2012-03-09 19:58:45 -0800634 mModule->getNamedMetadata(ExportForEachMetadataName);
David Grossa48ea362016-06-02 14:46:55 -0700635 const llvm::NamedMDNode *ExportReduceMetadata =
636 mModule->getNamedMetadata(ExportReduceMetadataName);
Stephen Hines932bc6e2011-07-27 16:26:26 -0700637 const llvm::NamedMDNode *PragmaMetadata =
Stephen Hines569986d2012-03-09 19:58:45 -0800638 mModule->getNamedMetadata(PragmaMetadataName);
Stephen Hines932bc6e2011-07-27 16:26:26 -0700639 const llvm::NamedMDNode *ObjectSlotMetadata =
Stephen Hines569986d2012-03-09 19:58:45 -0800640 mModule->getNamedMetadata(ObjectSlotMetadataName);
Pirama Arumuga Nainar9fe081b2015-01-27 14:09:19 -0800641 const llvm::NamedMDNode *ThreadableMetadata =
642 mModule->getNamedMetadata(ThreadableMetadataName);
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -0800643 const llvm::NamedMDNode *ChecksumMetadata =
644 mModule->getNamedMetadata(ChecksumMetadataName);
Dean De Leofff398d2015-11-25 13:00:31 +0000645 const llvm::NamedMDNode *DebugInfoMetadata =
646 mModule->getNamedMetadata(DebugInfoMetadataName);
Daniel Malea094881f2011-12-14 17:39:16 -0500647
Matt Wala1895ac12015-07-16 15:34:29 -0700648 if (!populateNameMetadata(ExportVarMetadata, mExportVarNameList,
649 mExportVarCount)) {
Stephen Hines569986d2012-03-09 19:58:45 -0800650 ALOGE("Could not populate export variable metadata");
Pirama Arumuga Nainar8e908932016-03-06 23:05:45 -0800651 goto err;
Stephen Hines932bc6e2011-07-27 16:26:26 -0700652 }
653
Matt Wala1895ac12015-07-16 15:34:29 -0700654 if (!populateNameMetadata(ExportFuncMetadata, mExportFuncNameList,
655 mExportFuncCount)) {
Stephen Hines569986d2012-03-09 19:58:45 -0800656 ALOGE("Could not populate export function metadata");
Pirama Arumuga Nainar8e908932016-03-06 23:05:45 -0800657 goto err;
Stephen Hines932bc6e2011-07-27 16:26:26 -0700658 }
659
Stephen Hinescc366e52012-02-21 17:22:04 -0800660 if (!populateForEachMetadata(ExportForEachNameMetadata,
661 ExportForEachMetadata)) {
Steve Block10c14122012-01-08 10:15:06 +0000662 ALOGE("Could not populate ForEach signature metadata");
Pirama Arumuga Nainar8e908932016-03-06 23:05:45 -0800663 goto err;
Stephen Hines33f8fe22011-08-17 12:56:22 -0700664 }
665
David Grossa48ea362016-06-02 14:46:55 -0700666 if (!populateReduceMetadata(ExportReduceMetadata)) {
David Gross79e1a052016-01-11 14:42:51 -0800667 ALOGE("Could not populate export general reduction metadata");
Pirama Arumuga Nainar8e908932016-03-06 23:05:45 -0800668 goto err;
David Gross79e1a052016-01-11 14:42:51 -0800669 }
670
Stephen Hines932bc6e2011-07-27 16:26:26 -0700671 populatePragmaMetadata(PragmaMetadata);
672
673 if (!populateObjectSlotMetadata(ObjectSlotMetadata)) {
Steve Block10c14122012-01-08 10:15:06 +0000674 ALOGE("Could not populate object slot metadata");
Pirama Arumuga Nainar8e908932016-03-06 23:05:45 -0800675 goto err;
Stephen Hines932bc6e2011-07-27 16:26:26 -0700676 }
677
Pirama Arumuga Nainar9fe081b2015-01-27 14:09:19 -0800678 readThreadableFlag(ThreadableMetadata);
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -0800679 readBuildChecksumMetadata(ChecksumMetadata);
Pirama Arumuga Nainar9fe081b2015-01-27 14:09:19 -0800680
Dean De Leofff398d2015-11-25 13:00:31 +0000681 mHasDebugInfo = DebugInfoMetadata != nullptr;
682
Pirama Arumuga Nainar8e908932016-03-06 23:05:45 -0800683 if (shouldNullModule) {
684 mModule = nullptr;
685 }
Stephen Hines932bc6e2011-07-27 16:26:26 -0700686 return true;
Pirama Arumuga Nainar8e908932016-03-06 23:05:45 -0800687
688err:
689 if (shouldNullModule) {
690 mModule = nullptr;
691 }
692 return false;
Stephen Hines932bc6e2011-07-27 16:26:26 -0700693}
694
695} // namespace bcinfo