Expressing dependency to a ModulePass from a FunctionPass

Using the new PassManager I’d like to express a dependency to a ModuleAnalysisPass from a function pass.

If I simply get the Result via auto result = FAM.getResult<ModuleAnalysisPass>(F); in my FunctionPass::run(), I get AssertionAnalysisPasses.count(PassT::ID()) && “This analysis pass was not registered prior to being queried”`.

If I create another module-pass doing the same on MAM, it works, the analysis-pass is loaded and run.

I’m find results with using getAnalysisUsage() but I have to create an ID-field, which seems to me the legacy pass-manager’s way.

When adding a ModulePass which gets the result via its MAM, I can then fetch the result in my function pass via

  auto MAM = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
  auto result = MAM.getCachedResult<ModuleAnalysisPass>(*F.getParent());

Is there a shortcut?

maybe you didn’t cross register the analysis managers, e.g. https://github.com/llvm/llvm-project/blob/efcb07bf6e5fb0ff12e5855ab0ad86cd534d5578/llvm/tools/opt/NewPMDriver.cpp#L453 ?

No, I didn’t see it, and at a first glance, I’m not sure how this can help me.

if you register module analyses with one ModuleAnalysisManager but don’t register that ModuleAnalysisManager with the FunctionAnalysisManager, FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F) will give you a fresh new ModuleAnalysisManager that doesn’t have any analyses registered

I’m using the default PassBuilder if I’m correct, so then managers should be cross-registered (?). I’m registering my passes via a plugin from a shared library.

The this line should cross-link the MAM to the FAM in my Pass, right?

  FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });

Then auto MAM = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F); should give me the MAM and then auto result = MAM.getCachedResult<ModuleAnalysisPass>(*F.getParent()); Should give me the resul. But it doesn’t.

yeah that line looks right

then maybe you’re running into the case where you need to have the module analysis already computed and in the cache beforehand, see Using the New Pass Manager — LLVM 18.0.0git documentation for more details. here’s an example