Step 04 · The new pass manager
LLVM has two pass managers: the legacy llvm::PassManager and the
"new" one rooted at llvm::PassBuilder. New code targets the new PM.
llvm::LoopAnalysisManager lam;
llvm::FunctionAnalysisManager fam;
llvm::CGSCCAnalysisManager cam;
llvm::ModuleAnalysisManager mam;
llvm::PassBuilder pb;
pb.registerModuleAnalyses(mam);
pb.registerCGSCCAnalyses(cam);
pb.registerFunctionAnalyses(fam);
pb.registerLoopAnalyses(lam);
pb.crossRegisterProxies(lam, fam, cam, mam);
auto mpm = pb.buildPerModuleDefaultPipeline(llvm::OptimizationLevel::O2);
mpm.run(mod, mam);
Anatomy
- Analysis managers cache analyses (dominator tree, loop info, alias analysis, …) per IR unit. They must be registered with each other so a function pass can ask for a module-level analysis.
- PassBuilder assembles a
ModulePassManagercorresponding to one of the standard-O0/-O1/-O2/-O3/-Os/-Ozpipelines. mpm.run(mod, mam)mutates the module in place.
Custom pipelines
You can build your own with addPass(MyPass()). We rely on the canned
O2 pipeline so we inherit decades of tuning.
Where this lives
llvm_codegen.cpp → optimise(Module&). mlcc calls it when given
-O. Tests pass runOpt = true to exercise specific transformations.