@@ -631,22 +631,6 @@ vector<char> CodeGen_PTX_Dev::compile_to_src() {
631631 raw_svector_ostream ostream (outstr);
632632 ostream.SetUnbuffered ();
633633
634- // NOTE: use of the "legacy" PassManager here is still required; it is deprecated
635- // for optimization, but is still the only complete API for codegen as of work-in-progress
636- // LLVM14. At the time of this comment (Dec 2021), there is no firm plan as to when codegen will
637- // be fully available in the new PassManager, so don't worry about this 'legacy'
638- // tag until there's any indication that the old APIs start breaking.
639- //
640- // See:
641- // https://lists.llvm.org/pipermail/llvm-dev/2021-April/150100.html
642- // https://releases.llvm.org/13.0.0/docs/ReleaseNotes.html#changes-to-the-llvm-ir
643- // https://groups.google.com/g/llvm-dev/c/HoS07gXx0p8
644- legacy::FunctionPassManager function_pass_manager (module .get ());
645- legacy::PassManager module_pass_manager;
646-
647- module_pass_manager.add (createTargetTransformInfoWrapperPass (target_machine->getTargetIRAnalysis ()));
648- function_pass_manager.add (createTargetTransformInfoWrapperPass (target_machine->getTargetIRAnalysis ()));
649-
650634 // NVidia's libdevice library uses a __nvvm_reflect to choose
651635 // how to handle denormalized numbers. (The pass replaces calls
652636 // to __nvvm_reflect with a constant via a map lookup. The inliner
@@ -682,17 +666,65 @@ vector<char> CodeGen_PTX_Dev::compile_to_src() {
682666 const bool do_loop_opt = !target.has_feature (Target::DisableLLVMLoopOpt) ||
683667 target.has_feature (Target::EnableLLVMLoopOpt);
684668
685- PassManagerBuilder b;
686- b.OptLevel = 3 ;
687- b.Inliner = createFunctionInliningPass (b.OptLevel , 0 , false );
688- b.LoopVectorize = do_loop_opt;
689- b.SLPVectorize = true ;
690- b.DisableUnrollLoops = !do_loop_opt;
669+ // Define and run optimization pipeline with new pass manager
670+ PipelineTuningOptions pto;
671+ pto.LoopInterleaving = do_loop_opt;
672+ pto.LoopVectorization = do_loop_opt;
673+ pto.SLPVectorization = true ; // Note: SLP vectorization has no analogue in the Halide scheduling model
674+ pto.LoopUnrolling = do_loop_opt;
675+ pto.ForgetAllSCEVInLoopUnroll = true ;
676+
677+ llvm::PassBuilder pb (target_machine.get (), pto);
678+
679+ bool debug_pass_manager = false ;
680+ // These analysis managers have to be declared in this order.
681+ llvm::LoopAnalysisManager lam;
682+ llvm::FunctionAnalysisManager fam;
683+ llvm::CGSCCAnalysisManager cgam;
684+ llvm::ModuleAnalysisManager mam;
685+
686+ // Register all the basic analyses with the managers.
687+ pb.registerModuleAnalyses (mam);
688+ pb.registerCGSCCAnalyses (cgam);
689+ pb.registerFunctionAnalyses (fam);
690+ pb.registerLoopAnalyses (lam);
691+ pb.crossRegisterProxies (lam, fam, cgam, mam);
692+ ModulePassManager mpm;
693+
694+ #if LLVM_VERSION >= 140
695+ using OptimizationLevel = llvm::OptimizationLevel;
696+ #else
697+ using OptimizationLevel = PassBuilder::OptimizationLevel;
698+ #endif
699+
700+ OptimizationLevel level = OptimizationLevel::O3;
701+
702+ target_machine->registerPassBuilderCallbacks (pb);
691703
692- target_machine->adjustPassManager (b);
704+ mpm = pb.buildPerModuleDefaultPipeline (level, debug_pass_manager);
705+ mpm.run (*module , mam);
693706
694- b.populateFunctionPassManager (function_pass_manager);
695- b.populateModulePassManager (module_pass_manager);
707+ if (llvm::verifyModule (*module , &errs ())) {
708+ report_fatal_error (" Transformation resulted in an invalid module\n " );
709+ }
710+
711+ // Optimization pipeline completed; run codegen pipeline
712+
713+ // NOTE: use of the "legacy" PassManager here is still required; it is deprecated
714+ // for optimization, but is still the only complete API for codegen as of work-in-progress
715+ // LLVM14. At the time of this comment (Dec 2021), there is no firm plan as to when codegen will
716+ // be fully available in the new PassManager, so don't worry about this 'legacy'
717+ // tag until there's any indication that the old APIs start breaking.
718+ //
719+ // See:
720+ // https://lists.llvm.org/pipermail/llvm-dev/2021-April/150100.html
721+ // https://releases.llvm.org/13.0.0/docs/ReleaseNotes.html#changes-to-the-llvm-ir
722+ // https://groups.google.com/g/llvm-dev/c/HoS07gXx0p8
723+ legacy::FunctionPassManager function_pass_manager (module .get ());
724+ legacy::PassManager module_pass_manager;
725+
726+ module_pass_manager.add (createTargetTransformInfoWrapperPass (target_machine->getTargetIRAnalysis ()));
727+ function_pass_manager.add (createTargetTransformInfoWrapperPass (target_machine->getTargetIRAnalysis ()));
696728
697729 // Override default to generate verbose assembly.
698730 target_machine->Options .MCOptions .AsmVerbose = true ;
@@ -707,14 +739,15 @@ vector<char> CodeGen_PTX_Dev::compile_to_src() {
707739 internal_error << " Failed to set up passes to emit PTX source\n " ;
708740 }
709741
710- // Run optimization passes
711742 function_pass_manager.doInitialization ();
712743 for (auto &function : *module ) {
713744 function_pass_manager.run (function);
714745 }
715746 function_pass_manager.doFinalization ();
716747 module_pass_manager.run (*module );
717748
749+ // Codegen pipeline completed.
750+
718751 if (debug::debug_level () >= 2 ) {
719752 dump ();
720753 }
0 commit comments