[clang] [CIR][CodeGen][NFC] Share AMDGCN SPIR-V HIP kernel max work-group size (PR #249471)

Arseniy Obolenskiy via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 6 11:05:53 PST 2026


https://github.com/aobolensk created https://github.com/llvm/llvm-project/pull/249471

None

>From 06ae72d932a0f46c181f2994ebf51d26b0d058fa Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Sun, 6 Dec 2026 20:05:16 +0100
Subject: [PATCH] [CIR][CodeGen][NFC] Share AMDGCN SPIR-V HIP kernel max
 work-group size

---
 clang/include/clang/CodeGenUtils/TargetUtils.h   |  5 +++++
 clang/lib/CIR/CodeGen/Targets/SPIRV.cpp          | 12 +-----------
 clang/lib/CodeGen/Targets/SPIR.cpp               | 10 +---------
 clang/lib/CodeGenUtils/TargetUtils.cpp           | 13 +++++++++++++
 clang/test/CIR/CodeGenHIP/amdgcnspirv-kernel.hip | 13 +++++++++++++
 5 files changed, 33 insertions(+), 20 deletions(-)

diff --git a/clang/include/clang/CodeGenUtils/TargetUtils.h b/clang/include/clang/CodeGenUtils/TargetUtils.h
index 822f72fbd89c2b..da275d38ef4ebd 100644
--- a/clang/include/clang/CodeGenUtils/TargetUtils.h
+++ b/clang/include/clang/CodeGenUtils/TargetUtils.h
@@ -155,6 +155,11 @@ bool requiresAMDGPUProtectedVisibility(const Decl *D, bool HasHiddenVisibility);
 /// non-generic address spaces is unspecified.
 bool spirNullPointerNeedsGenericCast(QualType QT, const llvm::Triple &Triple);
 
+/// Returns the maximum workgroup size of the HIP kernel \p FD on AMDGCN
+/// SPIR-V. amdgpu_flat_work_group_size takes precedence over a nonzero
+/// __launch_bounds__ max threads, falling back to --gpu-max-threads-per-block.
+unsigned getAMDGCNSPIRVMaxWorkGroupSize(const FunctionDecl *FD);
+
 } // namespace clang::CodeGenUtils
 
 #endif // LLVM_CLANG_CODEGENUTILS_TARGETUTILS_H
