Search code examples
compiler-constructionllvm

How to run alias analysis (AAManager) with the new pass manager in LLVM


I'm trying to use alias analysis in a pass written for the new pass manager in LLVM.

  PreservedAnalyses run(Module &m, ModuleAnalysisManager &mam) {
    auto &aa = mam.getResult<AAManager>(m);

    analysis(aa, m);

    return PreservedAnalyses::all();
  }

In my pass, I use mam.getResult (which appears to be equivalent to getAnalysis<AliasAnalysis>() for the old pass), however, the pass manager wants me to register the analysis first:

Assertion `AnalysisPasses.count(PassT::ID()) && "This analysis pass was not registered prior to being queried"' failed.

I couldn't find any documentation on how to do that. The closest I came to the solution was this question on the mailing list, but the response wasn't very helpful.

How do I register the analysis pass?


Solution

  • Turns out alias analysis cannot be run in a module pass (it's a function level analysis).

    From LLVM's page on the new pass manager:

    The analysis manager only provides analysis results for the same IR type as what the pass runs on. For example, a function pass receives an analysis manager that only provides function-level analyses.

    Workaround:

      PreservedAnalyses run(Module &m, ModuleAnalysisManager &mam) {
        // Get the functional analysis manager from the module analysis manager
        // which has access to the function level analyses
        FunctionAnalysisManager &fam =
            mam.getResult<FunctionAnalysisManagerModuleProxy>(m).getManager();
    
        assert(not fam.empty());
    
        // Iterate over each function and get the Alias Analysis for that 
        // particular function
        for (auto fi = m.begin(); fi != m.end(); ++fi) {
          auto demangledName = demangleSym(fi->getName().str());
          auto &aam = fam.getResult<AAManager>(*fi);
          analyseFunc(aam, *fi);
        }
      }