[llvm] [AMDGPU] Fix user SGPR accounting when parsing MIR (PR #220428)
Shilei Tian via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 1 16:59:57 PDT 2026
https://github.com/shiltian created https://github.com/llvm/llvm-project/pull/220428
Count privateSegmentSize and LDSKernelId as user SGPRs to preserve the kernel
descriptor count across MIR round trips.
Fixes LCOMPILER-2700.
>From 9c6654e46484bffed3e422ab4290c7ea4a3e2d57 Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Tue, 1 Sep 2026 19:58:37 -0400
Subject: [PATCH] [AMDGPU] Fix user SGPR accounting when parsing MIR
Count privateSegmentSize and LDSKernelId as user SGPRs to preserve the kernel
descriptor count across MIR round trips.
Fixes LCOMPILER-2700.
---
.../lib/Target/AMDGPU/AMDGPUTargetMachine.cpp | 18 +++----
.../machine-function-info-user-sgpr-count.mir | 47 +++++++++++++++++++
2 files changed, 56 insertions(+), 9 deletions(-)
create mode 100644 llvm/test/CodeGen/MIR/AMDGPU/machine-function-info-user-sgpr-count.mir
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
index 544245875f8b7..9c8c867cf51d9 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
@@ -2235,10 +2235,10 @@ bool GCNTargetMachine::parseMachineFunctionInfo(
MFI->ArgInfo.FlatScratchInit, 2, 0) ||
parseAndCheckArgument(YamlMFI.ArgInfo->PrivateSegmentSize,
AMDGPU::SGPR_32RegClass,
- MFI->ArgInfo.PrivateSegmentSize, 0, 0) ||
+ MFI->ArgInfo.PrivateSegmentSize, 1, 0) ||
parseAndCheckArgument(YamlMFI.ArgInfo->LDSKernelId,
- AMDGPU::SGPR_32RegClass,
- MFI->ArgInfo.LDSKernelId, 0, 1) ||
+ AMDGPU::SGPR_32RegClass, MFI->ArgInfo.LDSKernelId,
+ 1, 0) ||
parseAndCheckArgument(YamlMFI.ArgInfo->WorkGroupIDX,
AMDGPU::SGPR_32RegClass, MFI->ArgInfo.WorkGroupIDX,
0, 1) ||
@@ -2261,14 +2261,14 @@ bool GCNTargetMachine::parseMachineFunctionInfo(
AMDGPU::SReg_64RegClass,
MFI->ArgInfo.ImplicitBufferPtr, 2, 0) ||
parseAndCheckArgument(YamlMFI.ArgInfo->WorkItemIDX,
- AMDGPU::VGPR_32RegClass,
- MFI->ArgInfo.WorkItemIDX, 0, 0) ||
+ AMDGPU::VGPR_32RegClass, MFI->ArgInfo.WorkItemIDX,
+ 0, 0) ||
parseAndCheckArgument(YamlMFI.ArgInfo->WorkItemIDY,
- AMDGPU::VGPR_32RegClass,
- MFI->ArgInfo.WorkItemIDY, 0, 0) ||
+ AMDGPU::VGPR_32RegClass, MFI->ArgInfo.WorkItemIDY,
+ 0, 0) ||
parseAndCheckArgument(YamlMFI.ArgInfo->WorkItemIDZ,
- AMDGPU::VGPR_32RegClass,
- MFI->ArgInfo.WorkItemIDZ, 0, 0)))
+ AMDGPU::VGPR_32RegClass, MFI->ArgInfo.WorkItemIDZ,
+ 0, 0)))
return true;
// Parse FirstKernArgPreloadReg separately, since it's a Register,
diff --git a/llvm/test/CodeGen/MIR/AMDGPU/machine-function-info-user-sgpr-count.mir b/llvm/test/CodeGen/MIR/AMDGPU/machine-function-info-user-sgpr-count.mir
new file mode 100644
index 0000000000000..ee4cb704a891e
--- /dev/null
+++ b/llvm/test/CodeGen/MIR/AMDGPU/machine-function-info-user-sgpr-count.mir
@@ -0,0 +1,47 @@
+# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1100 -start-after=finalize-isel %s -o - | FileCheck %s
+
+# Verify that parsing either special user SGPR preserves the total user SGPR
+# count.
+
+# CHECK-LABEL: .amdhsa_kernel private_segment_size
+# CHECK: .amdhsa_user_sgpr_count 1
+# CHECK-LABEL: .amdhsa_kernel lds_kernel_id
+# CHECK: .amdhsa_user_sgpr_count 1
+
+--- |
+ target triple = "amdgcn-amd-amdhsa"
+
+ define amdgpu_kernel void @private_segment_size() #0 {
+ ret void
+ }
+
+ define amdgpu_kernel void @lds_kernel_id() #0 {
+ ret void
+ }
+
+ attributes #0 = { "amdgpu-no-dispatch-id" "amdgpu-no-dispatch-ptr" "amdgpu-no-implicitarg-ptr" "amdgpu-no-queue-ptr" }
+...
+---
+name: private_segment_size
+tracksRegLiveness: true
+machineFunctionInfo:
+ isEntryFunction: true
+ argumentInfo:
+ privateSegmentSize: { reg: '$sgpr0' }
+body: |
+ bb.0:
+ S_ENDPGM 0
+
+...
+---
+name: lds_kernel_id
+tracksRegLiveness: true
+machineFunctionInfo:
+ isEntryFunction: true
+ argumentInfo:
+ LDSKernelId: { reg: '$sgpr0' }
+body: |
+ bb.0:
+ S_ENDPGM 0
+
+...
More information about the llvm-commits
mailing list