diff --git a/clang/lib/CIR/CodeGen/Targets/SPIRV.cpp b/clang/lib/CIR/CodeGen/Targets/SPIRV.cpp
index 2231d96550cdc9..e17f3272c0118b 100644
--- a/clang/lib/CIR/CodeGen/Targets/SPIRV.cpp
+++ b/clang/lib/CIR/CodeGen/Targets/SPIRV.cpp
@@ -69,17 +69,7 @@ class CommonSPIRTargetCIRGenInfo : public TargetCIRGenInfo {
     if (!fd->hasAttr<CUDAGlobalAttr>())
       return;
 
-    unsigned n = cgm.getLangOpts().GPUMaxThreadsPerBlock;
-    if (const auto *flatWGS = fd->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) {
-      n = flatWGS->getMax()
-              ->EvaluateKnownConstInt(cgm.getASTContext())
-              .getExtValue();
-    } else if (const auto *lb = fd->getAttr<CUDALaunchBoundsAttr>()) {
-      if (uint64_t maxThreads = lb->getMaxThreads()
-                                    ->EvaluateKnownConstInt(cgm.getASTContext())
-                                    .getExtValue())
-        n = maxThreads;
-    }
+    unsigned n = CodeGenUtils::getAMDGCNSPIRVMaxWorkGroupSize(fd);
 
     // Only x carries the flat WG size, reverse translated for AMDGPU targets.
     func->setAttr(
diff --git a/clang/lib/CodeGen/Targets/SPIR.cpp b/clang/lib/CodeGen/Targets/SPIR.cpp
index c9b3fb0824b659..540cbb5b0139c1 100644
--- a/clang/lib/CodeGen/Targets/SPIR.cpp
+++ b/clang/lib/CodeGen/Targets/SPIR.cpp
@@ -538,15 +538,7 @@ void SPIRVTargetCodeGenInfo::setTargetAttributes(
   if (!FD->hasAttr<CUDAGlobalAttr>())
     return;
 
-  unsigned N = M.getLangOpts().GPUMaxThreadsPerBlock;
-  if (auto FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) {
-    N = FlatWGS->getMax()->EvaluateKnownConstInt(M.getContext()).getExtValue();
-  } else if (auto LB = FD->getAttr<CUDALaunchBoundsAttr>()) {
-    if (uint64_t MaxThreads = LB->getMaxThreads()
-                                  ->EvaluateKnownConstInt(M.getContext())
-                                  .getExtValue())
-      N = MaxThreads;
-  }
+  unsigned N = CodeGenUtils::getAMDGCNSPIRVMaxWorkGroupSize(FD);
 
   // We encode the maximum flat WG size in the first component of the 3D
   // max_work_group_size attribute, which will get reverse translated into the
diff --git a/clang/lib/CodeGenUtils/TargetUtils.cpp b/clang/lib/CodeGenUtils/TargetUtils.cpp
index c87ed7349282de..c5ed967df79e3a 100644
--- a/clang/lib/CodeGenUtils/TargetUtils.cpp
+++ b/clang/lib/CodeGenUtils/TargetUtils.cpp
@@ -7,8 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/CodeGenUtils/TargetUtils.h"
+#include "clang/AST/ASTContext.h"
 #include "clang/AST/Attr.h"
 #include "clang/AST/Decl.h"
+#include "clang/AST/Expr.h"
 #include "clang/AST/Type.h"
 #include "clang/Basic/AddressSpaces.h"
 #include "clang/Basic/Builtins.h"
@@ -225,4 +227,15 @@ bool spirNullPointerNeedsGenericCast(QualType QT, const llvm::Triple &Triple) {
            toTargetAddressSpace(AS) == SPIRVCodeSectionINTELAddrSpace);
 }
 
+unsigned getAMDGCNSPIRVMaxWorkGroupSize(const FunctionDecl *FD) {
+  const ASTContext &Ctx = FD->getASTContext();
+  if (const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>())
+    return FlatWGS->getMax()->EvaluateKnownConstInt(Ctx).getExtValue();
+  if (const auto *LB = FD->getAttr<CUDALaunchBoundsAttr>())
+    if (uint64_t MaxThreads =
+            LB->getMaxThreads()->EvaluateKnownConstInt(Ctx).getExtValue())
+      return MaxThreads;
+  return Ctx.getLangOpts().GPUMaxThreadsPerBlock;
+}
+
 } // namespace clang::CodeGenUtils
diff --git a/clang/test/CIR/CodeGenHIP/amdgcnspirv-kernel.hip b/clang/test/CIR/CodeGenHIP/amdgcnspirv-kernel.hip
index dfd66d0d8f3dba..151c87446fad7c 100644
--- a/clang/test/CIR/CodeGenHIP/amdgcnspirv-kernel.hip
+++ b/clang/test/CIR/CodeGenHIP/amdgcnspirv-kernel.hip
@@ -7,6 +7,10 @@
 // RUN:            -fcuda-is-device -emit-llvm %s -o %t.ll
 // RUN: FileCheck --check-prefixes=LLVM,LLVM-DEFAULT %s --input-file=%t.ll
 
+// RUN: %clang_cc1 -triple spirv64-amd-amdhsa -x hip \
+// RUN:            -fcuda-is-device -emit-llvm %s -o %t.ogcg.ll
+// RUN: FileCheck --check-prefixes=LLVM,LLVM-DEFAULT %s --input-file=%t.ogcg.ll
+
 // RUN: %clang_cc1 -triple spirv64-amd-amdhsa -x hip -fclangir \
 // RUN:            --gpu-max-threads-per-block=256 \
 // RUN:            -fcuda-is-device -emit-cir %s -o %t.max256.cir
@@ -17,6 +21,11 @@
 // RUN:            -fcuda-is-device -emit-llvm %s -o %t.max256.ll
 // RUN: FileCheck --check-prefixes=LLVM,LLVM-MAX256 %s --input-file=%t.max256.ll
 
+// RUN: %clang_cc1 -triple spirv64-amd-amdhsa -x hip \
+// RUN:            --gpu-max-threads-per-block=256 \
+// RUN:            -fcuda-is-device -emit-llvm %s -o %t.max256.ogcg.ll
+// RUN: FileCheck --check-prefixes=LLVM,LLVM-MAX256 %s --input-file=%t.max256.ogcg.ll
+
 // RUN: %clang_cc1 -triple spirv64 -x hip -fclangir \
 // RUN:            -fcuda-is-device -emit-cir %s -o %t.noamd.cir
 // RUN: FileCheck --check-prefix=NOAMD %s --input-file=%t.noamd.cir
@@ -25,6 +34,10 @@
 // RUN:            -fcuda-is-device -emit-llvm %s -o %t.noamd.ll
 // RUN: FileCheck --check-prefix=NOAMD %s --input-file=%t.noamd.ll
 
+// RUN: %clang_cc1 -triple spirv64 -x hip \
+// RUN:            -fcuda-is-device -emit-llvm %s -o %t.noamd.ogcg.ll
+// RUN: FileCheck --check-prefix=NOAMD %s --input-file=%t.noamd.ogcg.ll
+
 // RUN: %clang_cc1 -triple spir64-amd-unknown -x hip -fclangir \
 // RUN:            -fcuda-is-device -emit-cir %s -o %t.spir.cir
 // RUN: FileCheck --check-prefix=NOAMD %s --input-file=%t.spir.cir



More information about the cfe-commits mailing list