Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 1 | /* |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 2 | * Copyright 2011-2012, The Android Open Source Project |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 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 "bcinfo/MetadataExtractor.h" |
| 18 | |
Stephen Hines | b67c9e7 | 2012-03-22 11:02:48 -0700 | [diff] [blame] | 19 | #include "bcinfo/BitcodeWrapper.h" |
Stephen Hines | fb81ec1 | 2015-05-18 20:04:23 -0700 | [diff] [blame] | 20 | #include "rsDefines.h" |
Stephen Hines | b67c9e7 | 2012-03-22 11:02:48 -0700 | [diff] [blame] | 21 | |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 22 | #define LOG_TAG "bcinfo" |
Mark Salyzyn | d5c6901 | 2017-01-10 14:27:14 -0800 | [diff] [blame] | 23 | #include <log/log.h> |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 24 | |
David Gross | cf5afcb | 2017-03-20 15:23:26 -0700 | [diff] [blame] | 25 | #include "Assert.h" |
| 26 | |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 27 | #include "llvm/Bitcode/ReaderWriter.h" |
Stephen Hines | b730e23 | 2013-01-09 15:31:36 -0800 | [diff] [blame] | 28 | #include "llvm/IR/Constants.h" |
| 29 | #include "llvm/IR/LLVMContext.h" |
| 30 | #include "llvm/IR/Module.h" |
Chris Wailes | a668182 | 2014-07-11 15:15:46 -0700 | [diff] [blame] | 31 | #include "llvm/IR/Function.h" |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 32 | #include "llvm/Support/MemoryBuffer.h" |
| 33 | |
Miao Wang | 110b1c1 | 2017-03-03 12:20:24 -0800 | [diff] [blame] | 34 | #ifdef __ANDROID__ |
| 35 | #include "Properties.h" |
| 36 | #endif |
| 37 | |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 38 | #include <cstdlib> |
| 39 | |
| 40 | namespace bcinfo { |
| 41 | |
Stephen Hines | 1bd9f62 | 2015-03-18 14:53:10 -0700 | [diff] [blame] | 42 | namespace { |
| 43 | |
Stephen Hines | e1c7d29 | 2015-04-15 13:20:00 -0700 | [diff] [blame] | 44 | llvm::StringRef getStringOperand(const llvm::Metadata *node) { |
| 45 | if (auto *mds = llvm::dyn_cast_or_null<const llvm::MDString>(node)) { |
Stephen Hines | 1bd9f62 | 2015-03-18 14:53:10 -0700 | [diff] [blame] | 46 | return mds->getString(); |
| 47 | } |
| 48 | return llvm::StringRef(); |
| 49 | } |
Stephen Hines | e1c7d29 | 2015-04-15 13:20:00 -0700 | [diff] [blame] | 50 | |
| 51 | bool 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 Wala | 1895ac1 | 2015-07-16 15:34:29 -0700 | [diff] [blame] | 62 | const 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 Hines | 1bd9f62 | 2015-03-18 14:53:10 -0700 | [diff] [blame] | 68 | } |
| 69 | |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 70 | const 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 Wala | 1895ac1 | 2015-07-16 15:34:29 -0700 | [diff] [blame] | 77 | // 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. |
| 100 | bool 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 Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 138 | // Name of metadata node where pragma info resides (should be synced with |
| 139 | // slang.cpp) |
| 140 | static 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) |
| 144 | static 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) |
| 148 | static const llvm::StringRef ExportFuncMetadataName = "#rs_export_func"; |
| 149 | |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 150 | // Name of metadata node where exported ForEach name information resides |
| 151 | // (should be synced with slang_rs_metadata.h) |
| 152 | static const llvm::StringRef ExportForEachNameMetadataName = |
| 153 | "#rs_export_foreach_name"; |
| 154 | |
Stephen Hines | 33f8fe2 | 2011-08-17 12:56:22 -0700 | [diff] [blame] | 155 | // Name of metadata node where exported ForEach signature information resides |
| 156 | // (should be synced with slang_rs_metadata.h) |
| 157 | static const llvm::StringRef ExportForEachMetadataName = "#rs_export_foreach"; |
| 158 | |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 159 | // Name of metadata node where exported general reduce information resides |
| 160 | // (should be synced with slang_rs_metadata.h) |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 161 | static const llvm::StringRef ExportReduceMetadataName = "#rs_export_reduce"; |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 162 | |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 163 | // Name of metadata node where RS object slot info resides (should be |
| 164 | // synced with slang_rs_metadata.h) |
| 165 | static const llvm::StringRef ObjectSlotMetadataName = "#rs_object_slots"; |
| 166 | |
Pirama Arumuga Nainar | 9fe081b | 2015-01-27 14:09:19 -0800 | [diff] [blame] | 167 | static const llvm::StringRef ThreadableMetadataName = "#rs_is_threadable"; |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 168 | |
Pirama Arumuga Nainar | 51ee77b | 2015-02-19 16:33:27 -0800 | [diff] [blame] | 169 | // Name of metadata node where the checksum for this build is stored. (should |
| 170 | // be synced with libbcc/lib/Core/Source.cpp) |
| 171 | static const llvm::StringRef ChecksumMetadataName = "#rs_build_checksum"; |
| 172 | |
Dean De Leo | fff398d | 2015-11-25 13:00:31 +0000 | [diff] [blame] | 173 | // 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. |
| 176 | static const llvm::StringRef DebugInfoMetadataName = "llvm.dbg.cu"; |
| 177 | |
David Gross | cf5afcb | 2017-03-20 15:23:26 -0700 | [diff] [blame] | 178 | const char MetadataExtractor::kWrapperMetadataName[] = "#rs_wrapper"; |
| 179 | |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 180 | MetadataExtractor::MetadataExtractor(const char *bitcode, size_t bitcodeSize) |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 181 | : mModule(nullptr), mBitcode(bitcode), mBitcodeSize(bitcodeSize), |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 182 | mExportVarCount(0), mExportFuncCount(0), mExportForEachSignatureCount(0), |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 183 | mExportReduceCount(0), mExportVarNameList(nullptr), |
Matt Wala | 1895ac1 | 2015-07-16 15:34:29 -0700 | [diff] [blame] | 184 | mExportFuncNameList(nullptr), mExportForEachNameList(nullptr), |
| 185 | mExportForEachSignatureList(nullptr), |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 186 | mExportForEachInputCountList(nullptr), |
| 187 | mExportReduceList(nullptr), |
Matt Wala | 1895ac1 | 2015-07-16 15:34:29 -0700 | [diff] [blame] | 188 | mPragmaCount(0), mPragmaKeyList(nullptr), mPragmaValueList(nullptr), |
| 189 | mObjectSlotCount(0), mObjectSlotList(nullptr), |
| 190 | mRSFloatPrecision(RS_FP_Full), mIsThreadable(true), |
Dean De Leo | fff398d | 2015-11-25 13:00:31 +0000 | [diff] [blame] | 191 | mBuildChecksum(nullptr), mHasDebugInfo(false) { |
Stephen Hines | b67c9e7 | 2012-03-22 11:02:48 -0700 | [diff] [blame] | 192 | BitcodeWrapper wrapper(bitcode, bitcodeSize); |
| 193 | mCompilerVersion = wrapper.getCompilerVersion(); |
| 194 | mOptimizationLevel = wrapper.getOptimizationLevel(); |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 197 | MetadataExtractor::MetadataExtractor(const llvm::Module *module) |
Matt Wala | 1895ac1 | 2015-07-16 15:34:29 -0700 | [diff] [blame] | 198 | : mModule(module), mBitcode(nullptr), mBitcodeSize(0), |
| 199 | mExportVarCount(0), mExportFuncCount(0), mExportForEachSignatureCount(0), |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 200 | mExportReduceCount(0), mExportVarNameList(nullptr), |
Matt Wala | 1895ac1 | 2015-07-16 15:34:29 -0700 | [diff] [blame] | 201 | mExportFuncNameList(nullptr), mExportForEachNameList(nullptr), |
| 202 | mExportForEachSignatureList(nullptr), |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 203 | mExportForEachInputCountList(nullptr), |
| 204 | mExportReduceList(nullptr), |
Matt Wala | 1895ac1 | 2015-07-16 15:34:29 -0700 | [diff] [blame] | 205 | mPragmaCount(0), mPragmaKeyList(nullptr), mPragmaValueList(nullptr), |
| 206 | mObjectSlotCount(0), mObjectSlotList(nullptr), |
| 207 | mRSFloatPrecision(RS_FP_Full), mIsThreadable(true), |
| 208 | mBuildChecksum(nullptr) { |
David Gross | cf5afcb | 2017-03-20 15:23:26 -0700 | [diff] [blame] | 209 | 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 Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | |
| 220 | MetadataExtractor::~MetadataExtractor() { |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 221 | if (mExportVarNameList) { |
| 222 | for (size_t i = 0; i < mExportVarCount; i++) { |
| 223 | delete [] mExportVarNameList[i]; |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 224 | mExportVarNameList[i] = nullptr; |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | delete [] mExportVarNameList; |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 228 | mExportVarNameList = nullptr; |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 229 | |
| 230 | if (mExportFuncNameList) { |
| 231 | for (size_t i = 0; i < mExportFuncCount; i++) { |
| 232 | delete [] mExportFuncNameList[i]; |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 233 | mExportFuncNameList[i] = nullptr; |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | delete [] mExportFuncNameList; |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 237 | mExportFuncNameList = nullptr; |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 238 | |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 239 | if (mExportForEachNameList) { |
| 240 | for (size_t i = 0; i < mExportForEachSignatureCount; i++) { |
| 241 | delete [] mExportForEachNameList[i]; |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 242 | mExportForEachNameList[i] = nullptr; |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | delete [] mExportForEachNameList; |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 246 | mExportForEachNameList = nullptr; |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 247 | |
Stephen Hines | 33f8fe2 | 2011-08-17 12:56:22 -0700 | [diff] [blame] | 248 | delete [] mExportForEachSignatureList; |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 249 | mExportForEachSignatureList = nullptr; |
Stephen Hines | 33f8fe2 | 2011-08-17 12:56:22 -0700 | [diff] [blame] | 250 | |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 251 | delete [] mExportForEachInputCountList; |
| 252 | mExportForEachInputCountList = nullptr; |
| 253 | |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 254 | delete [] mExportReduceList; |
| 255 | mExportReduceList = nullptr; |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 256 | |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 257 | for (size_t i = 0; i < mPragmaCount; i++) { |
| 258 | if (mPragmaKeyList) { |
| 259 | delete [] mPragmaKeyList[i]; |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 260 | mPragmaKeyList[i] = nullptr; |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 261 | } |
| 262 | if (mPragmaValueList) { |
| 263 | delete [] mPragmaValueList[i]; |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 264 | mPragmaValueList[i] = nullptr; |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | delete [] mPragmaKeyList; |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 268 | mPragmaKeyList = nullptr; |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 269 | delete [] mPragmaValueList; |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 270 | mPragmaValueList = nullptr; |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 271 | |
| 272 | delete [] mObjectSlotList; |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 273 | mObjectSlotList = nullptr; |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 274 | |
Pirama Arumuga Nainar | 51ee77b | 2015-02-19 16:33:27 -0800 | [diff] [blame] | 275 | delete [] mBuildChecksum; |
| 276 | |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 277 | return; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | bool 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 | |
George Burgess IV | 156465e | 2018-04-24 16:38:13 -0700 | [diff] [blame] | 293 | std::unique_ptr<uint32_t[]> TmpSlotList(new uint32_t[mObjectSlotCount]()); |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 294 | for (size_t i = 0; i < mObjectSlotCount; i++) { |
| 295 | llvm::MDNode *ObjectSlot = ObjectSlotMetadata->getOperand(i); |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 296 | if (ObjectSlot != nullptr && ObjectSlot->getNumOperands() == 1) { |
Stephen Hines | e1c7d29 | 2015-04-15 13:20:00 -0700 | [diff] [blame] | 297 | if (!extractUIntFromMetadataString(&TmpSlotList[i], ObjectSlot->getOperand(0))) { |
| 298 | ALOGE("Non-integer object slot value"); |
| 299 | return false; |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 300 | } |
Stephen Hines | e1c7d29 | 2015-04-15 13:20:00 -0700 | [diff] [blame] | 301 | } else { |
| 302 | ALOGE("Corrupt object slot information"); |
| 303 | return false; |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
George Burgess IV | 156465e | 2018-04-24 16:38:13 -0700 | [diff] [blame] | 307 | delete [] mObjectSlotList; |
| 308 | mObjectSlotList = TmpSlotList.release(); |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 309 | return true; |
| 310 | } |
| 311 | |
| 312 | |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 313 | void MetadataExtractor::populatePragmaMetadata( |
| 314 | const llvm::NamedMDNode *PragmaMetadata) { |
| 315 | if (!PragmaMetadata) { |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | mPragmaCount = PragmaMetadata->getNumOperands(); |
| 320 | if (!mPragmaCount) { |
| 321 | return; |
| 322 | } |
| 323 | |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 324 | const char **TmpKeyList = new const char*[mPragmaCount]; |
| 325 | const char **TmpValueList = new const char*[mPragmaCount]; |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 326 | |
| 327 | for (size_t i = 0; i < mPragmaCount; i++) { |
| 328 | llvm::MDNode *Pragma = PragmaMetadata->getOperand(i); |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 329 | if (Pragma != nullptr && Pragma->getNumOperands() == 2) { |
Stephen Hines | 1bd9f62 | 2015-03-18 14:53:10 -0700 | [diff] [blame] | 330 | llvm::Metadata *PragmaKeyMDS = Pragma->getOperand(0); |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 331 | TmpKeyList[i] = createStringFromValue(PragmaKeyMDS); |
Stephen Hines | 1bd9f62 | 2015-03-18 14:53:10 -0700 | [diff] [blame] | 332 | llvm::Metadata *PragmaValueMDS = Pragma->getOperand(1); |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 333 | TmpValueList[i] = createStringFromValue(PragmaValueMDS); |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 337 | mPragmaKeyList = TmpKeyList; |
| 338 | mPragmaValueList = TmpValueList; |
| 339 | |
Stephen Hines | e1fd804 | 2012-03-27 11:03:46 -0700 | [diff] [blame] | 340 | // Check to see if we have any FP precision-related pragmas. |
| 341 | std::string Relaxed("rs_fp_relaxed"); |
| 342 | std::string Imprecise("rs_fp_imprecise"); |
Stephen Hines | 32c56ec | 2012-06-01 15:26:30 -0700 | [diff] [blame] | 343 | std::string Full("rs_fp_full"); |
Stephen Hines | e1fd804 | 2012-03-27 11:03:46 -0700 | [diff] [blame] | 344 | bool RelaxedPragmaSeen = false; |
Jean-Luc Brouillet | 5f2e6b9 | 2014-07-08 11:10:03 -0700 | [diff] [blame] | 345 | bool FullPragmaSeen = false; |
Stephen Hines | e1fd804 | 2012-03-27 11:03:46 -0700 | [diff] [blame] | 346 | for (size_t i = 0; i < mPragmaCount; i++) { |
| 347 | if (!Relaxed.compare(mPragmaKeyList[i])) { |
Stephen Hines | e1fd804 | 2012-03-27 11:03:46 -0700 | [diff] [blame] | 348 | RelaxedPragmaSeen = true; |
| 349 | } else if (!Imprecise.compare(mPragmaKeyList[i])) { |
Jean-Luc Brouillet | 5f2e6b9 | 2014-07-08 11:10:03 -0700 | [diff] [blame] | 350 | ALOGW("rs_fp_imprecise is deprecated. Assuming rs_fp_relaxed instead."); |
| 351 | RelaxedPragmaSeen = true; |
| 352 | } else if (!Full.compare(mPragmaKeyList[i])) { |
| 353 | FullPragmaSeen = true; |
Stephen Hines | e1fd804 | 2012-03-27 11:03:46 -0700 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
Jean-Luc Brouillet | 5f2e6b9 | 2014-07-08 11:10:03 -0700 | [diff] [blame] | 357 | if (RelaxedPragmaSeen && FullPragmaSeen) { |
| 358 | ALOGE("Full and relaxed precision specified at the same time!"); |
Stephen Hines | e1fd804 | 2012-03-27 11:03:46 -0700 | [diff] [blame] | 359 | } |
Jean-Luc Brouillet | 5f2e6b9 | 2014-07-08 11:10:03 -0700 | [diff] [blame] | 360 | mRSFloatPrecision = RelaxedPragmaSeen ? RS_FP_Relaxed : RS_FP_Full; |
Stephen Hines | e1fd804 | 2012-03-27 11:03:46 -0700 | [diff] [blame] | 361 | |
Elliott Hughes | fe0de78 | 2015-08-12 15:09:33 -0700 | [diff] [blame] | 362 | #ifdef __ANDROID__ |
Jean-Luc Brouillet | 5f2e6b9 | 2014-07-08 11:10:03 -0700 | [diff] [blame] | 363 | // Provide an override for precsiion via adb shell setprop |
Stephen Hines | 32c56ec | 2012-06-01 15:26:30 -0700 | [diff] [blame] | 364 | // adb shell setprop debug.rs.precision rs_fp_full |
| 365 | // adb shell setprop debug.rs.precision rs_fp_relaxed |
| 366 | // adb shell setprop debug.rs.precision rs_fp_imprecise |
Miao Wang | 110b1c1 | 2017-03-03 12:20:24 -0800 | [diff] [blame] | 367 | char PrecisionPropBuf[PROP_VALUE_MAX]; |
Stephen Hines | 32c56ec | 2012-06-01 15:26:30 -0700 | [diff] [blame] | 368 | const std::string PrecisionPropName("debug.rs.precision"); |
| 369 | property_get("debug.rs.precision", PrecisionPropBuf, ""); |
| 370 | if (PrecisionPropBuf[0]) { |
| 371 | if (!Relaxed.compare(PrecisionPropBuf)) { |
Jean-Luc Brouillet | 5f2e6b9 | 2014-07-08 11:10:03 -0700 | [diff] [blame] | 372 | ALOGI("Switching to RS FP relaxed mode via setprop"); |
Stephen Hines | 32c56ec | 2012-06-01 15:26:30 -0700 | [diff] [blame] | 373 | mRSFloatPrecision = RS_FP_Relaxed; |
| 374 | } else if (!Imprecise.compare(PrecisionPropBuf)) { |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 375 | ALOGW("Switching to RS FP relaxed mode via setprop. rs_fp_imprecise was " |
| 376 | "specified but is deprecated "); |
Jean-Luc Brouillet | 5f2e6b9 | 2014-07-08 11:10:03 -0700 | [diff] [blame] | 377 | mRSFloatPrecision = RS_FP_Relaxed; |
Stephen Hines | 32c56ec | 2012-06-01 15:26:30 -0700 | [diff] [blame] | 378 | } else if (!Full.compare(PrecisionPropBuf)) { |
Jean-Luc Brouillet | 5f2e6b9 | 2014-07-08 11:10:03 -0700 | [diff] [blame] | 379 | ALOGI("Switching to RS FP full mode via setprop"); |
Stephen Hines | 32c56ec | 2012-06-01 15:26:30 -0700 | [diff] [blame] | 380 | mRSFloatPrecision = RS_FP_Full; |
Jean-Luc Brouillet | 5f2e6b9 | 2014-07-08 11:10:03 -0700 | [diff] [blame] | 381 | } else { |
| 382 | ALOGE("Unrecognized debug.rs.precision %s", PrecisionPropBuf); |
Stephen Hines | 32c56ec | 2012-06-01 15:26:30 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
Nick Kralevich | 85d9571 | 2013-05-22 13:49:01 -0700 | [diff] [blame] | 385 | #endif |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Chris Wailes | a668182 | 2014-07-11 15:15:46 -0700 | [diff] [blame] | 388 | uint32_t MetadataExtractor::calculateNumInputs(const llvm::Function *Function, |
| 389 | uint32_t Signature) { |
| 390 | |
| 391 | if (hasForEachSignatureIn(Signature)) { |
| 392 | uint32_t OtherCount = 0; |
| 393 | |
| 394 | OtherCount += hasForEachSignatureUsrData(Signature); |
| 395 | OtherCount += hasForEachSignatureX(Signature); |
| 396 | OtherCount += hasForEachSignatureY(Signature); |
David Gross | 33cda5c | 2015-01-30 11:41:19 -0800 | [diff] [blame] | 397 | OtherCount += hasForEachSignatureZ(Signature); |
| 398 | OtherCount += hasForEachSignatureCtxt(Signature); |
Chris Wailes | a668182 | 2014-07-11 15:15:46 -0700 | [diff] [blame] | 399 | OtherCount += hasForEachSignatureOut(Signature) && |
| 400 | Function->getReturnType()->isVoidTy(); |
| 401 | |
| 402 | return Function->arg_size() - OtherCount; |
| 403 | |
| 404 | } else { |
| 405 | return 0; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | |
Stephen Hines | 33f8fe2 | 2011-08-17 12:56:22 -0700 | [diff] [blame] | 410 | bool MetadataExtractor::populateForEachMetadata( |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 411 | const llvm::NamedMDNode *Names, |
| 412 | const llvm::NamedMDNode *Signatures) { |
Stephen Hines | 11de13e | 2013-06-07 11:41:18 -0700 | [diff] [blame] | 413 | if (!Names && !Signatures && mCompilerVersion == 0) { |
Stephen Hines | 0231bb0 | 2011-10-10 15:30:25 -0700 | [diff] [blame] | 414 | // Handle legacy case for pre-ICS bitcode that doesn't contain a metadata |
| 415 | // section for ForEach. We generate a full signature for a "root" function |
| 416 | // which means that we need to set the bottom 5 bits in the mask. |
| 417 | mExportForEachSignatureCount = 1; |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 418 | char **TmpNameList = new char*[mExportForEachSignatureCount]; |
Stephen Hines | fb81ec1 | 2015-05-18 20:04:23 -0700 | [diff] [blame] | 419 | size_t RootLen = strlen(kRoot) + 1; |
| 420 | TmpNameList[0] = new char[RootLen]; |
| 421 | strncpy(TmpNameList[0], kRoot, RootLen); |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 422 | |
Stephen Hines | 0231bb0 | 2011-10-10 15:30:25 -0700 | [diff] [blame] | 423 | uint32_t *TmpSigList = new uint32_t[mExportForEachSignatureCount]; |
| 424 | TmpSigList[0] = 0x1f; |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 425 | |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 426 | mExportForEachNameList = (const char**)TmpNameList; |
Stephen Hines | 0231bb0 | 2011-10-10 15:30:25 -0700 | [diff] [blame] | 427 | mExportForEachSignatureList = TmpSigList; |
Stephen Hines | 33f8fe2 | 2011-08-17 12:56:22 -0700 | [diff] [blame] | 428 | return true; |
| 429 | } |
| 430 | |
Stephen Hines | 109be67 | 2012-06-06 16:01:59 -0700 | [diff] [blame] | 431 | if (Signatures) { |
| 432 | mExportForEachSignatureCount = Signatures->getNumOperands(); |
| 433 | if (!mExportForEachSignatureCount) { |
| 434 | return true; |
| 435 | } |
| 436 | } else { |
| 437 | mExportForEachSignatureCount = 0; |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 438 | mExportForEachSignatureList = nullptr; |
Stephen Hines | 33f8fe2 | 2011-08-17 12:56:22 -0700 | [diff] [blame] | 439 | return true; |
| 440 | } |
| 441 | |
George Burgess IV | 156465e | 2018-04-24 16:38:13 -0700 | [diff] [blame] | 442 | std::unique_ptr<uint32_t[]> TmpSigList(new uint32_t[mExportForEachSignatureCount]); |
| 443 | std::unique_ptr<const char *[]> TmpNameList(new const char*[mExportForEachSignatureCount]); |
| 444 | std::unique_ptr<uint32_t[]> TmpInputCountList(new uint32_t[mExportForEachSignatureCount]); |
Stephen Hines | 33f8fe2 | 2011-08-17 12:56:22 -0700 | [diff] [blame] | 445 | |
| 446 | for (size_t i = 0; i < mExportForEachSignatureCount; i++) { |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 447 | llvm::MDNode *SigNode = Signatures->getOperand(i); |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 448 | if (SigNode != nullptr && SigNode->getNumOperands() == 1) { |
Stephen Hines | e1c7d29 | 2015-04-15 13:20:00 -0700 | [diff] [blame] | 449 | if (!extractUIntFromMetadataString(&TmpSigList[i], SigNode->getOperand(0))) { |
| 450 | ALOGE("Non-integer signature value"); |
| 451 | return false; |
Stephen Hines | 33f8fe2 | 2011-08-17 12:56:22 -0700 | [diff] [blame] | 452 | } |
Stephen Hines | e1c7d29 | 2015-04-15 13:20:00 -0700 | [diff] [blame] | 453 | } else { |
| 454 | ALOGE("Corrupt signature information"); |
| 455 | return false; |
Stephen Hines | 33f8fe2 | 2011-08-17 12:56:22 -0700 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
Stephen Hines | 109be67 | 2012-06-06 16:01:59 -0700 | [diff] [blame] | 459 | if (Names) { |
| 460 | for (size_t i = 0; i < mExportForEachSignatureCount; i++) { |
| 461 | llvm::MDNode *Name = Names->getOperand(i); |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 462 | if (Name != nullptr && Name->getNumOperands() == 1) { |
Stephen Hines | 109be67 | 2012-06-06 16:01:59 -0700 | [diff] [blame] | 463 | TmpNameList[i] = createStringFromValue(Name->getOperand(0)); |
Chris Wailes | a668182 | 2014-07-11 15:15:46 -0700 | [diff] [blame] | 464 | |
David Gross | 8d7fe84 | 2016-02-18 14:00:32 -0800 | [diff] [blame] | 465 | // Note that looking up the function by name can fail: One of |
| 466 | // the uses of MetadataExtractor is as part of the |
| 467 | // RSEmbedInfoPass, which bcc_compat runs sufficiently late in |
| 468 | // the phase order that RSKernelExpandPass has already run and |
| 469 | // the original (UNexpanded) kernel function (TmpNameList[i]) |
| 470 | // may have been deleted as having no references (if it has |
| 471 | // been inlined into the expanded kernel function and is |
| 472 | // otherwise unreferenced). |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 473 | llvm::Function *Func = |
| 474 | mModule->getFunction(llvm::StringRef(TmpNameList[i])); |
Chris Wailes | a668182 | 2014-07-11 15:15:46 -0700 | [diff] [blame] | 475 | |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 476 | TmpInputCountList[i] = (Func != nullptr) ? |
Chris Wailes | a668182 | 2014-07-11 15:15:46 -0700 | [diff] [blame] | 477 | calculateNumInputs(Func, TmpSigList[i]) : 0; |
Stephen Hines | 109be67 | 2012-06-06 16:01:59 -0700 | [diff] [blame] | 478 | } |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 479 | } |
Stephen Hines | 109be67 | 2012-06-06 16:01:59 -0700 | [diff] [blame] | 480 | } else { |
| 481 | if (mExportForEachSignatureCount != 1) { |
Stephen Hines | 3e4cac4 | 2012-06-07 01:57:15 -0700 | [diff] [blame] | 482 | ALOGE("mExportForEachSignatureCount = %zu, but should be 1", |
Stephen Hines | 109be67 | 2012-06-06 16:01:59 -0700 | [diff] [blame] | 483 | mExportForEachSignatureCount); |
| 484 | } |
| 485 | char *RootName = new char[5]; |
| 486 | strncpy(RootName, "root", 5); |
| 487 | TmpNameList[0] = RootName; |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 488 | } |
| 489 | |
George Burgess IV | 156465e | 2018-04-24 16:38:13 -0700 | [diff] [blame] | 490 | delete [] mExportForEachNameList; |
| 491 | mExportForEachNameList = TmpNameList.release(); |
| 492 | |
| 493 | delete [] mExportForEachSignatureList; |
| 494 | mExportForEachSignatureList = TmpSigList.release(); |
| 495 | |
| 496 | delete [] mExportForEachInputCountList; |
| 497 | mExportForEachInputCountList = TmpInputCountList.release(); |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 498 | |
Stephen Hines | 33f8fe2 | 2011-08-17 12:56:22 -0700 | [diff] [blame] | 499 | return true; |
| 500 | } |
| 501 | |
| 502 | |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 503 | bool MetadataExtractor::populateReduceMetadata(const llvm::NamedMDNode *ReduceMetadata) { |
| 504 | mExportReduceCount = 0; |
| 505 | mExportReduceList = nullptr; |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 506 | |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 507 | if (!ReduceMetadata || !(mExportReduceCount = ReduceMetadata->getNumOperands())) |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 508 | return true; |
| 509 | |
Luis Lozano | d389fdd | 2016-10-18 17:18:56 -0700 | [diff] [blame] | 510 | std::unique_ptr<Reduce[]> TmpReduceList(new Reduce[mExportReduceCount]); |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 511 | |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 512 | for (size_t i = 0; i < mExportReduceCount; i++) { |
| 513 | llvm::MDNode *Node = ReduceMetadata->getOperand(i); |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 514 | if (!Node || Node->getNumOperands() < 3) { |
| 515 | ALOGE("Missing reduce metadata"); |
| 516 | return false; |
| 517 | } |
| 518 | |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 519 | TmpReduceList[i].mReduceName = createStringFromValue(Node->getOperand(0)); |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 520 | |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 521 | if (!extractUIntFromMetadataString(&TmpReduceList[i].mAccumulatorDataSize, |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 522 | Node->getOperand(1))) { |
| 523 | ALOGE("Non-integer accumulator data size value in reduce metadata"); |
| 524 | return false; |
| 525 | } |
| 526 | |
| 527 | llvm::MDNode *AccumulatorNode = llvm::dyn_cast<llvm::MDNode>(Node->getOperand(2)); |
| 528 | if (!AccumulatorNode || AccumulatorNode->getNumOperands() != 2) { |
| 529 | ALOGE("Malformed accumulator node in reduce metadata"); |
| 530 | return false; |
| 531 | } |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 532 | TmpReduceList[i].mAccumulatorName = createStringFromValue(AccumulatorNode->getOperand(0)); |
| 533 | if (!extractUIntFromMetadataString(&TmpReduceList[i].mSignature, |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 534 | AccumulatorNode->getOperand(1))) { |
| 535 | ALOGE("Non-integer signature value in reduce metadata"); |
| 536 | return false; |
| 537 | } |
David Gross | 8d7fe84 | 2016-02-18 14:00:32 -0800 | [diff] [blame] | 538 | // Note that looking up the function by name can fail: One of the |
| 539 | // uses of MetadataExtractor is as part of the RSEmbedInfoPass, |
| 540 | // which bcc_compat runs sufficiently late in the phase order that |
| 541 | // RSKernelExpandPass has already run and the original |
| 542 | // (UNexpanded) accumulator function (mAccumulatorName) may have |
| 543 | // been deleted as having no references (if it has been inlined |
| 544 | // into the expanded accumulator function and is otherwise |
| 545 | // unreferenced). |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 546 | llvm::Function *Func = |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 547 | mModule->getFunction(llvm::StringRef(TmpReduceList[i].mAccumulatorName)); |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 548 | // Why calculateNumInputs() - 1? The "-1" is because we don't |
| 549 | // want to treat the accumulator argument as an input. |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 550 | TmpReduceList[i].mInputCount = (Func ? calculateNumInputs(Func, TmpReduceList[i].mSignature) - 1 : 0); |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 551 | |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 552 | TmpReduceList[i].mInitializerName = createStringFromOptionalValue(Node, 3); |
| 553 | TmpReduceList[i].mCombinerName = createStringFromOptionalValue(Node, 4); |
| 554 | TmpReduceList[i].mOutConverterName = createStringFromOptionalValue(Node, 5); |
| 555 | TmpReduceList[i].mHalterName = createStringFromOptionalValue(Node, 6); |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 556 | } |
| 557 | |
Luis Lozano | d389fdd | 2016-10-18 17:18:56 -0700 | [diff] [blame] | 558 | mExportReduceList = TmpReduceList.release(); |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 559 | return true; |
| 560 | } |
| 561 | |
Pirama Arumuga Nainar | 9fe081b | 2015-01-27 14:09:19 -0800 | [diff] [blame] | 562 | void MetadataExtractor::readThreadableFlag( |
| 563 | const llvm::NamedMDNode *ThreadableMetadata) { |
| 564 | |
| 565 | // Scripts are threadable by default. If we read a valid metadata value for |
| 566 | // 'ThreadableMetadataName' and it is set to 'no', we mark script as non |
| 567 | // threadable. All other exception paths retain the default value. |
| 568 | |
| 569 | mIsThreadable = true; |
| 570 | if (ThreadableMetadata == nullptr) |
| 571 | return; |
| 572 | |
| 573 | llvm::MDNode *mdNode = ThreadableMetadata->getOperand(0); |
| 574 | if (mdNode == nullptr) |
| 575 | return; |
| 576 | |
Stephen Hines | 1bd9f62 | 2015-03-18 14:53:10 -0700 | [diff] [blame] | 577 | llvm::Metadata *mdValue = mdNode->getOperand(0); |
Pirama Arumuga Nainar | 9fe081b | 2015-01-27 14:09:19 -0800 | [diff] [blame] | 578 | if (mdValue == nullptr) |
| 579 | return; |
| 580 | |
Stephen Hines | e1c7d29 | 2015-04-15 13:20:00 -0700 | [diff] [blame] | 581 | if (getStringOperand(mdValue) == "no") |
Pirama Arumuga Nainar | 9fe081b | 2015-01-27 14:09:19 -0800 | [diff] [blame] | 582 | mIsThreadable = false; |
Pirama Arumuga Nainar | 9fe081b | 2015-01-27 14:09:19 -0800 | [diff] [blame] | 583 | } |
| 584 | |
Pirama Arumuga Nainar | 51ee77b | 2015-02-19 16:33:27 -0800 | [diff] [blame] | 585 | void MetadataExtractor::readBuildChecksumMetadata( |
| 586 | const llvm::NamedMDNode *ChecksumMetadata) { |
| 587 | |
| 588 | if (ChecksumMetadata == nullptr) |
| 589 | return; |
| 590 | |
Stephen Hines | 1bd9f62 | 2015-03-18 14:53:10 -0700 | [diff] [blame] | 591 | llvm::MDNode *mdNode = ChecksumMetadata->getOperand(0); |
Pirama Arumuga Nainar | 51ee77b | 2015-02-19 16:33:27 -0800 | [diff] [blame] | 592 | if (mdNode == nullptr) |
| 593 | return; |
| 594 | |
Stephen Hines | 1bd9f62 | 2015-03-18 14:53:10 -0700 | [diff] [blame] | 595 | llvm::Metadata *mdValue = mdNode->getOperand(0); |
Pirama Arumuga Nainar | 51ee77b | 2015-02-19 16:33:27 -0800 | [diff] [blame] | 596 | if (mdValue == nullptr) |
| 597 | return; |
| 598 | |
| 599 | mBuildChecksum = createStringFromValue(mdValue); |
| 600 | } |
Pirama Arumuga Nainar | 9fe081b | 2015-01-27 14:09:19 -0800 | [diff] [blame] | 601 | |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 602 | bool MetadataExtractor::extract() { |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 603 | if (!(mBitcode && mBitcodeSize) && !mModule) { |
| 604 | ALOGE("Invalid/empty bitcode/module"); |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 605 | return false; |
| 606 | } |
| 607 | |
Stephen Hines | d0993af | 2014-07-15 16:49:25 -0700 | [diff] [blame] | 608 | std::unique_ptr<llvm::LLVMContext> mContext; |
Pirama Arumuga Nainar | 8e90893 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 609 | bool shouldNullModule = false; |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 610 | |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 611 | if (!mModule) { |
| 612 | mContext.reset(new llvm::LLVMContext()); |
Stephen Hines | d0993af | 2014-07-15 16:49:25 -0700 | [diff] [blame] | 613 | std::unique_ptr<llvm::MemoryBuffer> MEM( |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 614 | llvm::MemoryBuffer::getMemBuffer( |
Stephen Hines | e708ffe | 2012-05-03 15:18:49 -0700 | [diff] [blame] | 615 | llvm::StringRef(mBitcode, mBitcodeSize), "", false)); |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 616 | std::string error; |
| 617 | |
Pirama Arumuga Nainar | 8e90893 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 618 | llvm::ErrorOr<std::unique_ptr<llvm::Module> > errval = |
| 619 | llvm::parseBitcodeFile(MEM.get()->getMemBufferRef(), *mContext); |
Stephen Hines | d0993af | 2014-07-15 16:49:25 -0700 | [diff] [blame] | 620 | if (std::error_code ec = errval.getError()) { |
Tim Murray | c2074ca | 2014-04-08 15:39:08 -0700 | [diff] [blame] | 621 | ALOGE("Could not parse bitcode file"); |
| 622 | ALOGE("%s", ec.message().c_str()); |
| 623 | return false; |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 624 | } |
Pirama Arumuga Nainar | 8e90893 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 625 | |
| 626 | mModule = errval.get().release(); |
| 627 | shouldNullModule = true; |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | const llvm::NamedMDNode *ExportVarMetadata = |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 631 | mModule->getNamedMetadata(ExportVarMetadataName); |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 632 | const llvm::NamedMDNode *ExportFuncMetadata = |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 633 | mModule->getNamedMetadata(ExportFuncMetadataName); |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 634 | const llvm::NamedMDNode *ExportForEachNameMetadata = |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 635 | mModule->getNamedMetadata(ExportForEachNameMetadataName); |
Stephen Hines | 33f8fe2 | 2011-08-17 12:56:22 -0700 | [diff] [blame] | 636 | const llvm::NamedMDNode *ExportForEachMetadata = |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 637 | mModule->getNamedMetadata(ExportForEachMetadataName); |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 638 | const llvm::NamedMDNode *ExportReduceMetadata = |
| 639 | mModule->getNamedMetadata(ExportReduceMetadataName); |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 640 | const llvm::NamedMDNode *PragmaMetadata = |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 641 | mModule->getNamedMetadata(PragmaMetadataName); |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 642 | const llvm::NamedMDNode *ObjectSlotMetadata = |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 643 | mModule->getNamedMetadata(ObjectSlotMetadataName); |
Pirama Arumuga Nainar | 9fe081b | 2015-01-27 14:09:19 -0800 | [diff] [blame] | 644 | const llvm::NamedMDNode *ThreadableMetadata = |
| 645 | mModule->getNamedMetadata(ThreadableMetadataName); |
Pirama Arumuga Nainar | 51ee77b | 2015-02-19 16:33:27 -0800 | [diff] [blame] | 646 | const llvm::NamedMDNode *ChecksumMetadata = |
| 647 | mModule->getNamedMetadata(ChecksumMetadataName); |
Dean De Leo | fff398d | 2015-11-25 13:00:31 +0000 | [diff] [blame] | 648 | const llvm::NamedMDNode *DebugInfoMetadata = |
| 649 | mModule->getNamedMetadata(DebugInfoMetadataName); |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 650 | |
Matt Wala | 1895ac1 | 2015-07-16 15:34:29 -0700 | [diff] [blame] | 651 | if (!populateNameMetadata(ExportVarMetadata, mExportVarNameList, |
| 652 | mExportVarCount)) { |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 653 | ALOGE("Could not populate export variable metadata"); |
Pirama Arumuga Nainar | 8e90893 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 654 | goto err; |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Matt Wala | 1895ac1 | 2015-07-16 15:34:29 -0700 | [diff] [blame] | 657 | if (!populateNameMetadata(ExportFuncMetadata, mExportFuncNameList, |
| 658 | mExportFuncCount)) { |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 659 | ALOGE("Could not populate export function metadata"); |
Pirama Arumuga Nainar | 8e90893 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 660 | goto err; |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 663 | if (!populateForEachMetadata(ExportForEachNameMetadata, |
| 664 | ExportForEachMetadata)) { |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 665 | ALOGE("Could not populate ForEach signature metadata"); |
Pirama Arumuga Nainar | 8e90893 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 666 | goto err; |
Stephen Hines | 33f8fe2 | 2011-08-17 12:56:22 -0700 | [diff] [blame] | 667 | } |
| 668 | |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 669 | if (!populateReduceMetadata(ExportReduceMetadata)) { |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 670 | ALOGE("Could not populate export general reduction metadata"); |
Pirama Arumuga Nainar | 8e90893 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 671 | goto err; |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 674 | populatePragmaMetadata(PragmaMetadata); |
| 675 | |
| 676 | if (!populateObjectSlotMetadata(ObjectSlotMetadata)) { |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 677 | ALOGE("Could not populate object slot metadata"); |
Pirama Arumuga Nainar | 8e90893 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 678 | goto err; |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Pirama Arumuga Nainar | 9fe081b | 2015-01-27 14:09:19 -0800 | [diff] [blame] | 681 | readThreadableFlag(ThreadableMetadata); |
Pirama Arumuga Nainar | 51ee77b | 2015-02-19 16:33:27 -0800 | [diff] [blame] | 682 | readBuildChecksumMetadata(ChecksumMetadata); |
Pirama Arumuga Nainar | 9fe081b | 2015-01-27 14:09:19 -0800 | [diff] [blame] | 683 | |
Dean De Leo | fff398d | 2015-11-25 13:00:31 +0000 | [diff] [blame] | 684 | mHasDebugInfo = DebugInfoMetadata != nullptr; |
| 685 | |
Pirama Arumuga Nainar | 8e90893 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 686 | if (shouldNullModule) { |
| 687 | mModule = nullptr; |
| 688 | } |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 689 | return true; |
Pirama Arumuga Nainar | 8e90893 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 690 | |
| 691 | err: |
| 692 | if (shouldNullModule) { |
| 693 | mModule = nullptr; |
| 694 | } |
| 695 | return false; |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | } // namespace bcinfo |