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 | |
Jean-Luc Brouillet | a2dd52f | 2017-02-16 20:57:26 -0800 | [diff] [blame] | 17 | #include "bcc/RSCompilerDriver.h" |
| 18 | |
| 19 | #include "Assert.h" |
| 20 | #include "FileMutex.h" |
| 21 | #include "InputFile.h" |
| 22 | #include "Log.h" |
| 23 | #include "OutputFile.h" |
| 24 | #include "RSScriptGroupFusion.h" |
| 25 | |
| 26 | #include "bcc/BCCContext.h" |
| 27 | #include "bcc/Compiler.h" |
| 28 | #include "bcc/CompilerConfig.h" |
| 29 | #include "bcc/Config.h" |
| 30 | #include "bcc/Initialization.h" |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame^] | 31 | #include "bcc/Script.h" |
Jean-Luc Brouillet | a2dd52f | 2017-02-16 20:57:26 -0800 | [diff] [blame] | 32 | #include "bcc/Source.h" |
| 33 | #include "bcinfo/BitcodeWrapper.h" |
| 34 | #include "bcinfo/MetadataExtractor.h" |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 35 | |
Pirama Arumuga Nainar | f229c40 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 36 | #include "llvm/ADT/STLExtras.h" |
Yang Ni | a4ded13 | 2014-11-17 17:44:08 -0800 | [diff] [blame] | 37 | #include "llvm/IR/AssemblyAnnotationWriter.h" |
Tobias Grosser | 7b980e1 | 2013-06-20 10:12:13 -0700 | [diff] [blame] | 38 | #include <llvm/IR/Module.h> |
Yang Ni | 0ab50b8 | 2015-02-16 12:31:56 -0800 | [diff] [blame] | 39 | #include "llvm/Linker/Linker.h" |
Stephen Hines | ad69476 | 2013-04-29 18:59:47 -0700 | [diff] [blame] | 40 | #include <llvm/Support/CommandLine.h> |
Stephen Hines | b10c3a7 | 2013-08-07 23:15:22 -0700 | [diff] [blame] | 41 | #include <llvm/Support/Path.h> |
Tobias Grosser | 7b980e1 | 2013-06-20 10:12:13 -0700 | [diff] [blame] | 42 | #include <llvm/Support/raw_ostream.h> |
Pirama Arumuga Nainar | d2d5ee3 | 2016-04-12 14:04:50 -0700 | [diff] [blame] | 43 | #include <llvm/Target/TargetMachine.h> |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 44 | |
Yang Ni | 0ab50b8 | 2015-02-16 12:31:56 -0800 | [diff] [blame] | 45 | #include <sstream> |
Yang Ni | a4ded13 | 2014-11-17 17:44:08 -0800 | [diff] [blame] | 46 | #include <string> |
| 47 | |
Elliott Hughes | fe0de78 | 2015-08-12 15:09:33 -0700 | [diff] [blame] | 48 | #ifdef __ANDROID__ |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 49 | #include <cutils/properties.h> |
Nick Kralevich | b81d697 | 2013-05-21 16:52:35 -0700 | [diff] [blame] | 50 | #endif |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 51 | #include <utils/StopWatch.h> |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 52 | |
| 53 | using namespace bcc; |
| 54 | |
David Gross | efe1434 | 2016-02-18 11:46:57 -0800 | [diff] [blame] | 55 | RSCompilerDriver::RSCompilerDriver() : |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 56 | mConfig(nullptr), mCompiler(), mDebugContext(false), |
Stephen Hines | 750ee65 | 2015-04-16 16:24:18 -0700 | [diff] [blame] | 57 | mLinkRuntimeCallback(nullptr), mEnableGlobalMerge(true), |
| 58 | mEmbedGlobalInfo(false), mEmbedGlobalInfoSkipConstant(false) { |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 59 | init::Initialize(); |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | RSCompilerDriver::~RSCompilerDriver() { |
| 63 | delete mConfig; |
| 64 | } |
| 65 | |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 66 | |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 67 | #if defined(PROVIDE_ARM_CODEGEN) |
Stephen Hines | ad69476 | 2013-04-29 18:59:47 -0700 | [diff] [blame] | 68 | extern llvm::cl::opt<bool> EnableGlobalMerge; |
Stephen Hines | c06cd06 | 2013-07-12 10:51:29 -0700 | [diff] [blame] | 69 | #endif |
| 70 | |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame^] | 71 | bool RSCompilerDriver::setupConfig(const Script &pScript) { |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 72 | bool changed = false; |
| 73 | |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame^] | 74 | const llvm::CodeGenOpt::Level script_opt_level = pScript.getOptimizationLevel(); |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 75 | |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 76 | #if defined(PROVIDE_ARM_CODEGEN) |
Stephen Hines | 045558b | 2014-02-18 14:07:15 -0800 | [diff] [blame] | 77 | EnableGlobalMerge = mEnableGlobalMerge; |
| 78 | #endif |
| 79 | |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 80 | if (mConfig != nullptr) { |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 81 | // Renderscript bitcode may have their optimization flag configuration |
| 82 | // different than the previous run of RS compilation. |
| 83 | if (mConfig->getOptimizationLevel() != script_opt_level) { |
| 84 | mConfig->setOptimizationLevel(script_opt_level); |
| 85 | changed = true; |
| 86 | } |
| 87 | } else { |
| 88 | // Haven't run the compiler ever. |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 89 | mConfig = new (std::nothrow) CompilerConfig(DEFAULT_TARGET_TRIPLE_STRING); |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 90 | if (mConfig == nullptr) { |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 91 | // Return false since mConfig remains NULL and out-of-memory. |
| 92 | return false; |
| 93 | } |
| 94 | mConfig->setOptimizationLevel(script_opt_level); |
| 95 | changed = true; |
| 96 | } |
| 97 | |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 98 | #if defined(PROVIDE_ARM_CODEGEN) |
Pirama Arumuga Nainar | a65fba6 | 2015-02-19 11:01:30 -0800 | [diff] [blame] | 99 | bcinfo::MetadataExtractor me(&pScript.getSource().getModule()); |
| 100 | if (!me.extract()) { |
David Gross | c2ca742 | 2015-05-29 14:54:33 -0700 | [diff] [blame] | 101 | bccAssert("Could not extract RS pragma metadata for module!"); |
Pirama Arumuga Nainar | a65fba6 | 2015-02-19 11:01:30 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | bool script_full_prec = (me.getRSFloatPrecision() == bcinfo::RS_FP_Full); |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 105 | if (mConfig->getFullPrecision() != script_full_prec) { |
| 106 | mConfig->setFullPrecision(script_full_prec); |
| 107 | changed = true; |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 108 | } |
| 109 | #endif |
| 110 | |
| 111 | return changed; |
| 112 | } |
| 113 | |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame^] | 114 | Compiler::ErrorCode RSCompilerDriver::compileScript(Script& pScript, const char* pScriptName, |
Jean-Luc Brouillet | f2ac317 | 2014-06-25 18:21:36 -0700 | [diff] [blame] | 115 | const char* pOutputPath, |
| 116 | const char* pRuntimePath, |
Pirama Arumuga Nainar | 51ee77b | 2015-02-19 16:33:27 -0800 | [diff] [blame] | 117 | const char* pBuildChecksum, |
Pirama Arumuga Nainar | a65fba6 | 2015-02-19 11:01:30 -0800 | [diff] [blame] | 118 | bool pDumpIR) { |
Pirama Arumuga Nainar | 51ee77b | 2015-02-19 16:33:27 -0800 | [diff] [blame] | 119 | // embed build checksum metadata into the source |
| 120 | if (pBuildChecksum != nullptr && strlen(pBuildChecksum) > 0) { |
| 121 | pScript.getSource().addBuildChecksumMetadata(pBuildChecksum); |
| 122 | } |
| 123 | |
Pirama Arumuga Nainar | ebff2ea | 2015-05-21 15:45:05 -0700 | [diff] [blame] | 124 | // Verify that the only external functions in pScript are Renderscript |
| 125 | // functions. Fail if verification returns an error. |
| 126 | if (mCompiler.screenGlobalFunctions(pScript) != Compiler::kSuccess) { |
| 127 | return Compiler::kErrInvalidSource; |
| 128 | } |
| 129 | |
Pirama Arumuga Nainar | d2d5ee3 | 2016-04-12 14:04:50 -0700 | [diff] [blame] | 130 | // For (32-bit) x86, translate GEPs on structs or arrays of structs to GEPs on |
| 131 | // int8* with byte offsets. This is to ensure that layout of structs with |
| 132 | // 64-bit scalar fields matches frontend-generated code that adheres to ARM |
| 133 | // data layout. |
| 134 | // |
| 135 | // The translation is done before RenderScript runtime library is linked |
| 136 | // (during LinkRuntime below) to ensure that RenderScript-driver-provided |
| 137 | // structs (like Allocation_t) don't get forced into using the ARM layout |
| 138 | // rules. |
| 139 | if (mCompiler.getTargetMachine().getTargetTriple().getArch() == llvm::Triple::x86) { |
| 140 | mCompiler.translateGEPs(pScript); |
| 141 | } |
| 142 | |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 143 | //===--------------------------------------------------------------------===// |
Stephen Hines | e198abe | 2012-07-27 18:05:41 -0700 | [diff] [blame] | 144 | // Link RS script with Renderscript runtime. |
Shih-wei Liao | ba42064 | 2012-06-30 11:27:37 -0700 | [diff] [blame] | 145 | //===--------------------------------------------------------------------===// |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame^] | 146 | if (!pScript.LinkRuntime(pRuntimePath)) { |
Yang Ni | 6da4e25 | 2015-03-11 10:00:42 -0700 | [diff] [blame] | 147 | ALOGE("Failed to link script '%s' with Renderscript runtime %s!", |
| 148 | pScriptName, pRuntimePath); |
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. |
Elliott Hughes | 495e806 | 2016-10-05 09:47:59 -0700 | [diff] [blame] | 155 | #ifndef _WIN32 |
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 | |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 221 | return Compiler::kSuccess; |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 222 | } |
| 223 | |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 224 | bool RSCompilerDriver::build(BCCContext &pContext, |
| 225 | const char *pCacheDir, |
| 226 | const char *pResName, |
| 227 | const char *pBitcode, |
| 228 | size_t pBitcodeSize, |
Pirama Arumuga Nainar | 51ee77b | 2015-02-19 16:33:27 -0800 | [diff] [blame] | 229 | const char *pBuildChecksum, |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 230 | const char *pRuntimePath, |
Tobias Grosser | 7b980e1 | 2013-06-20 10:12:13 -0700 | [diff] [blame] | 231 | RSLinkRuntimeCallback pLinkRuntimeCallback, |
| 232 | bool pDumpIR) { |
Tim Murray | c89f78b | 2013-05-09 11:57:12 -0700 | [diff] [blame] | 233 | // android::StopWatch build_time("bcc: RSCompilerDriver::build time"); |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 234 | //===--------------------------------------------------------------------===// |
| 235 | // Check parameters. |
| 236 | //===--------------------------------------------------------------------===// |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 237 | if ((pCacheDir == nullptr) || (pResName == nullptr)) { |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 238 | ALOGE("Invalid parameter passed to RSCompilerDriver::build()! (cache dir: " |
| 239 | "%s, resource name: %s)", ((pCacheDir) ? pCacheDir : "(null)"), |
| 240 | ((pResName) ? pResName : "(null)")); |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 241 | return false; |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 244 | if ((pBitcode == nullptr) || (pBitcodeSize <= 0)) { |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 245 | ALOGE("No bitcode supplied! (bitcode: %p, size of bitcode: %u)", |
| 246 | pBitcode, static_cast<unsigned>(pBitcodeSize)); |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 247 | return false; |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | //===--------------------------------------------------------------------===// |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 251 | // Construct output path. |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 252 | // {pCacheDir}/{pResName}.o |
Stephen Hines | b10c3a7 | 2013-08-07 23:15:22 -0700 | [diff] [blame] | 253 | //===--------------------------------------------------------------------===// |
| 254 | llvm::SmallString<80> output_path(pCacheDir); |
| 255 | llvm::sys::path::append(output_path, pResName); |
| 256 | llvm::sys::path::replace_extension(output_path, ".o"); |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 257 | |
| 258 | //===--------------------------------------------------------------------===// |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 259 | // Load the bitcode and create script. |
| 260 | //===--------------------------------------------------------------------===// |
| 261 | Source *source = Source::CreateFromBuffer(pContext, pResName, |
| 262 | pBitcode, pBitcodeSize); |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 263 | if (source == nullptr) { |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 264 | return false; |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame^] | 267 | Script script(source); |
| 268 | script.setOptimizationLevel(getConfig()->getOptimizationLevel()); |
Stephen Hines | c3437f0 | 2014-01-30 17:57:21 -0800 | [diff] [blame] | 269 | if (pLinkRuntimeCallback) { |
| 270 | setLinkRuntimeCallback(pLinkRuntimeCallback); |
| 271 | } |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 272 | |
Jean-Luc Brouillet | f2ac317 | 2014-06-25 18:21:36 -0700 | [diff] [blame] | 273 | script.setLinkRuntimeCallback(getLinkRuntimeCallback()); |
Stephen Hines | 06731a6 | 2013-02-12 19:29:42 -0800 | [diff] [blame] | 274 | |
Stephen Hines | 750ee65 | 2015-04-16 16:24:18 -0700 | [diff] [blame] | 275 | script.setEmbedGlobalInfo(mEmbedGlobalInfo); |
| 276 | script.setEmbedGlobalInfoSkipConstant(mEmbedGlobalInfoSkipConstant); |
| 277 | |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 278 | // Read information from bitcode wrapper. |
| 279 | bcinfo::BitcodeWrapper wrapper(pBitcode, pBitcodeSize); |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame^] | 280 | script.setOptimizationLevel(static_cast<llvm::CodeGenOpt::Level>( |
Jean-Luc Brouillet | f2ac317 | 2014-06-25 18:21:36 -0700 | [diff] [blame] | 281 | wrapper.getOptimizationLevel())); |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 282 | |
Stephen Hines | c31e101 | 2015-08-25 23:10:18 -0700 | [diff] [blame] | 283 | // Assertion-enabled builds can't compile legacy bitcode (due to the use of |
| 284 | // getName() with anonymous structure definitions). |
Colin Cross | 1d3ac56 | 2016-12-07 14:21:42 -0800 | [diff] [blame] | 285 | #ifdef _DEBUG |
Stephen Hines | c31e101 | 2015-08-25 23:10:18 -0700 | [diff] [blame] | 286 | static const uint32_t kSlangMinimumFixedStructureNames = 2310; |
| 287 | uint32_t version = wrapper.getCompilerVersion(); |
| 288 | if (version < kSlangMinimumFixedStructureNames) { |
| 289 | ALOGE("Found invalid legacy bitcode compiled with a version %u llvm-rs-cc " |
| 290 | "used with an assertion build", version); |
| 291 | ALOGE("Please recompile this apk with a more recent llvm-rs-cc " |
| 292 | "(at least %u)", kSlangMinimumFixedStructureNames); |
| 293 | return false; |
| 294 | } |
| 295 | #endif |
| 296 | |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 297 | //===--------------------------------------------------------------------===// |
| 298 | // Compile the script |
| 299 | //===--------------------------------------------------------------------===// |
Jean-Luc Brouillet | f2ac317 | 2014-06-25 18:21:36 -0700 | [diff] [blame] | 300 | Compiler::ErrorCode status = compileScript(script, pResName, |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 301 | output_path.c_str(), |
Pirama Arumuga Nainar | a65fba6 | 2015-02-19 11:01:30 -0800 | [diff] [blame] | 302 | pRuntimePath, |
| 303 | pBuildChecksum, |
| 304 | pDumpIR); |
Shih-wei Liao | 7bcec85 | 2012-04-25 04:07:09 -0700 | [diff] [blame] | 305 | |
Jean-Luc Brouillet | f2ac317 | 2014-06-25 18:21:36 -0700 | [diff] [blame] | 306 | return status == Compiler::kSuccess; |
Zonr Chang | 0fffa7e | 2012-04-12 19:43:53 +0800 | [diff] [blame] | 307 | } |
Stephen Hines | 331310e | 2012-10-26 19:27:55 -0700 | [diff] [blame] | 308 | |
Yang Ni | a4ded13 | 2014-11-17 17:44:08 -0800 | [diff] [blame] | 309 | bool RSCompilerDriver::buildScriptGroup( |
Yang Ni | 0ab50b8 | 2015-02-16 12:31:56 -0800 | [diff] [blame] | 310 | BCCContext& Context, const char* pOutputFilepath, const char* pRuntimePath, |
Yang Ni | 186d2f3 | 2015-04-07 14:51:47 -0700 | [diff] [blame] | 311 | const char* pRuntimeRelaxedPath, bool dumpIR, const char* buildChecksum, |
Yang Ni | 6da4e25 | 2015-03-11 10:00:42 -0700 | [diff] [blame] | 312 | const std::vector<Source*>& sources, |
Yang Ni | 0ab50b8 | 2015-02-16 12:31:56 -0800 | [diff] [blame] | 313 | const std::list<std::list<std::pair<int, int>>>& toFuse, |
| 314 | const std::list<std::string>& fused, |
| 315 | const std::list<std::list<std::pair<int, int>>>& invokes, |
| 316 | const std::list<std::string>& invokeBatchNames) { |
Pirama Arumuga Nainar | f229c40 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 317 | |
| 318 | // Read and store metadata before linking the modules together |
| 319 | std::vector<bcinfo::MetadataExtractor*> metadata; |
| 320 | for (Source* source : sources) { |
| 321 | if (!source->extractMetadata()) { |
| 322 | ALOGE("Cannot extract metadata from module"); |
| 323 | return false; |
| 324 | } |
| 325 | } |
| 326 | |
Yang Ni | 0ab50b8 | 2015-02-16 12:31:56 -0800 | [diff] [blame] | 327 | // --------------------------------------------------------------------------- |
| 328 | // Link all input modules into a single module |
| 329 | // --------------------------------------------------------------------------- |
| 330 | |
| 331 | llvm::LLVMContext& context = Context.getLLVMContext(); |
| 332 | llvm::Module module("Merged Script Group", context); |
| 333 | |
Pirama Arumuga Nainar | f229c40 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 334 | llvm::Linker linker(module); |
Yang Ni | 0ab50b8 | 2015-02-16 12:31:56 -0800 | [diff] [blame] | 335 | for (Source* source : sources) { |
Pirama Arumuga Nainar | f229c40 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 336 | std::unique_ptr<llvm::Module> sourceModule(&source->getModule()); |
| 337 | if (linker.linkInModule(std::move(sourceModule))) { |
Yang Ni | 0ab50b8 | 2015-02-16 12:31:56 -0800 | [diff] [blame] | 338 | ALOGE("Linking for module in source failed."); |
| 339 | return false; |
| 340 | } |
Pirama Arumuga Nainar | f229c40 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 341 | // source->getModule() is destroyed after linking. |
| 342 | source->markModuleDestroyed(); |
Yang Ni | a4ded13 | 2014-11-17 17:44:08 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Yang Ni | 0ab50b8 | 2015-02-16 12:31:56 -0800 | [diff] [blame] | 345 | // --------------------------------------------------------------------------- |
| 346 | // Create fused kernels |
| 347 | // --------------------------------------------------------------------------- |
| 348 | |
| 349 | auto inputIter = toFuse.begin(); |
| 350 | for (const std::string& nameOfFused : fused) { |
| 351 | auto inputKernels = *inputIter++; |
| 352 | std::vector<Source*> sourcesToFuse; |
| 353 | std::vector<int> slots; |
| 354 | |
| 355 | for (auto p : inputKernels) { |
| 356 | sourcesToFuse.push_back(sources[p.first]); |
| 357 | slots.push_back(p.second); |
| 358 | } |
| 359 | |
| 360 | if (!fuseKernels(Context, sourcesToFuse, slots, nameOfFused, &module)) { |
| 361 | return false; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | // --------------------------------------------------------------------------- |
| 366 | // Rename invokes |
| 367 | // --------------------------------------------------------------------------- |
| 368 | |
| 369 | auto invokeIter = invokes.begin(); |
| 370 | for (const std::string& newName : invokeBatchNames) { |
| 371 | auto inputInvoke = *invokeIter++; |
| 372 | auto p = inputInvoke.front(); |
| 373 | Source* source = sources[p.first]; |
| 374 | int slot = p.second; |
| 375 | |
| 376 | if (!renameInvoke(Context, source, slot, newName, &module)) { |
| 377 | return false; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | // --------------------------------------------------------------------------- |
| 382 | // Compile the new module with fused kernels |
| 383 | // --------------------------------------------------------------------------- |
| 384 | |
Yang Ni | a4ded13 | 2014-11-17 17:44:08 -0800 | [diff] [blame] | 385 | const std::unique_ptr<Source> source( |
Yang Ni | 0ab50b8 | 2015-02-16 12:31:56 -0800 | [diff] [blame] | 386 | Source::CreateFromModule(Context, pOutputFilepath, module, true)); |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame^] | 387 | Script script(source.get()); |
Yang Ni | a4ded13 | 2014-11-17 17:44:08 -0800 | [diff] [blame] | 388 | |
Yang Ni | 0ab50b8 | 2015-02-16 12:31:56 -0800 | [diff] [blame] | 389 | // Embed the info string directly in the ELF |
| 390 | script.setEmbedInfo(true); |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame^] | 391 | // TODO jeanluc Should we override the config's optimization? |
| 392 | // i.e., why not script.setOptimizationLevel(getConfig()->getOptimizationLevel)? |
| 393 | script.setOptimizationLevel(llvm::CodeGenOpt::Level::Aggressive); |
Stephen Hines | 750ee65 | 2015-04-16 16:24:18 -0700 | [diff] [blame] | 394 | script.setEmbedGlobalInfo(mEmbedGlobalInfo); |
| 395 | script.setEmbedGlobalInfoSkipConstant(mEmbedGlobalInfoSkipConstant); |
Yang Ni | a4ded13 | 2014-11-17 17:44:08 -0800 | [diff] [blame] | 396 | |
| 397 | llvm::SmallString<80> output_path(pOutputFilepath); |
| 398 | llvm::sys::path::replace_extension(output_path, ".o"); |
| 399 | |
Yang Ni | 6da4e25 | 2015-03-11 10:00:42 -0700 | [diff] [blame] | 400 | // Pick the right runtime lib |
| 401 | const char* coreLibPath = pRuntimePath; |
| 402 | if (strcmp(pRuntimeRelaxedPath, "")) { |
| 403 | bcinfo::MetadataExtractor me(&module); |
| 404 | me.extract(); |
| 405 | if (me.getRSFloatPrecision() == bcinfo::RS_FP_Relaxed) { |
| 406 | coreLibPath = pRuntimeRelaxedPath; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | compileScript(script, pOutputFilepath, output_path.c_str(), coreLibPath, |
Pirama Arumuga Nainar | a65fba6 | 2015-02-19 11:01:30 -0800 | [diff] [blame] | 411 | buildChecksum, dumpIR); |
Yang Ni | a4ded13 | 2014-11-17 17:44:08 -0800 | [diff] [blame] | 412 | |
| 413 | return true; |
| 414 | } |
Stephen Hines | 331310e | 2012-10-26 19:27:55 -0700 | [diff] [blame] | 415 | |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame^] | 416 | bool RSCompilerDriver::buildForCompatLib(Script &pScript, const char *pOut, |
Pirama Arumuga Nainar | 51ee77b | 2015-02-19 16:33:27 -0800 | [diff] [blame] | 417 | const char *pBuildChecksum, |
Tim Murray | 77c1d55 | 2015-01-22 15:15:17 -0800 | [diff] [blame] | 418 | const char *pRuntimePath, |
| 419 | bool pDumpIR) { |
Stephen Hines | 86a0b79 | 2012-11-06 20:04:47 -0800 | [diff] [blame] | 420 | // Embed the info string directly in the ELF, since this path is for an |
| 421 | // offline (host) compilation. |
| 422 | pScript.setEmbedInfo(true); |
| 423 | |
Stephen Hines | 750ee65 | 2015-04-16 16:24:18 -0700 | [diff] [blame] | 424 | pScript.setEmbedGlobalInfo(mEmbedGlobalInfo); |
| 425 | pScript.setEmbedGlobalInfoSkipConstant(mEmbedGlobalInfoSkipConstant); |
Stephen Hines | efb3af3 | 2015-07-29 00:34:30 -0700 | [diff] [blame] | 426 | pScript.setLinkRuntimeCallback(getLinkRuntimeCallback()); |
Stephen Hines | 750ee65 | 2015-04-16 16:24:18 -0700 | [diff] [blame] | 427 | |
Yang Ni | 6da4e25 | 2015-03-11 10:00:42 -0700 | [diff] [blame] | 428 | Compiler::ErrorCode status = compileScript(pScript, pOut, pOut, pRuntimePath, |
Pirama Arumuga Nainar | a65fba6 | 2015-02-19 11:01:30 -0800 | [diff] [blame] | 429 | pBuildChecksum, pDumpIR); |
Stephen Hines | 47f0d5a | 2013-06-05 00:27:38 -0700 | [diff] [blame] | 430 | if (status != Compiler::kSuccess) { |
| 431 | return false; |
| 432 | } |
| 433 | |
| 434 | return true; |
Stephen Hines | 331310e | 2012-10-26 19:27:55 -0700 | [diff] [blame] | 435 | } |