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