Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 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 | |
Stephen Hines | e198abe | 2012-07-27 18:05:41 -0700 | [diff] [blame] | 17 | #include "bcc/Renderscript/RSCompilerDriver.h" |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 18 | |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 19 | #include <llvm/Support/Path.h> |
| 20 | |
| 21 | #include "bcinfo/BitcodeWrapper.h" |
| 22 | |
Stephen Hines | e198abe | 2012-07-27 18:05:41 -0700 | [diff] [blame] | 23 | #include "bcc/Renderscript/RSExecutable.h" |
| 24 | #include "bcc/Renderscript/RSScript.h" |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 25 | #include "bcc/Support/CompilerConfig.h" |
| 26 | #include "bcc/Support/TargetCompilerConfigs.h" |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 27 | #include "bcc/Source.h" |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 28 | #include "bcc/Support/FileMutex.h" |
Zonr Chang | ef73a24 | 2012-04-12 16:44:01 +0800 | [diff] [blame] | 29 | #include "bcc/Support/Log.h" |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 30 | #include "bcc/Support/InputFile.h" |
| 31 | #include "bcc/Support/Initialization.h" |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 32 | #include "bcc/Support/Sha1Util.h" |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 33 | #include "bcc/Support/OutputFile.h" |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 34 | |
| 35 | #include <cutils/properties.h> |
| 36 | #include <utils/String8.h> |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 37 | #include <utils/StopWatch.h> |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 38 | |
| 39 | using namespace bcc; |
| 40 | |
| 41 | namespace { |
| 42 | |
| 43 | bool is_force_recompile() { |
| 44 | char buf[PROPERTY_VALUE_MAX]; |
| 45 | |
Shih-wei Liao | ed7fffb | 2012-06-30 11:27:59 -0700 | [diff] [blame] | 46 | // Re-compile if floating point precision has been overridden. |
| 47 | property_get("debug.rs.precision", buf, ""); |
| 48 | if (buf[0] != '\0') { |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | // Re-compile if debug.rs.forcerecompile is set. |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 53 | property_get("debug.rs.forcerecompile", buf, "0"); |
| 54 | if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) { |
| 55 | return true; |
| 56 | } else { |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | } // end anonymous namespace |
| 62 | |
Stephen Hines | 3ab9da1 | 2013-02-01 18:39:15 -0800 | [diff] [blame^] | 63 | RSCompilerDriver::RSCompilerDriver(bool pUseCompilerRT) : |
| 64 | mConfig(NULL), mCompiler(), mCompilerRuntime(NULL) { |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 65 | init::Initialize(); |
Stephen Hines | 3ab9da1 | 2013-02-01 18:39:15 -0800 | [diff] [blame^] | 66 | // Chain the symbol resolvers for compiler_rt and RS runtimes. |
| 67 | if (pUseCompilerRT) { |
| 68 | mCompilerRuntime = new CompilerRTSymbolResolver(); |
| 69 | mResolver.chainResolver(*mCompilerRuntime); |
| 70 | } |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 71 | mResolver.chainResolver(mRSRuntime); |
| 72 | } |
| 73 | |
| 74 | RSCompilerDriver::~RSCompilerDriver() { |
Stephen Hines | 3ab9da1 | 2013-02-01 18:39:15 -0800 | [diff] [blame^] | 75 | delete mCompilerRuntime; |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 76 | delete mConfig; |
| 77 | } |
| 78 | |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 79 | RSExecutable * |
| 80 | RSCompilerDriver::loadScriptCache(const char *pOutputPath, |
| 81 | const RSInfo::DependencyTableTy &pDeps) { |
| 82 | android::StopWatch load_time("bcc: RSCompilerDriver::loadScriptCache time"); |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 83 | 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()) { |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 94 | ALOGE("Unable to acquire the read lock for %s! (%s)", pOutputPath, |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 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()) { |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 105 | ALOGE("Unable to open the %s for read! (%s)", pOutputPath, |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 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)", |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 118 | pOutputPath, info_path.string(), |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 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()); |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 128 | RSInfo *info = RSInfo::ReadFromFile(info_file, pDeps); |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 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 | |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 148 | return result; |
| 149 | } |
| 150 | |
| 151 | bool 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!"); |
Shih-wei Liao | ed7fffb | 2012-06-30 11:27:59 -0700 | [diff] [blame] | 178 | if (pScript.getInfo()->getFloatPrecisionRequirement() == RSInfo::FP_Full) { |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 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 | |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 188 | RSExecutable * |
| 189 | RSCompilerDriver::compileScript(RSScript &pScript, |
Shih-wei Liao | ba42064 | 2012-06-30 11:27:37 -0700 | [diff] [blame] | 190 | const char* pScriptName, |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 191 | const char *pOutputPath, |
Stephen Hines | 331310e | 2012-10-26 19:27:55 -0700 | [diff] [blame] | 192 | const char *pRuntimePath, |
| 193 | const RSInfo::DependencyTableTy &pDeps, |
| 194 | bool pSkipLoad) { |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 195 | android::StopWatch compile_time("bcc: RSCompilerDriver::compileScript time"); |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 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. |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 204 | info = RSInfo::ExtractFromSource(pScript.getSource(), pDeps); |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 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 | //===--------------------------------------------------------------------===// |
Stephen Hines | e198abe | 2012-07-27 18:05:41 -0700 | [diff] [blame] | 217 | // Link RS script with Renderscript runtime. |
Shih-wei Liao | ba42064 | 2012-06-30 11:27:37 -0700 | [diff] [blame] | 218 | //===--------------------------------------------------------------------===// |
Stephen Hines | 331310e | 2012-10-26 19:27:55 -0700 | [diff] [blame] | 219 | if (!RSScript::LinkRuntime(pScript, pRuntimePath)) { |
Stephen Hines | e198abe | 2012-07-27 18:05:41 -0700 | [diff] [blame] | 220 | ALOGE("Failed to link script '%s' with Renderscript runtime!", pScriptName); |
Shih-wei Liao | ba42064 | 2012-06-30 11:27:37 -0700 | [diff] [blame] | 221 | return NULL; |
| 222 | } |
| 223 | |
| 224 | //===--------------------------------------------------------------------===// |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 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)", |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 231 | pOutputPath, write_output_mutex.getErrorMessage().c_str()); |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 232 | return NULL; |
| 233 | } |
| 234 | |
| 235 | //===--------------------------------------------------------------------===// |
| 236 | // Open the output file for write. |
| 237 | //===--------------------------------------------------------------------===// |
Shih-wei Liao | c02eae6 | 2012-07-22 16:23:32 -0700 | [diff] [blame] | 238 | OutputFile *output_file = |
| 239 | new (std::nothrow) OutputFile(pOutputPath, FileBase::kTruncate); |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 240 | |
| 241 | if ((output_file == NULL) || output_file->hasError()) { |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 242 | ALOGE("Unable to open the %s for write! (%s)", pOutputPath, |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 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) { |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 255 | ALOGE("Failed to setup config for RS compiler to compile %s!", pOutputPath); |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 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) { |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 266 | ALOGE("Failed to config the RS compiler for %s! (%s)",pOutputPath, |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 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) { |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 279 | ALOGE("Unable to compile the source to file %s! (%s)", pOutputPath, |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 280 | Compiler::GetErrorString(compile_result)); |
| 281 | delete info; |
| 282 | delete output_file; |
| 283 | return NULL; |
| 284 | } |
| 285 | |
Stephen Hines | 331310e | 2012-10-26 19:27:55 -0700 | [diff] [blame] | 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 | |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 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 | |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 302 | //===--------------------------------------------------------------------===// |
Shih-wei Liao | 6d01af6 | 2012-07-22 16:26:22 -0700 | [diff] [blame] | 303 | // Dump the disassembly for debug when possible. |
| 304 | //===--------------------------------------------------------------------===// |
Shih-wei Liao | d7f1bd6 | 2012-07-23 23:14:34 -0700 | [diff] [blame] | 305 | #if USE_DISASSEMBLER |
Shih-wei Liao | 6d01af6 | 2012-07-22 16:26:22 -0700 | [diff] [blame] | 306 | OutputFile *disassembly_output = |
Shih-wei Liao | d7f1bd6 | 2012-07-23 23:14:34 -0700 | [diff] [blame] | 307 | new (std::nothrow) OutputFile(DEBUG_DISASSEMBLER_FILE, |
Shih-wei Liao | 6d01af6 | 2012-07-22 16:26:22 -0700 | [diff] [blame] | 308 | FileBase::kAppend); |
| 309 | |
| 310 | if (disassembly_output != NULL) { |
| 311 | result->dumpDisassembly(*disassembly_output); |
| 312 | delete disassembly_output; |
| 313 | } |
| 314 | #endif |
| 315 | |
| 316 | //===--------------------------------------------------------------------===// |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 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 " |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 323 | "to write out!", pOutputPath); |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | return result; |
| 327 | } |
| 328 | |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 329 | RSExecutable *RSCompilerDriver::build(BCCContext &pContext, |
| 330 | const char *pCacheDir, |
| 331 | const char *pResName, |
| 332 | const char *pBitcode, |
Stephen Hines | 331310e | 2012-10-26 19:27:55 -0700 | [diff] [blame] | 333 | size_t pBitcodeSize, |
| 334 | const char *pRuntimePath) { |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 335 | android::StopWatch build_time("bcc: RSCompilerDriver::build time"); |
| 336 | //===--------------------------------------------------------------------===// |
| 337 | // Check parameters. |
| 338 | //===--------------------------------------------------------------------===// |
| 339 | if ((pCacheDir == NULL) || (pResName == NULL)) { |
| 340 | ALOGE("Invalid parameter passed to RSCompilerDriver::build()! (cache dir: " |
| 341 | "%s, resource name: %s)", ((pCacheDir) ? pCacheDir : "(null)"), |
| 342 | ((pResName) ? pResName : "(null)")); |
| 343 | return NULL; |
| 344 | } |
| 345 | |
| 346 | if ((pBitcode == NULL) || (pBitcodeSize <= 0)) { |
| 347 | ALOGE("No bitcode supplied! (bitcode: %p, size of bitcode: %u)", |
| 348 | pBitcode, static_cast<unsigned>(pBitcodeSize)); |
| 349 | return NULL; |
| 350 | } |
| 351 | |
| 352 | //===--------------------------------------------------------------------===// |
| 353 | // Prepare dependency information. |
| 354 | //===--------------------------------------------------------------------===// |
| 355 | RSInfo::DependencyTableTy dep_info; |
| 356 | uint8_t bitcode_sha1[20]; |
| 357 | Sha1Util::GetSHA1DigestFromBuffer(bitcode_sha1, pBitcode, pBitcodeSize); |
| 358 | dep_info.push(std::make_pair(pResName, bitcode_sha1)); |
| 359 | |
| 360 | //===--------------------------------------------------------------------===// |
| 361 | // Construct output path. |
| 362 | //===--------------------------------------------------------------------===// |
| 363 | llvm::sys::Path output_path(pCacheDir); |
| 364 | |
| 365 | // {pCacheDir}/{pResName} |
| 366 | if (!output_path.appendComponent(pResName)) { |
| 367 | ALOGE("Failed to construct output path %s/%s!", pCacheDir, pResName); |
| 368 | return NULL; |
| 369 | } |
| 370 | |
| 371 | // {pCacheDir}/{pResName}.o |
| 372 | output_path.appendSuffix("o"); |
| 373 | |
| 374 | //===--------------------------------------------------------------------===// |
| 375 | // Load cache. |
| 376 | //===--------------------------------------------------------------------===// |
| 377 | RSExecutable *result = loadScriptCache(output_path.c_str(), dep_info); |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 378 | |
| 379 | if (result != NULL) { |
| 380 | // Cache hit |
| 381 | return result; |
| 382 | } |
| 383 | |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 384 | //===--------------------------------------------------------------------===// |
| 385 | // Load the bitcode and create script. |
| 386 | //===--------------------------------------------------------------------===// |
| 387 | Source *source = Source::CreateFromBuffer(pContext, pResName, |
| 388 | pBitcode, pBitcodeSize); |
| 389 | if (source == NULL) { |
| 390 | return NULL; |
| 391 | } |
| 392 | |
| 393 | RSScript *script = new (std::nothrow) RSScript(*source); |
| 394 | if (script == NULL) { |
| 395 | ALOGE("Out of memory when create Script object for '%s'! (output: %s)", |
| 396 | pResName, output_path.c_str()); |
| 397 | delete source; |
| 398 | return NULL; |
| 399 | } |
| 400 | |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 401 | // Read information from bitcode wrapper. |
| 402 | bcinfo::BitcodeWrapper wrapper(pBitcode, pBitcodeSize); |
| 403 | script->setCompilerVersion(wrapper.getCompilerVersion()); |
| 404 | script->setOptimizationLevel(static_cast<RSScript::OptimizationLevel>( |
| 405 | wrapper.getOptimizationLevel())); |
| 406 | |
| 407 | //===--------------------------------------------------------------------===// |
| 408 | // Compile the script |
| 409 | //===--------------------------------------------------------------------===// |
Stephen Hines | 331310e | 2012-10-26 19:27:55 -0700 | [diff] [blame] | 410 | result = compileScript(*script, pResName, output_path.c_str(), pRuntimePath, |
| 411 | dep_info, false); |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 412 | |
| 413 | // Script is no longer used. Free it to get more memory. |
| 414 | delete script; |
| 415 | |
| 416 | if (result == NULL) { |
| 417 | return NULL; |
| 418 | } |
| 419 | |
| 420 | return result; |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 421 | } |
Stephen Hines | 331310e | 2012-10-26 19:27:55 -0700 | [diff] [blame] | 422 | |
| 423 | |
| 424 | RSExecutable *RSCompilerDriver::build(RSScript &pScript, const char *pOut, |
| 425 | const char *pRuntimePath) { |
| 426 | RSInfo::DependencyTableTy dep_info; |
| 427 | RSInfo *info = RSInfo::ExtractFromSource(pScript.getSource(), dep_info); |
| 428 | if (info == NULL) { |
| 429 | return NULL; |
| 430 | } |
| 431 | pScript.setInfo(info); |
| 432 | |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 433 | // Embed the info string directly in the ELF, since this path is for an |
| 434 | // offline (host) compilation. |
| 435 | pScript.setEmbedInfo(true); |
| 436 | |
Stephen Hines | 331310e | 2012-10-26 19:27:55 -0700 | [diff] [blame] | 437 | RSExecutable *result = compileScript(pScript, pOut, pOut, pRuntimePath, |
| 438 | dep_info, true); |
| 439 | return result; |
| 440 | } |
| 441 | |