[NewPM][CodeGen] The *Correct* Way to Inject Custom CodeGen Passes with the New PM
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
Hi,
With the new PM support finally maturing for CodeGen and select backends, I wanted to know how I can inject custom-written Machine Function passes into the CodeGen pipeline. In the old PM, this was feasible with `TargetPassConfig::insertPass`. Now, with the new `TargetMachine::buildCodeGenPipeline` interface, the backends' codegen pass builder is never exposed, so I cannot directly call `insertPass` or `disablePass` on it before the pipeline is built to inject my custom passes.
The only option available for me is to use the `PassInstrumentationCallbacks`, and manually run my pass and perform the tasks the PM is supposed to do (e.g. invalidate the analysis results) inside one of its callbacks at a pre-determined point. But even that is not the cleanest (the post pass instrumentation callback doesn't expect you to modify the preserved analysis, its prototype is `using AfterPassFunc = void(StringRef, Any, const PreservedAnalyses &)`).
This is significantly more cumbersome than modifying the IR pipeline with the `PassBuilder` interface, so I was wondering if there's a better way to do this, or if there is a planned way to support this.
Thanks
cc: @arsenm @aeubanks @paperchalice
Contributor guide
Research direction
Start by reading TargetMachine::buildCodeGenPipeline and the former TargetPassConfig::insertPass and disablePass interfaces, then compare them with PassBuilder-based IR pipeline injection. Determine what supported CodeGen pass-injection mechanism is needed and what behavior would demonstrate that custom Machine Function passes can be added cleanly.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100