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?

Alias analysis if a function analysis, not a module analysis. You need to fetch it from a FunctionAnalysisManager while passing in a specific function.

You are right. I tried that and it worked.

Turns out the page on new pass manager says that:

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.

Should’ve read more carefully.

Thanks a lot!