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