Compiler.cpp revision 1bd9f627fa0affb457507e86b0b6684c695fe726
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 44#include <string> 45 46using namespace bcc; 47 48const char *Compiler::GetErrorString(enum ErrorCode pErrCode) { 49 switch (pErrCode) { 50 case kSuccess: 51 return "Successfully compiled."; 52 case kInvalidConfigNoTarget: 53 return "Invalid compiler config supplied (getTarget() returns nullptr.) " 54 "(missing call to CompilerConfig::initialize()?)"; 55 case kErrCreateTargetMachine: 56 return "Failed to create llvm::TargetMachine."; 57 case kErrSwitchTargetMachine: 58 return "Failed to switch llvm::TargetMachine."; 59 case kErrNoTargetMachine: 60 return "Failed to compile the script since there's no available " 61 "TargetMachine. (missing call to Compiler::config()?)"; 62 case kErrDataLayoutNoMemory: 63 return "Out of memory when create DataLayout during compilation."; 64 case kErrMaterialization: 65 return "Failed to materialize the module."; 66 case kErrInvalidOutputFileState: 67 return "Supplied output file was invalid (in the error state.)"; 68 case kErrPrepareOutput: 69 return "Failed to prepare file for output."; 70 case kPrepareCodeGenPass: 71 return "Failed to construct pass list for code-generation."; 72 case kErrCustomPasses: 73 return "Error occurred while adding custom passes."; 74 case kErrInvalidSource: 75 return "Error loading input bitcode"; 76 case kIllegalGlobalFunction: 77 return "Use of undefined external function"; 78 } 79 80 // This assert should never be reached as the compiler verifies that the 81 // above switch coveres all enum values. 82 assert(false && "Unknown error code encountered"); 83 return ""; 84} 85 86//===----------------------------------------------------------------------===// 87// Instance Methods 88//===----------------------------------------------------------------------===// 89Compiler::Compiler() : mTarget(nullptr), mEnableOpt(true) { 90 return; 91} 92 93Compiler::Compiler(const CompilerConfig &pConfig) : mTarget(nullptr), 94 mEnableOpt(true) { 95 const std::string &triple = pConfig.getTriple(); 96 97 enum ErrorCode err = config(pConfig); 98 if (err != kSuccess) { 99 ALOGE("%s (%s, features: %s)", GetErrorString(err), 100 triple.c_str(), pConfig.getFeatureString().c_str()); 101 return; 102 } 103 104 return; 105} 106 107enum Compiler::ErrorCode Compiler::config(const CompilerConfig &pConfig) { 108 if (pConfig.getTarget() == nullptr) { 109 return kInvalidConfigNoTarget; 110 } 111 112 llvm::TargetMachine *new_target = 113 (pConfig.getTarget())->createTargetMachine(pConfig.getTriple(), 114 pConfig.getCPU(), 115 pConfig.getFeatureString(), 116 pConfig.getTargetOptions(), 117 pConfig.getRelocationModel(), 118 pConfig.getCodeModel(), 119 pConfig.getOptimizationLevel()); 120 121 if (new_target == nullptr) { 122 return ((mTarget != nullptr) ? kErrSwitchTargetMachine : 123 kErrCreateTargetMachine); 124 } 125 126 // Replace the old TargetMachine. 127 delete mTarget; 128 mTarget = new_target; 129 130 // Adjust register allocation policy according to the optimization level. 131 // createFastRegisterAllocator: fast but bad quality 132 // createLinearScanRegisterAllocator: not so fast but good quality 133 if ((pConfig.getOptimizationLevel() == llvm::CodeGenOpt::None)) { 134 llvm::RegisterRegAlloc::setDefault(llvm::createFastRegisterAllocator); 135 } else { 136 llvm::RegisterRegAlloc::setDefault(llvm::createGreedyRegisterAllocator); 137 } 138 139 return kSuccess; 140} 141 142Compiler::~Compiler() { 143 delete mTarget; 144} 145 146 147enum Compiler::ErrorCode Compiler::runPasses(Script &pScript, 148 llvm::raw_ostream &pResult) { 149 // Pass manager for link-time optimization 150 llvm::legacy::PassManager passes; 151 152 // Empty MCContext. 153 llvm::MCContext *mc_context = nullptr; 154 155 passes.add(createTargetTransformInfoWrapperPass(mTarget->getTargetIRAnalysis())); 156 //mTarget->addAnalysisPasses(passes); 157 158 // Prepare DataLayout target data from Module 159 llvm::DataLayoutPass *data_layout_pass = 160 new (std::nothrow) llvm::DataLayoutPass(); 161 162 if (data_layout_pass == nullptr) { 163 return kErrDataLayoutNoMemory; 164 } 165 166 // Add DataLayout to the pass manager. 167 passes.add(data_layout_pass); 168 169 // Add our custom passes. 170 if (!addCustomPasses(pScript, passes)) { 171 return kErrCustomPasses; 172 } 173 174 if (mTarget->getOptLevel() == llvm::CodeGenOpt::None) { 175 passes.add(llvm::createGlobalOptimizerPass()); 176 passes.add(llvm::createConstantMergePass()); 177 178 } else { 179 // FIXME: Figure out which passes should be executed. 180 llvm::PassManagerBuilder Builder; 181 Builder.Inliner = llvm::createFunctionInliningPass(); 182 Builder.populateLTOPassManager(passes); 183 184 /* FIXME: Reenable autovectorization after rebase. 185 bug 19324423 186 // Add vectorization passes after LTO passes are in 187 // additional flag: -unroll-runtime 188 passes.add(llvm::createLoopUnrollPass(-1, 16, 0, 1)); 189 // Need to pass appropriate flags here: -scalarize-load-store 190 passes.add(llvm::createScalarizerPass()); 191 passes.add(llvm::createCFGSimplificationPass()); 192 passes.add(llvm::createScopedNoAliasAAPass()); 193 passes.add(llvm::createScalarEvolutionAliasAnalysisPass()); 194 // additional flags: -slp-vectorize-hor -slp-vectorize-hor-store (unnecessary?) 195 passes.add(llvm::createSLPVectorizerPass()); 196 passes.add(llvm::createDeadCodeEliminationPass()); 197 passes.add(llvm::createInstructionCombiningPass()); 198 */ 199 } 200 201 // Add our pass to check for illegal function calls. 202 // This has to come after LTO, since we don't want to examine functions that 203 // are never actually called. 204 passes.add(createRSScreenFunctionsPass()); 205 passes.add(createRSIsThreadablePass()); 206 207 // RSEmbedInfoPass needs to come after we have scanned for non-threadable 208 // functions. 209 // Script passed to RSCompiler must be a RSScript. 210 RSScript &script = static_cast<RSScript &>(pScript); 211 if (script.getEmbedInfo()) 212 passes.add(createRSEmbedInfoPass()); 213 214 // Add passes to the pass manager to emit machine code through MC layer. 215 if (mTarget->addPassesToEmitMC(passes, mc_context, pResult, 216 /* DisableVerify */false)) { 217 return kPrepareCodeGenPass; 218 } 219 220 // Execute the passes. 221 passes.run(pScript.getSource().getModule()); 222 223 return kSuccess; 224} 225 226enum Compiler::ErrorCode Compiler::compile(Script &pScript, 227 llvm::raw_ostream &pResult, 228 llvm::raw_ostream *IRStream) { 229 llvm::Module &module = pScript.getSource().getModule(); 230 enum ErrorCode err; 231 232 if (mTarget == nullptr) { 233 return kErrNoTargetMachine; 234 } 235 236 const std::string &triple = module.getTargetTriple(); 237 const llvm::DataLayout *dl = getTargetMachine().getDataLayout(); 238 unsigned int pointerSize = dl->getPointerSizeInBits(); 239 if (triple == "armv7-none-linux-gnueabi") { 240 if (pointerSize != 32) { 241 return kErrInvalidSource; 242 } 243 } else if (triple == "aarch64-none-linux-gnueabi") { 244 if (pointerSize != 64) { 245 return kErrInvalidSource; 246 } 247 } else { 248 return kErrInvalidSource; 249 } 250 251 // Materialize the bitcode module. 252 if (module.getMaterializer() != nullptr) { 253 // A module with non-null materializer means that it is a lazy-load module. 254 // Materialize it now via invoking MaterializeAllPermanently(). This 255 // function returns false when the materialization is successful. 256 std::error_code ec = module.materializeAllPermanently(); 257 if (ec) { 258 ALOGE("Failed to materialize the module `%s'! (%s)", 259 module.getModuleIdentifier().c_str(), ec.message().c_str()); 260 return kErrMaterialization; 261 } 262 } 263 264 if ((err = runPasses(pScript, pResult)) != kSuccess) { 265 return err; 266 } 267 268 if (IRStream) { 269 *IRStream << module; 270 } 271 272 return kSuccess; 273} 274 275enum Compiler::ErrorCode Compiler::compile(Script &pScript, 276 OutputFile &pResult, 277 llvm::raw_ostream *IRStream) { 278 // Check the state of the specified output file. 279 if (pResult.hasError()) { 280 return kErrInvalidOutputFileState; 281 } 282 283 // Open the output file decorated in llvm::raw_ostream. 284 llvm::raw_ostream *out = pResult.dup(); 285 if (out == nullptr) { 286 return kErrPrepareOutput; 287 } 288 289 // Delegate the request. 290 enum Compiler::ErrorCode err = compile(pScript, *out, IRStream); 291 292 // Close the output before return. 293 delete out; 294 295 return err; 296} 297 298bool Compiler::addInternalizeSymbolsPass(Script &pScript, llvm::legacy::PassManager &pPM) { 299 // Add a pass to internalize the symbols that don't need to have global 300 // visibility. 301 RSScript &script = static_cast<RSScript &>(pScript); 302 llvm::Module &module = script.getSource().getModule(); 303 bcinfo::MetadataExtractor me(&module); 304 if (!me.extract()) { 305 bccAssert(false && "Could not extract metadata for module!"); 306 return false; 307 } 308 309 // The vector contains the symbols that should not be internalized. 310 std::vector<const char *> export_symbols; 311 312 const char *sf[] = { 313 "root", // Graphics drawing function or compute kernel. 314 "init", // Initialization routine called implicitly on startup. 315 ".rs.dtor", // Static global destructor for a script instance. 316 ".rs.info", // Variable containing string of RS metadata info. 317 nullptr // Must be nullptr-terminated. 318 }; 319 const char **special_functions = sf; 320 // Special RS functions should always be global symbols. 321 while (*special_functions != nullptr) { 322 export_symbols.push_back(*special_functions); 323 special_functions++; 324 } 325 326 // Visibility of symbols appeared in rs_export_var and rs_export_func should 327 // also be preserved. 328 size_t exportVarCount = me.getExportVarCount(); 329 size_t exportFuncCount = me.getExportFuncCount(); 330 size_t exportForEachCount = me.getExportForEachSignatureCount(); 331 const char **exportVarNameList = me.getExportVarNameList(); 332 const char **exportFuncNameList = me.getExportFuncNameList(); 333 const char **exportForEachNameList = me.getExportForEachNameList(); 334 size_t i; 335 336 for (i = 0; i < exportVarCount; ++i) { 337 export_symbols.push_back(exportVarNameList[i]); 338 } 339 340 for (i = 0; i < exportFuncCount; ++i) { 341 export_symbols.push_back(exportFuncNameList[i]); 342 } 343 344 // Expanded foreach functions should not be internalized, too. 345 // expanded_foreach_funcs keeps the .expand version of the kernel names 346 // around until createInternalizePass() is finished making its own 347 // copy of the visible symbols. 348 std::vector<std::string> expanded_foreach_funcs; 349 for (i = 0; i < exportForEachCount; ++i) { 350 expanded_foreach_funcs.push_back( 351 std::string(exportForEachNameList[i]) + ".expand"); 352 } 353 354 for (i = 0; i < exportForEachCount; i++) { 355 export_symbols.push_back(expanded_foreach_funcs[i].c_str()); 356 } 357 358 pPM.add(llvm::createInternalizePass(export_symbols)); 359 360 return true; 361} 362 363bool Compiler::addInvokeHelperPass(llvm::legacy::PassManager &pPM) { 364 llvm::Triple arch(getTargetMachine().getTargetTriple()); 365 if (arch.isArch64Bit()) { 366 pPM.add(createRSInvokeHelperPass()); 367 } 368 return true; 369} 370 371bool Compiler::addExpandForEachPass(Script &pScript, llvm::legacy::PassManager &pPM) { 372 // Expand ForEach on CPU path to reduce launch overhead. 373 bool pEnableStepOpt = true; 374 pPM.add(createRSForEachExpandPass(pEnableStepOpt)); 375 376 return true; 377} 378 379bool Compiler::addInvariantPass(llvm::legacy::PassManager &pPM) { 380 // Mark Loads from RsExpandKernelDriverInfo as "load.invariant". 381 // Should run after ExpandForEach and before inlining. 382 pPM.add(createRSInvariantPass()); 383 384 return true; 385} 386 387bool Compiler::addCustomPasses(Script &pScript, llvm::legacy::PassManager &pPM) { 388 if (!addInvokeHelperPass(pPM)) 389 return false; 390 391 if (!addExpandForEachPass(pScript, pPM)) 392 return false; 393 394 if (!addInvariantPass(pPM)) 395 return false; 396 397 if (!addInternalizeSymbolsPass(pScript, pPM)) 398 return false; 399 400 return true; 401} 402