How to use alias analysis results in a ModulePass?

Is is possible to use AliasAnalysis in a ModulePass? It was pretty straightforward using it in a FunctionPass, I just added the following lines:

  void getAnalysisUsage(AnalysisUsage &AU) const override {


  AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();

in the runOnFunction function.

However, if I adapt my FunctionPass into a ModulePass and leave those “settings” the same, then it doesn’t work anymore, I get an exception when compiling a C file:

llvm-project/llvm/include/llvm/PassAnalysisSupport.h:245: AnalysisType &llvm::Pass::getAnalysisID(llvm::AnalysisID) const [AnalysisType = llvm::AAResultsWrapperPass]: Assertion `ResultPass && "getAnalysis*() called on an analysis that was not " "'required' by pass!"' failed.

Any ideas how I can get this to work?

If you want the result of a function analysis, try
AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>(/* Function*/FnPtr).getAAResults();

1 Like

So basically what I need is the AliasAnalysis for an entire module. And you are saying the AliasAnalysis is only always performed per function? So then: Can I then iterate over all functions in the module and perform the alias analysis for each?
And there is no way to have llvm perform the alias analysis for entire modules before calling my modulepass?

Yes. IIRC, all in-tree alias analyses are.

Yes, but you cannot use two results at the same time, with the old PM at least. You can also not use the results for inter-procedural AA queries.

Not with in-tree analysis, I think. Maybe once we implement AAAliasAnalsyis…

1 Like
Yes, but you cannot use two results at the same time, with the old PM at least.

What does this mean exactly? Naively, I thought (I know this is probably super inefficient, it’s more to get an MVP to see if what I want to do works) I could fill some sort of hashmap with a function->aliasanalysisresult mapping, and then access the AA when I need it for a particular function. Does this count as using two results at the same time?

IIRC, and I am not 100% sure about this, the old PM will invalidate a result as soon as you query a result for a different function.
I would also assume that the AAResult is actually lazy, causing it to be filled only on demand, thus the analysis needs to be around.
I could image what you are planning to do might work with the new PM though.

Thanks for the help already!

I am fairly new to llvm and have mostly worked with the old PM so far, and the infrastructure I am currently working with also uses the old PM, so I want to try to make that work for now.

So are you saying that one way to work around these issues could be to request the alias analysis results each time I need them, especially if they are evaluated lazily anyways? So if I need the results of an alias analysis, i just request them with

AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>(/* Function*/FnPtr).getAAResults();



1 Like

What I mean more specifically is that, usually, you would probably only call that line once for a function pass. But I would instead probably call it many times in my module pass. Does this sound like a terrible idea, or acceptable (performance aside of course)?

Also, while we’re on the topic of Alias Analysis: I read here LLVM Alias Analysis Infrastructure — LLVM 17.0.0git documentation that there are several different implementations that provide different qualities of results. However, I didn’t quite understand how I choose one implementation or the other, since I am just using the line of code you provided earlier in my pass, and I’m not specifying the implementation I want to use anywhere. Do I have to pass some cli flags to choose an implementation maybe?

Given your situation, it sounds acceptable.

If you run O3, or similar, you get a bunch of AA implementations chained together. You can use the pass manager or potentially CLI to request others.

1 Like