Target specific pass before SimplifyCFGPass

Is there any possibility to execute target specific pass before SimplifyCFGPass,
or it is required to put it here:

void PassManagerBuilder::populateFunctionPassManager(
legacy::FunctionPassManager &FPM) {
addExtensionsToPM(EP_EarlyAsPossible, FPM);
FPM.add(createEntryExitInstrumenterPass());
// Add LibraryInfo if we have some.
if (LibraryInfo)
FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
// The backends do not handle matrix intrinsics currently.
// Make sure they are also lowered in O0.
// FIXME: A lightweight version of the pass should run in the backend
// pipeline on demand.
if (EnableMatrix && OptLevel == 0)
FPM.add(createLowerMatrixIntrinsicsMinimalPass());
if (OptLevel == 0) return;

addInitialAliasAnalysisPasses(FPM);

FPM.add(createPassExecutedBeforeCFGSimplifyPass());
FPM.add(createCFGSimplificationPass());
FPM.add(createSROAPass());
FPM.add(createEarlyCSEPass());

}

and

INITIALIZE_PASS_BEGIN(CFGSimplifyPass, “simplifycfg”, “Simplify the CFG”, false,
false)
INITIALIZE_PASS_DEPENDENCY(PassExecutedBeforeCFGSimplifyPass)
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
INITIALIZE_PASS_END(CFGSimplifyPass, “simplifycfg”, “Simplify the CFG”, false,
false)

I see that adding the pass inside addIRPasses() causes that it is executed too late…
… or am I doing sth wrong…

Hi!

As usual it depends on what you exactly want to do. The SimplifyCFGPass pass is part of the target-independent optimization pipeline, and depending on what pipeline is run, it is executed several times, see e.g. in PassBuilder::buildO1FunctionSimplificationPipeline(). If you need to run your pass always exactly before the SimplifyCFGPass pass then I guess your best chance is to create your own pipeline. With the new pass manager you can create a string describing the optimization pipeline. Of course, changing the LLVM source is always possible, too.
If your requirements are more relaxed then you can check the several callback hooks provided to customize the pass pipeline.

Please note that all I wrote above refers to the platform-independent pipeline, and this pipeline is different from backend pipeline. adIRPasses() sets up the IR passes run before code generation, usually to prepare the IR for code generation. As you already found out, this is not the right place to add a pass which must run much earlier.

Regards,
Kai

Hello,

if it is feasible in your case not to use an optimization pipeline you could disable this pipeline with the -O0 flag and then use the addIRPasses() hook to run the passes in the order you want.

Thanks Kai for your suggestions. I will try sporach with own pipeline.
In general I want to execute different optimization passes so -O0 doesn’t work for me.

Thanks all,
Przemek