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