Hi,
Motivation
AMDGPU code has some specific requirements for codegen that we need to take into account when splitting modules. The main one being that we have to perform codegen in SCC order and keep all reachable functions in the same module in order to correctly estimate resource usage analysis, which is needed to launch kernels.
The default SplitModule pass does not work for this purpose as it depends on externalizing to achieve good balancing (at least for AMDGPU, because most functions are internal in such modules), and it’s not CFG-aware, so it doesn’t try to put callers and callees together.
We thus need a new approach, so I’m implementing a custom module splitting algorithm built from the start with AMDGPU code in mind (the PR for this will be posted in the coming weeks), but I need a way to tell LTO to use it instead of using SplitModule.
Proposal
This patch adds a TargetMachine function called splitModule and changes the splitCodegen function from the LTOBackend to use it instead, and fallback to SplitModule otherwise. It also changes llvm-split so it can be used to test target-specific module splitting logic.
I believe this is a good approach and it’s fairly non-intrusive, with the exception of llvm-split seeing large changes needed to link against Target libraries.
NOTE: If this patch is approved, it would only land if/when the AMDGPU-specific splitModule implementation lands as well. I will not commit this API without the use case for it.
Alternatives
I could improve the custom SplitModule pass instead, but I’m trending towards a completely new module splitting approach instead of just making SplitModule CallGraph-aware; this means that SplitModule would end up with two different (mostly) unrelated code paths in the same file.
I’m also using some backend-specific implementation details (e.g. the pass is aware of how AMDGPUResourceUsageAnalysis works exactly, and it’s likely we’ll want to leverage AMDGPU target features as well in the future), and leaking those inside a target-agnostic library feels like bad design.