[Solved]Migrating legacyPM passes to NewPM

Continuing the questions from:

My Legacy PM pass heavily uses doInitialization() and doFinalization(), aeubanks suggested that it could be done with lazy construction in NPM, however this doesn’t give access to doFinalization.

So the solution would be implement the pass as ModulePass?
However this raises another question w.r.t. to analysis passes,for example, in legacyPM, in order to get DominatorTree from DominatorTreeWrapperPass, I could:

  • getAnalysis<DominatorTreeWrapperPass>(Function& F) in a ModulePass
  • getAnalysis<DominatorTreeWrapperPass>() in a FunctionPass

However this doesn’t work in NewPM:

PreservedAnalyses TestPass::run(Module &M, ModuleAnalysisManager &MAM){
    MAM.getResult<FunctionAnalysisManager>(MAM);

I mainly uses those two methods for the following:

  • Maintaining my pass’s internal states and data structures from Module, that is required only once, and before my pass is actually run.
  • Insert new GlobalValues in doFinalization / doInitialization so I could keep my core pass as a FunctionPass.
    • For example, I insert printf 's prototype into the Module so I could print messages later

Seems like

FunctionAnalysisManager & FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();

would do the job

Yeah that’s how you access function analyses from a module pass.

Inserting intrinsic declarations from a function pass is considered okay, but inserting a non-intrinsic declaration is a bit less clear.

What exactly are you using doFinalization for? Everything you’ve mentioned happens before the pass runs.

Yeah that’s how you access function analyses from a module pass.

Thank you

Inserting intrinsic declarations from a function pass is considered okay, but inserting a non-intrinsic declaration is a bit less clear.

What exactly are you using doFinalization for? Everything you’ve mentioned happens before the pass runs.

I add for example printf() as my transform pass uses it to print messages on some branches, and remove it in doFinalization if the prototype is actually not being used

If you lazily create the printf declaration then that shouldn’t be an issue.

1 Like