RSCompilerDriver.cpp revision c2ca742d7d0197c52e49467862844463fb42280f
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 17#include "bcc/Renderscript/RSCompilerDriver.h" 18 19#include "llvm/IR/AssemblyAnnotationWriter.h" 20#include <llvm/IR/Module.h> 21#include "llvm/Linker/Linker.h" 22#include <llvm/Support/CommandLine.h> 23#include <llvm/Support/Path.h> 24#include <llvm/Support/raw_ostream.h> 25 26#include "bcinfo/BitcodeWrapper.h" 27#include "bcc/Assert.h" 28#include "bcinfo/MetadataExtractor.h" 29#include "bcc/BCCContext.h" 30#include "bcc/Compiler.h" 31#include "bcc/Config/Config.h" 32#include "bcc/Renderscript/RSScript.h" 33#include "bcc/Renderscript/RSScriptGroupFusion.h" 34#include "bcc/Support/CompilerConfig.h" 35#include "bcc/Source.h" 36#include "bcc/Support/FileMutex.h" 37#include "bcc/Support/Log.h" 38#include "bcc/Support/InputFile.h" 39#include "bcc/Support/Initialization.h" 40#include "bcc/Support/OutputFile.h" 41 42#include <sstream> 43#include <string> 44 45#ifdef HAVE_ANDROID_OS 46#include <cutils/properties.h> 47#endif 48#include <utils/StopWatch.h> 49 50using namespace bcc; 51 52RSCompilerDriver::RSCompilerDriver(bool pUseCompilerRT) : 53 mConfig(nullptr), mCompiler(), mDebugContext(false), 54 mLinkRuntimeCallback(nullptr), mEnableGlobalMerge(true), 55 mEmbedGlobalInfo(false), mEmbedGlobalInfoSkipConstant(false) { 56 init::Initialize(); 57} 58 59RSCompilerDriver::~RSCompilerDriver() { 60 delete mConfig; 61} 62 63 64#if defined(PROVIDE_ARM_CODEGEN) 65extern llvm::cl::opt<bool> EnableGlobalMerge; 66#endif 67 68bool 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 74#if defined(PROVIDE_ARM_CODEGEN) 75 EnableGlobalMerge = mEnableGlobalMerge; 76#endif 77 78 if (mConfig != nullptr) { 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. 87 mConfig = new (std::nothrow) CompilerConfig(DEFAULT_TARGET_TRIPLE_STRING); 88 if (mConfig == nullptr) { 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 96#if defined(PROVIDE_ARM_CODEGEN) 97 bcinfo::MetadataExtractor me(&pScript.getSource().getModule()); 98 if (!me.extract()) { 99 bccAssert("Could not extract RS pragma metadata for module!"); 100 } 101 102 bool script_full_prec = (me.getRSFloatPrecision() == bcinfo::RS_FP_Full); 103 if (mConfig->getFullPrecision() != script_full_prec) { 104 mConfig->setFullPrecision(script_full_prec); 105 changed = true; 106 } 107#endif 108 109 return changed; 110} 111 112Compiler::ErrorCode RSCompilerDriver::compileScript(RSScript& pScript, const char* pScriptName, 113 const char* pOutputPath, 114 const char* pRuntimePath, 115 const char* pBuildChecksum, 116 bool pDumpIR) { 117 // embed build checksum metadata into the source 118 if (pBuildChecksum != nullptr && strlen(pBuildChecksum) > 0) { 119 pScript.getSource().addBuildChecksumMetadata(pBuildChecksum); 120 } 121 122 //===--------------------------------------------------------------------===// 123 // Link RS script with Renderscript runtime. 124 //===--------------------------------------------------------------------===// 125 if (!RSScript::LinkRuntime(pScript, pRuntimePath)) { 126 ALOGE("Failed to link script '%s' with Renderscript runtime %s!", 127 pScriptName, pRuntimePath); 128 return Compiler::kErrInvalidSource; 129 } 130 131 { 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. 134#ifndef USE_MINGW 135 //===------------------------------------------------------------------===// 136 // Acquire the write lock for writing output object file. 137 //===------------------------------------------------------------------===// 138 FileMutex<FileBase::kWriteLock> write_output_mutex(pOutputPath); 139 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()); 143 return Compiler::kErrInvalidSource; 144 } 145#endif 146 147 // Open the output file for write. 148 OutputFile output_file(pOutputPath, 149 FileBase::kTruncate | FileBase::kBinary); 150 151 if (output_file.hasError()) { 152 ALOGE("Unable to open %s for write! (%s)", pOutputPath, 153 output_file.getErrorMessage().c_str()); 154 return Compiler::kErrInvalidSource; 155 } 156 157 // Setup the config to the compiler. 158 bool compiler_need_reconfigure = setupConfig(pScript); 159 160 if (mConfig == nullptr) { 161 ALOGE("Failed to setup config for RS compiler to compile %s!", 162 pOutputPath); 163 return Compiler::kErrInvalidSource; 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)); 171 return Compiler::kErrInvalidSource; 172 } 173 } 174 175 OutputFile *ir_file = nullptr; 176 llvm::raw_fd_ostream *IRStream = nullptr; 177 if (pDumpIR) { 178 std::string path(pOutputPath); 179 path.append(".ll"); 180 ir_file = new OutputFile(path.c_str(), FileBase::kTruncate); 181 IRStream = ir_file->dup(); 182 } 183 184 // Run the compiler. 185 Compiler::ErrorCode compile_result = 186 mCompiler.compile(pScript, output_file, IRStream); 187 188 if (ir_file) { 189 ir_file->close(); 190 delete ir_file; 191 } 192 193 if (compile_result != Compiler::kSuccess) { 194 ALOGE("Unable to compile the source to file %s! (%s)", pOutputPath, 195 Compiler::GetErrorString(compile_result)); 196 return Compiler::kErrInvalidSource; 197 } 198 } 199 200 return Compiler::kSuccess; 201} 202 203bool RSCompilerDriver::build(BCCContext &pContext, 204 const char *pCacheDir, 205 const char *pResName, 206 const char *pBitcode, 207 size_t pBitcodeSize, 208 const char *pBuildChecksum, 209 const char *pRuntimePath, 210 RSLinkRuntimeCallback pLinkRuntimeCallback, 211 bool pDumpIR) { 212 // android::StopWatch build_time("bcc: RSCompilerDriver::build time"); 213 //===--------------------------------------------------------------------===// 214 // Check parameters. 215 //===--------------------------------------------------------------------===// 216 if ((pCacheDir == nullptr) || (pResName == nullptr)) { 217 ALOGE("Invalid parameter passed to RSCompilerDriver::build()! (cache dir: " 218 "%s, resource name: %s)", ((pCacheDir) ? pCacheDir : "(null)"), 219 ((pResName) ? pResName : "(null)")); 220 return false; 221 } 222 223 if ((pBitcode == nullptr) || (pBitcodeSize <= 0)) { 224 ALOGE("No bitcode supplied! (bitcode: %p, size of bitcode: %u)", 225 pBitcode, static_cast<unsigned>(pBitcodeSize)); 226 return false; 227 } 228 229 //===--------------------------------------------------------------------===// 230 // Construct output path. 231 // {pCacheDir}/{pResName}.o 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"); 236 237 //===--------------------------------------------------------------------===// 238 // Load the bitcode and create script. 239 //===--------------------------------------------------------------------===// 240 Source *source = Source::CreateFromBuffer(pContext, pResName, 241 pBitcode, pBitcodeSize); 242 if (source == nullptr) { 243 return false; 244 } 245 246 RSScript script(*source); 247 if (pLinkRuntimeCallback) { 248 setLinkRuntimeCallback(pLinkRuntimeCallback); 249 } 250 251 script.setLinkRuntimeCallback(getLinkRuntimeCallback()); 252 253 script.setEmbedGlobalInfo(mEmbedGlobalInfo); 254 script.setEmbedGlobalInfoSkipConstant(mEmbedGlobalInfoSkipConstant); 255 256 // Read information from bitcode wrapper. 257 bcinfo::BitcodeWrapper wrapper(pBitcode, pBitcodeSize); 258 script.setCompilerVersion(wrapper.getCompilerVersion()); 259 script.setOptimizationLevel(static_cast<RSScript::OptimizationLevel>( 260 wrapper.getOptimizationLevel())); 261 262 //===--------------------------------------------------------------------===// 263 // Compile the script 264 //===--------------------------------------------------------------------===// 265 Compiler::ErrorCode status = compileScript(script, pResName, 266 output_path.c_str(), 267 pRuntimePath, 268 pBuildChecksum, 269 pDumpIR); 270 271 return status == Compiler::kSuccess; 272} 273 274bool RSCompilerDriver::buildScriptGroup( 275 BCCContext& Context, const char* pOutputFilepath, const char* pRuntimePath, 276 const char* pRuntimeRelaxedPath, bool dumpIR, const char* buildChecksum, 277 const std::vector<Source*>& sources, 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 } 295 } 296 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 337 const std::unique_ptr<Source> source( 338 Source::CreateFromModule(Context, pOutputFilepath, module, true)); 339 RSScript script(*source); 340 341 // Embed the info string directly in the ELF 342 script.setEmbedInfo(true); 343 script.setOptimizationLevel(RSScript::kOptLvl3); 344 script.setEmbedGlobalInfo(mEmbedGlobalInfo); 345 script.setEmbedGlobalInfoSkipConstant(mEmbedGlobalInfoSkipConstant); 346 347 llvm::SmallString<80> output_path(pOutputFilepath); 348 llvm::sys::path::replace_extension(output_path, ".o"); 349 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, 361 buildChecksum, dumpIR); 362 363 return true; 364} 365 366bool RSCompilerDriver::buildForCompatLib(RSScript &pScript, const char *pOut, 367 const char *pBuildChecksum, 368 const char *pRuntimePath, 369 bool pDumpIR) { 370 // Embed the info string directly in the ELF, since this path is for an 371 // offline (host) compilation. 372 pScript.setEmbedInfo(true); 373 374 pScript.setEmbedGlobalInfo(mEmbedGlobalInfo); 375 pScript.setEmbedGlobalInfoSkipConstant(mEmbedGlobalInfoSkipConstant); 376 377 Compiler::ErrorCode status = compileScript(pScript, pOut, pOut, pRuntimePath, 378 pBuildChecksum, pDumpIR); 379 if (status != Compiler::kSuccess) { 380 return false; 381 } 382 383 return true; 384} 385