LLVM (3.5+) PassManager vs LegacyPassManager

雨燕双飞 提交于 2020-08-01 02:58:26

问题


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?


回答1:


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);



回答2:


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.



来源:https://stackoverflow.com/questions/34255383/llvm-3-5-passmanager-vs-legacypassmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!