Compiler.cpp revision 7f59b5f76c7149b42ff6d4910c6f6165b3358e72
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/CodeGen/RegAllocRegistry.h> 21#include <llvm/IR/Module.h> 22#include <llvm/PassManager.h> 23#include <llvm/Support/TargetRegistry.h> 24#include <llvm/Support/raw_ostream.h> 25#include <llvm/IR/DataLayout.h> 26#include <llvm/Target/TargetSubtargetInfo.h> 27#include <llvm/Target/TargetMachine.h> 28#include <llvm/Transforms/IPO.h> 29#include <llvm/Transforms/IPO/PassManagerBuilder.h> 30#include <llvm/Transforms/Scalar.h> 31#include <llvm/Transforms/Vectorize.h> 32 33#include "bcc/Assert.h" 34#include "bcc/Renderscript/RSScript.h" 35#include "bcc/Renderscript/RSTransforms.h" 36#include "bcc/Script.h" 37#include "bcc/Source.h" 38#include "bcc/Support/CompilerConfig.h" 39#include "bcc/Support/Log.h" 40#include "bcc/Support/OutputFile.h" 41#include "bcinfo/MetadataExtractor.h" 42 43#include <string> 44 45using namespace bcc; 46 47const char *Compiler::GetErrorString(enum ErrorCode pErrCode) { 48 switch (pErrCode) { 49 case kSuccess: 50 return "Successfully compiled."; 51 case kInvalidConfigNoTarget: 52 return "Invalid compiler config supplied (getTarget() returns nullptr.) " 53 "(missing call to CompilerConfig::initialize()?)"; 54 case kErrCreateTargetMachine: 55 return "Failed to create llvm::TargetMachine."; 56 case kErrSwitchTargetMachine: 57 return "Failed to switch llvm::TargetMachine."; 58 case kErrNoTargetMachine: 59 return "Failed to compile the script since there's no available " 60 "TargetMachine. (missing call to Compiler::config()?)"; 61 case kErrDataLayoutNoMemory: 62 return "Out of memory when create DataLayout during compilation."; 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 assert(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 146enum Compiler::ErrorCode Compiler::runPasses(Script &pScript, 147 llvm::raw_ostream &pResult) { 148 // Pass manager for link-time optimization 149 llvm::PassManager passes; 150 151 // Empty MCContext. 152 llvm::MCContext *mc_context = nullptr; 153 154 mTarget->addAnalysisPasses(passes); 155 156 // Prepare DataLayout target data from Module 157 llvm::DataLayoutPass *data_layout_pass = 158 new (std::nothrow) llvm::DataLayoutPass(); 159 160 if (data_layout_pass == nullptr) { 161 return kErrDataLayoutNoMemory; 162 } 163 164 // Add DataLayout to the pass manager. 165 passes.add(data_layout_pass); 166 167 // Add our custom passes. 168 if (!addCustomPasses(pScript, passes)) { 169 return kErrCustomPasses; 170 } 171 172 if (mTarget->getOptLevel() == llvm::CodeGenOpt::None) { 173 passes.add(llvm::createGlobalOptimizerPass()); 174 passes.add(llvm::createConstantMergePass()); 175 176 } else { 177 // FIXME: Figure out which passes should be executed. 178 llvm::PassManagerBuilder Builder; 179 Builder.Inliner = llvm::createFunctionInliningPass(); 180 Builder.populateLTOPassManager(passes, mTarget); 181 182 /* FIXME: Reenable autovectorization after rebase. 183 bug 19324423 184 // Add vectorization passes after LTO passes are in 185 // additional flag: -unroll-runtime 186 passes.add(llvm::createLoopUnrollPass(-1, 16, 0, 1)); 187 // Need to pass appropriate flags here: -scalarize-load-store 188 passes.add(llvm::createScalarizerPass()); 189 passes.add(llvm::createCFGSimplificationPass()); 190 passes.add(llvm::createScopedNoAliasAAPass()); 191 passes.add(llvm::createScalarEvolutionAliasAnalysisPass()); 192 // additional flags: -slp-vectorize-hor -slp-vectorize-hor-store (unnecessary?) 193 passes.add(llvm::createSLPVectorizerPass()); 194 passes.add(llvm::createDeadCodeEliminationPass()); 195 passes.add(llvm::createInstructionCombiningPass()); 196 */ 197 } 198 199 // Add our pass to check for illegal function calls. 200 // This has to come after LTO, since we don't want to examine functions that 201 // are never actually called. 202 passes.add(createRSScreenFunctionsPass()); 203 passes.add(createRSIsThreadablePass()); 204 205 // RSEmbedInfoPass needs to come after we have scanned for non-threadable 206 // functions. 207 // Script passed to RSCompiler must be a RSScript. 208 RSScript &script = static_cast<RSScript &>(pScript); 209 if (script.getEmbedInfo()) 210 passes.add(createRSEmbedInfoPass()); 211 212 // Add passes to the pass manager to emit machine code through MC layer. 213 if (mTarget->addPassesToEmitMC(passes, mc_context, pResult, 214 /* DisableVerify */false)) { 215 return kPrepareCodeGenPass; 216 } 217 218 // Execute the passes. 219 passes.run(pScript.getSource().getModule()); 220 221 return kSuccess; 222} 223 224enum Compiler::ErrorCode Compiler::compile(Script &pScript, 225 llvm::raw_ostream &pResult, 226 llvm::raw_ostream *IRStream) { 227 llvm::Module &module = pScript.getSource().getModule(); 228 enum ErrorCode err; 229 230 if (mTarget == nullptr) { 231 return kErrNoTargetMachine; 232 } 233 234 const std::string &triple = module.getTargetTriple(); 235 const llvm::DataLayout *dl = getTargetMachine().getSubtargetImpl()->getDataLayout(); 236 unsigned int pointerSize = dl->getPointerSizeInBits(); 237 if (triple == "armv7-none-linux-gnueabi") { 238 if (pointerSize != 32) { 239 return kErrInvalidSource; 240 } 241 } else if (triple == "aarch64-none-linux-gnueabi") { 242 if (pointerSize != 64) { 243 return kErrInvalidSource; 244 } 245 } else { 246 return kErrInvalidSource; 247 } 248 249 // Materialize the bitcode module. 250 if (module.getMaterializer() != nullptr) { 251 // A module with non-null materializer means that it is a lazy-load module. 252 // Materialize it now via invoking MaterializeAllPermanently(). This 253 // function returns false when the materialization is successful. 254 std::error_code ec = module.materializeAllPermanently(); 255 if (ec) { 256 ALOGE("Failed to materialize the module `%s'! (%s)", 257 module.getModuleIdentifier().c_str(), ec.message().c_str()); 258 return kErrMaterialization; 259 } 260 } 261 262 if ((err = runPasses(pScript, pResult)) != kSuccess) { 263 return err; 264 } 265 266 if (IRStream) { 267 *IRStream << module; 268 } 269 270 return kSuccess; 271} 272 273enum Compiler::ErrorCode Compiler::compile(Script &pScript, 274 OutputFile &pResult, 275 llvm::raw_ostream *IRStream) { 276 // Check the state of the specified output file. 277 if (pResult.hasError()) { 278 return kErrInvalidOutputFileState; 279 } 280 281 // Open the output file decorated in llvm::raw_ostream. 282 llvm::raw_ostream *out = pResult.dup(); 283 if (out == nullptr) { 284 return kErrPrepareOutput; 285 } 286 287 // Delegate the request. 288 enum Compiler::ErrorCode err = compile(pScript, *out, IRStream); 289 290 // Close the output before return. 291 delete out; 292 293 return err; 294} 295 296bool Compiler::addInternalizeSymbolsPass(Script &pScript, llvm::PassManager &pPM) { 297 // Add a pass to internalize the symbols that don't need to have global 298 // visibility. 299 RSScript &script = static_cast<RSScript &>(pScript); 300 llvm::Module &module = script.getSource().getModule(); 301 bcinfo::MetadataExtractor me(&module); 302 if (!me.extract()) { 303 bccAssert(false && "Could not extract metadata for module!"); 304 return false; 305 } 306 307 // The vector contains the symbols that should not be internalized. 308 std::vector<const char *> export_symbols; 309 310 const char *sf[] = { 311 "root", // Graphics drawing function or compute kernel. 312 "init", // Initialization routine called implicitly on startup. 313 ".rs.dtor", // Static global destructor for a script instance. 314 ".rs.info", // Variable containing string of RS metadata info. 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.push_back(*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 const char **exportVarNameList = me.getExportVarNameList(); 330 const char **exportFuncNameList = me.getExportFuncNameList(); 331 const char **exportForEachNameList = me.getExportForEachNameList(); 332 size_t i; 333 334 for (i = 0; i < exportVarCount; ++i) { 335 export_symbols.push_back(exportVarNameList[i]); 336 } 337 338 for (i = 0; i < exportFuncCount; ++i) { 339 export_symbols.push_back(exportFuncNameList[i]); 340 } 341 342 // Expanded foreach functions should not be internalized, too. 343 // expanded_foreach_funcs keeps the .expand version of the kernel names 344 // around until createInternalizePass() is finished making its own 345 // copy of the visible symbols. 346 std::vector<std::string> expanded_foreach_funcs; 347 for (i = 0; i < exportForEachCount; ++i) { 348 expanded_foreach_funcs.push_back( 349 std::string(exportForEachNameList[i]) + ".expand"); 350 } 351 352 for (i = 0; i < exportForEachCount; i++) { 353 export_symbols.push_back(expanded_foreach_funcs[i].c_str()); 354 } 355 356 pPM.add(llvm::createInternalizePass(export_symbols)); 357 358 return true; 359} 360 361bool Compiler::addInvokeHelperPass(llvm::PassManager &pPM) { 362 llvm::Triple arch(getTargetMachine().getTargetTriple()); 363 if (arch.isArch64Bit()) { 364 pPM.add(createRSInvokeHelperPass()); 365 } 366 return true; 367} 368 369bool Compiler::addExpandForEachPass(Script &pScript, llvm::PassManager &pPM) { 370 // Expand ForEach on CPU path to reduce launch overhead. 371 bool pEnableStepOpt = true; 372 pPM.add(createRSForEachExpandPass(pEnableStepOpt)); 373 374 return true; 375} 376 377bool Compiler::addCustomPasses(Script &pScript, llvm::PassManager &pPM) { 378 if (!addInvokeHelperPass(pPM)) 379 return false; 380 381 if (!addExpandForEachPass(pScript, pPM)) 382 return false; 383 384 if (!addInternalizeSymbolsPass(pScript, pPM)) 385 return false; 386 387 return true; 388} 389