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