Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LLVM (3.5+) PassManager vs LegacyPassManager

I'm working on a new language using the LLVM C++ API and would like to take advantage of optimization passes. (Note: I'm currently using the latest from source LLVM which I believe equates to 3.8)

I have yet to find any examples that use the new PassManager and even Clang is still utilizing the LegacyPassManager.

I have come across posts such as this that are several years old now that mention the new PassManager, but they all still use the legacy system.

Is there any examples/tutorials on how to use this new(ish) PassManager? Should new LLVM projects prefer PassManager to LegacyPassManager? Does Clang plan on migrating or is this why the Legacy system has stuck around?

like image 472
Matthew Sanders Avatar asked Dec 13 '15 19:12

Matthew Sanders


2 Answers

From what I've gathered with help from the #llvm IRC:

FunctionPassManager FPM;
//Use the PassInfoMixin types
FPM.addPass(InstCombinePass());

//Register any analysis passes that the transform passes might need
FunctionAnalysisManager FAM;

//Use the AnalysisInfoMixin types
FAM.registerPass([&] { return AssumptionAnalysis(); });
FAM.registerPass([&] { return DominatorTreeAnalysis(); });
FAM.registerPass([&] { return BasicAA(); });
FAM.registerPass([&] { return TargetLibraryAnalysis(); });

FPM.run(*myFunction, FAM);

But to avoid the hassle of manually registering each pass you can use PassBuilder to register the analysis passes

FunctionPassManager FPM;
FPM.addPass(InstCombinePass());

FunctionAnalysisManager FAM;

PassBuilder PB;
PB.registerFunctionAnalyses(FAM);

FPM.run(*myFunction, FAM);
like image 182
Luke Avatar answered Oct 24 '22 10:10

Luke


Extending Lukes answer, with PassBuilder you can build predefined "out of box" simplification pipelines with different optimization levels:

llvm::FunctionAnalysisManager FAManager;
llvm::PassBuilder passBuilder;

passBuilder.registerFunctionAnalyses(FAManager);

passBuilder.buildFunctionSimplificationPipeline(
        llvm::PassBuilder::OptimizationLevel::O2,
        llvm::PassBuilder::ThinLTOPhase::None);

which will add a bunch of passes to FunctionAnalysisManager. This may simplify your life. The best place to see the full set of passes added for each OptimizationLevel is the original sources.

like image 30
WindyFields Avatar answered Oct 24 '22 11:10

WindyFields