[LLVMdev] Question regarding MCJIT and optimization

Dibyendu Majumdar mobile at majumdar.org.uk
Thu Mar 5 12:34:06 PST 2015


Hi,

I am wondering what the best way is to add optimization passes - I am
using MCJIT in my project. I initially used the example in
Kaleidoscope but found that I needed to add type based analysis to
help optimize away redundant loads.

Then I also discovered PassManagerBuilder.

I have tried following two options but I don't know if a) what the
recommended approach is, and b) is below correct way to use
PassManagerBuilder. Any help / guidance will be very much appreciated.

Regards

Option 1
-----------
  llvm::FunctionPassManager *FPM = new llvm::FunctionPassManager(module_);
  llvm::PassManager *MPM = new llvm::PassManager();

#if LLVM_VERSION_MINOR > 5
  module_->setDataLayout(engine_->getDataLayout());
  FPM->add(new llvm::DataLayoutPass());
#else
  auto target_layout = engine_->getTargetMachine()->getDataLayout();
  module_->setDataLayout(target_layout);
  FPM->add(new llvm::DataLayoutPass(*engine_->getDataLayout()));
#endif
  llvm::PassManagerBuilder pmb;
  pmb.OptLevel = 3;
  pmb.populateFunctionPassManager(*FPM);
  pmb.populateModulePassManager(*MPM);
  FPM->doInitialization();

  // For each function in the module
  // Run the FPM on this function
  FPM->run(*function_);
  MPM->run(*module_);


Option 2
-----------
  llvm::FunctionPassManager *FPM = new llvm::FunctionPassManager(module_);

#if LLVM_VERSION_MINOR > 5
  // LLVM 3.6.0 change
  module_->setDataLayout(engine_->getDataLayout());
  FPM->add(new llvm::DataLayoutPass());
#else
  auto target_layout = engine_->getTargetMachine()->getDataLayout();
  module_->setDataLayout(target_layout);
  FPM->add(new llvm::DataLayoutPass(*engine_->getDataLayout()));
#endif
  FPM->add(llvm::createTypeBasedAliasAnalysisPass());
  // Provide basic AliasAnalysis support for GVN.
  FPM->add(llvm::createBasicAliasAnalysisPass());
  FPM->add(llvm::createLICMPass());
  // Promote allocas to registers.
  FPM->add(llvm::createPromoteMemoryToRegisterPass());
  // Do simple "peephole" optimizations and bit-twiddling optzns.
  FPM->add(llvm::createInstructionCombiningPass());
  // Reassociate expressions.
  FPM->add(llvm::createReassociatePass());
  // Eliminate Common SubExpressions.
  FPM->add(llvm::createGVNPass());
  // Simplify the control flow graph (deleting unreachable blocks, etc).
  FPM->add(llvm::createCFGSimplificationPass());
  FPM->doInitialization();

  // For each function in the module
  // Run the FPM on this function
  FPM->run(*function_);



More information about the llvm-dev mailing list