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 | |
Tobias Grosser | 7b980e1 | 2013-06-20 10:12:13 -0700 | [diff] [blame] | 19 | #include <llvm/IR/Module.h> |
Stephen Hines | ad69476 | 2013-04-29 18:59:47 -0700 | [diff] [blame] | 20 | #include <llvm/Support/CommandLine.h> |
Stephen Hines | b10c3a7 | 2013-08-07 23:15:22 -0700 | [diff] [blame] | 21 | #include <llvm/Support/Path.h> |
Tobias Grosser | 7b980e1 | 2013-06-20 10:12:13 -0700 | [diff] [blame] | 22 | #include <llvm/Support/raw_ostream.h> |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 23 | |
| 24 | #include "bcinfo/BitcodeWrapper.h" |
| 25 | |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 26 | #include "bcc/Compiler.h" |
Stephen Hines | e198abe | 2012-07-27 18:05:41 -0700 | [diff] [blame] | 27 | #include "bcc/Renderscript/RSExecutable.h" |
| 28 | #include "bcc/Renderscript/RSScript.h" |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 29 | #include "bcc/Support/CompilerConfig.h" |
| 30 | #include "bcc/Support/TargetCompilerConfigs.h" |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 31 | #include "bcc/Source.h" |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 32 | #include "bcc/Support/FileMutex.h" |
Zonr Chang | ef73a24 | 2012-04-12 16:44:01 +0800 | [diff] [blame] | 33 | #include "bcc/Support/Log.h" |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 34 | #include "bcc/Support/InputFile.h" |
| 35 | #include "bcc/Support/Initialization.h" |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 36 | #include "bcc/Support/Sha1Util.h" |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 37 | #include "bcc/Support/OutputFile.h" |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 38 | |
Nick Kralevich | b81d697 | 2013-05-21 16:52:35 -0700 | [diff] [blame] | 39 | #ifdef HAVE_ANDROID_OS |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 40 | #include <cutils/properties.h> |
Nick Kralevich | b81d697 | 2013-05-21 16:52:35 -0700 | [diff] [blame] | 41 | #endif |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 42 | #include <utils/String8.h> |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 43 | #include <utils/StopWatch.h> |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 44 | |
| 45 | using namespace bcc; |
| 46 | |
Stephen Hines | 3ab9da1 | 2013-02-01 18:39:15 -0800 | [diff] [blame] | 47 | RSCompilerDriver::RSCompilerDriver(bool pUseCompilerRT) : |
Stephen Hines | ad69476 | 2013-04-29 18:59:47 -0700 | [diff] [blame] | 48 | mConfig(NULL), mCompiler(), mCompilerRuntime(NULL), mDebugContext(false), |
Stephen Hines | c3437f0 | 2014-01-30 17:57:21 -0800 | [diff] [blame] | 49 | mLinkRuntimeCallback(NULL), mEnableGlobalMerge(true) { |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 50 | init::Initialize(); |
Stephen Hines | 3ab9da1 | 2013-02-01 18:39:15 -0800 | [diff] [blame] | 51 | // Chain the symbol resolvers for compiler_rt and RS runtimes. |
| 52 | if (pUseCompilerRT) { |
| 53 | mCompilerRuntime = new CompilerRTSymbolResolver(); |
| 54 | mResolver.chainResolver(*mCompilerRuntime); |
| 55 | } |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 56 | mResolver.chainResolver(mRSRuntime); |
| 57 | } |
| 58 | |
| 59 | RSCompilerDriver::~RSCompilerDriver() { |
Stephen Hines | 3ab9da1 | 2013-02-01 18:39:15 -0800 | [diff] [blame] | 60 | delete mCompilerRuntime; |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 61 | delete mConfig; |
| 62 | } |
| 63 | |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 64 | RSExecutable * |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 65 | RSCompilerDriver::loadScript(const char *pCacheDir, const char *pResName, |
| 66 | const char *pBitcode, size_t pBitcodeSize) { |
| 67 | //android::StopWatch load_time("bcc: RSCompilerDriver::loadScript time"); |
| 68 | if ((pCacheDir == NULL) || (pResName == NULL)) { |
| 69 | ALOGE("Missing pCacheDir and/or pResName"); |
| 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | if ((pBitcode == NULL) || (pBitcodeSize <= 0)) { |
| 74 | ALOGE("No bitcode supplied! (bitcode: %p, size of bitcode: %zu)", |
| 75 | pBitcode, pBitcodeSize); |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | RSInfo::DependencyTableTy dep_info; |
| 80 | uint8_t bitcode_sha1[20]; |
| 81 | Sha1Util::GetSHA1DigestFromBuffer(bitcode_sha1, pBitcode, pBitcodeSize); |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 82 | |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 83 | // {pCacheDir}/{pResName}.o |
Stephen Hines | b10c3a7 | 2013-08-07 23:15:22 -0700 | [diff] [blame] | 84 | llvm::SmallString<80> output_path(pCacheDir); |
| 85 | llvm::sys::path::append(output_path, pResName); |
| 86 | llvm::sys::path::replace_extension(output_path, ".o"); |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 87 | |
Stephen Hines | 5eea973 | 2013-06-19 19:09:32 -0700 | [diff] [blame] | 88 | dep_info.push(std::make_pair(output_path.c_str(), bitcode_sha1)); |
| 89 | |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 90 | //===--------------------------------------------------------------------===// |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 91 | // Acquire the read lock for reading the Script object file. |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 92 | //===--------------------------------------------------------------------===// |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 93 | FileMutex<FileBase::kReadLock> read_output_mutex(output_path.c_str()); |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 94 | |
| 95 | if (read_output_mutex.hasError() || !read_output_mutex.lock()) { |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 96 | ALOGE("Unable to acquire the read lock for %s! (%s)", output_path.c_str(), |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 97 | read_output_mutex.getErrorMessage().c_str()); |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | //===--------------------------------------------------------------------===// |
| 102 | // Read the output object file. |
| 103 | //===--------------------------------------------------------------------===// |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 104 | InputFile *object_file = new (std::nothrow) InputFile(output_path.c_str()); |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 105 | |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 106 | if ((object_file == NULL) || object_file->hasError()) { |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 107 | // ALOGE("Unable to open the %s for read! (%s)", output_path.c_str(), |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 108 | // object_file->getErrorMessage().c_str()); |
| 109 | delete object_file; |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | //===--------------------------------------------------------------------===// |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 114 | // Acquire the read lock on object_file for reading its RS info file. |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 115 | //===--------------------------------------------------------------------===// |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 116 | android::String8 info_path = RSInfo::GetPath(output_path.c_str()); |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 117 | |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 118 | if (!object_file->lock()) { |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 119 | ALOGE("Unable to acquire the read lock on %s for reading %s! (%s)", |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 120 | output_path.c_str(), info_path.string(), |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 121 | object_file->getErrorMessage().c_str()); |
| 122 | delete object_file; |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 123 | return NULL; |
| 124 | } |
| 125 | |
| 126 | //===---------------------------------------------------------------------===// |
| 127 | // Open and load the RS info file. |
| 128 | //===--------------------------------------------------------------------===// |
| 129 | InputFile info_file(info_path.string()); |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 130 | RSInfo *info = RSInfo::ReadFromFile(info_file, dep_info); |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 131 | |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 132 | // Release the lock on object_file. |
| 133 | object_file->unlock(); |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 134 | |
| 135 | if (info == NULL) { |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 136 | delete object_file; |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | //===--------------------------------------------------------------------===// |
| 141 | // Create the RSExecutable. |
| 142 | //===--------------------------------------------------------------------===// |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 143 | RSExecutable *result = RSExecutable::Create(*info, *object_file, mResolver); |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 144 | if (result == NULL) { |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 145 | delete object_file; |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 146 | delete info; |
| 147 | return NULL; |
| 148 | } |
| 149 | |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 150 | return result; |
| 151 | } |
| 152 | |
Stephen Hines | c06cd06 | 2013-07-12 10:51:29 -0700 | [diff] [blame] | 153 | #if defined(DEFAULT_ARM_CODEGEN) |
Stephen Hines | ad69476 | 2013-04-29 18:59:47 -0700 | [diff] [blame] | 154 | extern llvm::cl::opt<bool> EnableGlobalMerge; |
Stephen Hines | c06cd06 | 2013-07-12 10:51:29 -0700 | [diff] [blame] | 155 | #endif |
| 156 | |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 157 | bool RSCompilerDriver::setupConfig(const RSScript &pScript) { |
| 158 | bool changed = false; |
| 159 | |
| 160 | const llvm::CodeGenOpt::Level script_opt_level = |
| 161 | static_cast<llvm::CodeGenOpt::Level>(pScript.getOptimizationLevel()); |
| 162 | |
Stephen Hines | 045558b | 2014-02-18 14:07:15 -0800 | [diff] [blame] | 163 | #if defined(DEFAULT_ARM_CODEGEN) |
| 164 | EnableGlobalMerge = mEnableGlobalMerge; |
| 165 | #endif |
| 166 | |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 167 | if (mConfig != NULL) { |
| 168 | // Renderscript bitcode may have their optimization flag configuration |
| 169 | // different than the previous run of RS compilation. |
| 170 | if (mConfig->getOptimizationLevel() != script_opt_level) { |
| 171 | mConfig->setOptimizationLevel(script_opt_level); |
| 172 | changed = true; |
| 173 | } |
| 174 | } else { |
| 175 | // Haven't run the compiler ever. |
| 176 | mConfig = new (std::nothrow) DefaultCompilerConfig(); |
| 177 | if (mConfig == NULL) { |
| 178 | // Return false since mConfig remains NULL and out-of-memory. |
| 179 | return false; |
| 180 | } |
| 181 | mConfig->setOptimizationLevel(script_opt_level); |
| 182 | changed = true; |
| 183 | } |
| 184 | |
| 185 | #if defined(DEFAULT_ARM_CODEGEN) |
| 186 | // NEON should be disable when full-precision floating point is required. |
| 187 | assert((pScript.getInfo() != NULL) && "NULL RS info!"); |
Shih-wei Liao | ed7fffb | 2012-06-30 11:27:59 -0700 | [diff] [blame] | 188 | if (pScript.getInfo()->getFloatPrecisionRequirement() == RSInfo::FP_Full) { |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 189 | // Must be ARMCompilerConfig. |
| 190 | ARMCompilerConfig *arm_config = static_cast<ARMCompilerConfig *>(mConfig); |
| 191 | changed |= arm_config->enableNEON(/* pEnable */false); |
| 192 | } |
| 193 | #endif |
| 194 | |
| 195 | return changed; |
| 196 | } |
| 197 | |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 198 | Compiler::ErrorCode |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 199 | RSCompilerDriver::compileScript(RSScript &pScript, |
Shih-wei Liao | ba42064 | 2012-06-30 11:27:37 -0700 | [diff] [blame] | 200 | const char* pScriptName, |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 201 | const char *pOutputPath, |
Stephen Hines | 331310e | 2012-10-26 19:27:55 -0700 | [diff] [blame] | 202 | const char *pRuntimePath, |
| 203 | const RSInfo::DependencyTableTy &pDeps, |
Tobias Grosser | 7b980e1 | 2013-06-20 10:12:13 -0700 | [diff] [blame] | 204 | bool pSkipLoad, bool pDumpIR) { |
Tim Murray | c89f78b | 2013-05-09 11:57:12 -0700 | [diff] [blame] | 205 | //android::StopWatch compile_time("bcc: RSCompilerDriver::compileScript time"); |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 206 | RSInfo *info = NULL; |
| 207 | |
| 208 | //===--------------------------------------------------------------------===// |
| 209 | // Extract RS-specific information from source bitcode. |
| 210 | //===--------------------------------------------------------------------===// |
| 211 | // RS info may contains configuration (such as #optimization_level) to the |
| 212 | // compiler therefore it should be extracted before compilation. |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 213 | info = RSInfo::ExtractFromSource(pScript.getSource(), pDeps); |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 214 | if (info == NULL) { |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 215 | return Compiler::kErrInvalidSource; |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | //===--------------------------------------------------------------------===// |
| 219 | // Associate script with its info |
| 220 | //===--------------------------------------------------------------------===// |
| 221 | // This is required since RS compiler may need information in the info file |
| 222 | // to do some transformation (e.g., expand foreach-able function.) |
| 223 | pScript.setInfo(info); |
| 224 | |
| 225 | //===--------------------------------------------------------------------===// |
Stephen Hines | e198abe | 2012-07-27 18:05:41 -0700 | [diff] [blame] | 226 | // Link RS script with Renderscript runtime. |
Shih-wei Liao | ba42064 | 2012-06-30 11:27:37 -0700 | [diff] [blame] | 227 | //===--------------------------------------------------------------------===// |
Stephen Hines | 331310e | 2012-10-26 19:27:55 -0700 | [diff] [blame] | 228 | if (!RSScript::LinkRuntime(pScript, pRuntimePath)) { |
Stephen Hines | e198abe | 2012-07-27 18:05:41 -0700 | [diff] [blame] | 229 | ALOGE("Failed to link script '%s' with Renderscript runtime!", pScriptName); |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 230 | return Compiler::kErrInvalidSource; |
Shih-wei Liao | ba42064 | 2012-06-30 11:27:37 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 233 | { |
Stephen Hines | 0784365 | 2013-08-15 15:41:47 -0700 | [diff] [blame] | 234 | // FIXME(srhines): Windows compilation can't use locking like this, but |
| 235 | // we also don't need to worry about concurrent writers of the same file. |
Stephen Hines | d7a9526 | 2013-08-08 16:03:19 -0700 | [diff] [blame] | 236 | #ifndef USE_MINGW |
Stephen Hines | 0784365 | 2013-08-15 15:41:47 -0700 | [diff] [blame] | 237 | //===------------------------------------------------------------------===// |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 238 | // Acquire the write lock for writing output object file. |
Stephen Hines | 0784365 | 2013-08-15 15:41:47 -0700 | [diff] [blame] | 239 | //===------------------------------------------------------------------===// |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 240 | FileMutex<FileBase::kWriteLock> write_output_mutex(pOutputPath); |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 241 | |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 242 | if (write_output_mutex.hasError() || !write_output_mutex.lock()) { |
| 243 | ALOGE("Unable to acquire the lock for writing %s! (%s)", |
| 244 | pOutputPath, write_output_mutex.getErrorMessage().c_str()); |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 245 | return Compiler::kErrInvalidSource; |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 246 | } |
Stephen Hines | d7a9526 | 2013-08-08 16:03:19 -0700 | [diff] [blame] | 247 | #endif |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 248 | |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 249 | // Open the output file for write. |
Stephen Hines | acf9c9e | 2013-09-26 16:32:31 -0700 | [diff] [blame] | 250 | OutputFile output_file(pOutputPath, |
| 251 | FileBase::kTruncate | FileBase::kBinary); |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 252 | |
| 253 | if (output_file.hasError()) { |
| 254 | ALOGE("Unable to open %s for write! (%s)", pOutputPath, |
| 255 | output_file.getErrorMessage().c_str()); |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 256 | return Compiler::kErrInvalidSource; |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | // Setup the config to the compiler. |
| 260 | bool compiler_need_reconfigure = setupConfig(pScript); |
| 261 | |
| 262 | if (mConfig == NULL) { |
| 263 | ALOGE("Failed to setup config for RS compiler to compile %s!", |
| 264 | pOutputPath); |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 265 | return Compiler::kErrInvalidSource; |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | if (compiler_need_reconfigure) { |
| 269 | Compiler::ErrorCode err = mCompiler.config(*mConfig); |
| 270 | if (err != Compiler::kSuccess) { |
| 271 | ALOGE("Failed to config the RS compiler for %s! (%s)",pOutputPath, |
| 272 | Compiler::GetErrorString(err)); |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 273 | return Compiler::kErrInvalidSource; |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
Tobias Grosser | 27fb7ed | 2013-06-21 18:34:56 -0700 | [diff] [blame] | 277 | OutputFile *ir_file = NULL; |
| 278 | llvm::raw_fd_ostream *IRStream = NULL; |
| 279 | if (pDumpIR) { |
| 280 | android::String8 path(pOutputPath); |
| 281 | path.append(".ll"); |
| 282 | ir_file = new OutputFile(path.string(), FileBase::kTruncate); |
| 283 | IRStream = ir_file->dup(); |
| 284 | } |
| 285 | |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 286 | // Run the compiler. |
Tobias Grosser | 27fb7ed | 2013-06-21 18:34:56 -0700 | [diff] [blame] | 287 | Compiler::ErrorCode compile_result = mCompiler.compile(pScript, |
| 288 | output_file, IRStream); |
| 289 | |
| 290 | if (ir_file) { |
| 291 | ir_file->close(); |
| 292 | delete ir_file; |
| 293 | } |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 294 | |
| 295 | if (compile_result != Compiler::kSuccess) { |
| 296 | ALOGE("Unable to compile the source to file %s! (%s)", pOutputPath, |
| 297 | Compiler::GetErrorString(compile_result)); |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 298 | return Compiler::kErrInvalidSource; |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 299 | } |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 300 | } |
| 301 | |
Stephen Hines | 331310e | 2012-10-26 19:27:55 -0700 | [diff] [blame] | 302 | // No need to produce an RSExecutable in this case. |
| 303 | // TODO: Error handling in this case is nonexistent. |
| 304 | if (pSkipLoad) { |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 305 | return Compiler::kSuccess; |
Stephen Hines | 331310e | 2012-10-26 19:27:55 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 308 | { |
| 309 | android::String8 info_path = RSInfo::GetPath(pOutputPath); |
| 310 | OutputFile info_file(info_path.string(), FileBase::kTruncate); |
| 311 | |
| 312 | if (info_file.hasError()) { |
| 313 | ALOGE("Failed to open the info file %s for write! (%s)", |
| 314 | info_path.string(), info_file.getErrorMessage().c_str()); |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 315 | return Compiler::kErrInvalidSource; |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | FileMutex<FileBase::kWriteLock> write_info_mutex(info_path.string()); |
| 319 | if (write_info_mutex.hasError() || !write_info_mutex.lock()) { |
| 320 | ALOGE("Unable to acquire the lock for writing %s! (%s)", |
| 321 | info_path.string(), write_info_mutex.getErrorMessage().c_str()); |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 322 | return Compiler::kErrInvalidSource; |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | // Perform the write. |
| 326 | if (!info->write(info_file)) { |
| 327 | ALOGE("Failed to sync the RS info file %s!", info_path.string()); |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 328 | return Compiler::kErrInvalidSource; |
Stephen Hines | 01f05d4 | 2013-05-31 20:51:27 -0700 | [diff] [blame] | 329 | } |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 330 | } |
| 331 | |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 332 | return Compiler::kSuccess; |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 333 | } |
| 334 | |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 335 | bool RSCompilerDriver::build(BCCContext &pContext, |
| 336 | const char *pCacheDir, |
| 337 | const char *pResName, |
| 338 | const char *pBitcode, |
| 339 | size_t pBitcodeSize, |
| 340 | const char *pRuntimePath, |
Tobias Grosser | 7b980e1 | 2013-06-20 10:12:13 -0700 | [diff] [blame] | 341 | RSLinkRuntimeCallback pLinkRuntimeCallback, |
| 342 | bool pDumpIR) { |
Tim Murray | c89f78b | 2013-05-09 11:57:12 -0700 | [diff] [blame] | 343 | // android::StopWatch build_time("bcc: RSCompilerDriver::build time"); |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 344 | //===--------------------------------------------------------------------===// |
| 345 | // Check parameters. |
| 346 | //===--------------------------------------------------------------------===// |
| 347 | if ((pCacheDir == NULL) || (pResName == NULL)) { |
| 348 | ALOGE("Invalid parameter passed to RSCompilerDriver::build()! (cache dir: " |
| 349 | "%s, resource name: %s)", ((pCacheDir) ? pCacheDir : "(null)"), |
| 350 | ((pResName) ? pResName : "(null)")); |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 351 | return false; |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | if ((pBitcode == NULL) || (pBitcodeSize <= 0)) { |
| 355 | ALOGE("No bitcode supplied! (bitcode: %p, size of bitcode: %u)", |
| 356 | pBitcode, static_cast<unsigned>(pBitcodeSize)); |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 357 | return false; |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | //===--------------------------------------------------------------------===// |
| 361 | // Prepare dependency information. |
| 362 | //===--------------------------------------------------------------------===// |
| 363 | RSInfo::DependencyTableTy dep_info; |
| 364 | uint8_t bitcode_sha1[20]; |
| 365 | Sha1Util::GetSHA1DigestFromBuffer(bitcode_sha1, pBitcode, pBitcodeSize); |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 366 | |
| 367 | //===--------------------------------------------------------------------===// |
| 368 | // Construct output path. |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 369 | // {pCacheDir}/{pResName}.o |
Stephen Hines | b10c3a7 | 2013-08-07 23:15:22 -0700 | [diff] [blame] | 370 | //===--------------------------------------------------------------------===// |
| 371 | llvm::SmallString<80> output_path(pCacheDir); |
| 372 | llvm::sys::path::append(output_path, pResName); |
| 373 | llvm::sys::path::replace_extension(output_path, ".o"); |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 374 | |
Stephen Hines | 5eea973 | 2013-06-19 19:09:32 -0700 | [diff] [blame] | 375 | dep_info.push(std::make_pair(output_path.c_str(), bitcode_sha1)); |
| 376 | |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 377 | //===--------------------------------------------------------------------===// |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 378 | // Load the bitcode and create script. |
| 379 | //===--------------------------------------------------------------------===// |
| 380 | Source *source = Source::CreateFromBuffer(pContext, pResName, |
| 381 | pBitcode, pBitcodeSize); |
| 382 | if (source == NULL) { |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 383 | return false; |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | RSScript *script = new (std::nothrow) RSScript(*source); |
| 387 | if (script == NULL) { |
| 388 | ALOGE("Out of memory when create Script object for '%s'! (output: %s)", |
| 389 | pResName, output_path.c_str()); |
| 390 | delete source; |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 391 | return false; |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 392 | } |
Stephen Hines | c3437f0 | 2014-01-30 17:57:21 -0800 | [diff] [blame] | 393 | if (pLinkRuntimeCallback) { |
| 394 | setLinkRuntimeCallback(pLinkRuntimeCallback); |
| 395 | } |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 396 | |
Stephen Hines | c3437f0 | 2014-01-30 17:57:21 -0800 | [diff] [blame] | 397 | script->setLinkRuntimeCallback(getLinkRuntimeCallback()); |
Stephen Hines | 06731a6 | 2013-02-12 19:29:42 -0800 | [diff] [blame] | 398 | |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 399 | // Read information from bitcode wrapper. |
| 400 | bcinfo::BitcodeWrapper wrapper(pBitcode, pBitcodeSize); |
| 401 | script->setCompilerVersion(wrapper.getCompilerVersion()); |
| 402 | script->setOptimizationLevel(static_cast<RSScript::OptimizationLevel>( |
| 403 | wrapper.getOptimizationLevel())); |
| 404 | |
| 405 | //===--------------------------------------------------------------------===// |
| 406 | // Compile the script |
| 407 | //===--------------------------------------------------------------------===// |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 408 | Compiler::ErrorCode status = compileScript(*script, pResName, |
| 409 | output_path.c_str(), |
Tobias Grosser | 7b980e1 | 2013-06-20 10:12:13 -0700 | [diff] [blame] | 410 | pRuntimePath, dep_info, false, |
| 411 | pDumpIR); |
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 | |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 416 | if (status != Compiler::kSuccess) { |
| 417 | return false; |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 420 | return true; |
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 | |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 424 | bool RSCompilerDriver::build(RSScript &pScript, const char *pOut, |
| 425 | const char *pRuntimePath) { |
Stephen Hines | 331310e | 2012-10-26 19:27:55 -0700 | [diff] [blame] | 426 | RSInfo::DependencyTableTy dep_info; |
| 427 | RSInfo *info = RSInfo::ExtractFromSource(pScript.getSource(), dep_info); |
| 428 | if (info == NULL) { |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 429 | return false; |
Stephen Hines | 331310e | 2012-10-26 19:27:55 -0700 | [diff] [blame] | 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 | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 437 | Compiler::ErrorCode status = compileScript(pScript, pOut, pOut, pRuntimePath, |
| 438 | dep_info, true); |
| 439 | if (status != Compiler::kSuccess) { |
| 440 | return false; |
| 441 | } |
| 442 | |
| 443 | return true; |
Stephen Hines | 331310e | 2012-10-26 19:27:55 -0700 | [diff] [blame] | 444 | } |
| 445 | |