From c6e4f112950c17edfb66a88efb621d7effc88966 Mon Sep 17 00:00:00 2001 From: Yue Ni Date: Mon, 25 Sep 2023 15:01:03 +0800 Subject: [PATCH] Update the FinalizeModule function to use the new LLVM PassManager API. --- cpp/src/gandiva/engine.cc | 69 ++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/cpp/src/gandiva/engine.cc b/cpp/src/gandiva/engine.cc index 1ac3e22f1741f..b4eae03e1a2c0 100644 --- a/cpp/src/gandiva/engine.cc +++ b/cpp/src/gandiva/engine.cc @@ -59,12 +59,14 @@ #include #include #include -#include #include +#include #include -#include -#include -#include +#include +#include +#include +#include +#include #if LLVM_VERSION_MAJOR >= 14 #include #else @@ -302,28 +304,43 @@ Status Engine::FinalizeModule() { ARROW_RETURN_NOT_OK(RemoveUnusedFunctions()); if (optimize_) { - // misc passes to allow for inlining, vectorization, .. - std::unique_ptr pass_manager( - new llvm::legacy::PassManager()); - - llvm::TargetIRAnalysis target_analysis = - execution_engine_->getTargetMachine()->getTargetIRAnalysis(); - pass_manager->add(llvm::createTargetTransformInfoWrapperPass(target_analysis)); - pass_manager->add(llvm::createFunctionInliningPass()); - pass_manager->add(llvm::createInstructionCombiningPass()); - pass_manager->add(llvm::createPromoteMemoryToRegisterPass()); - pass_manager->add(llvm::createGVNPass()); - pass_manager->add(llvm::createNewGVNPass()); - pass_manager->add(llvm::createCFGSimplificationPass()); - pass_manager->add(llvm::createLoopVectorizePass()); - pass_manager->add(llvm::createSLPVectorizerPass()); - pass_manager->add(llvm::createGlobalOptimizerPass()); - - // run the optimiser - llvm::PassManagerBuilder pass_builder; - pass_builder.OptLevel = 3; - pass_builder.populateModulePassManager(*pass_manager); - pass_manager->run(*module_); + // Setup an optimiser pipeline + llvm::PassBuilder pass_builder; + llvm::LoopAnalysisManager loop_am; + llvm::FunctionAnalysisManager function_am; + llvm::CGSCCAnalysisManager cgscc_am; + llvm::ModuleAnalysisManager module_am; + + function_am.registerPass( + [&] { return execution_engine_->getTargetMachine()->getTargetIRAnalysis(); }); + + // Register required analysis managers + pass_builder.registerModuleAnalyses(module_am); + pass_builder.registerCGSCCAnalyses(cgscc_am); + pass_builder.registerFunctionAnalyses(function_am); + pass_builder.registerLoopAnalyses(loop_am); + pass_builder.crossRegisterProxies(loop_am, function_am, cgscc_am, module_am); + + pass_builder.registerPipelineStartEPCallback( + [&](llvm::ModulePassManager& module_pm, llvm::OptimizationLevel Level) { + module_pm.addPass(llvm::ModuleInlinerPass()); + + llvm::FunctionPassManager function_pm; + function_pm.addPass(llvm::InstCombinePass()); + function_pm.addPass(llvm::PromotePass()); + function_pm.addPass(llvm::GVNPass()); + function_pm.addPass(llvm::NewGVNPass()); + function_pm.addPass(llvm::SimplifyCFGPass()); + function_pm.addPass(llvm::LoopVectorizePass()); + function_pm.addPass(llvm::SLPVectorizerPass()); + module_pm.addPass( + llvm::createModuleToFunctionPassAdaptor(std::move(function_pm))); + + module_pm.addPass(llvm::GlobalOptPass()); + }); + + pass_builder.buildPerModuleDefaultPipeline(llvm::OptimizationLevel::O3) + .run(*module_, module_am); } ARROW_RETURN_IF(llvm::verifyModule(*module_, &llvm::errs()),