RSCompilerDriver.cpp revision 06731a6150ae8014d37258d5f32ef8bc14a3db63
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* 29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright 2012, The Android Open Source Project 39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); 59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License. 69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at 79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software 119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and 149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License. 159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 171d3165f10b12165f02b7015ac1a817c5f60e6399Neal Nguyen#include "bcc/Renderscript/RSCompilerDriver.h" 189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <llvm/Support/Path.h> 209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "bcinfo/BitcodeWrapper.h" 221d3165f10b12165f02b7015ac1a817c5f60e6399Neal Nguyen 231d3165f10b12165f02b7015ac1a817c5f60e6399Neal Nguyen#include "bcc/Renderscript/RSExecutable.h" 249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "bcc/Renderscript/RSScript.h" 259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "bcc/Support/CompilerConfig.h" 269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "bcc/Support/TargetCompilerConfigs.h" 279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "bcc/Source.h" 289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "bcc/Support/FileMutex.h" 299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "bcc/Support/Log.h" 309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "bcc/Support/InputFile.h" 319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "bcc/Support/Initialization.h" 329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "bcc/Support/Sha1Util.h" 339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "bcc/Support/OutputFile.h" 349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <cutils/properties.h> 369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/String8.h> 379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/StopWatch.h> 389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectusing namespace bcc; 409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectnamespace { 429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectbool is_force_recompile() { 449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project char buf[PROPERTY_VALUE_MAX]; 459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Re-compile if floating point precision has been overridden. 479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project property_get("debug.rs.precision", buf, ""); 489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (buf[0] != '\0') { 499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return true; 509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Re-compile if debug.rs.forcerecompile is set. 539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project property_get("debug.rs.forcerecompile", buf, "0"); 549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) { 559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return true; 569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } else { 579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return false; 589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} // end anonymous namespace 629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectRSCompilerDriver::RSCompilerDriver(bool pUseCompilerRT) : 649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mConfig(NULL), mCompiler(), mCompilerRuntime(NULL) { 659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project init::Initialize(); 669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Chain the symbol resolvers for compiler_rt and RS runtimes. 679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (pUseCompilerRT) { 689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mCompilerRuntime = new CompilerRTSymbolResolver(); 699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mResolver.chainResolver(*mCompilerRuntime); 709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mResolver.chainResolver(mRSRuntime); 729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectRSCompilerDriver::~RSCompilerDriver() { 759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project delete mCompilerRuntime; 769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project delete mConfig; 779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectRSExecutable * 809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectRSCompilerDriver::loadScriptCache(const char *pOutputPath, 819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project const RSInfo::DependencyTableTy &pDeps) { 829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project android::StopWatch load_time("bcc: RSCompilerDriver::loadScriptCache time"); 839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project RSExecutable *result = NULL; 84 85 if (is_force_recompile()) 86 return NULL; 87 88 //===--------------------------------------------------------------------===// 89 // Acquire the read lock for reading output object file. 90 //===--------------------------------------------------------------------===// 91 FileMutex<FileBase::kReadLock> read_output_mutex(pOutputPath); 92 93 if (read_output_mutex.hasError() || !read_output_mutex.lock()) { 94 ALOGE("Unable to acquire the read lock for %s! (%s)", pOutputPath, 95 read_output_mutex.getErrorMessage().c_str()); 96 return NULL; 97 } 98 99 //===--------------------------------------------------------------------===// 100 // Read the output object file. 101 //===--------------------------------------------------------------------===// 102 InputFile *output_file = new (std::nothrow) InputFile(pOutputPath); 103 104 if ((output_file == NULL) || output_file->hasError()) { 105 ALOGE("Unable to open the %s for read! (%s)", pOutputPath, 106 output_file->getErrorMessage().c_str()); 107 delete output_file; 108 return NULL; 109 } 110 111 //===--------------------------------------------------------------------===// 112 // Acquire the read lock on output_file for reading its RS info file. 113 //===--------------------------------------------------------------------===// 114 android::String8 info_path = RSInfo::GetPath(*output_file); 115 116 if (!output_file->lock()) { 117 ALOGE("Unable to acquire the read lock on %s for reading %s! (%s)", 118 pOutputPath, info_path.string(), 119 output_file->getErrorMessage().c_str()); 120 delete output_file; 121 return NULL; 122 } 123 124 //===---------------------------------------------------------------------===// 125 // Open and load the RS info file. 126 //===--------------------------------------------------------------------===// 127 InputFile info_file(info_path.string()); 128 RSInfo *info = RSInfo::ReadFromFile(info_file, pDeps); 129 130 // Release the lock on output_file. 131 output_file->unlock(); 132 133 if (info == NULL) { 134 delete output_file; 135 return NULL; 136 } 137 138 //===--------------------------------------------------------------------===// 139 // Create the RSExecutable. 140 //===--------------------------------------------------------------------===// 141 result = RSExecutable::Create(*info, *output_file, mResolver); 142 if (result == NULL) { 143 delete output_file; 144 delete info; 145 return NULL; 146 } 147 148 return result; 149} 150 151bool RSCompilerDriver::setupConfig(const RSScript &pScript) { 152 bool changed = false; 153 154 const llvm::CodeGenOpt::Level script_opt_level = 155 static_cast<llvm::CodeGenOpt::Level>(pScript.getOptimizationLevel()); 156 157 if (mConfig != NULL) { 158 // Renderscript bitcode may have their optimization flag configuration 159 // different than the previous run of RS compilation. 160 if (mConfig->getOptimizationLevel() != script_opt_level) { 161 mConfig->setOptimizationLevel(script_opt_level); 162 changed = true; 163 } 164 } else { 165 // Haven't run the compiler ever. 166 mConfig = new (std::nothrow) DefaultCompilerConfig(); 167 if (mConfig == NULL) { 168 // Return false since mConfig remains NULL and out-of-memory. 169 return false; 170 } 171 mConfig->setOptimizationLevel(script_opt_level); 172 changed = true; 173 } 174 175#if defined(DEFAULT_ARM_CODEGEN) 176 // NEON should be disable when full-precision floating point is required. 177 assert((pScript.getInfo() != NULL) && "NULL RS info!"); 178 if (pScript.getInfo()->getFloatPrecisionRequirement() == RSInfo::FP_Full) { 179 // Must be ARMCompilerConfig. 180 ARMCompilerConfig *arm_config = static_cast<ARMCompilerConfig *>(mConfig); 181 changed |= arm_config->enableNEON(/* pEnable */false); 182 } 183#endif 184 185 return changed; 186} 187 188RSExecutable * 189RSCompilerDriver::compileScript(RSScript &pScript, 190 const char* pScriptName, 191 const char *pOutputPath, 192 const char *pRuntimePath, 193 const RSInfo::DependencyTableTy &pDeps, 194 bool pSkipLoad) { 195 android::StopWatch compile_time("bcc: RSCompilerDriver::compileScript time"); 196 RSExecutable *result = NULL; 197 RSInfo *info = NULL; 198 199 //===--------------------------------------------------------------------===// 200 // Extract RS-specific information from source bitcode. 201 //===--------------------------------------------------------------------===// 202 // RS info may contains configuration (such as #optimization_level) to the 203 // compiler therefore it should be extracted before compilation. 204 info = RSInfo::ExtractFromSource(pScript.getSource(), pDeps); 205 if (info == NULL) { 206 return NULL; 207 } 208 209 //===--------------------------------------------------------------------===// 210 // Associate script with its info 211 //===--------------------------------------------------------------------===// 212 // This is required since RS compiler may need information in the info file 213 // to do some transformation (e.g., expand foreach-able function.) 214 pScript.setInfo(info); 215 216 //===--------------------------------------------------------------------===// 217 // Link RS script with Renderscript runtime. 218 //===--------------------------------------------------------------------===// 219 if (!RSScript::LinkRuntime(pScript, pRuntimePath)) { 220 ALOGE("Failed to link script '%s' with Renderscript runtime!", pScriptName); 221 return NULL; 222 } 223 224 //===--------------------------------------------------------------------===// 225 // Acquire the write lock for writing output object file. 226 //===--------------------------------------------------------------------===// 227 FileMutex<FileBase::kWriteLock> write_output_mutex(pOutputPath); 228 229 if (write_output_mutex.hasError() || !write_output_mutex.lock()) { 230 ALOGE("Unable to acquire the lock for writing %s! (%s)", 231 pOutputPath, write_output_mutex.getErrorMessage().c_str()); 232 return NULL; 233 } 234 235 //===--------------------------------------------------------------------===// 236 // Open the output file for write. 237 //===--------------------------------------------------------------------===// 238 OutputFile *output_file = 239 new (std::nothrow) OutputFile(pOutputPath, FileBase::kTruncate); 240 241 if ((output_file == NULL) || output_file->hasError()) { 242 ALOGE("Unable to open the %s for write! (%s)", pOutputPath, 243 output_file->getErrorMessage().c_str()); 244 delete info; 245 delete output_file; 246 return NULL; 247 } 248 249 //===--------------------------------------------------------------------===// 250 // Setup the config to the compiler. 251 //===--------------------------------------------------------------------===// 252 bool compiler_need_reconfigure = setupConfig(pScript); 253 254 if (mConfig == NULL) { 255 ALOGE("Failed to setup config for RS compiler to compile %s!", pOutputPath); 256 delete info; 257 delete output_file; 258 return NULL; 259 } 260 261 // Compiler need to re-config if it's haven't run the config() yet or the 262 // configuration it referenced is changed. 263 if (compiler_need_reconfigure) { 264 Compiler::ErrorCode err = mCompiler.config(*mConfig); 265 if (err != Compiler::kSuccess) { 266 ALOGE("Failed to config the RS compiler for %s! (%s)",pOutputPath, 267 Compiler::GetErrorString(err)); 268 delete info; 269 delete output_file; 270 return NULL; 271 } 272 } 273 274 //===--------------------------------------------------------------------===// 275 // Run the compiler. 276 //===--------------------------------------------------------------------===// 277 Compiler::ErrorCode compile_result = mCompiler.compile(pScript, *output_file); 278 if (compile_result != Compiler::kSuccess) { 279 ALOGE("Unable to compile the source to file %s! (%s)", pOutputPath, 280 Compiler::GetErrorString(compile_result)); 281 delete info; 282 delete output_file; 283 return NULL; 284 } 285 286 // No need to produce an RSExecutable in this case. 287 // TODO: Error handling in this case is nonexistent. 288 if (pSkipLoad) { 289 return NULL; 290 } 291 292 //===--------------------------------------------------------------------===// 293 // Create the RSExecutable. 294 //===--------------------------------------------------------------------===// 295 result = RSExecutable::Create(*info, *output_file, mResolver); 296 if (result == NULL) { 297 delete info; 298 delete output_file; 299 return NULL; 300 } 301 302 //===--------------------------------------------------------------------===// 303 // Dump the disassembly for debug when possible. 304 //===--------------------------------------------------------------------===// 305#if USE_DISASSEMBLER 306 OutputFile *disassembly_output = 307 new (std::nothrow) OutputFile(DEBUG_DISASSEMBLER_FILE, 308 FileBase::kAppend); 309 310 if (disassembly_output != NULL) { 311 result->dumpDisassembly(*disassembly_output); 312 delete disassembly_output; 313 } 314#endif 315 316 //===--------------------------------------------------------------------===// 317 // Write out the RS info file. 318 //===--------------------------------------------------------------------===// 319 // Note that write failure only results in a warning since the source is 320 // successfully compiled and loaded. 321 if (!result->syncInfo(/* pForce */true)) { 322 ALOGW("%s was successfully compiled and loaded but its RS info file failed " 323 "to write out!", pOutputPath); 324 } 325 326 return result; 327} 328 329RSExecutable *RSCompilerDriver::build(BCCContext &pContext, 330 const char *pCacheDir, 331 const char *pResName, 332 const char *pBitcode, 333 size_t pBitcodeSize, 334 const char *pRuntimePath, 335 RSLinkRuntimeCallback pLinkRuntimeCallback) { 336 android::StopWatch build_time("bcc: RSCompilerDriver::build time"); 337 //===--------------------------------------------------------------------===// 338 // Check parameters. 339 //===--------------------------------------------------------------------===// 340 if ((pCacheDir == NULL) || (pResName == NULL)) { 341 ALOGE("Invalid parameter passed to RSCompilerDriver::build()! (cache dir: " 342 "%s, resource name: %s)", ((pCacheDir) ? pCacheDir : "(null)"), 343 ((pResName) ? pResName : "(null)")); 344 return NULL; 345 } 346 347 if ((pBitcode == NULL) || (pBitcodeSize <= 0)) { 348 ALOGE("No bitcode supplied! (bitcode: %p, size of bitcode: %u)", 349 pBitcode, static_cast<unsigned>(pBitcodeSize)); 350 return NULL; 351 } 352 353 //===--------------------------------------------------------------------===// 354 // Prepare dependency information. 355 //===--------------------------------------------------------------------===// 356 RSInfo::DependencyTableTy dep_info; 357 uint8_t bitcode_sha1[20]; 358 Sha1Util::GetSHA1DigestFromBuffer(bitcode_sha1, pBitcode, pBitcodeSize); 359 dep_info.push(std::make_pair(pResName, bitcode_sha1)); 360 361 //===--------------------------------------------------------------------===// 362 // Construct output path. 363 //===--------------------------------------------------------------------===// 364 llvm::sys::Path output_path(pCacheDir); 365 366 // {pCacheDir}/{pResName} 367 if (!output_path.appendComponent(pResName)) { 368 ALOGE("Failed to construct output path %s/%s!", pCacheDir, pResName); 369 return NULL; 370 } 371 372 // {pCacheDir}/{pResName}.o 373 output_path.appendSuffix("o"); 374 375 //===--------------------------------------------------------------------===// 376 // Load cache. 377 //===--------------------------------------------------------------------===// 378 RSExecutable *result = loadScriptCache(output_path.c_str(), dep_info); 379 380 if (result != NULL) { 381 // Cache hit 382 return result; 383 } 384 385 //===--------------------------------------------------------------------===// 386 // Load the bitcode and create script. 387 //===--------------------------------------------------------------------===// 388 Source *source = Source::CreateFromBuffer(pContext, pResName, 389 pBitcode, pBitcodeSize); 390 if (source == NULL) { 391 return NULL; 392 } 393 394 RSScript *script = new (std::nothrow) RSScript(*source); 395 if (script == NULL) { 396 ALOGE("Out of memory when create Script object for '%s'! (output: %s)", 397 pResName, output_path.c_str()); 398 delete source; 399 return NULL; 400 } 401 402 script->setLinkRuntimeCallback(pLinkRuntimeCallback); 403 404 // Read information from bitcode wrapper. 405 bcinfo::BitcodeWrapper wrapper(pBitcode, pBitcodeSize); 406 script->setCompilerVersion(wrapper.getCompilerVersion()); 407 script->setOptimizationLevel(static_cast<RSScript::OptimizationLevel>( 408 wrapper.getOptimizationLevel())); 409 410 //===--------------------------------------------------------------------===// 411 // Compile the script 412 //===--------------------------------------------------------------------===// 413 result = compileScript(*script, pResName, output_path.c_str(), pRuntimePath, 414 dep_info, false); 415 416 // Script is no longer used. Free it to get more memory. 417 delete script; 418 419 if (result == NULL) { 420 return NULL; 421 } 422 423 return result; 424} 425 426 427RSExecutable *RSCompilerDriver::build(RSScript &pScript, const char *pOut, 428 const char *pRuntimePath) { 429 RSInfo::DependencyTableTy dep_info; 430 RSInfo *info = RSInfo::ExtractFromSource(pScript.getSource(), dep_info); 431 if (info == NULL) { 432 return NULL; 433 } 434 pScript.setInfo(info); 435 436 // Embed the info string directly in the ELF, since this path is for an 437 // offline (host) compilation. 438 pScript.setEmbedInfo(true); 439 440 RSExecutable *result = compileScript(pScript, pOut, pOut, pRuntimePath, 441 dep_info, true); 442 return result; 443} 444 445