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