Using function level analysis in module pass using new pass manager (out of tree)

Hi,

I want to use dominator analysis inside my module pass. I have the following code that will create my pipeline. However this crashes at runtime. What I am doing wrong here? Can someone help.

Thanks
Charitha

ModulePassManager mpm;
mpm.addPass(MyModulePass());
ModuleAnalysisManager module_analysis;
FunctionAnalysisManager function_analysis;
function_analysis.registerPass([] { return DominatorTreeAnalysis(); });
function_analysis.registerPass([] { return PassInstrumentationAnalysis(); });
module_analysis.registerPass([] { return PassInstrumentationAnalysis(); });
module_analysis.registerPass([&] { return FunctionAnalysisManagerModuleProxy(function_analysis); });
    
mpm.run(*module, module_analysis);

Run time error,

#0 0x0000561b8787db6f PrintStackTraceSignalHandler(void*) (../../build/bin/klee+0x1d38b6f)
 #1 0x0000561b8787b31d SignalHandler(int) (../../build/bin/klee+0x1d3631d)
 #2 0x00007f92a8d2a980 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12980)
 #3 0x0000561b86675b60 std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Function, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Function, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function>::Invalidator> > >::~unique_ptr() /usr/include/c++/7/bits/unique_ptr.h:262:0
 #4 0x0000561b86675b60 std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Function, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Function, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function>::Invalidator> > > >::~pair() /usr/include/c++/7/bits/stl_pair.h:208:0
 #5 0x0000561b86675b60 void __gnu_cxx::new_allocator<std::_List_node<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Function, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Function, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function>::Invalidator> > > > > >::destroy<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Function, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Function, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function>::Invalidator> > > > >(std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Function, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Function, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function>::Invalidator> > > >*) /usr/include/c++/7/ext/new_allocator.h:140:0
PreservedAnalyses MyModulePass::run(Module &M,
                                         ModuleAnalysisManager &MAM) {
  FunctionAnalysisManager &FAM =
      MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
 auto yourAnalysis = FAM.getResult<Analysis>(F)

Thanks for the reply. But in this case I want to build my own pass pipeline with specific passes and run it, not just use my pass with opt. Assume a case where you want to run your pass in a standalone llvm tool and its a module pass that uses functional level analysis.

After some research I found following approach seems to work fine.

llvm::PassBuilder passBuilder;
llvm::LoopAnalysisManager loopAnalysisManager; 
llvm::FunctionAnalysisManager functionAnalysisManager;
llvm::CGSCCAnalysisManager cGSCCAnalysisManager;
llvm::ModuleAnalysisManager moduleAnalysisManager;



passBuilder.registerModuleAnalyses(moduleAnalysisManager);
passBuilder.registerCGSCCAnalyses(cGSCCAnalysisManager);
passBuilder.registerFunctionAnalyses(functionAnalysisManager);
passBuilder.registerLoopAnalyses(loopAnalysisManager);
  
passBuilder.crossRegisterProxies(
    loopAnalysisManager, functionAnalysisManager, cGSCCAnalysisManager,
    moduleAnalysisManager);

llvm::ModulePassManager modulePassManager;
  
  
modulePassManager.addPass(MyModulePass());
modulePassManager.run(*module, moduleAnalysisManager);