RSInfoExtractor.cpp revision f7401ab4b42dcbd782f20ba5c279694edbd28516
1/* 2 * Copyright 2012, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17//===----------------------------------------------------------------------===// 18// This file implements RSInfo::ExtractFromSource() 19//===----------------------------------------------------------------------===// 20#include "bcc/Renderscript/RSInfo.h" 21 22#include <llvm/IR/Constants.h> 23#include <llvm/IR/Metadata.h> 24#include <llvm/IR/Module.h> 25 26#include "bcc/Source.h" 27#include "bcc/Support/Log.h" 28 29using namespace bcc; 30 31namespace { 32 33// Name of metadata node where pragma info resides (should be synced with 34// slang.cpp) 35const llvm::StringRef pragma_metadata_name("#pragma"); 36 37/* 38 * The following names should be synced with the one appeared in 39 * slang_rs_metadata.h. 40 */ 41// Name of metadata node where exported variable names reside 42const llvm::StringRef export_var_metadata_name("#rs_export_var"); 43 44// Name of metadata node where exported function names reside 45const llvm::StringRef export_func_metadata_name("#rs_export_func"); 46 47// Name of metadata node where exported ForEach name information resides 48const llvm::StringRef export_foreach_name_metadata_name("#rs_export_foreach_name"); 49 50// Name of metadata node where exported ForEach signature information resides 51const llvm::StringRef export_foreach_metadata_name("#rs_export_foreach"); 52 53// Name of metadata node where RS object slot info resides (should be 54const llvm::StringRef object_slot_metadata_name("#rs_object_slots"); 55 56inline llvm::StringRef getStringFromOperand(const llvm::Value *pString) { 57 if ((pString != NULL) && (pString->getValueID() == llvm::Value::MDStringVal)) { 58 return static_cast<const llvm::MDString *>(pString)->getString(); 59 } 60 return llvm::StringRef(); 61} 62 63template<size_t NumOperands> 64inline size_t getMetadataStringLength(const llvm::NamedMDNode *pMetadata) { 65 if (pMetadata == NULL) { 66 return 0; 67 } 68 69 size_t string_size = 0; 70 for (unsigned i = 0, e = pMetadata->getNumOperands(); i < e; i++) { 71 llvm::MDNode *node = pMetadata->getOperand(i); 72 if ((node != NULL) && (node->getNumOperands() >= NumOperands)) { 73 // Compiler try its best to unroll this loop since NumOperands is a 74 // template parameter (therefore the number of iteration can be determined 75 // at compile-time and it's usually small.) 76 for (unsigned j = 0; j < NumOperands; j++) { 77 llvm::StringRef s = getStringFromOperand(node->getOperand(j)); 78 if (s.size() > 0) { 79 // +1 is for the null-terminator at the end of string. 80 string_size += (s.size() + 1); 81 } 82 } 83 } 84 } 85 86 return string_size; 87} 88 89// Write a string pString to the string pool pStringPool at offset pWriteStart. 90// Return the pointer the pString resides within the string pool. 91const char *writeString(const llvm::StringRef &pString, char *pStringPool, 92 off_t *pWriteStart) { 93 if (pString.empty()) { 94 return pStringPool; 95 } 96 97 char *pStringWriteStart = pStringPool + *pWriteStart; 98 // Copy the string. 99 ::memcpy(pStringWriteStart, pString.data(), pString.size()); 100 // Write null-terminator at the end of the string. 101 pStringWriteStart[ pString.size() ] = '\0'; 102 // Update pWriteStart. 103 *pWriteStart += (pString.size() + 1); 104 105 return pStringWriteStart; 106} 107 108bool writeDependency(const std::string &pSourceName, const uint8_t *pSHA1, 109 char *pStringPool, off_t *pWriteStart, 110 RSInfo::DependencyTableTy &pDepTable) { 111 const char *source_name = writeString(pSourceName, pStringPool, pWriteStart); 112 113 uint8_t *sha1 = reinterpret_cast<uint8_t *>(pStringPool + *pWriteStart); 114 115 // SHA-1 is special. It's size of SHA1_DIGEST_LENGTH (=20) bytes long without 116 // null-terminator. 117 ::memcpy(sha1, pSHA1, SHA1_DIGEST_LENGTH); 118 // Record in the result RSInfo object. 119 pDepTable.push(std::make_pair(source_name, sha1)); 120 // Update the string pool pointer. 121 *pWriteStart += SHA1_DIGEST_LENGTH; 122 123 return true; 124} 125 126} // end anonymous namespace 127 128RSInfo *RSInfo::ExtractFromSource(const Source &pSource, 129 const DependencyTableTy &pDeps) 130{ 131 const llvm::Module &module = pSource.getModule(); 132 const char *module_name = module.getModuleIdentifier().c_str(); 133 134 const llvm::NamedMDNode *pragma = 135 module.getNamedMetadata(pragma_metadata_name); 136 const llvm::NamedMDNode *export_var = 137 module.getNamedMetadata(export_var_metadata_name); 138 const llvm::NamedMDNode *export_func = 139 module.getNamedMetadata(export_func_metadata_name); 140 const llvm::NamedMDNode *export_foreach_name = 141 module.getNamedMetadata(export_foreach_name_metadata_name); 142 const llvm::NamedMDNode *export_foreach_signature = 143 module.getNamedMetadata(export_foreach_metadata_name); 144 const llvm::NamedMDNode *object_slots = 145 module.getNamedMetadata(object_slot_metadata_name); 146 147 // Always write a byte 0x0 at the beginning of the string pool. 148 size_t string_pool_size = 1; 149 off_t cur_string_pool_offset = 0; 150 151 RSInfo *result = NULL; 152 153 // Handle legacy case for pre-ICS bitcode that doesn't contain a metadata 154 // section for ForEach. We generate a full signature for a "root" function. 155 if ((export_foreach_name == NULL) || (export_foreach_signature == NULL)) { 156 export_foreach_name = NULL; 157 export_foreach_signature = NULL; 158 string_pool_size += 5; // insert "root\0" for #rs_export_foreach_name 159 } 160 161 string_pool_size += getMetadataStringLength<2>(pragma); 162 string_pool_size += getMetadataStringLength<1>(export_var); 163 string_pool_size += getMetadataStringLength<1>(export_func); 164 string_pool_size += getMetadataStringLength<1>(export_foreach_name); 165 166 // Don't forget to reserve the space for the dependency informationin string 167 // pool. 168 string_pool_size += ::strlen(LibBCCPath) + 1 + SHA1_DIGEST_LENGTH; 169 string_pool_size += ::strlen(LibCompilerRTPath) + 1 + SHA1_DIGEST_LENGTH; 170 string_pool_size += ::strlen(LibRSPath) + 1 + SHA1_DIGEST_LENGTH; 171 string_pool_size += ::strlen(LibCLCorePath) + 1 + SHA1_DIGEST_LENGTH; 172#if defined(ARCH_ARM_HAVE_NEON) 173 string_pool_size += ::strlen(LibCLCoreNEONPath) + 1 + SHA1_DIGEST_LENGTH; 174#endif 175 for (unsigned i = 0, e = pDeps.size(); i != e; i++) { 176 // +1 for null-terminator 177 string_pool_size += ::strlen(/* name */pDeps[i].first) + 1; 178 // +SHA1_DIGEST_LENGTH for SHA-1 checksum 179 string_pool_size += SHA1_DIGEST_LENGTH; 180 } 181 182 // Allocate result object 183 result = new (std::nothrow) RSInfo(string_pool_size); 184 if (result == NULL) { 185 ALOGE("Out of memory when create RSInfo object for %s!", module_name); 186 goto bail; 187 } 188 189 // Check string pool. 190 if (result->mStringPool == NULL) { 191 ALOGE("Out of memory when allocate string pool in RSInfo object for %s!", 192 module_name); 193 goto bail; 194 } 195 196 // First byte of string pool should be an empty string 197 result->mStringPool[ cur_string_pool_offset++ ] = '\0'; 198 199 // Populate all the strings and data. 200#define FOR_EACH_NODE_IN(_metadata, _node) \ 201 for (unsigned i = 0, e = (_metadata)->getNumOperands(); i != e; i++) \ 202 if (((_node) = (_metadata)->getOperand(i)) != NULL) 203 //===--------------------------------------------------------------------===// 204 // #pragma 205 //===--------------------------------------------------------------------===// 206 // Pragma is actually a key-value pair. The value can be an empty string while 207 // the key cannot. 208 if (pragma != NULL) { 209 llvm::MDNode *node; 210 FOR_EACH_NODE_IN(pragma, node) { 211 llvm::StringRef key = getStringFromOperand(node->getOperand(0)); 212 llvm::StringRef val = getStringFromOperand(node->getOperand(1)); 213 if (key.empty()) { 214 ALOGW("%s contains pragma metadata with empty key (skip)!", 215 module_name); 216 } else { 217 result->mPragmas.push(std::make_pair( 218 writeString(key, result->mStringPool, &cur_string_pool_offset), 219 writeString(val, result->mStringPool, &cur_string_pool_offset))); 220 } // key.empty() 221 } // FOR_EACH_NODE_IN 222 } // pragma != NULL 223 224 //===--------------------------------------------------------------------===// 225 // #rs_export_var 226 //===--------------------------------------------------------------------===// 227 if (export_var != NULL) { 228 llvm::MDNode *node; 229 FOR_EACH_NODE_IN(export_var, node) { 230 llvm::StringRef name = getStringFromOperand(node->getOperand(0)); 231 if (name.empty()) { 232 ALOGW("%s contains empty entry in #rs_export_var metadata (skip)!", 233 module_name); 234 } else { 235 result->mExportVarNames.push( 236 writeString(name, result->mStringPool, &cur_string_pool_offset)); 237 } 238 } 239 } 240 241 //===--------------------------------------------------------------------===// 242 // #rs_export_func 243 //===--------------------------------------------------------------------===// 244 if (export_func != NULL) { 245 llvm::MDNode *node; 246 FOR_EACH_NODE_IN(export_func, node) { 247 llvm::StringRef name = getStringFromOperand(node->getOperand(0)); 248 if (name.empty()) { 249 ALOGW("%s contains empty entry in #rs_export_func metadata (skip)!", 250 module_name); 251 } else { 252 result->mExportFuncNames.push( 253 writeString(name, result->mStringPool, &cur_string_pool_offset)); 254 } 255 } 256 } 257 258 //===--------------------------------------------------------------------===// 259 // #rs_export_foreach and #rs_export_foreach_name 260 //===--------------------------------------------------------------------===// 261 // It's a little bit complicated to deal with #rs_export_foreach (the 262 // signature of foreach-able function) and #rs_export_foreach_name (the name 263 // of function which is foreach-able). We have to maintain a legacy case: 264 // 265 // In pre-ICS bitcode, forEach feature only supports non-graphic root() 266 // function and only one signature corresponded to that non-graphic root() 267 // was written to the #rs_export_foreach metadata section. There's no 268 // #rs_export_foreach_name metadata section. 269 // 270 // Currently, not only non-graphic root() is supported but also other 271 // functions that are exportable. Therefore, a new metadata section 272 // #rs_export_foreach_name is added to specify which functions are 273 // for-eachable. In this case, #rs_export_foreach (the function name) and 274 // #rs_export_foreach metadata (the signature) is one-to-one mapping among 275 // their entries. 276 if ((export_foreach_name != NULL) && (export_foreach_signature != NULL)) { 277 unsigned num_foreach_function; 278 279 // Should be one-to-one mapping. 280 if (export_foreach_name->getNumOperands() != 281 export_foreach_signature->getNumOperands()) { 282 ALOGE("Mismatch number of foreach-able function names (%u) in " 283 "#rs_export_foreach_name and number of signatures (%u) " 284 "in %s!", export_foreach_name->getNumOperands(), 285 export_foreach_signature->getNumOperands(), module_name); 286 goto bail; 287 } 288 289 num_foreach_function = export_foreach_name->getNumOperands(); 290 for (unsigned i = 0; i < num_foreach_function; i++) { 291 llvm::MDNode *name_node = export_foreach_name->getOperand(i); 292 llvm::MDNode *signature_node = export_foreach_signature->getOperand(i); 293 294 llvm::StringRef name, signature_string; 295 if (name_node != NULL) { 296 name = getStringFromOperand(name_node->getOperand(0)); 297 } 298 if (signature_node != NULL) { 299 signature_string = getStringFromOperand(signature_node->getOperand(0)); 300 } 301 302 if (!name.empty() && !signature_string.empty()) { 303 // Both name_node and signature_node are not NULL nodes. 304 uint32_t signature; 305 if (signature_string.getAsInteger(10, signature)) { 306 ALOGE("Non-integer signature value '%s' for function %s found in %s!", 307 signature_string.str().c_str(), name.str().c_str(), module_name); 308 goto bail; 309 } 310 result->mExportForeachFuncs.push(std::make_pair( 311 writeString(name, result->mStringPool, &cur_string_pool_offset), 312 signature)); 313 } else { 314 // One or both of the name and signature value are empty. It's safe only 315 // if both of them are empty. 316 if (name.empty() && signature_string.empty()) { 317 ALOGW("Entries #%u at #rs_export_foreach_name and #rs_export_foreach" 318 " are both NULL in %s! (skip)", i, module_name); 319 continue; 320 } else { 321 ALOGE("Entries #%u at %s is NULL in %s! (skip)", i, 322 (name.empty() ? "#rs_export_foreach_name" : 323 "#rs_export_foreach"), module_name); 324 goto bail; 325 } 326 } 327 } // end for 328 } else { 329 // To handle the legacy case, we generate a full signature for a "root" 330 // function which means that we need to set the bottom 5 bits (0x1f) in the 331 // mask. 332 result->mExportForeachFuncs.push(std::make_pair( 333 writeString(llvm::StringRef("root"), result->mStringPool, 334 &cur_string_pool_offset), 0x1f)); 335 } 336 337 //===--------------------------------------------------------------------===// 338 // #rs_object_slots 339 //===--------------------------------------------------------------------===// 340 if (object_slots != NULL) { 341 llvm::MDNode *node; 342 for (unsigned int i = 0; i <= export_var->getNumOperands(); i++) { 343 result->mObjectSlots.push(0); 344 } 345 FOR_EACH_NODE_IN(object_slots, node) { 346 llvm::StringRef val = getStringFromOperand(node->getOperand(0)); 347 if (val.empty()) { 348 ALOGW("%s contains empty entry in #rs_object_slots (skip)!", 349 module.getModuleIdentifier().c_str()); 350 } else { 351 uint32_t slot; 352 if (val.getAsInteger(10, slot)) { 353 ALOGE("Non-integer object slot value '%s' in %s!", val.str().c_str(), 354 module.getModuleIdentifier().c_str()); 355 goto bail; 356 } else { 357 result->mObjectSlots.editItemAt(slot) = 1; 358 } 359 } 360 } 361 } 362#undef FOR_EACH_NODE_IN 363 364 if (LoadBuiltInSHA1Information()) { 365 //===------------------------------------------------------------------===// 366 // Record built-in dependency information. 367 //===------------------------------------------------------------------===// 368 if (!writeDependency(LibBCCPath, LibBCCSHA1, 369 result->mStringPool, &cur_string_pool_offset, 370 result->mDependencyTable)) { 371 goto bail; 372 } 373 374 if (!writeDependency(LibCompilerRTPath, LibCompilerRTSHA1, 375 result->mStringPool, &cur_string_pool_offset, 376 result->mDependencyTable)) { 377 goto bail; 378 } 379 380 if (!writeDependency(LibRSPath, LibRSSHA1, 381 result->mStringPool, &cur_string_pool_offset, 382 result->mDependencyTable)) { 383 goto bail; 384 } 385 386 if (!writeDependency(LibCLCorePath, LibCLCoreSHA1, 387 result->mStringPool, &cur_string_pool_offset, 388 result->mDependencyTable)) { 389 goto bail; 390 } 391 392#if defined(ARCH_ARM_HAVE_NEON) 393 if (!writeDependency(LibCLCoreNEONPath, LibCLCoreNEONSHA1, 394 result->mStringPool, &cur_string_pool_offset, 395 result->mDependencyTable)) { 396 goto bail; 397 } 398#endif 399 400 //===------------------------------------------------------------------===// 401 // Record dependency information. 402 //===------------------------------------------------------------------===// 403 for (unsigned i = 0, e = pDeps.size(); i != e; i++) { 404 if (!writeDependency(/* name */pDeps[i].first, /* SHA-1 */pDeps[i].second, 405 result->mStringPool, &cur_string_pool_offset, 406 result->mDependencyTable)) { 407 goto bail; 408 } 409 } 410 } 411 412 //===--------------------------------------------------------------------===// 413 // Determine whether the bitcode contains debug information 414 //===--------------------------------------------------------------------===// 415 // The root context of the debug information in the bitcode is put under 416 // the metadata named "llvm.dbg.cu". 417 result->mHeader.hasDebugInformation = 418 static_cast<uint8_t>(module.getNamedMetadata("llvm.dbg.cu") != NULL); 419 420 assert((cur_string_pool_offset == string_pool_size) && 421 "Unexpected string pool size!"); 422 423 return result; 424 425bail: 426 delete result; 427 return NULL; 428} 429