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