[llvm] AMDGPU: share LDS budget logic and add experimental LDS buffering pass (PR #166388)

via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 4 07:51:11 PST 2025


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff origin/main HEAD --extensions h,cpp -- llvm/lib/Target/AMDGPU/AMDGPULDSBuffering.cpp llvm/lib/Target/AMDGPU/Utils/AMDGPULDSUtils.cpp llvm/lib/Target/AMDGPU/Utils/AMDGPULDSUtils.h llvm/lib/Target/AMDGPU/AMDGPU.h llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp --diff_from_common_commit
``````````

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/llvm/lib/Target/AMDGPU/AMDGPULDSBuffering.cpp b/llvm/lib/Target/AMDGPU/AMDGPULDSBuffering.cpp
index 95a755788..91e7823a7 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPULDSBuffering.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPULDSBuffering.cpp
@@ -36,16 +36,16 @@
 #include "Utils/AMDGPUBaseInfo.h"
 #include "Utils/AMDGPULDSUtils.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
 #include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/Instructions.h"
 #include "llvm/IR/IntrinsicsAMDGPU.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/IR/PatternMatch.h"
-#include "llvm/IR/Instructions.h"
-#include "llvm/CodeGen/TargetPassConfig.h"
 #include "llvm/InitializePasses.h"
 #include "llvm/Pass.h"
-#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Alignment.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Target/TargetMachine.h"
 
@@ -55,9 +55,10 @@ using namespace llvm;
 
 namespace {
 
-static cl::opt<unsigned> LDSBufferingMaxBytes(
-    "amdgpu-lds-buffering-max-bytes",
-    cl::desc("Max byte size for LDS buffering candidates"), cl::init(64));
+static cl::opt<unsigned>
+    LDSBufferingMaxBytes("amdgpu-lds-buffering-max-bytes",
+                         cl::desc("Max byte size for LDS buffering candidates"),
+                         cl::init(64));
 
 class AMDGPULDSBufferingImpl {
   const TargetMachine &TM;
@@ -70,8 +71,7 @@ public:
   AMDGPULDSBufferingImpl(const TargetMachine &TM) : TM(TM) {}
 
   bool run(Function &F) {
-    LLVM_DEBUG(dbgs() << "[LDSBuffer] Visit function: " << F.getName()
-                      << '\n');
+    LLVM_DEBUG(dbgs() << "[LDSBuffer] Visit function: " << F.getName() << '\n');
     const Triple &TT = TM.getTargetTriple();
     if (!TT.isAMDGCN())
       return false;
@@ -127,11 +127,11 @@ public:
         if (SPtr->stripPointerCasts() != Ptr->stripPointerCasts())
           continue;
 
-    TypeSize TS = DL->getTypeStoreSize(ValTy);
-    if (TS.isScalable())
+        TypeSize TS = DL->getTypeStoreSize(ValTy);
+        if (TS.isScalable())
           continue;
-    uint64_t Size = TS.getFixedValue();
-    if (Size == 0 || Size > LDSBufferingMaxBytes)
+        uint64_t Size = TS.getFixedValue();
+        if (Size == 0 || Size > LDSBufferingMaxBytes)
           continue;
         Align LoadAlign = LI->getAlign();
         Align MinAlign = Align(16);
@@ -141,10 +141,10 @@ public:
         // Create LDS slot near the load and emit memcpy global->LDS.
         LLVM_DEBUG({
           dbgs() << "[LDSBuffer] Candidate found: load->store same ptr in "
-                  << F.getName() << '\n';
-          dbgs() << "            size=" << Size << "B, align="
-                  << LoadAlign.value() << ", ptr AS="
-                  << PtrTy->getAddressSpace() << "\n";
+                 << F.getName() << '\n';
+          dbgs() << "            size=" << Size
+                 << "B, align=" << LoadAlign.value()
+                 << ", ptr AS=" << PtrTy->getAddressSpace() << "\n";
         });
         IRBuilder<> BLoad(LI);
         Align Alignment = LoadAlign;
@@ -257,7 +257,8 @@ private:
     return TID;
   }
 
-  // Create an LDS array [WGSize x ElemTy] and return pointer to per-thread slot.
+  // Create an LDS array [WGSize x ElemTy] and return pointer to per-thread
+  // slot.
   std::pair<GlobalVariable *, Value *>
   createLDSGlobalAndThreadSlot(Function &F, Type *ElemTy, Align Alignment,
                                StringRef BaseName, IRBuilder<> &Builder) {
@@ -266,15 +267,15 @@ private:
     Type *ArrTy = ArrayType::get(ElemTy, WorkGroupSize);
     GlobalVariable *GV = new GlobalVariable(
         *Mod, ArrTy, /*isConstant=*/false, GlobalValue::InternalLinkage,
-        PoisonValue::get(ArrTy), (F.getName() + "." + BaseName).str(),
-        nullptr, GlobalVariable::NotThreadLocal, AMDGPUAS::LOCAL_ADDRESS);
+        PoisonValue::get(ArrTy), (F.getName() + "." + BaseName).str(), nullptr,
+        GlobalVariable::NotThreadLocal, AMDGPUAS::LOCAL_ADDRESS);
     GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
     GV->setAlignment(Alignment);
 
     LLVM_DEBUG({
       dbgs() << "[LDSBuffer] Create LDS global: name=" << GV->getName()
-              << ", elemTy=" << *ElemTy << ", WGSize=" << WorkGroupSize
-              << ", align=" << Alignment.value() << '\n';
+             << ", elemTy=" << *ElemTy << ", WGSize=" << WorkGroupSize
+             << ", align=" << Alignment.value() << '\n';
     });
 
     Value *LinearTID = buildLinearThreadId(Builder);
@@ -288,8 +289,8 @@ private:
 
 } // end anonymous namespace
 
-PreservedAnalyses
-AMDGPULDSBufferingPass::run(Function &F, FunctionAnalysisManager &AM) {
+PreservedAnalyses AMDGPULDSBufferingPass::run(Function &F,
+                                              FunctionAnalysisManager &AM) {
   bool Changed = AMDGPULDSBufferingImpl(TM).run(F);
   if (!Changed)
     return PreservedAnalyses::all();
diff --git a/llvm/lib/Target/AMDGPU/Utils/AMDGPULDSUtils.cpp b/llvm/lib/Target/AMDGPU/Utils/AMDGPULDSUtils.cpp
index 5f5dc9f06..b7b51056a 100644
--- a/llvm/lib/Target/AMDGPU/Utils/AMDGPULDSUtils.cpp
+++ b/llvm/lib/Target/AMDGPU/Utils/AMDGPULDSUtils.cpp
@@ -17,11 +17,11 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/IR/Constants.h"
 #include "llvm/IR/DataLayout.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/GlobalVariable.h"
 #include "llvm/IR/Instructions.h"
-#include "llvm/IR/Constants.h"
 #include "llvm/IR/Module.h"
 #include "llvm/Support/Alignment.h"
 
@@ -98,8 +98,8 @@ AMDGPULDSBudget llvm::computeLDSBudget(const Function &F,
   AllocatedSizes.reserve(UsedLDS.size());
 
   for (const GlobalVariable *GV : UsedLDS) {
-    Align Alignment = DL.getValueOrABITypeAlignment(GV->getAlign(),
-                                                    GV->getValueType());
+    Align Alignment =
+        DL.getValueOrABITypeAlignment(GV->getAlign(), GV->getValueType());
     uint64_t AllocSize = DL.getTypeAllocSize(GV->getValueType());
 
     // HIP uses an extern unsized array in local address space for dynamically

``````````

</details>


https://github.com/llvm/llvm-project/pull/166388


More information about the llvm-commits mailing list