Compiler.cpp revision 7a9a96766a6636218b516d61fbe47a99dea8afc4
1/* 2 * Copyright 2010-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/Compiler.h" 18 19#include <llvm/Analysis/Passes.h> 20#include <llvm/Analysis/TargetTransformInfo.h> 21#include <llvm/CodeGen/RegAllocRegistry.h> 22#include <llvm/IR/LegacyPassManager.h> 23#include <llvm/IR/Module.h> 24#include <llvm/Support/TargetRegistry.h> 25#include <llvm/Support/raw_ostream.h> 26#include <llvm/IR/DataLayout.h> 27#include <llvm/Target/TargetSubtargetInfo.h> 28#include <llvm/Target/TargetMachine.h> 29#include <llvm/Transforms/IPO.h> 30#include <llvm/Transforms/IPO/PassManagerBuilder.h> 31#include <llvm/Transforms/Scalar.h> 32#include <llvm/Transforms/Vectorize.h> 33 34#include "bcc/Assert.h" 35#include "bcc/Renderscript/RSScript.h" 36#include "bcc/Renderscript/RSTransforms.h" 37#include "bcc/Script.h" 38#include "bcc/Source.h" 39#include "bcc/Support/CompilerConfig.h" 40#include "bcc/Support/Log.h" 41#include "bcc/Support/OutputFile.h" 42#include "bcinfo/MetadataExtractor.h" 43#include "rsDefines.h" 44 45#include <string> 46 47using namespace bcc; 48 49const char *Compiler::GetErrorString(enum ErrorCode pErrCode) { 50 switch (pErrCode) { 51 case kSuccess: 52 return "Successfully compiled."; 53 case kInvalidConfigNoTarget: 54 return "Invalid compiler config supplied (getTarget() returns nullptr.) " 55 "(missing call to CompilerConfig::initialize()?)"; 56 case kErrCreateTargetMachine: 57 return "Failed to create llvm::TargetMachine."; 58 case kErrSwitchTargetMachine: 59 return "Failed to switch llvm::TargetMachine."; 60 case kErrNoTargetMachine: 61 return "Failed to compile the script since there's no available " 62 "TargetMachine. (missing call to Compiler::config()?)"; 63 case kErrMaterialization: 64 return "Failed to materialize the module."; 65 case kErrInvalidOutputFileState: 66 return "Supplied output file was invalid (in the error state.)"; 67 case kErrPrepareOutput: 68 return "Failed to prepare file for output."; 69 case kPrepareCodeGenPass: 70 return "Failed to construct pass list for code-generation."; 71 case kErrCustomPasses: 72 return "Error occurred while adding custom passes."; 73 case kErrInvalidSource: 74 return "Error loading input bitcode"; 75 case kIllegalGlobalFunction: 76 return "Use of undefined external function"; 77 } 78 79 // This assert should never be reached as the compiler verifies that the 80 // above switch coveres all enum values. 81 bccAssert(false && "Unknown error code encountered"); 82 return ""; 83} 84 85//===----------------------------------------------------------------------===// 86// Instance Methods 87//===----------------------------------------------------------------------===// 88Compiler::Compiler() : mTarget(nullptr), mEnableOpt(true) { 89 return; 90} 91 92Compiler::Compiler(const CompilerConfig &pConfig) : mTarget(nullptr), 93 mEnableOpt(true) { 94 const std::string &triple = pConfig.getTriple(); 95 96 enum ErrorCode err = config(pConfig); 97 if (err != kSuccess) { 98 ALOGE("%s (%s, features: %s)", GetErrorString(err), 99 triple.c_str(), pConfig.getFeatureString().c_str()); 100 return; 101 } 102 103 return; 104} 105 106enum Compiler::ErrorCode Compiler::config(const CompilerConfig &pConfig) { 107 if (pConfig.getTarget() == nullptr) { 108 return kInvalidConfigNoTarget; 109 } 110 111 llvm::TargetMachine *new_target = 112 (pConfig.getTarget())->createTargetMachine(pConfig.getTriple(), 113 pConfig.getCPU(), 114 pConfig.getFeatureString(), 115 pConfig.getTargetOptions(), 116 pConfig.getRelocationModel(), 117 pConfig.getCodeModel(), 118 pConfig.getOptimizationLevel()); 119 120 if (new_target == nullptr) { 121 return ((mTarget != nullptr) ? kErrSwitchTargetMachine : 122 kErrCreateTargetMachine); 123 } 124 125 // Replace the old TargetMachine. 126 delete mTarget; 127 mTarget = new_target; 128 129 // Adjust register allocation policy according to the optimization level. 130 // createFastRegisterAllocator: fast but bad quality 131 // createLinearScanRegisterAllocator: not so fast but good quality 132 if ((pConfig.getOptimizationLevel() == llvm::CodeGenOpt::None)) { 133 llvm::RegisterRegAlloc::setDefault(llvm::createFastRegisterAllocator); 134 } else { 135 llvm::RegisterRegAlloc::setDefault(llvm::createGreedyRegisterAllocator); 136 } 137 138 return kSuccess; 139} 140 141Compiler::~Compiler() { 142 delete mTarget; 143} 144 145 146// This function has complete responsibility for creating and executing the 147// exact list of compiler passes. 148enum Compiler::ErrorCode Compiler::runPasses(Script &pScript, 149 llvm::raw_pwrite_stream &pResult) { 150 // Pass manager for link-time optimization 151 llvm::legacy::PassManager transformPasses; 152 153 // Empty MCContext. 154 llvm::MCContext *mc_context = nullptr; 155 156 transformPasses.add( 157 createTargetTransformInfoWrapperPass(mTarget->getTargetIRAnalysis())); 158 159 // Add some initial custom passes. 160 addInvokeHelperPass(transformPasses); 161 addExpandKernelPass(transformPasses); 162 addDebugInfoPass(pScript, transformPasses); 163 addInvariantPass(transformPasses); 164 if (mTarget->getOptLevel() != llvm::CodeGenOpt::None) { 165 if (!addInternalizeSymbolsPass(pScript, transformPasses)) 166 return kErrCustomPasses; 167 } 168 addGlobalInfoPass(pScript, transformPasses); 169 170 if (mTarget->getOptLevel() == llvm::CodeGenOpt::None) { 171 transformPasses.add(llvm::createGlobalOptimizerPass()); 172 transformPasses.add(llvm::createConstantMergePass()); 173 174 } else { 175 // FIXME: Figure out which passes should be executed. 176 llvm::PassManagerBuilder Builder; 177 Builder.Inliner = llvm::createFunctionInliningPass(); 178 Builder.populateLTOPassManager(transformPasses); 179 180 /* FIXME: Reenable autovectorization after rebase. 181 bug 19324423 182 // Add vectorization passes after LTO passes are in 183 // additional flag: -unroll-runtime 184 transformPasses.add(llvm::createLoopUnrollPass(-1, 16, 0, 1)); 185 // Need to pass appropriate flags here: -scalarize-load-store 186 transformPasses.add(llvm::createScalarizerPass()); 187 transformPasses.add(llvm::createCFGSimplificationPass()); 188 transformPasses.add(llvm::createScopedNoAliasAAPass()); 189 transformPasses.add(llvm::createScalarEvolutionAliasAnalysisPass()); 190 // additional flags: -slp-vectorize-hor -slp-vectorize-hor-store (unnecessary?) 191 transformPasses.add(llvm::createSLPVectorizerPass()); 192 transformPasses.add(llvm::createDeadCodeEliminationPass()); 193 transformPasses.add(llvm::createInstructionCombiningPass()); 194 */ 195 } 196 197 // These passes have to come after LTO, since we don't want to examine 198 // functions that are never actually called. 199 if (llvm::Triple(getTargetMachine().getTargetTriple()).getArch() == llvm::Triple::x86_64) 200 transformPasses.add(createRSX86_64CallConvPass()); // Add pass to correct calling convention for X86-64. 201 transformPasses.add(createRSIsThreadablePass()); // Add pass to mark script as threadable. 202 203 // RSEmbedInfoPass needs to come after we have scanned for non-threadable 204 // functions. 205 // Script passed to RSCompiler must be a RSScript. 206 RSScript &script = static_cast<RSScript &>(pScript); 207 if (script.getEmbedInfo()) 208 transformPasses.add(createRSEmbedInfoPass()); 209 210 // Execute the passes. 211 transformPasses.run(pScript.getSource().getModule()); 212 213 // Run backend separately to avoid interference between debug metadata 214 // generation and backend initialization. 215 llvm::legacy::PassManager codeGenPasses; 216 217 // Add passes to the pass manager to emit machine code through MC layer. 218 if (mTarget->addPassesToEmitMC(codeGenPasses, mc_context, pResult, 219 /* DisableVerify */false)) { 220 return kPrepareCodeGenPass; 221 } 222 223 // Execute the passes. 224 codeGenPasses.run(pScript.getSource().getModule()); 225 226 return kSuccess; 227} 228 229enum Compiler::ErrorCode Compiler::compile(Script &pScript, 230 llvm::raw_pwrite_stream &pResult, 231 llvm::raw_ostream *IRStream) { 232 llvm::Module &module = pScript.getSource().getModule(); 233 enum ErrorCode err; 234 235 if (mTarget == nullptr) { 236 return kErrNoTargetMachine; 237 } 238 239 const std::string &triple = module.getTargetTriple(); 240 const llvm::DataLayout *dl = getTargetMachine().getDataLayout(); 241 unsigned int pointerSize = dl->getPointerSizeInBits(); 242 if (triple == "armv7-none-linux-gnueabi") { 243 if (pointerSize != 32) { 244 return kErrInvalidSource; 245 } 246 } else if (triple == "aarch64-none-linux-gnueabi") { 247 if (pointerSize != 64) { 248 return kErrInvalidSource; 249 } 250 } else { 251 return kErrInvalidSource; 252 } 253 254 // Sanitize module's target information. 255 module.setTargetTriple(getTargetMachine().getTargetTriple()); 256 module.setDataLayout(*getTargetMachine().getDataLayout()); 257 258 // Materialize the bitcode module. 259 if (module.getMaterializer() != nullptr) { 260 // A module with non-null materializer means that it is a lazy-load module. 261 // Materialize it now via invoking MaterializeAllPermanently(). This 262 // function returns false when the materialization is successful. 263 std::error_code ec = module.materializeAllPermanently(); 264 if (ec) { 265 ALOGE("Failed to materialize the module `%s'! (%s)", 266 module.getModuleIdentifier().c_str(), ec.message().c_str()); 267 return kErrMaterialization; 268 } 269 } 270 271 if ((err = runPasses(pScript, pResult)) != kSuccess) { 272 return err; 273 } 274 275 if (IRStream) { 276 *IRStream << module; 277 } 278 279 return kSuccess; 280} 281 282enum Compiler::ErrorCode Compiler::compile(Script &pScript, 283 OutputFile &pResult, 284 llvm::raw_ostream *IRStream) { 285 // Check the state of the specified output file. 286 if (pResult.hasError()) { 287 return kErrInvalidOutputFileState; 288 } 289 290 // Open the output file decorated in llvm::raw_ostream. 291 llvm::raw_pwrite_stream *out = pResult.dup(); 292 if (out == nullptr) { 293 return kErrPrepareOutput; 294 } 295 296 // Delegate the request. 297 enum Compiler::ErrorCode err = compile(pScript, *out, IRStream); 298 299 // Close the output before return. 300 delete out; 301 302 return err; 303} 304 305bool Compiler::addInternalizeSymbolsPass(Script &pScript, llvm::legacy::PassManager &pPM) { 306 // Add a pass to internalize the symbols that don't need to have global 307 // visibility. 308 RSScript &script = static_cast<RSScript &>(pScript); 309 llvm::Module &module = script.getSource().getModule(); 310 bcinfo::MetadataExtractor me(&module); 311 if (!me.extract()) { 312 bccAssert(false && "Could not extract metadata for module!"); 313 return false; 314 } 315 316 // The vector contains the symbols that should not be internalized. 317 std::vector<const char *> export_symbols; 318 319 const char *sf[] = { 320 kRoot, // Graphics drawing function or compute kernel. 321 kInit, // Initialization routine called implicitly on startup. 322 kRsDtor, // Static global destructor for a script instance. 323 kRsInfo, // Variable containing string of RS metadata info. 324 kRsGlobalEntries, // Optional number of global variables. 325 kRsGlobalNames, // Optional global variable name info. 326 kRsGlobalAddresses, // Optional global variable address info. 327 kRsGlobalSizes, // Optional global variable size info. 328 kRsGlobalProperties, // Optional global variable properties. 329 nullptr // Must be nullptr-terminated. 330 }; 331 const char **special_functions = sf; 332 // Special RS functions should always be global symbols. 333 while (*special_functions != nullptr) { 334 export_symbols.push_back(*special_functions); 335 special_functions++; 336 } 337 338 // Visibility of symbols appeared in rs_export_var and rs_export_func should 339 // also be preserved. 340 size_t exportVarCount = me.getExportVarCount(); 341 size_t exportFuncCount = me.getExportFuncCount(); 342 size_t exportForEachCount = me.getExportForEachSignatureCount(); 343 size_t exportReduceCount = me.getExportReduceCount(); 344 size_t exportReduceNewCount = me.getExportReduceNewCount(); 345 const char **exportVarNameList = me.getExportVarNameList(); 346 const char **exportFuncNameList = me.getExportFuncNameList(); 347 const char **exportForEachNameList = me.getExportForEachNameList(); 348 const char **exportReduceNameList = me.getExportReduceNameList(); 349 const bcinfo::MetadataExtractor::ReduceNew *exportReduceNewList = me.getExportReduceNewList(); 350 size_t i; 351 352 for (i = 0; i < exportVarCount; ++i) { 353 export_symbols.push_back(exportVarNameList[i]); 354 } 355 356 for (i = 0; i < exportFuncCount; ++i) { 357 export_symbols.push_back(exportFuncNameList[i]); 358 } 359 360 // Expanded foreach and reduce functions should not be internalized; 361 // nor should general reduction initializer and outconverter 362 // functions. keep_funcs keeps the names of these functions around 363 // until createInternalizePass() is finished making its own copy of 364 // the visible symbols. 365 std::vector<std::string> keep_funcs; 366 keep_funcs.reserve(exportForEachCount + exportReduceCount + exportReduceNewCount*3); 367 368 for (i = 0; i < exportForEachCount; ++i) { 369 keep_funcs.push_back(std::string(exportForEachNameList[i]) + ".expand"); 370 } 371 for (i = 0; i < exportReduceCount; ++i) { 372 keep_funcs.push_back(std::string(exportReduceNameList[i]) + ".expand"); 373 } 374 auto keepFuncsPushBackIfPresent = [&keep_funcs](const char *Name) { 375 if (Name) keep_funcs.push_back(Name); 376 }; 377 for (i = 0; i < exportReduceNewCount; ++i) { 378 keep_funcs.push_back(std::string(exportReduceNewList[i].mAccumulatorName) + ".expand"); 379 // Note: driver does not currently use the combiner function 380 keepFuncsPushBackIfPresent(exportReduceNewList[i].mInitializerName); 381 keepFuncsPushBackIfPresent(exportReduceNewList[i].mOutConverterName); 382 } 383 384 for (auto &symbol_name : keep_funcs) { 385 export_symbols.push_back(symbol_name.c_str()); 386 } 387 388 // http://b/26165616 - WAR for this bug defines the __truncxfhf2 function in 389 // frameworks/rs/driver/runtime. Don't internalize this function for x86, so 390 // that a script can find and link against it. 391 llvm::Triple triple(getTargetMachine().getTargetTriple()); 392 if (triple.getArch() == llvm::Triple::x86) { 393 export_symbols.push_back("__truncxfhf2"); 394 } 395 396 pPM.add(llvm::createInternalizePass(export_symbols)); 397 398 return true; 399} 400 401void Compiler::addInvokeHelperPass(llvm::legacy::PassManager &pPM) { 402 llvm::Triple arch(getTargetMachine().getTargetTriple()); 403 if (arch.isArch64Bit()) { 404 pPM.add(createRSInvokeHelperPass()); 405 } 406} 407 408void Compiler::addDebugInfoPass(Script &pScript, llvm::legacy::PassManager &pPM) { 409 if (pScript.getSource().getDebugInfoEnabled()) 410 pPM.add(createRSAddDebugInfoPass()); 411} 412 413void Compiler::addExpandKernelPass(llvm::legacy::PassManager &pPM) { 414 // Expand ForEach and reduce on CPU path to reduce launch overhead. 415 bool pEnableStepOpt = true; 416 pPM.add(createRSKernelExpandPass(pEnableStepOpt)); 417} 418 419void Compiler::addGlobalInfoPass(Script &pScript, llvm::legacy::PassManager &pPM) { 420 // Add additional information about RS global variables inside the Module. 421 RSScript &script = static_cast<RSScript &>(pScript); 422 if (script.getEmbedGlobalInfo()) { 423 pPM.add(createRSGlobalInfoPass(script.getEmbedGlobalInfoSkipConstant())); 424 } 425} 426 427void Compiler::addInvariantPass(llvm::legacy::PassManager &pPM) { 428 // Mark Loads from RsExpandKernelDriverInfo as "load.invariant". 429 // Should run after ExpandForEach and before inlining. 430 pPM.add(createRSInvariantPass()); 431} 432 433enum Compiler::ErrorCode Compiler::screenGlobalFunctions(Script &pScript) { 434 llvm::Module &module = pScript.getSource().getModule(); 435 436 // Materialize the bitcode module in case this is a lazy-load module. Do not 437 // clear the materializer by calling materializeAllPermanently since the 438 // runtime library has not been merged into the module yet. 439 if (module.getMaterializer() != nullptr) { 440 std::error_code ec = module.materializeAll(); 441 if (ec) { 442 ALOGE("Failed to materialize module `%s' when screening globals! (%s)", 443 module.getModuleIdentifier().c_str(), ec.message().c_str()); 444 return kErrMaterialization; 445 } 446 } 447 448 // Add pass to check for illegal function calls. 449 llvm::legacy::PassManager pPM; 450 pPM.add(createRSScreenFunctionsPass()); 451 pPM.run(module); 452 453 return kSuccess; 454 455} 456