[llvm] [AMDGPU] Clamp promote alloca LDS limit to addressable LDS size (PR #228987)

Arseniy Obolenskiy via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 5 01:43:22 PDT 2026


https://github.com/aobolensk updated https://github.com/llvm/llvm-project/pull/228987

>From d968a66ca5fd2622ca17be0f3a9ef16acd4e790c Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Mon, 5 Oct 2026 08:49:04 +0200
Subject: [PATCH] [AMDGPU] Clamp promote-alloca LDS limit to addressable LDS
 size

On GFX10+ a workgroup can only address 64k of the 128k LDS shared by the WGP, so promote alloca could grow LDS past the limit and fail codegen
---
 llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp          |  4 +++-
 .../AMDGPU/large-work-group-promote-alloca.ll       | 13 +++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp b/llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp
index b9ba99cf8db19c1..7799c45d2e7bbb4 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp
@@ -46,7 +46,9 @@ AMDGPUSubtarget::getMaxLocalMemSizeWithWaveCount(unsigned NWaves,
       std::max(1u, (NWaves * getNumWorkGroupSIMDs()) / WavesPerWorkgroup);
 
   const unsigned Granularity = std::max(LDSAllocationGranularity, 1u);
-  return alignDown(getLocalMemorySize() / WorkGroupsPerCU, Granularity);
+  return alignDown(std::min(getLocalMemorySize() / WorkGroupsPerCU,
+                            getAddressableLocalMemorySize()),
+                   Granularity);
 }
 
 std::pair<unsigned, unsigned> AMDGPUSubtarget::getOccupancyWithWorkGroupSizes(
diff --git a/llvm/test/CodeGen/AMDGPU/large-work-group-promote-alloca.ll b/llvm/test/CodeGen/AMDGPU/large-work-group-promote-alloca.ll
index dc688121b41307d..fc03ba112a646b3 100644
--- a/llvm/test/CodeGen/AMDGPU/large-work-group-promote-alloca.ll
+++ b/llvm/test/CodeGen/AMDGPU/large-work-group-promote-alloca.ll
@@ -69,6 +69,18 @@ entry:
   ret void
 }
 
+; ALL-LABEL: @promote_alloca_size_81920(
+; ALL: alloca [20 x i32]
+define amdgpu_kernel void @promote_alloca_size_81920(ptr addrspace(1) nocapture %out, i32 %idx) #8 {
+entry:
+  %stack = alloca [20 x i32], align 4, addrspace(5)
+  %arrayidx = getelementptr inbounds [20 x i32], ptr addrspace(5) %stack, i32 0, i32 %idx
+  store i32 7, ptr addrspace(5) %arrayidx, align 4
+  %0 = load i32, ptr addrspace(5) %arrayidx, align 4
+  store i32 %0, ptr addrspace(1) %out, align 4
+  ret void
+}
+
 ; ALL-LABEL: @occupancy_0(
 ; CI-NOT: alloca [5 x i32]
 ; SI: alloca [5 x i32]
@@ -271,3 +283,4 @@ attributes #4 = { nounwind "amdgpu-waves-per-eu"="1,10" }
 attributes #5 = { nounwind "amdgpu-waves-per-eu"="1,6" "amdgpu-flat-work-group-size"="64,64" }
 attributes #6 = { nounwind "amdgpu-waves-per-eu"="1,8" "amdgpu-flat-work-group-size"="64,64" }
 attributes #7 = { nounwind "amdgpu-waves-per-eu"="1,9" "amdgpu-flat-work-group-size"="64,64" }
+attributes #8 = { nounwind "amdgpu-waves-per-eu"="8,8" "amdgpu-flat-work-group-size"="1024,1024" }



More information about the llvm-commits mailing list