Add pass to clang pipeline (Solved)

Big Bosses helps!!!
I was able to register a custom static linked pass to the opt tool, and use the pass like:

opt --passes=“strict-opt” no-strict.ll -S -o no-strict2.ll

Now I wish add the strict-opt static pass to clang and run the pass like:

clang -emit-llvm -mllvm -strictopt no-strict.c -S -o nostrict.ll

This is a new pass manager, and I tried serverl ways, but fails again and over again. I think there should be a default pass pipeline registration function for clang.
Or I should modify the clang diver? I don’t think this is a complicate problem, because OLLVM can use clang -mllvm -bcf …

Problem solved. Modify llvm-project/clang/lib/CodeGen/BackendUtil.cpp source code,add a cl::opt<> instance:

static cl::opt pltObf(“PltFla”,
cl::init(false),
cl::desc(“Enable the Pluto Flattening pass”));

in function RunOptimizationPipeline’s body, register callback after:

PassBuilder PB(TM.get(), PTO, PGOOpt, &PIC);

just like dynamic register pass:

PB.registerPipelineStartEPCallback(
              [] (ModulePassManager &MPM, OptimizationLevel OL) {
                if(OL.getSpeedupLevel() >= 2 && pltObf) {
                      MPM.addPass(
                          createModuleToFunctionPassAdaptor(Flattening())
                      );
                      errs() << " add pluto to MPM pass\n";
                } 
              }
          );

And then, we could use the pass like this:

clang -O2 -mllvm -PltFla -emit-llvm -S flat.c -o flat.ll

1 Like

Maybe use Pass Plugin Infrastructure instead

Yes, I use Pass Plugin Infrastructure,I don’t know whether my implements are right.
But I still can’t invoke Pass through clang’s command line argument. Because my Pass is not dynamic linked, I wish clang static link my pass so that I don’t have to invoke clang like

clang  -Xclang -load -Xclang /path/to/pass.so