[llvm] [AMDGPU] Fix LDS allocation granularity in occupancy for RDNA 2 (PR #219495)
Frederik Harwath via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 28 07:46:44 PDT 2026
https://github.com/frederik-h created https://github.com/llvm/llvm-project/pull/219495
The AMDGPU backend uses the wrong LDS allocation granularity when computing occupancy for RDNA targets starting from GFX10.3.
The granularity is correct for the kernel metadata, but the actual allocation uses a different granularity. This is documented in the LLVM AMDGPUUsage.rst (metadata) and the ISA manual (actual allocation size) and the different runtimes (Mesa, ROCclr, and PAL).
Distinguish between "allocation" and "encoding" granularity. Add a new function for computing the allocation granularity and use it for occupancy calculation, keeping the preexisting function for metadata.
>From b00d70d970334130ac60a845e330f9c6e34ae0d7 Mon Sep 17 00:00:00 2001
From: Frederik Harwath <fharwath at amd.com>
Date: Mon, 24 Aug 2026 13:05:44 -0400
Subject: [PATCH] [AMDGPU] Fix LDS allocation granularity in occupancy for RDNA
2
The AMDGPU backend uses the wrong LDS allocation granularity when
computing occupancy for RDNA targets starting from GFX10.3.
The granularity is correct for the kernel metadata, but the actual
allocation uses a different granularity. This is documented in the
LLVM AMDGPUUsage.rst (metadata) and the ISA manual (actual allocation
size) and the different runtimes (Mesa, ROCclr, and PAL).
Distinguish between "allocation" and "encoding" granularity.
Add a new function for computing the allocation granularity
and use it for occupancy calculation, keeping the preexisting
function for metadata.
---
llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp | 4 +-
llvm/lib/Target/AMDGPU/GCNSubtarget.cpp | 2 +-
.../Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp | 15 +++++-
llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h | 12 +++--
llvm/test/CodeGen/AMDGPU/occupancy-levels.ll | 52 +++++++++++--------
5 files changed, 55 insertions(+), 30 deletions(-)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
index 3d337a466a75e..c5b9b37164006 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
@@ -1422,7 +1422,7 @@ void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &ProgInfo,
ProgInfo.LDSSize = MFI->getLDSSize();
- unsigned LDSGranularityBytes = getLdsDwGranularity(STM) * 4;
+ unsigned LDSGranularityBytes = getLdsGranularityEncodingDw(STM) * 4;
ProgInfo.LDSBlocks =
alignTo(ProgInfo.LDSSize, LDSGranularityBytes) / LDSGranularityBytes;
@@ -1661,7 +1661,7 @@ static void EmitPALMetadataCommon(AMDGPUPALMetadata *MD,
MD->updateHwStageMaximum(
CC, ".lds_size",
- (unsigned)(CurrentProgramInfo.LdsSize * getLdsDwGranularity(ST) *
+ (unsigned)(CurrentProgramInfo.LdsSize * getLdsGranularityEncodingDw(ST) *
sizeof(uint32_t)));
}
diff --git a/llvm/lib/Target/AMDGPU/GCNSubtarget.cpp b/llvm/lib/Target/AMDGPU/GCNSubtarget.cpp
index 225cdfeb914c6..51164c093991e 100644
--- a/llvm/lib/Target/AMDGPU/GCNSubtarget.cpp
+++ b/llvm/lib/Target/AMDGPU/GCNSubtarget.cpp
@@ -181,7 +181,7 @@ GCNSubtarget &GCNSubtarget::initializeSubtargetDependencies(const Triple &TT,
AMDGPU::IsaInfo::getAddressableLocalMemorySize(*this);
// LDS Allocation Granularity calculated in bytes from dwords
LDSAllocationGranularity =
- AMDGPU::getLdsDwGranularity(*this) * sizeof(uint32_t);
+ AMDGPU::getLdsGranularityAllocDw(*this) * sizeof(uint32_t);
HasFminFmaxLegacy = getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS;
HasSMulHi = getGeneration() >= AMDGPUSubtarget::GFX9;
diff --git a/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp b/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp
index 9d54b5f0d1c84..457243271801a 100644
--- a/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp
@@ -3740,7 +3740,7 @@ bool isDPALU_DPP(const MCInstrDesc &OpDesc, const MCInstrInfo &MII,
return hasAny64BitVGPROperands(OpDesc, MII, ST);
}
-unsigned getLdsDwGranularity(const MCSubtargetInfo &ST) {
+unsigned getLdsGranularityEncodingDw(const MCSubtargetInfo &ST) {
if (ST.getFeatureBits().test(FeatureAddressableLocalMemorySize32768))
return 64;
if (ST.getFeatureBits().test(FeatureAddressableLocalMemorySize65536))
@@ -3754,6 +3754,19 @@ unsigned getLdsDwGranularity(const MCSubtargetInfo &ST) {
return 64; // In sync with getAddressableLocalMemorySize
}
+/// Returns the hardware LDS allocation block size in dwords. This is the
+/// granularity at which the Command Processor allocates LDS to workgroups and
+/// is used for occupancy calculations. For most architectures this matches
+/// getLdsGranularityEncodingDw(), but some architectures allocate at a
+/// different granularity than they encode.
+unsigned getLdsGranularityAllocDw(const MCSubtargetInfo &ST) {
+ if (hasGFX10_3Insts(ST) &&
+ ST.getFeatureBits().test(FeatureAddressableLocalMemorySize65536))
+ return 256;
+
+ return getLdsGranularityEncodingDw(ST);
+}
+
bool isPackedSingleSGPRFP32Inst(unsigned Opc) {
switch (Opc) {
case AMDGPU::V_PK_ADD_F32_gfx1250:
diff --git a/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h b/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h
index e9756fa2a9d7f..7de3ac7d27a0b 100644
--- a/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h
+++ b/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h
@@ -1805,10 +1805,14 @@ getVGPRLoweringOperandTables(const MCInstrDesc &Desc);
/// \returns true if a memory instruction supports scale_offset modifier.
bool supportsScaleOffset(const MCInstrInfo &MII, unsigned Opcode);
-/// \returns lds block size in terms of dwords. \p
-/// This is used to calculate the lds size encoded for PAL metadata 3.0+ which
-/// must be defined in terms of bytes.
-unsigned getLdsDwGranularity(const MCSubtargetInfo &ST);
+/// \returns LDS encoding granularity in dwords for PAL metadata.
+/// This is used to calculate the LDS size field value for COMPUTE_PGM_RSRC2.
+unsigned getLdsGranularityEncodingDw(const MCSubtargetInfo &ST);
+
+/// \returns LDS allocation granularity in dwords.
+/// This is the block size at which hardware allocates LDS to workgroups,
+/// used for occupancy calculations.
+unsigned getLdsGranularityAllocDw(const MCSubtargetInfo &ST);
class ClusterDimsAttr {
public:
diff --git a/llvm/test/CodeGen/AMDGPU/occupancy-levels.ll b/llvm/test/CodeGen/AMDGPU/occupancy-levels.ll
index 1d63d62fc8524..7593c22ec9918 100644
--- a/llvm/test/CodeGen/AMDGPU/occupancy-levels.ll
+++ b/llvm/test/CodeGen/AMDGPU/occupancy-levels.ll
@@ -657,11 +657,13 @@ define amdgpu_kernel void @used_lds_13112() {
; GFX8: ; Occupancy: 2{{$}}
; GFX9: ; Occupancy: 2{{$}}
; GFX950: ; Occupancy: 5{{$}}
-; GFX10W64: ; Occupancy: 4{{$}}
-; GFX10W32: ; Occupancy: 8{{$}}
+; GFX1010W64: ; Occupancy: 4{{$}}
+; GFX1030W64: ; Occupancy: 4{{$}}
+; GFX1010W32: ; Occupancy: 8{{$}}
+; GFX1030W32: ; Occupancy: 7{{$}}
; GFX1100W64: ; Occupancy: 4{{$}}
-; GFX1100W32: ; Occupancy: 8{{$}}
-; GFX1200W32WG: ; Occupancy: 8{{$}}
+; GFX1100W32: ; Occupancy: 7{{$}}
+; GFX1200W32WG: ; Occupancy: 7{{$}}
; GFX1200W32CU: ; Occupancy: 7{{$}}
; GFX1200W64: ; Occupancy: 4{{$}}
; GFX1250: ; Occupancy: 8{{$}}
@@ -681,13 +683,15 @@ define amdgpu_kernel void @used_lds_8252_max_group_size_64() #3 {
; GFX8: ; Occupancy: 4{{$}}
; GFX9: ; Occupancy: 4{{$}}
; GFX950: ; Occupancy: 8{{$}}
-; GFX10W64: ; Occupancy: 8{{$}}
-; GFX10W32: ; Occupancy: 12{{$}}
-; GFX1100W64: ; Occupancy: 8{{$}}
-; GFX1100W32: ; Occupancy: 12{{$}}
-; GFX1200W32WG: ; Occupancy: 12{{$}}
+; GFX1010W64: ; Occupancy: 8{{$}}
+; GFX1030W64: ; Occupancy: 7{{$}}
+; GFX1010W32: ; Occupancy: 12{{$}}
+; GFX1030W32: ; Occupancy: 11{{$}}
+; GFX1100W64: ; Occupancy: 7{{$}}
+; GFX1100W32: ; Occupancy: 11{{$}}
+; GFX1200W32WG: ; Occupancy: 11{{$}}
; GFX1200W32CU: ; Occupancy: 11{{$}}
-; GFX1200W64WG: ; Occupancy: 8{{$}}
+; GFX1200W64WG: ; Occupancy: 7{{$}}
; GFX1200W64CU: ; Occupancy: 7{{$}}
; GFX1250: ; Occupancy: 12{{$}}
; GFX1310W32WG: ; Occupancy: 16{{$}}
@@ -705,13 +709,15 @@ define amdgpu_kernel void @used_lds_8252_max_group_size_96() #4 {
; GFX8: ; Occupancy: 4{{$}}
; GFX9: ; Occupancy: 4{{$}}
; GFX950: ; Occupancy: 8{{$}}
-; GFX10W64: ; Occupancy: 8{{$}}
-; GFX10W32: ; Occupancy: 15{{$}}
-; GFX1100W64: ; Occupancy: 8{{$}}
-; GFX1100W32: ; Occupancy: 15{{$}}
-; GFX1200W32WG: ; Occupancy: 15{{$}}
+; GFX1010W64: ; Occupancy: 8{{$}}
+; GFX1030W64: ; Occupancy: 7{{$}}
+; GFX1010W32: ; Occupancy: 15{{$}}
+; GFX1030W32: ; Occupancy: 14{{$}}
+; GFX1100W64: ; Occupancy: 7{{$}}
+; GFX1100W32: ; Occupancy: 14{{$}}
+; GFX1200W32WG: ; Occupancy: 14{{$}}
; GFX1200W32CU: ; Occupancy: 14{{$}}
-; GFX1200W64WG: ; Occupancy: 8{{$}}
+; GFX1200W64WG: ; Occupancy: 7{{$}}
; GFX1200W64CU: ; Occupancy: 7{{$}}
; GFX1250: ; Occupancy: 16{{$}}
; GFX1310W32: ; Occupancy: 16{{$}}
@@ -728,13 +734,14 @@ define amdgpu_kernel void @used_lds_8252_max_group_size_128() #5 {
; GFX8: ; Occupancy: 6{{$}}
; GFX9: ; Occupancy: 6{{$}}
; GFX950: ; Occupancy: 8{{$}}
-; GFX10W64: ; Occupancy: 12{{$}}
+; GFX1010W64: ; Occupancy: 12{{$}}
+; GFX1030W64: ; Occupancy: 11{{$}}
; GFX1010W32: ; Occupancy: 20{{$}}
; GFX1030W32: ; Occupancy: 15{{$}}
-; GFX1100W64: ; Occupancy: 12{{$}}
+; GFX1100W64: ; Occupancy: 11{{$}}
; GFX1100W32: ; Occupancy: 15{{$}}
; GFX1200W32: ; Occupancy: 15{{$}}
-; GFX1200W64WG: ; Occupancy: 12{{$}}
+; GFX1200W64WG: ; Occupancy: 11{{$}}
; GFX1200W64CU: ; Occupancy: 11{{$}}
; GFX1250: ; Occupancy: 15{{$}}
; GFX1310W32: ; Occupancy: 15{{$}}
@@ -751,13 +758,14 @@ define amdgpu_kernel void @used_lds_8252_max_group_size_192() #6 {
; GFX8: ; Occupancy: 7{{$}}
; GFX9: ; Occupancy: 7{{$}}
; GFX950: ; Occupancy: 8{{$}}
-; GFX10W64: ; Occupancy: 15{{$}}
+; GFX1010W64: ; Occupancy: 15{{$}}
+; GFX1030W64: ; Occupancy: 14{{$}}
; GFX1010W32: ; Occupancy: 20{{$}}
; GFX1030W32: ; Occupancy: 16{{$}}
-; GFX1100W64: ; Occupancy: 15{{$}}
+; GFX1100W64: ; Occupancy: 14{{$}}
; GFX1100W32: ; Occupancy: 16{{$}}
; GFX1200W32: ; Occupancy: 16{{$}}
-; GFX1200W64WG: ; Occupancy: 15{{$}}
+; GFX1200W64WG: ; Occupancy: 14{{$}}
; GFX1200W64CU: ; Occupancy: 14{{$}}
; GFX1250: ; Occupancy: 16{{$}}
; GFX1310: ; Occupancy: 16{{$}}
More information about the llvm-commits
mailing list