Run llvm pass from standalone project

Hi all,
I am trying to use LLVM’s AliasAnalysis pass, but from a standalone tool that uses llvm libraries.

The following is the code snippet I am currently using.
PassBuilder PB;

auto mod_manager = ModuleAnalysisManager { };

PB.registerModuleAnalyses(mod_manager);

AAResults& AAR = mod_manager.getResult(*M);

But the code fails at .getResult with the following error:
/llvm/include/llvm/IR/PassManager.h:778: typename PassT::Result& llvm::AnalysisManager<IRUnitT, ExtraArgTs>::getResult(IRUnitT&, ExtraArgTs …) [with PassT = llvm::AAManager; IRUnitT = llvm::Module; ExtraArgTs = {}; typename PassT::Result = llvm::AAResults]: Assertion `AnalysisPasses.count(PassT::ID()) && “This analysis pass was not registered prior to being queried”’ failed.
Aborted (core dumped)

Please point me towards some documentation that explains how to run llvm passes from a standalone project.

Thanks,
Akash Banerjee

Hi Akash,

this isn’t exactly documented, but you can refer to the implementation in the opt tool (see NewPMDriver.cpp). The problem is that you’re missing a full stack of analysis managers.

Cheers,
Philip

Hi,
Thanks for the help, I am now able to run passes from my tool, however, I primarily wanted to use Alias Analysis for getting the Alias Sets for the following problem:
Sample C program:

int f1(int a, int b){return a+b;}
int f2(int a, int b){return a-b;}
int f3(int a, int b){return a*b;}

int main(){
int c;
int (*f_ptr)(int, int);

if(c==10) f_ptr = f1;
else f_ptr = f2;

c = f_ptr(c, 20);
assert(c == 30);

f_ptr = f3;
c = f_ptr(c, 20);
assert(c == 10);

return 0;
}
What I want is just before the first assert call, what are the functions that f_ptr can possibly point to(f1 or f2 in this case), and similarly just before the second assert what are the functions it can point to(always f3) in this case.
Basically, at any given point in the program, I want to find what are the possible functions that a particular function pointer could point to.
I was hoping AliasSetTrackers would give this answer but that doesn’t seem to be the case.
The following are the results of opt -print-alias-sets:

Alias sets for function ‘f1’:
Alias Set Tracker: 2 alias sets for 2 pointer values.
AliasSet[0x557db2e4b7f0, 1] must alias, Mod/Ref Pointers: (i32* %a.addr, LocationSize::precise(4))
AliasSet[0x557db2e4b920, 1] must alias, Mod/Ref Pointers: (i32* %b.addr, LocationSize::precise(4))

Alias sets for function ‘f2’:
Alias Set Tracker: 2 alias sets for 2 pointer values.
AliasSet[0x557db2e4b7f0, 1] must alias, Mod/Ref Pointers: (i32* %a.addr, LocationSize::precise(4))
AliasSet[0x557db2e49b90, 1] must alias, Mod/Ref Pointers: (i32* %b.addr, LocationSize::precise(4))

Alias sets for function ‘main’:
Alias Set Tracker: 4 alias sets for 3 pointer values.
AliasSet[0x557db2e4b7f0, 1] must alias, Mod Pointers: (i32* %retval, LocationSize::precise(4))
AliasSet[0x557db2e4b920, 1] must alias, Mod/Ref Pointers: (i32* %c, LocationSize::precise(4))
AliasSet[0x557db2e58710, 1] must alias, Mod/Ref Pointers: (i32 (i32, i32)** %f_ptr, LocationSize::precise(8))
AliasSet[0x557db2e58760, 1] may alias, Mod/Ref
4 Unknown instructions: i32 %call, i32 %call1, i32 %call2, i32 %call5

Is it possible to do what I am trying using Alias Sets, if not is there something else built into llvm which would help me do the same?

Thanks,
Akash Banerjee.