← Back to Pull requests

[AMDGPU] Add module flag for LDS size limit #229557

Open
mariusz-sikora-at-amd opened this pull request with 353 lines diff across 11 files
View on GitHub
mariusz-sikora-at-amd mariusz-sikora-at-amd commented on Oct 06, 2026 20:38 UTC
Author

Add amdgpu.lds.size.limit module flag to limit the LDS size below the hardware maximum. Allowed values are 65536 and 131072. When the flag is absent, the hardware maximum is used. It is not set by default and is meant to be used only by a frontend like LLPC.

Diff: 353 lines (+333 / -20) across 11 files Reviewer: krzysz00 Review duration: 79743m
All Vouch review confidence checks passed
Residual risk 0.06 = Change Risk (0.60) × (1 − Review Confidence (0.90)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.60 · 353 lines diff across 11 files
0.60
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.92 · 5 substantive comment(s)
0.92
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
89%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.351
Real PR Conversation Timeline
14 items
llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Oct 06, 2026 20:38 UTC
Bot

<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-amdgpu

@llvm/pr-subscribers-llvm-ir

Author: Mariusz Sikora (mariusz-sikora-at-amd)

<details>
<summary>Changes</summary>

Add amdgpu.lds.size.limit module flag to limit the LDS size below the hardware maximum. Allowed values are 65536 and 131072. When the flag is absent, the hardware maximum is used. It is not set by default and is meant to be used only by a frontend like LLPC.

---

Patch is 24.01 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/229557.diff

11 Files Affected:

  • (modified) llvm/docs/AMDGPUUsage.rst (+11)

  • (modified) llvm/include/llvm/TargetParser/AMDGPUTargetParser.h (+12-6)

  • (modified) llvm/lib/IR/VerifierAMDGPU.cpp (+15)

  • (modified) llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp (+13-1)

  • (modified) llvm/lib/Target/AMDGPU/GCNSubtarget.cpp (+6-4)

  • (modified) llvm/lib/Target/AMDGPU/GCNSubtarget.h (+5-1)

  • (modified) llvm/lib/TargetParser/AMDGPUTargetParser.cpp (+17-8)

  • (added) llvm/test/CodeGen/AMDGPU/lds-limit-diagnostics-gfx13-limit.ll (+19)

  • (added) llvm/test/CodeGen/AMDGPU/occupancy-levels-gfx1310-lds-limit.ll (+151)

  • (added) llvm/test/Verifier/AMDGPU/module-flag-lds-size-limit.ll (+56)

  • (modified) llvm/unittests/TargetParser/TargetParserTest.cpp (+28)
  • ``````

    diff --git a/llvm/docs/AMDGPUUsage.rst b/llvm/docs/AMDGPUUsage.rst
    index c57a013ff889ae..8424a0e43e11e3 100644
    --- a/llvm/docs/AMDGPUUsage.rst
    +++ b/llvm/docs/AMDGPUUsage.rst
    @@ -1054,6 +1054,17 @@ consumed by the AMDGPU backend during code generation.
            produce an error. Modules with **any** (absent flag) are compatible
            with any setting.
     
    +   * - ``amdgpu.lds.size.limit``
    +     - ``i32``
    +     - Error
    +     - Limits the LDS size which one work-group can use. If it is smaller
    +       than the hardware maximum, code generation uses it instead. The value
    +       is for FullSIMD mode. When not in FullSIMD mode, half of it is
    +       available.
    +
    +       Allowed values are ``65536`` and ``131072``. When the flag is absent,
    +       the hardware maximum is used.
    +
     .. note::
     
        Frontends that require misaligned-access merging for performance should
    diff --git a/llvm/include/llvm/TargetParser/AMDGPUTargetParser.h b/llvm/include/llvm/TargetParser/AMDGPUTargetParser.h
    index 042542ced349da..a1f4db9b4e1fee 100644
    --- a/llvm/include/llvm/TargetParser/AMDGPUTargetParser.h
    +++ b/llvm/include/llvm/TargetParser/AMDGPUTargetParser.h
    @@ -227,15 +227,21 @@ getMaxHWAddressableLocalMemorySize(Triple::SubArchType SubArch);
     
     /// \returns Total LDS in bytes available to work-groups sharing a physical
     /// block. \p FullSIMDMode selects full-SIMD mode (four SIMDs) when true and
    -/// half-SIMD mode (two SIMDs) otherwise.
    -LLVM_ABI unsigned getLocalMemorySize(GPUKind AK, bool FullSIMDMode);
    +/// half-SIMD mode (two SIMDs) otherwise. If \p LimitBytes is not 0, it limits
    +/// the LDS a work-group can address below the HW cap.
    +LLVM_ABI unsigned getLocalMemorySize(GPUKind AK, bool FullSIMDMode,
    +                                     unsigned LimitBytes = 0);
     LLVM_ABI unsigned getLocalMemorySize(Triple::SubArchType SubArch,
    -                                     bool FullSIMDMode);
    +                                     bool FullSIMDMode,
    +                                     unsigned LimitBytes = 0);
     
    -/// \returns LDS in bytes a single work-group can allocate.
    -LLVM_ABI unsigned getAddressableLocalMemorySize(GPUKind AK, bool FullSIMDMode);
    +/// \returns LDS in bytes a single work-group can allocate. \p LimitBytes is
    +/// the same as for getLocalMemorySize.
    +LLVM_ABI unsigned getAddressableLocalMemorySize(GPUKind AK, bool FullSIMDMode,
    +                                                unsigned LimitBytes = 0);
     LLVM_ABI unsigned getAddressableLocalMemorySize(Triple::SubArchType SubArch,
    -                                                bool FullSIMDMode);
    +                                                bool FullSIMDMode,
    +                                                unsigned LimitBytes = 0);
     
     /// \returns Number of LDS banks per compute unit.
     LLVM_ABI unsigned getLDSBankCount(GPUKind AK);
    diff --git a/llvm/lib/IR/VerifierAMDGPU.cpp b/llvm/lib/IR/VerifierAMDGPU.cpp
    index 5d7b46357d2849..c67a5e7ce6aea8 100644
    --- a/llvm/lib/IR/VerifierAMDGPU.cpp
    +++ b/llvm/lib/IR/VerifierAMDGPU.cpp
    @@ -70,6 +70,21 @@ void llvm::verifyAMDGPUModuleFlag(VerifierSupport &VS, const MDString *ID,
               "'" + ID->getString() + "' module flag must be 0 or 1");
         return;
       }
    +
    +  if (FlagName == "lds.size.limit") {
    +    Check(MFB == Module::Error,
    +          "'" + ID->getString() +
    +              "' module flag must use 'error' merge behaviour");
    +    ConstantInt *Value =
    +        mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2));
    +    Check(Value, "'" + ID->getString() +
    +                     "' module flag must have a constant integer value");
    +    uint64_t Limit = Value->getZExtValue();
    +    Check(Value->getType()->isIntegerTy(32) &&
    +              (Limit == 65536 || Limit == 131072),
    +          "'" + ID->getString() + "' module flag must be i32 65536 or 131072");
    +    return;
    +  }
     }
     
     // Verify that when a function has !reqd_work_group_size metadata, it also has
    diff --git a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
    index 4ff5d2295fc6ad..96dd59d7879a46 100644
    --- a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
    +++ b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
    @@ -1326,6 +1326,14 @@ static OOBFlagValue getOOBFlagValue(const Module &M, StringRef FlagName) {
       return static_cast<OOBFlagValue>(Flag->getZExtValue());
     }
     
    +/// Returns the LDS size limit in bytes encoded by the "amdgpu.lds.size.limit"
    +/// module flag. An absent flag means no limit (0).
    +static unsigned getLDSSizeLimit(const Module &M) {
    +  const auto *Flag = mdconst::dyn_extract_or_null<ConstantInt>(
    +      M.getModuleFlag("amdgpu.lds.size.limit"));
    +  return Flag ? Flag->getZExtValue() : 0;
    +}
    +
     /// Returns the xnack/sramecc setting encoded by a module flag.
     /// Module flag values: 0 = disabled, 1 = enabled.
     /// An absent flag defaults to Any.
    @@ -1361,6 +1369,7 @@ GCNTargetMachine::getSubtargetImpl(const Function &F) const {
       TargetIDSetting Xnack = getTargetIDSettingFromModuleFlag(M, "amdgpu.xnack");
       TargetIDSetting SramEcc =
           getTargetIDSettingFromModuleFlag(M, "amdgpu.sramecc");
    +  unsigned LDSSizeLimit = getLDSSizeLimit(M);
     
       SmallString<128> SubtargetKey(GPU);
       SubtargetKey.append(FS);
    @@ -1376,6 +1385,8 @@ GCNTargetMachine::getSubtargetImpl(const Function &F) const {
         SubtargetKey.append(",sramecc=");
         SubtargetKey.push_back(Xnack == TargetIDSetting::On ? '1' : '0');
       }
    +  if (LDSSizeLimit)
    +    (",lds-size-limit=" + Twine(LDSSizeLimit)).toVector(SubtargetKey);
     
       auto &I = SubtargetMap[SubtargetKey];
       if (!I) {
    @@ -1399,7 +1410,8 @@ GCNTargetMachine::getSubtargetImpl(const Function &F) const {
         }
     
         I = std::make_unique<GCNSubtarget>(TargetTriple, GPU, FS, *this, BufRelaxed,
    -                                       TBufRelaxed, Xnack, SramEcc);
    +                                       TBufRelaxed, Xnack, SramEcc,
    +                                       LDSSizeLimit);
       }
     
       I->setScalarizeGlobalBehavior(ScalarizeGlobal);
    diff --git a/llvm/lib/Target/AMDGPU/GCNSubtarget.cpp b/llvm/lib/Target/AMDGPU/GCNSubtarget.cpp
    index 31dd8c8be27818..c8c83db4720b70 100644
    --- a/llvm/lib/Target/AMDGPU/GCNSubtarget.cpp
    +++ b/llvm/lib/Target/AMDGPU/GCNSubtarget.cpp
    @@ -181,10 +181,10 @@ GCNSubtarget &GCNSubtarget::initializeSubtargetDependencies(const Triple &TT,
       if (FlatOffsetBitWidth == 0)
         FlatOffsetBitWidth = 13;
     
    -  LocalMemorySize =
    -      AMDGPU::getLocalMemorySize(getTargetID().getGPUKind(), isFullSIMDMode());
    +  LocalMemorySize = AMDGPU::getLocalMemorySize(getTargetID().getGPUKind(),
    +                                               isFullSIMDMode(), LDSSizeLimit);
       AddressableLocalMemorySize = AMDGPU::getAddressableLocalMemorySize(
    -      getTargetID().getGPUKind(), isFullSIMDMode());
    +      getTargetID().getGPUKind(), isFullSIMDMode(), LDSSizeLimit);
       // LDS allocation granularity is in bytes.
       LDSAllocationGranularity =
           AMDGPU::getLDSAllocGranule(getTargetID().getGPUKind());
    @@ -219,7 +219,8 @@ GCNSubtarget::GCNSubtarget(const Triple &TT, StringRef GPU, StringRef FS,
                                const GCNTargetMachine &TM, bool BufferOOBRelaxed,
                                bool TBufferOOBRelaxed,
                                AMDGPU::TargetIDSetting XnackSetting,
    -                           AMDGPU::TargetIDSetting SramEccSetting)
    +                           AMDGPU::TargetIDSetting SramEccSetting,
    +                           unsigned LDSSizeLimit)
         : // clang-format off
         AMDGPUGenSubtargetInfo(TT, GPU, /*TuneCPU*/ GPU, FS),
         AMDGPUSubtarget(TT),
    @@ -227,6 +228,7 @@ GCNSubtarget::GCNSubtarget(const Triple &TT, StringRef GPU, StringRef FS,
         InstrItins(getInstrItineraryForCPU(GPU)),
         BufferOOBRelaxed(BufferOOBRelaxed),
         TBufferOOBRelaxed(TBufferOOBRelaxed),
    +    LDSSizeLimit(LDSSizeLimit),
         InstrInfo(initializeSubtargetDependencies(TT, GPU, FS)),
         TLInfo(TM, *this),
         // Frame index expansion sometimes assumes the low bit of SP is 0
    diff --git a/llvm/lib/Target/AMDGPU/GCNSubtarget.h b/llvm/lib/Target/AMDGPU/GCNSubtarget.h
    index 28e57bcf6a1c06..37bc8fe54dd5fa 100644
    --- a/llvm/lib/Target/AMDGPU/GCNSubtarget.h
    +++ b/llvm/lib/Target/AMDGPU/GCNSubtarget.h
    @@ -92,6 +92,9 @@ class GCNSubtarget final : public AMDGPUGenSubtargetInfo,
       bool ScalarizeGlobal = false;
       const bool BufferOOBRelaxed;
       const bool TBufferOOBRelaxed;
    +  /// LDS size limit in bytes from the "amdgpu.lds.size.limit" module flag, 0
    +  /// if there is no limit.
    +  const unsigned LDSSizeLimit;
     
       /// The maximum number of instructions that may be placed within an S_CLAUSE,
       /// which is one greater than the maximum argument to S_CLAUSE. A value of 0
    @@ -121,7 +124,8 @@ class GCNSubtarget final : public AMDGPUGenSubtargetInfo,
           const Triple &TT, StringRef GPU, StringRef FS, const GCNTargetMachine &TM,
           bool BufferOOBRelaxed = false, bool TBufferOOBRelaxed = false,
           AMDGPU::TargetIDSetting XnackSetting = AMDGPU::TargetIDSetting::Any,
    -      AMDGPU::TargetIDSetting SramEccSetting = AMDGPU::TargetIDSetting::Any);
    +      AMDGPU::TargetIDSetting SramEccSetting = AMDGPU::TargetIDSetting::Any,
    +      unsigned LDSSizeLimit = 0);
       ~GCNSubtarget() override;
     
       GCNSubtarget &initializeSubtargetDependencies(const Triple &TT, StringRef GPU,
    diff --git a/llvm/lib/TargetParser/AMDGPUTargetParser.cpp b/llvm/lib/TargetParser/AMDGPUTargetParser.cpp
    index 116014826ed679..e8933e16a87a4e 100644
    --- a/llvm/lib/TargetParser/AMDGPUTargetParser.cpp
    +++ b/llvm/lib/TargetParser/AMDGPUTargetParser.cpp
    @@ -478,9 +478,12 @@ AMDGPU::getMaxHWAddressableLocalMemorySize(Triple::SubArchType SubArch) {
       return getMaxHWAddressableLocalMemorySize(getGPUKindFromSubArch(SubArch));
     }
     
    -unsigned AMDGPU::getLocalMemorySize(GPUKind AK, bool FullSIMDMode) {
    +unsigned AMDGPU::getLocalMemorySize(GPUKind AK, bool FullSIMDMode,
    +                                    unsigned LimitBytes) {
       // gfx6 and gfx10/11/12 address half of the physical block.
       unsigned Size = getMaxHWAddressableLocalMemorySize(AK);
    +  if (LimitBytes)
    +    Size = std::min(Size, LimitBytes);
       if (getFeatureBitset(AK).test(FEAT_HALF_ADDRESSABLE_PHYSICAL_LOCAL_MEMORY))
         Size *= 2;
     
    @@ -492,19 +495,25 @@ unsigned AMDGPU::getLocalMemorySize(GPUKind AK, bool FullSIMDMode) {
     }
     
     unsigned AMDGPU::getLocalMemorySize(Triple::SubArchType SubArch,
    -                                    bool FullSIMDMode) {
    -  return getLocalMemorySize(getGPUKindFromSubArch(SubArch), FullSIMDMode);
    +                                    bool FullSIMDMode, unsigned LimitBytes) {
    +  return getLocalMemorySize(getGPUKindFromSubArch(SubArch), FullSIMDMode,
    +                            LimitBytes);
     }
     
    -unsigned AMDGPU::getAddressableLocalMemorySize(GPUKind AK, bool FullSIMDMode) {
    -  return std::min(getMaxHWAddressableLocalMemorySize(AK),
    -                  getLocalMemorySize(AK, FullSIMDMode));
    +unsigned AMDGPU::getAddressableLocalMemorySize(GPUKind AK, bool FullSIMDMode,
    +                                               unsigned LimitBytes) {
    +  unsigned MaxAddressable = getMaxHWAddressableLocalMemorySize(AK);
    +  if (LimitBytes)
    +    MaxAddressable = std::min(MaxAddressable, LimitBytes);
    +  return std::min(MaxAddressable,
    +                  getLocalMemorySize(AK, FullSIMDMode, LimitBytes));
     }
     
     unsigned AMDGPU::getAddressableLocalMemorySize(Triple::SubArchType SubArch,
    -                                               bool FullSIMDMode) {
    +                                               bool FullSIMDMode,
    +                                               unsigned LimitBytes) {
       return getAddressableLocalMemorySize(getGPUKindFromSubArch(SubArch),
    -                                       FullSIMDMode);
    +                                       FullSIMDMode, LimitBytes);
     }
     
     unsigned AMDGPU::getLDSBankCount(GPUKind AK) {
    diff --git a/llvm/test/CodeGen/AMDGPU/lds-limit-diagnostics-gfx13-limit.ll b/llvm/test/CodeGen/AMDGPU/lds-limit-diagnostics-gfx13-limit.ll
    new file mode 100644
    index 00000000000000..b5f3d6e928e4c1
    --- /dev/null
    +++ b/llvm/test/CodeGen/AMDGPU/lds-limit-diagnostics-gfx13-limit.ll
    @@ -0,0 +1,19 @@
    +; RUN: sed 's/LDS_LIMIT/131072/' %s | not llc -mtriple=amdgpu13.10-amd-amdhsa 2>&1 | FileCheck -check-prefix=ERROR-128K %s
    +; RUN: sed 's/LDS_LIMIT/65536/' %s | not llc -mtriple=amdgpu13.10-amd-amdhsa 2>&1 | FileCheck -check-prefix=ERROR-64K %s
    +; RUN: sed 's/LDS_LIMIT/131072/' %s | not llc -mtriple=amdgpu13.10-amd-amdhsa -mattr=+cumode 2>&1 | FileCheck -check-prefix=ERROR-128K-CU %s
    +; RUN: sed 's/LDS_LIMIT/65536/' %s | not llc -mtriple=amdgpu13.10-amd-amdhsa -mattr=+cumode 2>&1 | FileCheck -check-prefix=ERROR-64K-CU %s
    +
    +; ERROR-128K: error: <unknown>:0:0: local memory (131076) exceeds limit (131072) in function 'test_lds_limit'
    +; ERROR-64K: error: <unknown>:0:0: local memory (131076) exceeds limit (65536) in function 'test_lds_limit'
    +; ERROR-128K-CU: error: <unknown>:0:0: local memory (131076) exceeds limit (65536) in function 'test_lds_limit'
    +; ERROR-64K-CU: error: <unknown>:0:0: local memory (131076) exceeds limit (32768) in function 'test_lds_limit'
    +@dst = addrspace(3) global [131076 x i8] poison
    +
    +define amdgpu_kernel void @test_lds_limit(i8 %val) {
    +  %gep = getelementptr [131076 x i8], ptr addrspace(3) @dst, i32 0, i32 100
    +  store i8 %val, ptr addrspace(3) %gep
    +  ret void
    +}
    +
    +!llvm.module.flags = !{!0}
    +!0 = !{i32 1, !"amdgpu.lds.size.limit", i32 LDS_LIMIT}
    diff --git a/llvm/test/CodeGen/AMDGPU/occupancy-levels-gfx1310-lds-limit.ll b/llvm/test/CodeGen/AMDGPU/occupancy-levels-gfx1310-lds-limit.ll
    new file mode 100644
    index 00000000000000..4c51886de86993
    --- /dev/null
    +++ b/llvm/test/CodeGen/AMDGPU/occupancy-levels-gfx1310-lds-limit.ll
    @@ -0,0 +1,151 @@
    +; RUN: sed -e '/LDS_LIMIT/d' -e '/llvm.module.flags/d' %s | llc -mtriple=amdgpu13.10 | FileCheck --check-prefixes=GCN,W32WG-LDS192 %s
    +; RUN: sed -e '/LDS_LIMIT/d' -e '/llvm.module.flags/d' %s | llc -mtriple=amdgpu13.10 -mattr=+wavefrontsize64 | FileCheck --check-prefixes=GCN,W64WG-LDS192 %s
    +; RUN: sed -e '/LDS_LIMIT/d' -e '/llvm.module.flags/d' %s | llc -mtriple=amdgpu13.10 -mattr=+cumode | FileCheck --check-prefixes=GCN,W32CU-LDS192 %s
    +; RUN: sed -e '/LDS_LIMIT/d' -e '/llvm.module.flags/d' %s | llc -mtriple=amdgpu13.10 -mattr=+wavefrontsize64,+cumode | FileCheck --check-prefixes=GCN,W64CU-LDS192 %s
    +; RUN: sed 's/LDS_LIMIT/131072/' %s | llc -mtriple=amdgpu13.10 | FileCheck --check-prefixes=GCN,W32WG-LDS128 %s
    +; RUN: sed 's/LDS_LIMIT/131072/' %s | llc -mtriple=amdgpu13.10 -mattr=+wavefrontsize64 | FileCheck --check-prefixes=GCN,W64WG-LDS128 %s
    +; RUN: sed 's/LDS_LIMIT/131072/' %s | llc -mtriple=amdgpu13.10 -mattr=+cumode | FileCheck --check-prefixes=GCN,W32CU-LDS128 %s
    +; RUN: sed 's/LDS_LIMIT/131072/' %s | llc -mtriple=amdgpu13.10 -mattr=+wavefrontsize64,+cumode | FileCheck --check-prefixes=GCN,W64CU-LDS128 %s
    +; RUN: sed 's/LDS_LIMIT/65536/' %s | llc -mtriple=amdgpu13.10 | FileCheck --check-prefixes=GCN,W32WG-LDS64 %s
    +; RUN: sed 's/LDS_LIMIT/65536/' %s | llc -mtriple=amdgpu13.10 -mattr=+wavefrontsize64 | FileCheck --check-prefixes=GCN,W64WG-LDS64 %s
    +; RUN: sed 's/LDS_LIMIT/65536/' %s | llc -mtriple=amdgpu13.10 -mattr=+cumode | FileCheck --check-prefixes=GCN,W32CU-LDS64 %s
    +; RUN: sed 's/LDS_LIMIT/65536/' %s | llc -mtriple=amdgpu13.10 -mattr=+wavefrontsize64,+cumode | FileCheck --check-prefixes=GCN,W64CU-LDS64 %s
    +
    +@lds8252 = internal addrspace(3) global [8252 x i8] poison, align 4
    +
    +; GCN-LABEL: {{^}}used_lds_8252_max_group_size_32:
    +; W32WG-LDS192: ; Occupancy: 6{{$}}
    +; W32CU-LDS192: ; Occupancy: 5{{$}}
    +; W64WG-LDS192: ; Occupancy: 6{{$}}
    +; W64CU-LDS192: ; Occupancy: 5{{$}}
    +; W32WG-LDS128: ; Occupancy: 4{{$}}
    +; W32CU-LDS128: ; Occupancy: 4{{$}}
    +; W64WG-LDS128: ; Occupancy: 4{{$}}
    +; W64CU-LDS128: ; Occupancy: 4{{$}}
    +; W32WG-LDS64: ; Occupancy: 2{{$}}
    +; W32CU-LDS64: ; Occupancy: 2{{$}}
    +; W64WG-LDS64: ; Occupancy: 2{{$}}
    +; W64CU-LDS64: ; Occupancy: 2{{$}}
    +define amdgpu_kernel void @used_lds_8252_max_group_size_32() #0 {
    +  store volatile i8 1, ptr addrspace(3) @lds8252
    +  ret void
    +}
    +
    +; GCN-LABEL: {{^}}used_lds_8252_max_group_size_64:
    +; W32WG-LDS192: ; Occupancy: 11{{$}}
    +; W32CU-LDS192: ; Occupancy: 10{{$}}
    +; W64WG-LDS192: ; Occupancy: 6{{$}}
    +; W64CU-LDS192: ; Occupancy: 5{{$}}
    +; W32WG-LDS128: ; Occupancy: 7{{$}}
    +; W32CU-LDS128: ; Occupancy: 7{{$}}
    +; W64WG-LDS128: ; Occupancy: 4{{$}}
    +; W64CU-LDS128: ; Occupancy: 4{{$}}
    +; W32WG-LDS64: ; Occupancy: 4{{$}}
    +; W32CU-LDS64: ; Occupancy: 3{{$}}
    +; W64WG-LDS64: ; Occupancy: 2{{$}}
    +; W64CU-LDS64: ; Occupancy: 2{{$}}
    +define amdgpu_kernel void @used_lds_8252_max_group_size_64() #1 {
    +  store volatile i8 1, ptr addrspace(3) @lds8252
    +  ret void
    +}
    +
    +; GCN-LABEL: {{^}}used_lds_8252_max_group_size_96:
    +; W32WG-LDS192: ; Occupancy: 16{{$}}
    +; W32CU-LDS192: ; Occupancy: 15{{$}}
    +; W64WG-LDS192: ; Occupancy: 11{{$}}
    +; W64CU-LDS192: ; Occupancy: 10{{$}}
    +; W32WG-LDS128: ; Occupancy: 11{{$}}
    +; W32CU-LDS128: ; Occupancy: 11{{$}}
    +; W64WG-LDS128: ; Occupancy: 7{{$}}
    +; W64CU-LDS128: ; Occupancy: 7{{$}}
    +; W32WG-LDS64: ; Occupancy: 6{{$}}
    +; W32CU-LDS64: ; Occupancy: 5{{$}}
    +; W64WG-LDS64: ; Occupancy: 4{{$}}
    +; W64CU-LDS64: ; Occupancy: 3{{$}}
    +define amdgpu_kernel void @used_lds_8252_max_group_size_96() #2 {
    +  store volatile i8 1, ptr addrspace(3) @lds8252
    +  ret void
    +}
    +
    +; GCN-LABEL: {{^}}used_lds_8252_max_group_size_128:
    +; W32WG-LDS192: ; Occupancy: 16{{$}}
    +; W32CU-LDS192: ; Occupancy: 16{{$}}
    +; W64WG-LDS192: ; Occupancy: 11{{$}}
    +; W64CU-LDS192: ; Occupancy: 10{{$}}
    +; W32WG-LDS128: ; Occupancy: 14{{$}}
    +; W32CU-LDS128: ; Occupancy: 14{{$}}
    +; W64WG-LDS128: ; Occupancy: 7{{$}}
    +; W64CU-LDS128: ; Occupancy: 7{{$}}
    +; W32WG-LDS64: ; Occupancy: 7{{$}}
    +; W32CU-LDS64: ; Occupancy: 6{{$}}
    +; W64WG-LDS64: ; Occupancy: 4{{$}}
    +; W64CU-LDS64: ; Occupancy: 3{{$}}
    +define amdgpu_kernel void @used_lds_8252_max_group_size_128() #3 {
    +  store volatile i8 1, ptr addrspace(3) @lds8252
    +  ret void
    +}
    +
    +; GCN-LABEL: {{^}}used_lds_8252_max_group_size_192:
    +; W32WG-LDS192: ; Occupancy: 15{{$}}
    +; W32CU-LDS192: ; Occupancy: 15{{$}}
    +; W64WG-LDS192: ; Occupancy: 16{{$}}
    +; W64CU-LDS192: ; Occupancy: 15{{$}}
    +; W32WG-LDS128: ; Occupancy: 15{{$}}
    +; W32CU-LDS128: ; Occupancy: 15{{$}}
    +; W64WG-LDS128: ; Occupancy: 11{{$}}
    +; W64CU-LDS128: ; Occupancy: 11{{$}}
    +; W32WG-LDS64: ; Occupancy: 11{{$}}
    +; W32CU-LDS64: ; Occupancy: 9{{$}}
    +; W64WG-LDS64: ; Occupancy: 6{{$}}
    +; W64CU-LDS64: ; Occupancy: 5{{$}}
    +define amdgpu_kernel void @used_lds_8252_max_group_size_192() #4 {
    +  store volatile i8 1, ptr addrspace(3) @lds8252
    +  ret void
    +}
    +
    +; GCN-LABEL: {{^}}used_lds_8252_max_group_size_256:
    +; W32WG-LDS192: ; Occupancy: 16{{$}}
    +; W32CU-LDS192: ; Occupancy: 16{{$}}
    +; W64WG-LDS192: ; Occupancy: 16{{$}}
    +; W64CU-LDS192: ; Occupancy: 16{{$}}
    +; W32WG-LDS128: ; Occupancy: 16{{$}}
    +; W32CU-LDS128: ; Occupancy: 16{{$}}
    +; W64WG-LDS128: ; Occupancy: 14{{$}}
    +; W64CU-LDS128: ; Occupancy: 14{{$}}
    +; W32WG-LDS64: ; Occupancy: 14{{$}}
    +; W32CU-LDS64: ; Occupancy: 12{{$}}
    +; W64WG-LDS64: ; Occupancy: 7{{$}}
    +; W64CU-LDS64: ; Occupancy: 6{{$}}
    +define amdgpu_kernel void @used_lds_8252_max_group_size_256() #5 {
    +  store volatile i8 1, ptr addrspace(3) @lds8252
    +  ret void
    +}
    +
    +; GCN-LABEL: {{^}}used_lds_8252_max_group_size_512:
    +; W32WG-LDS192: ; Occupancy: 16{{$}}
    +; W32CU-LDS192: ; Occupancy: 16{{$}}
    +; W64WG-LDS192: ; Occupancy: 16{{$}}
    +; W64CU-LDS192: ; Occupancy: 16{{$}}
    +; W32WG-LDS128: ; Occupancy: 16{{$}}
    +; W32CU-LDS128: ; Occupancy: 16{{$}}
    +; W64WG-LDS128: ; Occupancy: 16{{$}}
    +; W64CU-LDS128: ; Occupancy: 16{{$}}
    +; W32WG-LDS64: ; Occupancy: 16{{$}}
    +; W32CU-LDS64: ; Occupancy: 16{{$}}
    +; W64WG-LDS64: ; Occupancy: 14{{$}}
    +; W64CU-LDS64: ; Occupancy: 12{{$}}
    +define amdgpu_kernel void @used_lds_8252_max_group_size_512() #6 {
    +  store volatile i8 1, ptr addrspace(3) @lds8252
    +  ret void
    +}
    +
    +attributes #0 = { "amdgpu-flat-work-group-size"="1,32" }
    +attributes #1 = { "amdgpu-flat-work-group-size"="1,64" }
    +attributes #2 = { "amdgpu-flat-work-group-size"="1,96" }
    +attributes #3 = { "amdgpu-flat-work-group-size"="1,128" }
    +attributes #4 = { "amdgpu-flat-work-group-size"="1,192" }
    +attributes #5 = { "amdgpu-flat-work-group-size"="1,256" }
    +attributes #6 = { "amdgpu-flat-work-group-size"="1,512" }
    +
    +!llvm.module.flags = !{!0}
    +!0 = !{i32 1, !"amdgpu.lds.size.limit", i32 LDS_LIMIT}
    diff --git a/llvm/test/Verifier/AMDGPU/module-flag-lds-size-limit.ll b/llvm/test/Verifier/AMDGPU...
    [truncated]
    
    
    ``````

    </details>

    krzysz00 krzysz00 commented on code on Oct 06, 2026 21:30 UTC
    Code Review
    llvm/docs/AMDGPUUsage.rst:1
    @@ -1054,6 +1054,17 @@ consumed by the AMDGPU backend during code generation.
            produce an error. Modules with **any** (absent flag) are compatible
            with any setting.
     
    +   * - ``amdgpu.lds.size.limit``
    +     - ``i32``
    +     - Error

    I'm not sure "error" is the right semantics here? Perhaps "min"?

    krzysz00 krzysz00 commented on code on Oct 06, 2026 21:31 UTC
    Code Review
    llvm/docs/AMDGPUUsage.rst:1
    @@ -1054,6 +1054,17 @@ consumed by the AMDGPU backend during code generation.
            produce an error. Modules with **any** (absent flag) are compatible
            with any setting.
     
    +   * - ``amdgpu.lds.size.limit``
    +     - ``i32``
    +     - Error
    +     - Limits the LDS size which one work-group can use. If it is smaller
    +       than the hardware maximum, code generation uses it instead. The value
    +       is for FullSIMD mode. When not in FullSIMD mode, half of it is

    Is FullSIMD the terminology we use in this document? I've heard this as WGP and CU mode.

    Also ... it's weird that we're calling for halves here. Do we need these halving semantics?

    krzysz00 krzysz00 commented on code on Oct 06, 2026 21:32 UTC
    Code Review
    llvm/docs/AMDGPUUsage.rst:1
    @@ -1054,6 +1054,17 @@ consumed by the AMDGPU backend during code generation.
            produce an error. Modules with **any** (absent flag) are compatible
            with any setting.
     
    +   * - ``amdgpu.lds.size.limit``
    +     - ``i32``
    +     - Error
    +     - Limits the LDS size which one work-group can use. If it is smaller
    +       than the hardware maximum, code generation uses it instead. The value
    +       is for FullSIMD mode. When not in FullSIMD mode, half of it is
    +       available.
    +
    +       Allowed values are ``65536`` and ``131072``. When the flag is absent,

    Why only these values?

    krzysz00 krzysz00 commented on code on Oct 06, 2026 21:34 UTC
    Code Review
    llvm/docs/AMDGPUUsage.rst:1
    @@ -1054,6 +1054,17 @@ consumed by the AMDGPU backend during code generation.
            produce an error. Modules with **any** (absent flag) are compatible
            with any setting.
     
    +   * - ``amdgpu.lds.size.limit``
    +     - ``i32``
    +     - Error
    +     - Limits the LDS size which one work-group can use. If it is smaller
    +       than the hardware maximum, code generation uses it instead. The value
    +       is for FullSIMD mode. When not in FullSIMD mode, half of it is
    +       available.
    +
    +       Allowed values are ``65536`` and ``131072``. When the flag is absent,
    +       the hardware maximum is used.
    +

    Perhaps the semantics we want here is that amdgpu.lds.size.limit - or maybe something like amdgpu.lds.upper.bound is a guarantee that this module will not use more than N bytes of LDS for a given workgroup. Then you'd possibly use max semantics (merging two modules means the upper bound is the maximum of their upper bounds) and that the default value is "0xff_ff_ff_ff".

    krzysz00 krzysz00 commented on Oct 06, 2026 21:35 UTC
    Reviewer

    Something about the design here feels too special-case

    chinmaydd chinmaydd commented on code on Oct 06, 2026 21:44 UTC
    Code Review
    llvm/docs/AMDGPUUsage.rst:1
    @@ -1054,6 +1054,17 @@ consumed by the AMDGPU backend during code generation.
            produce an error. Modules with **any** (absent flag) are compatible
            with any setting.
     
    +   * - ``amdgpu.lds.size.limit``
    +     - ``i32``
    +     - Error
    +     - Limits the LDS size which one work-group can use. If it is smaller
    +       than the hardware maximum, code generation uses it instead. The value
    +       is for FullSIMD mode. When not in FullSIMD mode, half of it is

    Is FullSIMD the terminology we use in this document? I've heard this as WGP and CU mode.

    More generally, we are trying to get away from WGP/CU terminology and move towards half/fullSIMD. I ran into a bunch of this during some of the earlier TargetParser changes. Atleast that aspect of it makes sense to me.

    mariusz-sikora-at-amd mariusz-sikora-at-amd commented on code on Oct 07, 2026 07:24 UTC
    Code Review
    llvm/docs/AMDGPUUsage.rst:1
    @@ -1054,6 +1054,17 @@ consumed by the AMDGPU backend during code generation.
            produce an error. Modules with **any** (absent flag) are compatible
            with any setting.
     
    +   * - ``amdgpu.lds.size.limit``
    +     - ``i32``
    +     - Error
    +     - Limits the LDS size which one work-group can use. If it is smaller
    +       than the hardware maximum, code generation uses it instead. The value
    +       is for FullSIMD mode. When not in FullSIMD mode, half of it is

    I followed the full-SIMD / half-SIMD names that the TargetParser LDS functions use getLocalMemorySize(GPUKind, bool FullSIMDMode)

    mariusz-sikora-at-amd mariusz-sikora-at-amd commented on code on Oct 07, 2026 07:41 UTC
    Code Review
    llvm/docs/AMDGPUUsage.rst:1
    @@ -1054,6 +1054,17 @@ consumed by the AMDGPU backend during code generation.
            produce an error. Modules with **any** (absent flag) are compatible
            with any setting.
     
    +   * - ``amdgpu.lds.size.limit``
    +     - ``i32``
    +     - Error
    +     - Limits the LDS size which one work-group can use. If it is smaller
    +       than the hardware maximum, code generation uses it instead. The value
    +       is for FullSIMD mode. When not in FullSIMD mode, half of it is
    +       available.
    +
    +       Allowed values are ``65536`` and ``131072``. When the flag is absent,

    I guess we can make this value configurable, but in the end only few values make sense due to LDS ratio.

    arsenm arsenm requested changes on Oct 07, 2026 07:47 UTC
    Changes Requested Reviewer

    Between here and the previous review, you still haven't explained why you are trying to do this. This is also probably the wrong thing in the other direction

    mariusz-sikora-at-amd mariusz-sikora-at-amd commented on Oct 07, 2026 08:13 UTC
    Author

    Between here and the previous review, you still haven't explained why you are trying to do this. This is also probably the wrong thing in the other direction

    The hardware maximum on gfx13 is 192 KiB of LDS. The graphics driver may want to use the same total LDS as before gfx13 (128 KiB). This flag passes the LDS size to LLVM, so that occupancy is calculated correctly.

    krzysz00 krzysz00 commented on Oct 09, 2026 19:21 UTC
    Contributor

    As a note, if there's going to be an "I will only be using X bytes of LDS" mechanism ... wouldn't it make sense to have that in the binary, so that we can also check at runtime that any dynamic requests don't overshoot?

    shiltian shiltian commented on Oct 09, 2026 19:29 UTC
    Contributor

    This flag passes the LDS size to LLVM, so that occupancy is calculated correctly.

    How does this help with the occupancy calculation? The occupancy should always relate to the HW limit instead of a SW one.

    Reviewers
    Assignees
    mariusz-sikora-at-amd mariusz-sikora-at-amd
    Labels
    risk: low confidence: 89.9% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 59.8% (MEDIUM) · Higher = larger blast radius
    59.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 89.9% · Lower = rushed or shallow review
    89.9%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 6% · Safe to merge
    6%