[llvm] [AMDGPU] Remove isKernelLDS, add isKernel(const Function &). NFC. (PR #167300)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 25 06:39:15 PST 2025


https://github.com/jayfoad updated https://github.com/llvm/llvm-project/pull/167300

>From 2da20d84413c385801c20d407fb149d28dfce9d8 Mon Sep 17 00:00:00 2001
From: Jay Foad <jay.foad at amd.com>
Date: Mon, 10 Nov 2025 11:06:08 +0000
Subject: [PATCH 1/2] [AMDGPU] Remove isKernelLDS, add isKernel(const Function
 &). NFC.

Since #142598 isKernelLDS has been a pointless wrapper around isKernel.
---
 .../lib/Target/AMDGPU/AMDGPULegalizerInfo.cpp |  2 +-
 .../AMDGPU/AMDGPULowerModuleLDSPass.cpp       | 28 +++++++++----------
 llvm/lib/Target/AMDGPU/AMDGPUMemoryUtils.cpp  | 16 ++++-------
 llvm/lib/Target/AMDGPU/AMDGPUMemoryUtils.h    |  2 --
 llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp    |  2 +-
 llvm/lib/Target/AMDGPU/AMDGPUSwLowerLDS.cpp   |  6 ++--
 llvm/lib/Target/AMDGPU/SIISelLowering.cpp     |  2 +-
 llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h |  2 ++
 8 files changed, 28 insertions(+), 32 deletions(-)

diff --git a/llvm/lib/Target/AMDGPU/AMDGPULegalizerInfo.cpp b/llvm/lib/Target/AMDGPU/AMDGPULegalizerInfo.cpp
index 1a13b2226ecd6..8b4396cd63e9a 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPULegalizerInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPULegalizerInfo.cpp
@@ -7724,7 +7724,7 @@ bool AMDGPULegalizerInfo::legalizeIntrinsic(LegalizerHelper &Helper,
   case Intrinsic::amdgcn_make_buffer_rsrc:
     return legalizePointerAsRsrcIntrin(MI, MRI, B);
   case Intrinsic::amdgcn_kernarg_segment_ptr:
-    if (!AMDGPU::isKernel(B.getMF().getFunction().getCallingConv())) {
+    if (!AMDGPU::isKernel(B.getMF().getFunction())) {
       // This only makes sense to call in a kernel, so just lower to null.
       B.buildConstant(MI.getOperand(0).getReg(), 0);
       MI.eraseFromParent();
diff --git a/llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp b/llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp
index a4ef524c43466..56365284ffe32 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp
@@ -441,7 +441,7 @@ class AMDGPULowerModuleLDS {
       return KernelSet;
 
     for (Function &Func : M.functions()) {
-      if (Func.isDeclaration() || !isKernelLDS(&Func))
+      if (Func.isDeclaration() || !isKernel(Func))
         continue;
       for (GlobalVariable *GV : LDSUsesInfo.indirect_access[&Func]) {
         if (VariableSet.contains(GV)) {
@@ -555,7 +555,7 @@ class AMDGPULowerModuleLDS {
       for (Function &Func : M->functions()) {
         if (Func.isDeclaration())
           continue;
-        if (!isKernelLDS(&Func))
+        if (!isKernel(Func))
           continue;
 
         if (KernelsThatAllocateTableLDS.contains(&Func) ||
@@ -703,7 +703,7 @@ class AMDGPULowerModuleLDS {
             return false;
           }
           Function *F = I->getFunction();
-          return !isKernelLDS(F);
+          return !isKernel(*F);
         });
 
     // Replace uses of module scope variable from kernel functions that
@@ -711,7 +711,7 @@ class AMDGPULowerModuleLDS {
     // Record on each kernel whether the module scope global is used by it
 
     for (Function &Func : M.functions()) {
-      if (Func.isDeclaration() || !isKernelLDS(&Func))
+      if (Func.isDeclaration() || !isKernel(Func))
         continue;
 
       if (KernelsThatAllocateModuleLDS.contains(&Func)) {
@@ -743,7 +743,7 @@ class AMDGPULowerModuleLDS {
 
     DenseMap<Function *, LDSVariableReplacement> KernelToReplacement;
     for (Function &Func : M.functions()) {
-      if (Func.isDeclaration() || !isKernelLDS(&Func))
+      if (Func.isDeclaration() || !isKernel(Func))
         continue;
 
       DenseSet<GlobalVariable *> KernelUsedVariables;
@@ -828,7 +828,7 @@ class AMDGPULowerModuleLDS {
     // semantics. Setting the alignment here allows this IR pass to accurately
     // predict the exact constant at which it will be allocated.
 
-    assert(isKernelLDS(func));
+    assert(isKernel(*func));
 
     LLVMContext &Ctx = M.getContext();
     const DataLayout &DL = M.getDataLayout();
@@ -878,7 +878,7 @@ class AMDGPULowerModuleLDS {
       for (auto &func : OrderedKernels) {
 
         if (KernelsThatIndirectlyAllocateDynamicLDS.contains(func)) {
-          assert(isKernelLDS(func));
+          assert(isKernel(*func));
           if (!func->hasName()) {
             reportFatalUsageError("anonymous kernels cannot use LDS variables");
           }
@@ -912,7 +912,7 @@ class AMDGPULowerModuleLDS {
           auto *I = dyn_cast<Instruction>(U.getUser());
           if (!I)
             continue;
-          if (isKernelLDS(I->getFunction()))
+          if (isKernel(*I->getFunction()))
             continue;
 
           replaceUseWithTableLookup(M, Builder, table, GV, U, nullptr);
@@ -928,7 +928,7 @@ class AMDGPULowerModuleLDS {
     for (Use &U : GV->uses()) {
       if (auto *I = dyn_cast<Instruction>(U.getUser())) {
         Function *F = I->getFunction();
-        if (isKernelLDS(F) && F != KF) {
+        if (isKernel(*F) && F != KF) {
           NeedsReplacement = true;
           break;
         }
@@ -945,7 +945,7 @@ class AMDGPULowerModuleLDS {
     for (Use &U : make_early_inc_range(GV->uses())) {
       if (auto *I = dyn_cast<Instruction>(U.getUser())) {
         Function *F = I->getFunction();
-        if (!isKernelLDS(F) || F == KF) {
+        if (!isKernel(*F) || F == KF) {
           U.getUser()->replaceUsesOfWith(GV, NewGV);
         }
       }
@@ -997,7 +997,7 @@ class AMDGPULowerModuleLDS {
     std::vector<Function *> OrderedKernels;
     for (auto &K : LDSUsesInfo.direct_access) {
       Function *F = K.first;
-      assert(isKernelLDS(F));
+      assert(isKernel(*F));
       OrderedKernels.push_back(F);
     }
     OrderedKernels = sortByName(std::move(OrderedKernels));
@@ -1033,7 +1033,7 @@ class AMDGPULowerModuleLDS {
     }
     // Also erase those special LDS variables from indirect_access.
     for (auto &K : LDSUsesInfo.indirect_access) {
-      assert(isKernelLDS(K.first));
+      assert(isKernel(*K.first));
       for (GlobalVariable *GV : K.second) {
         if (isNamedBarrier(*GV))
           K.second.erase(GV);
@@ -1058,7 +1058,7 @@ class AMDGPULowerModuleLDS {
     VariableFunctionMap LDSToKernelsThatNeedToAccessItIndirectly;
     for (auto &K : LDSUsesInfo.indirect_access) {
       Function *F = K.first;
-      assert(isKernelLDS(F));
+      assert(isKernel(*F));
       for (GlobalVariable *GV : K.second) {
         LDSToKernelsThatNeedToAccessItIndirectly[GV].insert(F);
       }
@@ -1157,7 +1157,7 @@ class AMDGPULowerModuleLDS {
       const DataLayout &DL = M.getDataLayout();
 
       for (Function &Func : M.functions()) {
-        if (Func.isDeclaration() || !isKernelLDS(&Func))
+        if (Func.isDeclaration() || !isKernel(Func))
           continue;
 
         // All three of these are optional. The first variable is allocated at
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUMemoryUtils.cpp b/llvm/lib/Target/AMDGPU/AMDGPUMemoryUtils.cpp
index e17c2113ca398..f274471ffe6a4 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUMemoryUtils.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUMemoryUtils.cpp
@@ -126,7 +126,7 @@ void getUsesOfLDSByFunction(const CallGraph &CG, Module &M,
     for (User *V : GV.users()) {
       if (auto *I = dyn_cast<Instruction>(V)) {
         Function *F = I->getFunction();
-        if (isKernelLDS(F))
+        if (isKernel(*F))
           kernels[F].insert(&GV);
         else
           Functions[F].insert(&GV);
@@ -135,10 +135,6 @@ void getUsesOfLDSByFunction(const CallGraph &CG, Module &M,
   }
 }
 
-bool isKernelLDS(const Function *F) {
-  return AMDGPU::isKernel(F->getCallingConv());
-}
-
 LDSUsesInfoTy getTransitiveUsesOfLDS(const CallGraph &CG, Module &M) {
 
   FunctionVariableMap DirectMapKernel;
@@ -148,7 +144,7 @@ LDSUsesInfoTy getTransitiveUsesOfLDS(const CallGraph &CG, Module &M) {
   // Collect functions whose address has escaped
   DenseSet<Function *> AddressTakenFuncs;
   for (Function &F : M.functions()) {
-    if (!isKernelLDS(&F))
+    if (!isKernel(F))
       if (F.hasAddressTaken(nullptr,
                             /* IgnoreCallbackUses */ false,
                             /* IgnoreAssumeLikeCalls */ false,
@@ -180,7 +176,7 @@ LDSUsesInfoTy getTransitiveUsesOfLDS(const CallGraph &CG, Module &M) {
   // access all variables accessed by functions whose address escaped
   for (Function &F : M.functions()) {
     if (!F.isDeclaration() && FunctionMakesUnknownCall(&F)) {
-      if (!isKernelLDS(&F)) {
+      if (!isKernel(F)) {
         set_union(TransitiveMapFunction[&F],
                   VariablesReachableThroughFunctionPointer);
       }
@@ -190,7 +186,7 @@ LDSUsesInfoTy getTransitiveUsesOfLDS(const CallGraph &CG, Module &M) {
   // Direct implementation of collecting all variables reachable from each
   // function
   for (Function &Func : M.functions()) {
-    if (Func.isDeclaration() || isKernelLDS(&Func))
+    if (Func.isDeclaration() || isKernel(Func))
       continue;
 
     DenseSet<Function *> seen; // catches cycles
@@ -227,7 +223,7 @@ LDSUsesInfoTy getTransitiveUsesOfLDS(const CallGraph &CG, Module &M) {
   FunctionVariableMap IndirectMapKernel;
 
   for (Function &Func : M.functions()) {
-    if (Func.isDeclaration() || !isKernelLDS(&Func))
+    if (Func.isDeclaration() || !isKernel(Func))
       continue;
 
     for (const CallGraphNode::CallRecord &R : *CG[&Func]) {
@@ -335,7 +331,7 @@ void removeFnAttrFromReachable(CallGraph &CG, Function *KernelRoot,
             Function *PotentialCallee =
                 ExternalCallRecord.second->getFunction();
             assert(PotentialCallee);
-            if (!isKernelLDS(PotentialCallee)) {
+            if (!isKernel(*PotentialCallee)) {
               for (StringRef Attr : FnAttrs)
                 PotentialCallee->removeFnAttr(Attr);
             }
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUMemoryUtils.h b/llvm/lib/Target/AMDGPU/AMDGPUMemoryUtils.h
index 058e74452573c..8868b93440768 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUMemoryUtils.h
+++ b/llvm/lib/Target/AMDGPU/AMDGPUMemoryUtils.h
@@ -53,8 +53,6 @@ void getUsesOfLDSByFunction(const CallGraph &CG, Module &M,
                             FunctionVariableMap &kernels,
                             FunctionVariableMap &functions);
 
-bool isKernelLDS(const Function *F);
-
 LDSUsesInfoTy getTransitiveUsesOfLDS(const CallGraph &CG, Module &M);
 
 /// Strip FnAttr attribute from any functions where we may have
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp b/llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp
index 26e0b3dfc2e8a..38e7e17b85491 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp
@@ -350,7 +350,7 @@ bool AMDGPUSubtarget::makeLIDRangeMetadata(Instruction *I) const {
 }
 
 unsigned AMDGPUSubtarget::getImplicitArgNumBytes(const Function &F) const {
-  assert(AMDGPU::isKernel(F.getCallingConv()));
+  assert(AMDGPU::isKernel(F));
 
   // We don't allocate the segment if we know the implicit arguments weren't
   // used, even if the ABI implies we need them.
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUSwLowerLDS.cpp b/llvm/lib/Target/AMDGPU/AMDGPUSwLowerLDS.cpp
index 4a9437b37aa39..722b71b38141b 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUSwLowerLDS.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUSwLowerLDS.cpp
@@ -271,7 +271,7 @@ void AMDGPUSwLowerLDS::getNonKernelsWithLDSArguments(const CallGraph &CG) {
       Function *CalledFunc = CallerCGN->getFunction();
       if (!CalledFunc || CalledFunc->isDeclaration())
         continue;
-      if (AMDGPU::isKernelLDS(CalledFunc))
+      if (AMDGPU::isKernel(*CalledFunc))
         continue;
       for (auto AI = CalledFunc->arg_begin(), E = CalledFunc->arg_end();
            AI != E; ++AI) {
@@ -297,7 +297,7 @@ void AMDGPUSwLowerLDS::getUsesOfLDSByNonKernels() {
     for (User *V : GV->users()) {
       if (auto *I = dyn_cast<Instruction>(V)) {
         Function *F = I->getFunction();
-        if (!isKernelLDS(F) && !F->isDeclaration())
+        if (!isKernel(*F) && !F->isDeclaration())
           FuncLDSAccessInfo.NonKernelToLDSAccessMap[F].insert(GV);
       }
     }
@@ -1169,7 +1169,7 @@ bool AMDGPUSwLowerLDS::run() {
       if (!F || K.second.empty())
         continue;
 
-      assert(isKernelLDS(F));
+      assert(isKernel(*F));
 
       // Only inserts if key isn't already in the map.
       FuncLDSAccessInfo.KernelToLDSParametersMap.insert(
diff --git a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
index 8bb28084159e8..528ea73184a14 100644
--- a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
+++ b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
@@ -9737,7 +9737,7 @@ SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
                              AMDGPUFunctionArgInfo::IMPLICIT_ARG_PTR);
   }
   case Intrinsic::amdgcn_kernarg_segment_ptr: {
-    if (!AMDGPU::isKernel(MF.getFunction().getCallingConv())) {
+    if (!AMDGPU::isKernel(MF.getFunction())) {
       // This only makes sense to call in a kernel, so just lower to null.
       return DAG.getConstant(0, DL, VT);
     }
diff --git a/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h b/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h
index 5e3195b36fe4c..86b3cf767e565 100644
--- a/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h
+++ b/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h
@@ -1513,6 +1513,8 @@ constexpr inline bool isKernel(CallingConv::ID CC) {
   }
 }
 
+inline bool isKernel(const Function &F) { return isKernel(F.getCallingConv()); }
+
 LLVM_READNONE
 constexpr bool canGuaranteeTCO(CallingConv::ID CC) {
   return CC == CallingConv::Fast;

>From e971df367767bc872b92c4546b97989690e1a36f Mon Sep 17 00:00:00 2001
From: Jay Foad <jay.foad at amd.com>
Date: Tue, 25 Nov 2025 14:38:57 +0000
Subject: [PATCH 2/2] Update AMDGPULowerExecSync

---
 llvm/lib/Target/AMDGPU/AMDGPULowerExecSync.cpp | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Target/AMDGPU/AMDGPULowerExecSync.cpp b/llvm/lib/Target/AMDGPU/AMDGPULowerExecSync.cpp
index 89f6b38df9d56..38b01dc1a551c 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPULowerExecSync.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPULowerExecSync.cpp
@@ -44,7 +44,7 @@ static GlobalVariable *uniquifyGVPerKernel(Module &M, GlobalVariable *GV,
   for (Use &U : GV->uses()) {
     if (auto *I = dyn_cast<Instruction>(U.getUser())) {
       Function *F = I->getFunction();
-      if (isKernelLDS(F) && F != KF) {
+      if (isKernel(*F) && F != KF) {
         NeedsReplacement = true;
         break;
       }
@@ -61,7 +61,7 @@ static GlobalVariable *uniquifyGVPerKernel(Module &M, GlobalVariable *GV,
   for (Use &U : make_early_inc_range(GV->uses())) {
     if (auto *I = dyn_cast<Instruction>(U.getUser())) {
       Function *F = I->getFunction();
-      if (!isKernelLDS(F) || F == KF) {
+      if (!isKernel(*F) || F == KF) {
         U.getUser()->replaceUsesOfWith(GV, NewGV);
       }
     }
@@ -133,7 +133,7 @@ static bool lowerExecSyncGlobalVariables(
   SmallVector<Function *> OrderedKernels;
   for (auto &K : LDSUsesInfo.direct_access) {
     Function *F = K.first;
-    assert(isKernelLDS(F));
+    assert(isKernel(*F));
     OrderedKernels.push_back(F);
   }
   OrderedKernels = sortByName(std::move(OrderedKernels));
@@ -169,7 +169,7 @@ static bool lowerExecSyncGlobalVariables(
   }
   // Also erase those special LDS variables from indirect_access.
   for (auto &K : LDSUsesInfo.indirect_access) {
-    assert(isKernelLDS(K.first));
+    assert(isKernel(*K.first));
     for (GlobalVariable *GV : K.second) {
       if (isNamedBarrier(*GV))
         K.second.erase(GV);
@@ -191,7 +191,7 @@ static bool runLowerExecSyncGlobals(Module &M) {
   VariableFunctionMap LDSToKernelsThatNeedToAccessItIndirectly;
   for (auto &K : LDSUsesInfo.indirect_access) {
     Function *F = K.first;
-    assert(isKernelLDS(F));
+    assert(isKernel(*F));
     for (GlobalVariable *GV : K.second) {
       LDSToKernelsThatNeedToAccessItIndirectly[GV].insert(F);
     }



More information about the llvm-commits mailing list