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