Optimisation pipelines

Hi everyone,

I’m trying to identify where Clang defines optimsation pipelines (middle-end and back-end). For the sake of simplicity, I’m mostly interested in plain -O{0|1|2|3} (I appreciate that LTO, sanitizers and various code-gen flags make this much more nuanced).

I’ve always been under the impression that this is totally custom in Clang (i.e. defined from scratch), but on closer inspection I see that this is done through LLVM’s API instead (i.e. Clang relies on the pipelines defined in LLVM). More specifically, in BackendUtil.cpp:

So, the basic pipelines are in fact defined in LLVM. Clang simply forwards the optimisation level (e.g. -O3) to LLVM. Did I get this right?

For context, I would like to define optimisation flags in Flang. It would be great to make Clang and Flang behave consistently here!

Thank you,


Correct, majority of the pipeline body in any optimization level is dictated by PassBuilder, with only few differences, like sanitizer (when needed) + its preparation Passes.

1 Like

PassBuilder belongs to the new PM infra, right? Or is it also used for the legacy PM pipelines? IIRC, the backend still uses the legacy PM.

Indeed, the backend still uses the legacy PM.

The equivalent legacy functions for optimization level passes are defined in PassManagerBuilder.cpp (populateModulePassManager, populateLTOPassManager, etc.)

1 Like

Thank you both!