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