[llvm] [AMDGPU] Validate user SGPR count against HW range, not field width (PR #194449)
Mariusz Sikora via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 27 23:19:34 PDT 2026
https://github.com/mariusz-sikora-at-amd updated https://github.com/llvm/llvm-project/pull/194449
>From 538090ca459118c36f07eed12a5a3da1204e7073 Mon Sep 17 00:00:00 2001
From: Mariusz Sikora <mariusz.sikora at amd.com>
Date: Mon, 27 Apr 2026 15:20:14 -0400
Subject: [PATCH 1/2] [AMDGPU] Validate user SGPR count against HW range, not
field width
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
The previous validation checked only the field width,
allowing values that exceeded the actual hardware limits
(e.g. 0–16 on gfx6-gfx120 and 0–32 on gfx125x) as long
as they fit in the bit width.
Tighten validation to reject out-of-range user SGPR counts.
---
.../Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp | 7 ++-----
llvm/test/MC/AMDGPU/hsa-gfx12-v4-user-sgpr-err.s | 13 +++++++++++++
llvm/test/MC/AMDGPU/hsa-gfx125x-v4-user-sgpr-err.s | 14 ++++++++++++++
3 files changed, 29 insertions(+), 5 deletions(-)
create mode 100644 llvm/test/MC/AMDGPU/hsa-gfx12-v4-user-sgpr-err.s
create mode 100644 llvm/test/MC/AMDGPU/hsa-gfx125x-v4-user-sgpr-err.s
diff --git a/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp b/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
index 103372d6e4c5c..987585fea8a6b 100644
--- a/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
+++ b/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
@@ -6357,6 +6357,8 @@ bool AMDGPUAsmParser::ParseDirectiveAMDHSAKernel() {
return TokError(".amdhsa_next_free_sgpr directive is required");
unsigned UserSGPRCount = ExplicitUserSGPRCount.value_or(ImpliedUserSGPRCount);
+ if (UserSGPRCount > getMaxNumUserSGPRs())
+ return TokError("too many user SGPRs enabled");
// Consider the case where the total number of UserSGPRs with trailing
// allocated preload SGPRs, is greater than the number of explicitly
@@ -6404,17 +6406,12 @@ bool AMDGPUAsmParser::ParseDirectiveAMDHSAKernel() {
"enabled user SGPRs");
if (isGFX1250Plus()) {
- if (!isUInt<COMPUTE_PGM_RSRC2_GFX125_USER_SGPR_COUNT_WIDTH>(UserSGPRCount))
- return TokError("too many user SGPRs enabled");
AMDGPU::MCKernelDescriptor::bits_set(
KD.compute_pgm_rsrc2,
MCConstantExpr::create(UserSGPRCount, getContext()),
COMPUTE_PGM_RSRC2_GFX125_USER_SGPR_COUNT_SHIFT,
COMPUTE_PGM_RSRC2_GFX125_USER_SGPR_COUNT, getContext());
} else {
- if (!isUInt<COMPUTE_PGM_RSRC2_GFX6_GFX120_USER_SGPR_COUNT_WIDTH>(
- UserSGPRCount))
- return TokError("too many user SGPRs enabled");
AMDGPU::MCKernelDescriptor::bits_set(
KD.compute_pgm_rsrc2,
MCConstantExpr::create(UserSGPRCount, getContext()),
diff --git a/llvm/test/MC/AMDGPU/hsa-gfx12-v4-user-sgpr-err.s b/llvm/test/MC/AMDGPU/hsa-gfx12-v4-user-sgpr-err.s
new file mode 100644
index 0000000000000..8519672f15de1
--- /dev/null
+++ b/llvm/test/MC/AMDGPU/hsa-gfx12-v4-user-sgpr-err.s
@@ -0,0 +1,13 @@
+// RUN: not llvm-mc -triple amdgcn-amd-amdhsa -mcpu=gfx1200 -filetype=null %s 2>&1 | FileCheck --check-prefix=ERR %s
+
+.text
+
+.amdhsa_kernel complete
+// user_sgpr_count range: 0-16
+// ERR: error: too many user SGPRs enabled
+ .amdhsa_user_sgpr_count 17
+
+ .amdhsa_next_free_vgpr 9
+ .amdhsa_next_free_sgpr 27
+.end_amdhsa_kernel
+
diff --git a/llvm/test/MC/AMDGPU/hsa-gfx125x-v4-user-sgpr-err.s b/llvm/test/MC/AMDGPU/hsa-gfx125x-v4-user-sgpr-err.s
new file mode 100644
index 0000000000000..55567f9142ccf
--- /dev/null
+++ b/llvm/test/MC/AMDGPU/hsa-gfx125x-v4-user-sgpr-err.s
@@ -0,0 +1,14 @@
+// RUN: not llvm-mc -triple amdgcn-amd-amdhsa -mcpu=gfx1250 -filetype=null %s 2>&1 | FileCheck --check-prefix=ERR %s
+// RUN: not llvm-mc -triple amdgcn-amd-amdhsa -mcpu=gfx1251 -filetype=null %s 2>&1 | FileCheck --check-prefix=ERR %s
+
+.text
+
+.amdhsa_kernel complete
+// user_sgpr_count range: 0-32
+// ERR: error: too many user SGPRs enabled
+ .amdhsa_user_sgpr_count 33
+
+ .amdhsa_next_free_vgpr 9
+ .amdhsa_next_free_sgpr 27
+.end_amdhsa_kernel
+
>From 0ad22a23cd1a5fb5acf9c0c047bc6c6e1063c598 Mon Sep 17 00:00:00 2001
From: Mariusz Sikora <mariusz.sikora at amd.com>
Date: Tue, 28 Apr 2026 02:04:39 -0400
Subject: [PATCH 2/2] Update error msg and code format
---
llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp | 4 +++-
llvm/test/MC/AMDGPU/hsa-gfx12-v4-user-sgpr-err.s | 3 +--
llvm/test/MC/AMDGPU/hsa-gfx125x-v4-user-sgpr-err.s | 3 +--
3 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp b/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
index 987585fea8a6b..fe2cad3fcb053 100644
--- a/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
+++ b/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
@@ -6358,7 +6358,9 @@ bool AMDGPUAsmParser::ParseDirectiveAMDHSAKernel() {
unsigned UserSGPRCount = ExplicitUserSGPRCount.value_or(ImpliedUserSGPRCount);
if (UserSGPRCount > getMaxNumUserSGPRs())
- return TokError("too many user SGPRs enabled");
+ return TokError("too many user SGPRs enabled, found " +
+ Twine(UserSGPRCount) + ", but only " +
+ Twine(getMaxNumUserSGPRs()) + " are supported.");
// Consider the case where the total number of UserSGPRs with trailing
// allocated preload SGPRs, is greater than the number of explicitly
diff --git a/llvm/test/MC/AMDGPU/hsa-gfx12-v4-user-sgpr-err.s b/llvm/test/MC/AMDGPU/hsa-gfx12-v4-user-sgpr-err.s
index 8519672f15de1..785166a93a23f 100644
--- a/llvm/test/MC/AMDGPU/hsa-gfx12-v4-user-sgpr-err.s
+++ b/llvm/test/MC/AMDGPU/hsa-gfx12-v4-user-sgpr-err.s
@@ -3,8 +3,7 @@
.text
.amdhsa_kernel complete
-// user_sgpr_count range: 0-16
-// ERR: error: too many user SGPRs enabled
+// ERR: error: too many user SGPRs enabled, found 17, but only 16 are supported.
.amdhsa_user_sgpr_count 17
.amdhsa_next_free_vgpr 9
diff --git a/llvm/test/MC/AMDGPU/hsa-gfx125x-v4-user-sgpr-err.s b/llvm/test/MC/AMDGPU/hsa-gfx125x-v4-user-sgpr-err.s
index 55567f9142ccf..a9ba4c90498b1 100644
--- a/llvm/test/MC/AMDGPU/hsa-gfx125x-v4-user-sgpr-err.s
+++ b/llvm/test/MC/AMDGPU/hsa-gfx125x-v4-user-sgpr-err.s
@@ -4,8 +4,7 @@
.text
.amdhsa_kernel complete
-// user_sgpr_count range: 0-32
-// ERR: error: too many user SGPRs enabled
+// ERR: error: too many user SGPRs enabled, found 33, but only 32 are supported.
.amdhsa_user_sgpr_count 33
.amdhsa_next_free_vgpr 9
More information about the llvm-commits
mailing list