[llvm] [AMDGPU] Introduce ABI occupancy for object linking (PR #199475)
Shilei Tian via llvm-commits
llvm-commits at lists.llvm.org
Sun Sep 6 19:26:14 PDT 2026
https://github.com/shiltian updated https://github.com/llvm/llvm-project/pull/199475
>From ede6cdde87d6ae6503bc95f457396f20ea418f07 Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Sun, 24 May 2026 22:12:08 -0400
Subject: [PATCH] [AMDGPU] Introduce ABI occupancy for object linking
This PR introduces ABI occupancy as the contract used to compile functions under
object linking. The default is the occupancy needed for a 1024 workitem
workgroup. It can be overridden for a module by the `amdgpu_abi_waves_per_eu`
module flag, replaced for a function by `amdgpu-flat-work-group-size`, or
tightened by an accepted `amdgpu-waves-per-eu` hint.
The ABI occupancy replaces the workgroup-derived minimum that an
`amdgpu-waves-per-eu` hint is validated against, so the hint can only make the
contract stricter.
The backend emits the selected occupancy in `.amdgpu.info` and uses it to
enforce the object linking register budget.
---
llvm/docs/AMDGPUUsage.rst | 51 +++++++++++-
.../llvm/Support/AMDGPUObjLinkingInfo.h | 3 +
llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp | 80 ++++++++++++++-----
llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp | 72 +++++++++--------
llvm/lib/Target/AMDGPU/AMDGPUSubtarget.h | 9 ---
.../AMDGPU/AsmParser/AMDGPUAsmParser.cpp | 6 ++
.../MCTargetDesc/AMDGPUTargetStreamer.cpp | 2 +
.../MCTargetDesc/AMDGPUTargetStreamer.h | 1 +
.../Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp | 8 ++
llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h | 3 +
.../object-linking-abi-occupancy-agpr.ll | 31 +++++++
...king-abi-occupancy-flat-work-group-size.ll | 54 +++++++++++++
...bject-linking-abi-occupancy-module-flag.ll | 62 ++++++++++++++
...ject-linking-abi-occupancy-waves-per-eu.ll | 49 ++++++++++++
.../AMDGPU/object-linking-abi-occupancy.ll | 48 +++++++++++
llvm/test/MC/AMDGPU/amdgpu-info-roundtrip.s | 6 ++
16 files changed, 420 insertions(+), 65 deletions(-)
create mode 100644 llvm/test/CodeGen/AMDGPU/object-linking-abi-occupancy-agpr.ll
create mode 100644 llvm/test/CodeGen/AMDGPU/object-linking-abi-occupancy-flat-work-group-size.ll
create mode 100644 llvm/test/CodeGen/AMDGPU/object-linking-abi-occupancy-module-flag.ll
create mode 100644 llvm/test/CodeGen/AMDGPU/object-linking-abi-occupancy-waves-per-eu.ll
create mode 100644 llvm/test/CodeGen/AMDGPU/object-linking-abi-occupancy.ll
diff --git a/llvm/docs/AMDGPUUsage.rst b/llvm/docs/AMDGPUUsage.rst
index d09216f8fdc20..1166658d6c1c7 100644
--- a/llvm/docs/AMDGPUUsage.rst
+++ b/llvm/docs/AMDGPUUsage.rst
@@ -2530,8 +2530,10 @@ The AMDGPU backend supports the following LLVM IR attributes.
CLANG attribute [CLANG-ATTR]_. This is an optimization hint,
and the backend may not be able to satisfy the request. If
the specified range is incompatible with the function's
- "amdgpu-flat-work-group-size" value, the implied occupancy
- bounds by the workgroup size takes precedence.
+ "amdgpu-flat-work-group-size" value, the occupancy bounds
+ implied by the workgroup size take precedence. Under object
+ linking, this hint cannot lower the ABI occupancy budget;
+ see :ref:`amdgpu-abi-occupancy`.
"amdgpu-ieee" true/false. GFX6-GFX11 (Except GFX11.7) Only
Specify whether the function expects the IEEE field of the
@@ -2943,6 +2945,48 @@ unit's worst case (i.e, maxima) ``num_vgpr``, ``num_agpr``, and
symbolic expressions. These three symbols are ``amdgcn.max_num_vgpr``,
``amdgcn.max_num_agpr``, and ``amdgcn.max_num_sgpr``.
+.. _amdgpu-abi-occupancy:
+
+ABI Occupancy (Object Linking)
+------------------------------
+
+When object linking is enabled, the AMDGPU backend no longer assumes
+whole-program visibility. Individual translation units must agree on an ABI that
+callers and callees can rely on without seeing each other's register usage. The
+compiler enforces this by using an *ABI occupancy* as a resource-budget floor
+for separately compiled functions. The default ABI occupancy is the number of
+waves per EU required to support a 1024-workitem workgroup:
+
+``ceil(ceil(1024 / wavefront-size) / workgroup-SIMDs)``, where
+``workgroup-SIMDs`` is the number of SIMDs that a workgroup's waves run on.
+
+For example, on ``gfx900`` (wave64, 4 workgroup SIMDs) the default ABI occupancy
+is ``ceil(ceil(1024 / 64) / 4) = 4``, yielding a VGPR budget of
+``getMaxNumVGPRs(4) = 64``. On a gfx12 target such as ``gfx1200`` (wave32,
+4 workgroup SIMDs), the default ABI occupancy is ``ceil(ceil(1024 / 32) / 4) =
+8``, yielding a VGPR budget of ``getMaxNumVGPRs(8) = 192``.
+
+The ``amdgpu_abi_waves_per_eu`` module flag overrides the default ABI occupancy
+floor for a module. A value lower than the default accepts a looser ABI
+contract; a value higher than the default requests a stricter one. Any compiler
+driver option that exposes this control should set the module flag in generated
+IR rather than passing the value directly to the backend.
+
+``amdgpu-flat-work-group-size`` is ABI-significant. If a function carries this
+attribute, the occupancy implied by its maximum flat workgroup size replaces
+the default ABI occupancy for that function, so it can either raise or lower the
+register budget. The module flag does not apply to such a function.
+
+``amdgpu-waves-per-eu`` remains a hint, and it does not define the ABI
+occupancy. The ABI occupancy replaces the workgroup-derived minimum that the
+hint is validated against, so a hint that asks for fewer waves than the ABI
+occupancy is rejected and leaves the budget unchanged. A hint that asks for more
+waves is accepted and makes the register budget stricter.
+
+The backend records the resulting register-budget occupancy in each function's
+``.amdgpu.info`` metadata so link-time object linking can reject incompatible
+occupancy contracts across translation units.
+
.. _amdgpu-elf-code-object:
ELF Code Object
@@ -3412,6 +3456,7 @@ if needed.
8 ``INFO_CALL`` 8B symbol ref; direct call edge
9 ``INFO_INDIRECT_CALL`` u32 strtab offset; indirect call type-ID
10 ``INFO_TYPEID`` u32 strtab offset; function type-ID
+ 11 ``INFO_OCCUPANCY`` u32; occupancy used to compile the function
===== ============================== ==========================================
.. table:: AMDGPU Info Function Flags (``INFO_FLAGS``)
@@ -21871,6 +21916,7 @@ The following sub-directives may appear inside the block:
``.amdgpu_num_vgpr`` *value* Architectural VGPRs used (u32)
``.amdgpu_num_agpr`` *value* Accumulator VGPRs used (u32)
``.amdgpu_private_segment_size`` *n* Private segment size in bytes (u32)
+ ``.amdgpu_occupancy`` *value* Occupancy used to compile the function (u32)
``.amdgpu_use`` *symbol* Resource dependency (LDS or barrier)
``.amdgpu_call`` *symbol* Direct call edge to *symbol*
``.amdgpu_indirect_call`` *"type-id"* Indirect call with given type-ID string
@@ -21887,6 +21933,7 @@ Example:
.amdgpu_num_vgpr 32
.amdgpu_num_agpr 0
.amdgpu_private_segment_size 0
+ .amdgpu_occupancy 4
.amdgpu_use lds_var
.amdgpu_call helper
.amdgpu_indirect_call "vi"
diff --git a/llvm/include/llvm/Support/AMDGPUObjLinkingInfo.h b/llvm/include/llvm/Support/AMDGPUObjLinkingInfo.h
index e65161e6545fc..ef71e836bfc40 100644
--- a/llvm/include/llvm/Support/AMDGPUObjLinkingInfo.h
+++ b/llvm/include/llvm/Support/AMDGPUObjLinkingInfo.h
@@ -58,6 +58,9 @@ enum class InfoKind : uint8_t {
/// string (at the given `.amdgpu.strtab` offset) so the linker can match
/// it against INFO_INDIRECT_CALL entries. [u32]
INFO_TYPEID = 10,
+ /// Occupancy used to compile the function.
+ /// [u32]
+ INFO_OCCUPANCY = 11,
};
/// Per-function flags packed into INFO_FLAGS entries.
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
index f754fd8bd24f3..21ed7f31f5ec6 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
@@ -468,7 +468,7 @@ const AMDGPUMCExpr *createOccupancy(unsigned InitOcc, const MCExpr *NumSGPRs,
}
void AMDGPUAsmPrinter::validateMCResourceInfo(Function &F) {
- if (F.isDeclaration() || !AMDGPU::isModuleEntryFunctionCC(F.getCallingConv()))
+ if (F.isDeclaration())
return;
using RIK = MCResourceInfo::ResourceInfoKind;
@@ -484,6 +484,44 @@ void AMDGPUAsmPrinter::validateMCResourceInfo(Function &F) {
return false;
};
+ // Register allocation normally respects the ABI register budget. Final
+ // resource accounting can still exceed it when the IR names fixed physical
+ // registers, e.g. inline asm clobbers. Check the resolved per-function
+ // resource symbols here so object-linking metadata is not emitted for a
+ // function that cannot satisfy its ABI occupancy.
+ if (AMDGPUTargetMachine::EnableObjectLinking) {
+ auto GetResolvedValue = [&](RIK Kind, uint64_t &Value) {
+ MCSymbol *Sym = RI.getSymbol(FnSym->getName(), Kind, OutContext);
+ return Sym->isVariable() &&
+ TryGetMCExprValue(Sym->getVariableValue(), Value);
+ };
+
+ auto CheckBudget = [&](uint64_t Used, unsigned Budget, const char *What) {
+ if (Used <= Budget)
+ return false;
+ F.getContext().diagnose(DiagnosticInfoResourceLimit(
+ F, What, Used, Budget, DS_Error, DK_ResourceLimit));
+ return true;
+ };
+
+ // Architected and accumulator VGPRs share a single budget, so they have to
+ // be checked together rather than one bank at a time.
+ uint64_t NumVgpr = 0, NumAgpr = 0, NumSgpr = 0;
+ if (GetResolvedValue(RIK::RIK_NumVGPR, NumVgpr) &&
+ GetResolvedValue(RIK::RIK_NumAGPR, NumAgpr) &&
+ CheckBudget(getTotalNumVGPRs(STM.hasGFX90AInsts(), NumAgpr, NumVgpr),
+ STM.getMaxNumVGPRs(F), "VGPRs under object-linking ABI"))
+ return;
+
+ if (GetResolvedValue(RIK::RIK_NumSGPR, NumSgpr) &&
+ CheckBudget(NumSgpr, STM.getMaxNumSGPRs(F),
+ "SGPRs under object-linking ABI"))
+ return;
+ }
+
+ if (!AMDGPU::isModuleEntryFunctionCC(F.getCallingConv()))
+ return;
+
const uint64_t MaxScratchPerWorkitem =
STM.getMaxWaveScratchSize() / STM.getWavefrontSize();
MCSymbol *ScratchSizeSymbol =
@@ -739,30 +777,25 @@ bool AMDGPUAsmPrinter::doFinalization(Module &M) {
// LDS/named-barrier use edges, indirect calls, and address-taken type IDs).
emitAMDGPUInfo(M);
- // Assign expressions which can only be resolved when all other functions are
- // known.
- RI.finalize(OutContext);
-
- // Switch section and emit all GPR maximums within the processed module.
- OutStreamer->pushSection();
- MCSectionELF *MaxGPRSection =
- OutContext.getELFSection(".AMDGPU.gpr_maximums", ELF::SHT_PROGBITS, 0);
- OutStreamer->switchSection(MaxGPRSection);
- getTargetStreamer()->EmitMCResourceMaximums(
- RI.getMaxVGPRSymbol(OutContext), RI.getMaxAGPRSymbol(OutContext),
- RI.getMaxSGPRSymbol(OutContext), RI.getMaxNamedBarrierSymbol(OutContext));
- OutStreamer->popSection();
-
- // In the object-linking pipeline per-function resource MCExprs reference
- // external callee symbols that cannot be evaluated here, so cross-TU limit
- // checks would silently no-op for every non-leaf function. Defer resource
- // sanity checking to the linker, which re-validates against the aggregated
- // call graph in the combined .amdgpu.info metadata.
+ // Finalize non-object-linking resource propagation and emit the
+ // `amdgpu.max_num_*` fallback symbols.
if (!AMDGPUTargetMachine::EnableObjectLinking) {
- for (Function &F : M.functions())
- validateMCResourceInfo(F);
+ RI.finalize(OutContext);
+
+ OutStreamer->pushSection();
+ MCSectionELF *MaxGPRSection =
+ OutContext.getELFSection(".AMDGPU.gpr_maximums", ELF::SHT_PROGBITS, 0);
+ OutStreamer->switchSection(MaxGPRSection);
+ getTargetStreamer()->EmitMCResourceMaximums(
+ RI.getMaxVGPRSymbol(OutContext), RI.getMaxAGPRSymbol(OutContext),
+ RI.getMaxSGPRSymbol(OutContext),
+ RI.getMaxNamedBarrierSymbol(OutContext));
+ OutStreamer->popSection();
}
+ for (Function &F : M.functions())
+ validateMCResourceInfo(F);
+
RI.reset();
return AsmPrinter::doFinalization(M);
@@ -924,11 +957,14 @@ bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
if (AMDGPUTargetMachine::EnableObjectLinking) {
const AMDGPUResourceUsageAnalysisWrapperPass::FunctionResourceInfo &RU =
*ResourceUsage;
+ uint32_t Occupancy = static_cast<uint32_t>(
+ MF.getInfo<SIMachineFunctionInfo>()->getMinWavesPerEU());
FunctionInfos.push_back(
{/*NumSGPR=*/static_cast<uint32_t>(RU.NumExplicitSGPR),
/*NumArchVGPR=*/static_cast<uint32_t>(RU.NumVGPR),
/*NumAccVGPR=*/static_cast<uint32_t>(RU.NumAGPR),
/*PrivateSegmentSize=*/static_cast<uint32_t>(RU.PrivateSegmentSize),
+ /*Occupancy=*/Occupancy,
/*UsesVCC=*/RU.UsesVCC,
/*UsesFlatScratch=*/RU.UsesFlatScratch,
/*HasDynStack=*/RU.HasDynamicallySizedStack,
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp b/llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp
index b9ba99cf8db19..f25c2a66d5b4d 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp
@@ -16,6 +16,7 @@
#include "AMDGPUInstructionSelector.h"
#include "AMDGPULegalizerInfo.h"
#include "AMDGPURegisterBankInfo.h"
+#include "AMDGPUTargetMachine.h"
#include "R600Subtarget.h"
#include "SIMachineFunctionInfo.h"
#include "Utils/AMDGPUBaseInfo.h"
@@ -177,32 +178,6 @@ std::pair<unsigned, unsigned> AMDGPUSubtarget::getFlatWorkGroupSizes(
return Requested;
}
-std::pair<unsigned, unsigned> AMDGPUSubtarget::getEffectiveWavesPerEU(
- std::pair<unsigned, unsigned> RequestedWavesPerEU,
- std::pair<unsigned, unsigned> FlatWorkGroupSizes, unsigned LDSBytes) const {
- // Default minimum/maximum number of waves per EU. The range of flat workgroup
- // sizes limits the achievable maximum, and we aim to support enough waves per
- // EU so that we can concurrently execute all waves of a single workgroup of
- // maximum size on a CU.
- std::pair<unsigned, unsigned> Default = {
- getWavesPerEUForWorkGroup(FlatWorkGroupSizes.second),
- getOccupancyWithWorkGroupSizes(LDSBytes, FlatWorkGroupSizes).second};
- Default.first = std::min(Default.first, Default.second);
-
- // Make sure requested minimum is within the default range and lower than the
- // requested maximum. The latter must not violate target specification.
- if (RequestedWavesPerEU.first < Default.first ||
- RequestedWavesPerEU.first > Default.second ||
- RequestedWavesPerEU.first > RequestedWavesPerEU.second ||
- RequestedWavesPerEU.second > getMaxWavesPerEU())
- return Default;
-
- // We cannot exceed maximum occupancy implied by flat workgroup size and LDS.
- RequestedWavesPerEU.second =
- std::min(RequestedWavesPerEU.second, Default.second);
- return RequestedWavesPerEU;
-}
-
std::pair<unsigned, unsigned>
AMDGPUSubtarget::getWavesPerEU(const Function &F) const {
// Default/requested minimum/maximum flat work group sizes.
@@ -218,13 +193,46 @@ AMDGPUSubtarget::getWavesPerEU(const Function &F) const {
std::pair<unsigned, unsigned>
AMDGPUSubtarget::getWavesPerEU(std::pair<unsigned, unsigned> FlatWorkGroupSizes,
unsigned LDSBytes, const Function &F) const {
- // Default minimum/maximum number of waves per execution unit.
- std::pair<unsigned, unsigned> Default(1, getMaxWavesPerEU());
-
// Requested minimum/maximum number of waves per execution unit.
- std::pair<unsigned, unsigned> Requested =
- AMDGPU::getIntegerPairAttribute(F, "amdgpu-waves-per-eu", Default, true);
- return getEffectiveWavesPerEU(Requested, FlatWorkGroupSizes, LDSBytes);
+ std::pair<unsigned, unsigned> Requested = AMDGPU::getIntegerPairAttribute(
+ F, "amdgpu-waves-per-eu", {1, getMaxWavesPerEU()},
+ /*OnlyFirstRequired=*/true);
+
+ // Default minimum/maximum number of waves per EU. The range of flat workgroup
+ // sizes limits the achievable maximum, and we aim to support enough waves per
+ // EU so that we can concurrently execute all waves of a single workgroup of
+ // maximum size on a CU.
+ std::pair<unsigned, unsigned> Default = {
+ getWavesPerEUForWorkGroup(FlatWorkGroupSizes.second),
+ getOccupancyWithWorkGroupSizes(LDSBytes, FlatWorkGroupSizes).second};
+
+ // Under object linking the minimum is the ABI occupancy instead, the value
+ // that separately compiled callers and callees agree on. Only the module-wide
+ // override has to be handled here: without an override the ABI occupancy is
+ // the occupancy the maximum flat workgroup size implies, which is the default
+ // above. An explicit "amdgpu-flat-work-group-size" is ABI-significant, so it
+ // wins over the override.
+ if (AMDGPUTargetMachine::EnableObjectLinking &&
+ !F.hasFnAttribute("amdgpu-flat-work-group-size")) {
+ if (unsigned Override = AMDGPU::getAMDGPUABIWavesPerEU(*F.getParent())) {
+ Default.first =
+ std::clamp(Override, getMinWavesPerEU(), getMaxWavesPerEU());
+ }
+ }
+ Default.first = std::min(Default.first, Default.second);
+
+ // Make sure requested minimum is within the default range and lower than the
+ // requested maximum. The latter must not violate target specification. Under
+ // object linking this is what keeps an "amdgpu-waves-per-eu" hint from
+ // lowering the ABI occupancy: a hint below it is rejected.
+ if (Requested.first < Default.first || Requested.first > Default.second ||
+ Requested.first > Requested.second ||
+ Requested.second > getMaxWavesPerEU())
+ return Default;
+
+ // We cannot exceed maximum occupancy implied by flat workgroup size and LDS.
+ Requested.second = std::min(Requested.second, Default.second);
+ return Requested;
}
std::optional<unsigned>
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUSubtarget.h b/llvm/lib/Target/AMDGPU/AMDGPUSubtarget.h
index e1f331229c463..4f19290fec290 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUSubtarget.h
+++ b/llvm/lib/Target/AMDGPU/AMDGPUSubtarget.h
@@ -121,15 +121,6 @@ class AMDGPUSubtarget {
getWavesPerEU(std::pair<unsigned, unsigned> FlatWorkGroupSizes,
unsigned LDSBytes, const Function &F) const;
- /// Returns the target minimum/maximum number of waves per EU. This is based
- /// on the minimum/maximum number of \p RequestedWavesPerEU and further
- /// limited by the maximum achievable occupancy derived from the range of \p
- /// FlatWorkGroupSizes and number of \p LDSBytes per workgroup.
- std::pair<unsigned, unsigned>
- getEffectiveWavesPerEU(std::pair<unsigned, unsigned> RequestedWavesPerEU,
- std::pair<unsigned, unsigned> FlatWorkGroupSizes,
- unsigned LDSBytes) const;
-
/// Return the amount of LDS that can be used that will not restrict the
/// occupancy lower than WaveCount.
unsigned getMaxLocalMemSizeWithWaveCount(unsigned WaveCount,
diff --git a/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp b/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
index 46284628ae0c7..5fb36ab7497f7 100644
--- a/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
+++ b/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
@@ -7008,6 +7008,12 @@ bool AMDGPUAsmParser::ParseDirectiveAMDGPUInfo() {
return true;
FI.PrivateSegmentSize = static_cast<uint32_t>(Val);
HasScalarAttrs = true;
+ } else if (Dir == "occupancy") {
+ int64_t Val;
+ if (getParser().parseAbsoluteExpression(Val))
+ return true;
+ FI.Occupancy = static_cast<uint32_t>(Val);
+ HasScalarAttrs = true;
} else if (Dir == "use") {
StringRef ResName;
if (getParser().parseIdentifier(ResName))
diff --git a/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.cpp b/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.cpp
index be162919c46ce..4f38358daca0d 100644
--- a/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.cpp
+++ b/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.cpp
@@ -773,6 +773,7 @@ void AMDGPUTargetAsmStreamer::emitAMDGPUInfo(
OS << "\t\t.amdgpu_num_agpr " << Info->NumAccVGPR << '\n';
OS << "\t\t.amdgpu_private_segment_size " << Info->PrivateSegmentSize
<< '\n';
+ OS << "\t\t.amdgpu_occupancy " << Info->Occupancy << '\n';
}
for (MCSymbol *Res : Uses)
OS << "\t\t.amdgpu_use " << Res->getName() << '\n';
@@ -1247,6 +1248,7 @@ void AMDGPUTargetELFStreamer::emitAMDGPUInfo(
EmitU32Entry(AMDGPU::InfoKind::INFO_NUM_AGPR, Info->NumAccVGPR);
EmitU32Entry(AMDGPU::InfoKind::INFO_PRIVATE_SEGMENT_SIZE,
Info->PrivateSegmentSize);
+ EmitU32Entry(AMDGPU::InfoKind::INFO_OCCUPANCY, Info->Occupancy);
}
for (MCSymbol *Res : Uses)
diff --git a/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.h b/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.h
index d1ef710e9b180..92d2fbd6db068 100644
--- a/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.h
+++ b/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.h
@@ -35,6 +35,7 @@ struct FuncInfo {
uint32_t NumArchVGPR = 0;
uint32_t NumAccVGPR = 0;
uint32_t PrivateSegmentSize = 0;
+ uint32_t Occupancy = 0;
bool UsesVCC = false;
bool UsesFlatScratch = false;
bool HasDynStack = false;
diff --git a/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp b/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp
index 340d569edb199..eb1ea62f61686 100644
--- a/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp
@@ -219,6 +219,14 @@ unsigned getAMDHSACodeObjectVersion(const Module &M) {
return getDefaultAMDHSACodeObjectVersion();
}
+unsigned getAMDGPUABIWavesPerEU(const Module &M) {
+ if (auto *Val = mdconst::extract_or_null<ConstantInt>(
+ M.getModuleFlag("amdgpu_abi_waves_per_eu")))
+ return Val->getZExtValue();
+
+ return 0;
+}
+
unsigned getDefaultAMDHSACodeObjectVersion() {
return DefaultAMDHSACodeObjectVersion;
}
diff --git a/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h b/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h
index 089b195f11097..dcba7e8818373 100644
--- a/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h
+++ b/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h
@@ -77,6 +77,9 @@ bool isHsaAbi(const MCSubtargetInfo &STI);
/// \returns Code object version from the IR module flag.
unsigned getAMDHSACodeObjectVersion(const Module &M);
+/// \returns ABI occupancy override in waves per EU from the IR module flag.
+unsigned getAMDGPUABIWavesPerEU(const Module &M);
+
/// \returns Code object version from ELF's e_ident[EI_ABIVERSION].
unsigned getAMDHSACodeObjectVersion(unsigned ABIVersion);
diff --git a/llvm/test/CodeGen/AMDGPU/object-linking-abi-occupancy-agpr.ll b/llvm/test/CodeGen/AMDGPU/object-linking-abi-occupancy-agpr.ll
new file mode 100644
index 0000000000000..d0c8958ce193b
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/object-linking-abi-occupancy-agpr.ll
@@ -0,0 +1,31 @@
+; RUN: split-file %s %t
+; RUN: llc -mtriple=amdgpu9.0a-amd-amdhsa -amdgpu-enable-object-linking < %t/within.ll | FileCheck -check-prefix=WITHIN %s
+; RUN: not llc -mtriple=amdgpu9.0a-amd-amdhsa -amdgpu-enable-object-linking -filetype=null < %t/exceeds.ll 2>&1 | FileCheck -check-prefix=EXCEEDS %s
+
+; gfx90a has a unified vector register file, so architected and accumulator
+; VGPRs share one ABI register budget. The default ABI occupancy of 4 waves/EU
+; gives a 128 register budget, and the check must apply to the combined count
+; rather than to each bank on its own.
+
+; 61 architected VGPRs round up to 64, plus 61 AGPRs, is 125 registers.
+; WITHIN-LABEL: {{^}}combined_within_budget:
+; WITHIN: .set .Lcombined_within_budget.num_vgpr, 61
+; WITHIN-NEXT: .set .Lcombined_within_budget.num_agpr, 61
+
+; 121 architected VGPRs round up to 124, plus 121 AGPRs, is 245 registers.
+; Neither bank exceeds 128 on its own.
+; EXCEEDS: error: {{.*}}VGPRs under object-linking ABI (245) exceeds limit (128) in function 'combined_exceeds_budget'
+
+;--- within.ll
+define void @combined_within_budget(ptr addrspace(1) %p) {
+ call void asm sideeffect "; clobber", "~{v60},~{a60}"()
+ store i32 0, ptr addrspace(1) %p
+ ret void
+}
+
+;--- exceeds.ll
+define void @combined_exceeds_budget(ptr addrspace(1) %p) {
+ call void asm sideeffect "; clobber", "~{v120},~{a120}"()
+ store i32 0, ptr addrspace(1) %p
+ ret void
+}
diff --git a/llvm/test/CodeGen/AMDGPU/object-linking-abi-occupancy-flat-work-group-size.ll b/llvm/test/CodeGen/AMDGPU/object-linking-abi-occupancy-flat-work-group-size.ll
new file mode 100644
index 0000000000000..dea587df04f20
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/object-linking-abi-occupancy-flat-work-group-size.ll
@@ -0,0 +1,54 @@
+; RUN: split-file %s %t
+; RUN: llc -mtriple=amdgpu9.00-amd-amdhsa -mattr=+wavefrontsize64 -amdgpu-enable-object-linking < %t/lower.ll | FileCheck --check-prefixes=LOWER,LOWER-W64 %s
+; RUN: llc -mtriple=amdgpu10.10-amd-amdhsa -mattr=+wavefrontsize32 -amdgpu-enable-object-linking < %t/lower.ll | FileCheck --check-prefixes=LOWER,LOWER-W32 %s
+; RUN: llc -mtriple=amdgpu10.10-amd-amdhsa -mattr=+wavefrontsize64 -amdgpu-enable-object-linking < %t/lower.ll | FileCheck --check-prefixes=LOWER,LOWER-W64 %s
+; RUN: llc -mtriple=amdgpu11.00-amd-amdhsa -mattr=+wavefrontsize32 -amdgpu-enable-object-linking < %t/lower.ll | FileCheck --check-prefixes=LOWER,LOWER-W32 %s
+; RUN: llc -mtriple=amdgpu11.00-amd-amdhsa -mattr=+wavefrontsize64 -amdgpu-enable-object-linking < %t/lower.ll | FileCheck --check-prefixes=LOWER,LOWER-W64 %s
+; RUN: llc -mtriple=amdgpu12.00-amd-amdhsa -mattr=+wavefrontsize32 -amdgpu-enable-object-linking < %t/lower.ll | FileCheck --check-prefixes=LOWER,LOWER-W32 %s
+; RUN: llc -mtriple=amdgpu12.00-amd-amdhsa -mattr=+wavefrontsize64 -amdgpu-enable-object-linking < %t/lower.ll | FileCheck --check-prefixes=LOWER,LOWER-W64 %s
+; RUN: not llc -mtriple=amdgpu9.00-amd-amdhsa -mattr=+wavefrontsize64 -amdgpu-enable-object-linking -filetype=null < %t/wins.ll 2>&1 | FileCheck --check-prefix=GFX9 %s
+; RUN: not llc -mtriple=amdgpu10.10-amd-amdhsa -mattr=+wavefrontsize32 -amdgpu-enable-object-linking -filetype=null < %t/wins.ll 2>&1 | FileCheck --check-prefix=GFX10 %s
+; RUN: not llc -mtriple=amdgpu10.10-amd-amdhsa -mattr=+wavefrontsize64 -amdgpu-enable-object-linking -filetype=null < %t/wins.ll 2>&1 | FileCheck --check-prefix=GFX10 %s
+; RUN: not llc -mtriple=amdgpu11.00-amd-amdhsa -mattr=+wavefrontsize32 -amdgpu-enable-object-linking -filetype=null < %t/wins.ll 2>&1 | FileCheck --check-prefix=GFX11 %s
+; RUN: not llc -mtriple=amdgpu11.00-amd-amdhsa -mattr=+wavefrontsize64 -amdgpu-enable-object-linking -filetype=null < %t/wins.ll 2>&1 | FileCheck --check-prefix=GFX11 %s
+; RUN: not llc -mtriple=amdgpu12.00-amd-amdhsa -mattr=+wavefrontsize32 -amdgpu-enable-object-linking -filetype=null < %t/wins.ll 2>&1 | FileCheck --check-prefix=GFX12 %s
+; RUN: not llc -mtriple=amdgpu12.00-amd-amdhsa -mattr=+wavefrontsize64 -amdgpu-enable-object-linking -filetype=null < %t/wins.ll 2>&1 | FileCheck --check-prefix=GFX12 %s
+
+; amdgpu-flat-work-group-size is ABI-significant, so it defines the ABI
+; occupancy on its own.
+
+; A flat workgroup size of 512 needs fewer waves than the default 1024, so it
+; lowers the ABI occupancy.
+; LOWER-LABEL: {{^}}fixed_vgpr_flat_lower:
+; LOWER: .set .Lfixed_vgpr_flat_lower.num_vgpr, 71
+; LOWER-W64: .amdgpu_occupancy 2
+; LOWER-W32: .amdgpu_occupancy 4
+
+; The attribute also takes precedence over the amdgpu_abi_waves_per_eu module
+; flag. Even though the flag asks for 2 waves/EU, a flat workgroup size of 1024
+; requires the per-target occupancy that a 1024-workitem workgroup implies.
+; GFX9: error: {{.*}}VGPRs under object-linking ABI (193) exceeds limit (64) in function 'fixed_vgpr_flat'
+; GFX10: error: {{.*}}VGPRs under object-linking ABI (193) exceeds limit (128) in function 'fixed_vgpr_flat'
+; GFX11: error: {{.*}}VGPRs under object-linking ABI (193) exceeds limit (192) in function 'fixed_vgpr_flat'
+; GFX12: error: {{.*}}VGPRs under object-linking ABI (193) exceeds limit (192) in function 'fixed_vgpr_flat'
+
+;--- lower.ll
+define amdgpu_kernel void @fixed_vgpr_flat_lower(ptr addrspace(1) %p) #0 {
+ call void asm sideeffect "; clobber", "~{v70}"()
+ store i32 0, ptr addrspace(1) %p
+ ret void
+}
+
+attributes #0 = { "amdgpu-flat-work-group-size"="1,512" }
+
+;--- wins.ll
+define amdgpu_kernel void @fixed_vgpr_flat(ptr addrspace(1) %p) #0 {
+ call void asm sideeffect "; clobber", "~{v192}"()
+ store i32 0, ptr addrspace(1) %p
+ ret void
+}
+
+attributes #0 = { "amdgpu-flat-work-group-size"="1,1024" }
+
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"amdgpu_abi_waves_per_eu", i32 2}
diff --git a/llvm/test/CodeGen/AMDGPU/object-linking-abi-occupancy-module-flag.ll b/llvm/test/CodeGen/AMDGPU/object-linking-abi-occupancy-module-flag.ll
new file mode 100644
index 0000000000000..73bc796aa21b4
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/object-linking-abi-occupancy-module-flag.ll
@@ -0,0 +1,62 @@
+; RUN: split-file %s %t
+; RUN: not llc -mtriple=amdgpu9.00-amd-amdhsa -amdgpu-enable-object-linking -filetype=null < %t/default.ll 2>&1 | FileCheck -check-prefix=DEFAULT %s
+; RUN: not llc -mtriple=amdgpu9.00-amd-amdhsa -amdgpu-enable-object-linking -filetype=null < %t/zero.ll 2>&1 | FileCheck -check-prefix=DEFAULT %s
+; RUN: llc -mtriple=amdgpu9.00-amd-amdhsa -amdgpu-enable-object-linking < %t/override.ll | FileCheck -check-prefix=OVERRIDE %s
+; RUN: not llc -mtriple=amdgpu9.00-amd-amdhsa -amdgpu-enable-object-linking -filetype=null < %t/too-high.ll 2>&1 | FileCheck -check-prefix=HIGH %s
+
+; The amdgpu_abi_waves_per_eu module flag replaces the default ABI occupancy.
+; Out-of-range values must not escape the range of occupancies that the
+; subtarget supports.
+;
+; All four modules hold the same kernel. Only the module flag differs.
+
+; The default ABI occupancy on gfx900 is 4 waves/EU, which gives a 64 VGPR
+; budget. A flag value of 0 means "no override" and keeps that default, so the
+; two modules must report the same limit.
+; DEFAULT: error: {{.*}}VGPRs under object-linking ABI (71) exceeds limit (64) in function 'fixed_vgpr'
+
+; An override of 2 waves/EU raises the budget to 128 VGPRs.
+; OVERRIDE-LABEL: {{^}}fixed_vgpr:
+; OVERRIDE: .set .Lfixed_vgpr.num_vgpr, 71
+; OVERRIDE: .amdgpu_occupancy 2
+
+; A value above the maximum occupancy is clamped to it. gfx900 supports at most
+; 10 waves/EU, which gives a 24 VGPR budget.
+; HIGH: error: {{.*}}VGPRs under object-linking ABI (71) exceeds limit (24) in function 'fixed_vgpr'
+
+;--- default.ll
+define amdgpu_kernel void @fixed_vgpr(ptr addrspace(1) %p) {
+ call void asm sideeffect "; clobber", "~{v70}"()
+ store i32 0, ptr addrspace(1) %p
+ ret void
+}
+
+;--- zero.ll
+define amdgpu_kernel void @fixed_vgpr(ptr addrspace(1) %p) {
+ call void asm sideeffect "; clobber", "~{v70}"()
+ store i32 0, ptr addrspace(1) %p
+ ret void
+}
+
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"amdgpu_abi_waves_per_eu", i32 0}
+
+;--- override.ll
+define amdgpu_kernel void @fixed_vgpr(ptr addrspace(1) %p) {
+ call void asm sideeffect "; clobber", "~{v70}"()
+ store i32 0, ptr addrspace(1) %p
+ ret void
+}
+
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"amdgpu_abi_waves_per_eu", i32 2}
+
+;--- too-high.ll
+define amdgpu_kernel void @fixed_vgpr(ptr addrspace(1) %p) {
+ call void asm sideeffect "; clobber", "~{v70}"()
+ store i32 0, ptr addrspace(1) %p
+ ret void
+}
+
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"amdgpu_abi_waves_per_eu", i32 999}
diff --git a/llvm/test/CodeGen/AMDGPU/object-linking-abi-occupancy-waves-per-eu.ll b/llvm/test/CodeGen/AMDGPU/object-linking-abi-occupancy-waves-per-eu.ll
new file mode 100644
index 0000000000000..4e06a46c599cc
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/object-linking-abi-occupancy-waves-per-eu.ll
@@ -0,0 +1,49 @@
+; RUN: split-file %s %t
+; RUN: llc -mtriple=amdgpu9.00-amd-amdhsa -amdgpu-enable-object-linking < %t/below.ll | FileCheck -check-prefix=BELOW %s
+; RUN: not llc -mtriple=amdgpu9.00-amd-amdhsa -amdgpu-enable-object-linking -filetype=null < %t/above.ll 2>&1 | FileCheck -check-prefix=ABOVE %s
+
+; amdgpu-waves-per-eu is a hint and never defines the ABI occupancy. The ABI
+; occupancy replaces the workgroup-derived minimum that the hint is validated
+; against, so a hint below the ABI occupancy is rejected and leaves the budget
+; alone, while a hint above it is accepted and tightens the budget.
+;
+; Both modules override the ABI occupancy to 2 waves/EU, which gives a
+; 128 VGPR budget on gfx900. Both kernels use 71 VGPRs.
+
+; The "1,1" hint asks for fewer waves than the ABI occupancy, so it is dropped
+; and the 128 VGPR budget still applies. The rejected hint is not turned into an
+; exact occupancy request either, so the kernel descriptor keeps the
+; resource-derived occupancy.
+; BELOW-LABEL: {{^}}hint_below_abi:
+; BELOW: .set .Lhint_below_abi.num_vgpr, 71
+; BELOW: NumVGPRsForWavesPerEU: 71
+; BELOW: Occupancy: 3
+; BELOW: .amdgpu_occupancy 2
+
+; The "4,4" hint asks for more waves than the ABI occupancy, so it is accepted
+; and lowers the budget to 64 VGPRs.
+; ABOVE: error: {{.*}}VGPRs under object-linking ABI (71) exceeds limit (64) in function 'hint_above_abi'
+
+;--- below.ll
+define amdgpu_kernel void @hint_below_abi(ptr addrspace(1) %p) #0 {
+ call void asm sideeffect "; clobber", "~{v70}"()
+ store i32 0, ptr addrspace(1) %p
+ ret void
+}
+
+attributes #0 = { "amdgpu-waves-per-eu"="1,1" }
+
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"amdgpu_abi_waves_per_eu", i32 2}
+
+;--- above.ll
+define amdgpu_kernel void @hint_above_abi(ptr addrspace(1) %p) #0 {
+ call void asm sideeffect "; clobber", "~{v70}"()
+ store i32 0, ptr addrspace(1) %p
+ ret void
+}
+
+attributes #0 = { "amdgpu-waves-per-eu"="4,4" }
+
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"amdgpu_abi_waves_per_eu", i32 2}
diff --git a/llvm/test/CodeGen/AMDGPU/object-linking-abi-occupancy.ll b/llvm/test/CodeGen/AMDGPU/object-linking-abi-occupancy.ll
new file mode 100644
index 0000000000000..f6e2ec93ba0c2
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/object-linking-abi-occupancy.ll
@@ -0,0 +1,48 @@
+; RUN: llc -mtriple=amdgpu9.00-amd-amdhsa -amdgpu-enable-object-linking < %s | FileCheck -check-prefix=ABI %s
+; RUN: llc -mtriple=amdgpu9.00-amd-amdhsa < %s | FileCheck -check-prefix=NOABI %s
+
+; Without an amdgpu-waves-per-eu attribute the object-linking ABI occupancy is
+; the occupancy that a 1024-workitem workgroup needs, which is 4 waves/EU on
+; gfx900. It applies to kernels and to device functions alike, and it is
+; recorded in `.amdgpu.info`.
+;
+; The ABI occupancy is a register-budget floor, not an exact occupancy request.
+; The reported resource usage, the kernel descriptor and the kernel metadata
+; therefore stay the same as without object linking.
+
+; ABI: .set .Lkernel_no_attr.num_vgpr, 1
+; ABI: NumVGPRsForWavesPerEU: 1
+; ABI: Occupancy: 10
+; ABI: .set .Ldevice_fn_no_attr.num_vgpr, 3
+
+; ABI: .amdgpu_info kernel_no_attr
+; ABI: .amdgpu_num_vgpr 1
+; ABI: .amdgpu_occupancy 4
+; ABI: .amdgpu_info device_fn_no_attr
+; ABI: .amdgpu_num_vgpr 3
+; ABI: .amdgpu_occupancy 4
+
+; Per-function resource reporting is local-only under object linking, so the
+; module-level `amdgpu.max_num_*` symbols are suppressed.
+; ABI-NOT: amdgpu.max_num_
+
+; ABI: .sgpr_count: 14
+; ABI: .vgpr_count: 1
+
+; NOABI: .set .Lkernel_no_attr.num_vgpr, 1
+; NOABI: NumVGPRsForWavesPerEU: 1
+; NOABI: Occupancy: 10
+; NOABI: .set .Ldevice_fn_no_attr.num_vgpr, 3
+; NOABI: .set amdgpu.max_num_vgpr, 3
+; NOABI: .sgpr_count: 14
+; NOABI: .vgpr_count: 1
+
+define amdgpu_kernel void @kernel_no_attr(ptr addrspace(1) %p) {
+ store i32 0, ptr addrspace(1) %p
+ ret void
+}
+
+define void @device_fn_no_attr(ptr addrspace(1) %p) {
+ store i32 0, ptr addrspace(1) %p
+ ret void
+}
diff --git a/llvm/test/MC/AMDGPU/amdgpu-info-roundtrip.s b/llvm/test/MC/AMDGPU/amdgpu-info-roundtrip.s
index 1769f4cb97629..47680c8a3470f 100644
--- a/llvm/test/MC/AMDGPU/amdgpu-info-roundtrip.s
+++ b/llvm/test/MC/AMDGPU/amdgpu-info-roundtrip.s
@@ -40,6 +40,7 @@ addr_taken_func:
.amdgpu_num_vgpr 32
.amdgpu_num_agpr 4
.amdgpu_private_segment_size 0
+ .amdgpu_occupancy 4
.amdgpu_use lds_var
.amdgpu_call helper
.amdgpu_indirect_call "vi"
@@ -53,6 +54,7 @@ addr_taken_func:
.amdgpu_num_sgpr 8
.amdgpu_num_vgpr 10
.amdgpu_private_segment_size 16
+ .amdgpu_occupancy 8
.amdgpu_call extern_func
.end_amdgpu_info
@@ -62,6 +64,7 @@ addr_taken_func:
.amdgpu_num_sgpr 2
.amdgpu_num_vgpr 4
.amdgpu_private_segment_size 0
+ .amdgpu_occupancy 10
.amdgpu_typeid "vi"
.end_amdgpu_info
@@ -71,6 +74,7 @@ addr_taken_func:
// ASM: .amdgpu_num_vgpr 32
// ASM: .amdgpu_num_agpr 4
// ASM: .amdgpu_private_segment_size 0
+// ASM: .amdgpu_occupancy 4
// ASM: .amdgpu_use lds_var
// ASM: .amdgpu_call helper
// ASM: .amdgpu_indirect_call "vi"
@@ -82,6 +86,7 @@ addr_taken_func:
// ASM: .amdgpu_num_vgpr 10
// ASM-NOT: .amdgpu_num_agpr
// ASM: .amdgpu_private_segment_size 16
+// ASM: .amdgpu_occupancy 8
// ASM: .amdgpu_call extern_func
// ASM: .end_amdgpu_info
@@ -91,6 +96,7 @@ addr_taken_func:
// ASM: .amdgpu_num_vgpr 4
// ASM-NOT: .amdgpu_num_agpr
// ASM: .amdgpu_private_segment_size 0
+// ASM: .amdgpu_occupancy 10
// ASM: .amdgpu_typeid "vi"
// ASM: .end_amdgpu_info
More information about the llvm-commits
mailing list