We would like to propose an architecture for supporting scalable separate
compilation of GPU device code in LLVM, with AMDGPU as the first implementation
target. Separately generated device objects can contain per-function resource
information. However, a kernel’s final requirements depend on the definitions
selected from other device objects and the resources required by those device
functions. The complete requirements therefore become known only after final
object-link resolution. This RFC first describes that general finalization
requirement and then proposes an AMDGPU implementation with a narrow interface
between ordinary ELF linking and AMDGPU-owned resource analysis.
The underlying problem can apply to other GPUs and accelerators that
pre-allocate compute resources before execution so that threads can start and
run correctly. A device program’s entry point is often a kernel function.
Launching a kernel requires the runtime and hardware to reserve resources for
its lifetime, including resources needed by other device functions reachable
from that kernel. These include register usage, shared memory usage, scratch
usage, and other device-specific resources that affect dispatch and occupancy.
Under separate compilation, those values can depend on device functions
compiled in other translation units.
We propose to integrate this finalization into the AMDGPU ELF link in a way that
does not require full LTO for correctness. At the same time, we want the
target-specific logic to be documented, owned by the AMDGPU community, and kept
behind a small lld/ELF integration surface.
Background: why GPU object linking needs a finalization step
In a conventional ELF link, the generated machine code in each input object is
normally treated as finished. Linkers perform target-specific work, but the
output ABI generally does not require a transitive resource summary for every
entry point before execution can begin.
GPU kernels add another constraint. A kernel dispatch uses explicit metadata
from the device image to reserve finite on-chip resources and determine how many
work-groups can be resident on a compute unit. These values are part of the
execution contract between the compiled image, the runtime, and the hardware.
They are not just debug information or optimization hints. They are required for
correctness and forward progress guarantees.
The complication appears when the program is compiled separately. Consider the
following illustrative pseudocode:
// a.cpp
extern device void helper();
kernel void K() {
helper();
}
// b.cpp
device void helper() {
use_many_registers();
use_shared_memory_object();
}
When a.cpp is compiled, the compiler knows that K calls helper, but it
does not know the final resource usage of helper or the shared-memory objects
reachable through it. The object file for K therefore cannot contain complete
final metadata for K. The missing information is not available in any single
compiler invocation; it appears only when the separately compiled device objects
are linked together.
Full LTO is correct, but not enough as the only model
Full LTO is one correct way to avoid the separate compilation gap. It merges all
device IR into a single module before backend compilation. The backend then has
the same whole-program visibility that it has for single-file compilation, so
it can build the device call graph, propagate resource usage, lay out
compiler-managed resources, and emit final kernel metadata.
That model is correct, but as GPU programs become larger and more complex,
compiling and linking with full LTO is not scalable from a compilation and
development cost perspective.
ThinLTO can help recover parallelism by allowing backend compilation to happen
per translation unit with summary-guided optimization. However, it still
produces object files that need to be combined into a final device image. For
GPU code, that final combination still needs object linking to compute the
per-kernel metadata and resource-symbol values that depend on the resolved
cross-object program.
Information available at the final device link
The finalization described here needs two kinds of information. The first is
ordinary object-link state. For an ELF link, this includes:
- which definitions prevailed after symbol resolution;
- which archive members were selected;
- how aliases and weak definitions resolve; and
- which relocations are present in the selected input objects, including ones
against target-defined resource symbols.
The second is GPU-specific. Once the ordinary link state is fixed, the
finalization step can gather the per-function resource summaries computed by
the compiler backend. Since a kernel function is the entry point to a GPU
program, finalization can compute each kernel’s resource requirements by
analyzing the resolved resource graph rooted at that kernel:
- which device functions are reachable from each kernel;
- which indirect-call targets need to be conservatively considered;
- which compiler-managed resources are reachable from each kernel; and
- which resource-symbol values and metadata fields need final values derived
from that resolved graph.
Any implementation of this finalization needs access to the resolved device-link
state or an equivalent representation of it. For the AMDGPU implementation
proposed here, LLD is already the component that sees all input AMDGPU ELF
objects, performs symbol resolution, selects archive members, applies
relocations, and writes the final AMDGPU ELF output. We therefore propose to run
AMDGPU device object finalization as part of that link.
What the AMDGPU finalization step does
For AMDGPU, we propose to use .amdgpu.info as the object-file contract for
carrying the information needed by the link step. The AMDGPU backend emits
compact per-function summaries that describe local resource usage, call edges,
ABI-relevant properties, indirect-call information, and uses of
compiler-managed resources such as LDS objects and named barriers. The AMDGPU
ELF link step can then combine these summaries with the resolved symbol table
and produce final per-kernel metadata.
The current AMDGPU usage documentation
already describes the relevant ABI pieces, including .amdgpu.info. As part of
this work, we plan to reorganize the ABI-related text into a dedicated section
and expand it so that the object-file contract is easier to review without
already being deeply familiar with AMDGPU internals. The expanded documentation
would cover the summary format, SHN_AMDGPU_LDS, named-barrier symbols,
resource-symbol semantics, and the rules used by the device link finalization
step.
With that contract in place, the AMDGPU device-link finalization step consumes
those summaries and the result of ordinary ELF resolution. It then:
- builds a resolved device-link resource graph from the selected input objects;
- resolves direct calls, aliases, and indirect-call edges conservatively;
- validates cross-translation-unit ABI compatibility where required;
- propagates register, scratch, LDS, named-barrier, and other resource
requirements through the graph; - computes per-kernel reachability for compiler-managed resources;
- assigns concrete offsets or IDs for resources such as LDS objects and named
barriers; and - updates AMDGPU kernel descriptors and HSA metadata with the final values.
One distinction worth making is that this is not a generic optimization call
graph. It is a target-specific device-link resource graph. Its nodes and
edges are tied to AMDGPU ABI rules, resource summaries, conservative handling
of indirect-call targets, and compiler-managed resource symbols. Existing LLVM
call-graph encodings or algorithms may still be reusable where their semantics
fit. We propose to evaluate those pieces independently while keeping
AMDGPU-specific resource state and ABI behavior in the target-owned
finalization component.
For the first implementation, we propose to keep this graph construction
target-specific and close to the AMDGPU finalization logic. This does not rule
out future factoring. If another GPU target needs a similar device-link
finalization model, we would like to revisit the boundary and extract common
pieces based on multiple real users rather than guessing the abstraction from one
target.
The finalization step does not rerun instruction selection, register allocation,
or other backend code generation passes. It computes and writes final metadata
and resource-symbol values that are only knowable once the device objects have
been resolved together. Some of those values, such as LDS offsets or
named-barrier IDs, are naturally tied to symbol resolution and relocation.
Others, such as propagated resource counts in the kernel descriptor, are part of
the final code object contract consumed at dispatch time.
Proposed LLD integration point and data flow
The proposed hook has a specific position in the lld/ELF pipeline:
- all object files, including LTO-produced objects, have been added; archive
extraction and ordinary input-symbol resolution are complete; - target information and merged ELF flags are available, and section garbage
collection has run; and - output-section assignment, linker-script section discarding, ICF, and
ordinary output-relocation scanning and application have not yet run.
This position gives finalization the prevailing definitions, selected objects,
resolved aliases, GC-live input sections, and input relocations. It is still
early enough to assign values to LDS and named-barrier pseudo-symbols before
ordinary relocation processing, and to update or resize AMDGPU metadata before
output layout.
The AMDGPU-specific finalization path reads:
- the selected AMDGPU objects and the contents and relocations of
.amdgpu.info,.amdgpu.strtab, kernel descriptors, and HSA metadata; - the mapping from input symbols to prevailing definitions, including aliases,
weak definitions, and section-symbol references; - live-section information and the selected target’s merged ELF flags; and
- a diagnostic interface.
Its permitted side effects are limited to:
- concrete values and required final ELF attributes for LDS and named-barrier
pseudo-symbols; - updated AMDGPU kernel-descriptor and HSA-metadata contents; and
- validation failures for malformed or ABI-incompatible inputs.
It would not add input files, change which definitions prevail, or replace or
modify generic symbol resolution, archive extraction, section assignment, or
relocation processing. The relationship with ICF, which currently runs after
this proposed hook, is one ordering detail on which we would particularly
welcome feedback.
Code placement and dependency boundary
The proposed initial design follows the current implementation. It places the
.amdgpu.info parser, resource graph, and orchestration in the
AMDGPU-specific lld/ELF/AMDGPUObjectLinking.cpp path, where they directly
consume LLD’s resolved symbols, selected input objects, sections, and
relocations. Direct access avoids constructing and maintaining a second
representation of link state solely for finalization.
The surrounding LLD integration records the AMDGPU ABI section indices,
preserves SHN_AMDGPU_LDS pseudo-symbols for finalization, supports the
required AMDGPU relocation, and invokes one target-gated entry point.
Hardware-dependent resource encoding and kernel-descriptor and HSA-metadata
patching use AMDGPU-owned support APIs based on documented ABI structures and
TargetParser; they do not require AMDGPU code generation or
MCSubtargetInfo.
ABI evolution, ownership, and expected maintenance
The object-file contract is intended to make the expected location of future
changes explicit:
- hardware-generation resource limits and descriptor encodings should change
only in AMDGPU-specific finalization or support code and its
target-information tables; - new per-function summary records should require corresponding AMDGPU ABI
documentation, backend emission, and finalization support, but no generic LLD
change when the linker-facing inputs are unchanged; - new ELF-visible semantics, such as a pseudo-symbol or relocation kind, may
require a focused AMDGPU LLD target change; and - changes to the generic hook or to ordinary ELF invariants would require LLD
review.
The .amdgpu.info encoding is tagged and length-prefixed so that optional
records can be extended without changing generic LLD code. Records required for
correctness still need explicit compatibility and validation rules; they must
not be silently ignored by a consumer that cannot implement their semantics.
AMDGPU maintainers would own the target-specific finalization code, support
helpers, ABI documentation, and tests. LLD maintainers would retain ownership
of the generic integration points and linker invariants around them.
The implementation will include focused tests for ABI decoding and validation,
resource-graph algorithms, and end-to-end ELF finalization.
Preliminary results
The initial implementation shows the expected build-time improvement over full
LTO. For one GPU target, RCCL builds take roughly 30 minutes with full LTO and
roughly 5 minutes with object linking. In the LLVM libc GPU test suite,
compile-and-link time drops from about 145 seconds to 44.5 seconds, with the
relevant tests passing.
Feedback requested
We appreciate feedback on the proposed architecture and integration boundary,
especially any correctness, ABI, layering, ownership, or maintainability
concerns not addressed above.
For implementation context, an initial AMDGPU LLD patch is available at
[RFC][AMDGPU][lld] Link-time finalization for separately compiled AMDGPU device code by shiltian · Pull Request #206787 · llvm/llvm-project · GitHub. This RFC is intended to
discuss the design and integration model rather than the details of that patch.
@arsenm @bader @Artem-B @MaskRay @jrtc27 @jhuber6
This RFC was drafted by me but refined (many times) by AI.