[llvm-branch-commits] [llvm] AMDGPU: Include callees in backend_stack_size PAL metadata (PR #234522)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Oct 22 01:01:58 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-amdgpu
Author: Diana Picus (rovka)
<details>
<summary>Changes</summary>
The stack size reported in the PAL metadata of a chain function should include the stack sizes of any callees, since the consumers don't have access to the call graph (this is especially important for whole wave functions, because we don't even emit any PAL metadata for them). The consumers are generally expected to allocate scratch based on the maximum `.backend_stack_size` emitted for the functions in a pipeline. They will also have to deal with recursion and indirect calls.
The `.stack_frame_size_in_bytes` metadata remains unchanged, i.e. it represents just the stack size of the current function (without its callees).
Assisted-by: AI
---
Full diff: https://github.com/llvm/llvm-project/pull/234522.diff
8 Files Affected:
- (modified) llvm/docs/AMDGPUUsage.rst (+6-1)
- (modified) llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp (+2-1)
- (modified) llvm/lib/Target/AMDGPU/Utils/AMDGPUPALMetadata.cpp (+9-3)
- (modified) llvm/lib/Target/AMDGPU/Utils/AMDGPUPALMetadata.h (+8-2)
- (modified) llvm/test/CodeGen/AMDGPU/amdpal-callable.ll (+2-2)
- (modified) llvm/test/CodeGen/AMDGPU/amdpal-chain-metadata.ll (+56-4)
- (modified) llvm/test/CodeGen/AMDGPU/pal-metadata-3.0-callable-dvgpr.ll (+2-2)
- (modified) llvm/test/CodeGen/AMDGPU/pal-metadata-3.0-callable.ll (+2-2)
``````````diff
diff --git a/llvm/docs/AMDGPUUsage.rst b/llvm/docs/AMDGPUUsage.rst
index 3de94d5617ac7b..53cbb657ef9811 100644
--- a/llvm/docs/AMDGPUUsage.rst
+++ b/llvm/docs/AMDGPUUsage.rst
@@ -20999,7 +20999,12 @@ within a map that has been added by the same *vendor-name*.
".lds_size" integer Size in bytes of LDS memory.
".vgpr_count" integer Number of VGPRs used by the shader.
".sgpr_count" integer Number of SGPRs used by the shader.
- ".stack_frame_size_in_bytes" integer Amount of stack size used by the shader.
+ ".stack_frame_size_in_bytes" integer Amount of stack size used by the shader itself, not including
+ the stack used by any of its callees.
+ ".backend_stack_size" integer Amount of stack size used by the shader, including the stack
+ used by its known callees. Chain callees are excluded, since they
+ reuse the caller's stack frame. Indirect calls and recursion do
+ not contribute.
".shader_subtype" string Shader subtype/kind. Values include:
- "Unknown"
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
index f2761dc9755b76..9b326a12521c36 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
@@ -1815,7 +1815,8 @@ void AMDGPUAsmPrinter::emitPALFunctionMetadata(const MachineFunction &MF) {
auto *MD = getTargetStreamer()->getPALMetadata();
const MachineFrameInfo &MFI = MF.getFrameInfo();
StringRef FnName = MF.getFunction().getName();
- MD->setFunctionScratchSize(FnName, MFI.getStackSize());
+ MD->setFunctionStackFrameSize(FnName, MFI.getStackSize());
+ MD->setFunctionBackendStackSize(FnName, CurrentProgramInfo.ScratchSize);
const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
MCContext &Ctx = MF.getContext();
diff --git a/llvm/lib/Target/AMDGPU/Utils/AMDGPUPALMetadata.cpp b/llvm/lib/Target/AMDGPU/Utils/AMDGPUPALMetadata.cpp
index 14a9a217c01cc0..33d0c5e7cbc7a9 100644
--- a/llvm/lib/Target/AMDGPU/Utils/AMDGPUPALMetadata.cpp
+++ b/llvm/lib/Target/AMDGPU/Utils/AMDGPUPALMetadata.cpp
@@ -366,11 +366,17 @@ void AMDGPUPALMetadata::setScratchSize(unsigned CC, const MCExpr *Val,
setHwStage(CC, ".scratch_memory_size", msgpack::Type::UInt, Val);
}
-// Set the stack frame size of a function in the metadata.
-void AMDGPUPALMetadata::setFunctionScratchSize(StringRef FnName, unsigned Val) {
+void AMDGPUPALMetadata::setFunctionStackFrameSize(StringRef FnName,
+ unsigned Val) {
auto Node = getShaderFunction(FnName);
Node[".stack_frame_size_in_bytes"] = MsgPackDoc.getNode(Val);
- Node[".backend_stack_size"] = MsgPackDoc.getNode(Val);
+}
+
+void AMDGPUPALMetadata::setFunctionBackendStackSize(StringRef FnName,
+ const MCExpr *Val) {
+ msgpack::MapDocNode Node = getShaderFunction(FnName);
+ DelayedExprs.assignDocNode(Node[".backend_stack_size"], msgpack::Type::UInt,
+ Val);
}
// Set the amount of LDS used in bytes in the metadata.
diff --git a/llvm/lib/Target/AMDGPU/Utils/AMDGPUPALMetadata.h b/llvm/lib/Target/AMDGPU/Utils/AMDGPUPALMetadata.h
index 6669cf387c5b56..e619d140fd8395 100644
--- a/llvm/lib/Target/AMDGPU/Utils/AMDGPUPALMetadata.h
+++ b/llvm/lib/Target/AMDGPU/Utils/AMDGPUPALMetadata.h
@@ -103,8 +103,14 @@ class AMDGPUPALMetadata {
void setScratchSize(unsigned CC, unsigned Val);
void setScratchSize(unsigned CC, const MCExpr *Val, MCContext &Ctx);
- // Set the stack frame size of a function in the metadata.
- void setFunctionScratchSize(StringRef FnName, unsigned Val);
+ // Set the stack frame size of a function in the metadata. This only covers
+ // the frame of the function itself, not the frames of any of its callees.
+ void setFunctionStackFrameSize(StringRef FnName, unsigned Val);
+
+ // Set the backend stack size metadata, including the frames of its known
+ // calles. Recursion and indirect/external calls must be handled by the
+ // consumer.
+ void setFunctionBackendStackSize(StringRef FnName, const MCExpr *Val);
// Set the amount of LDS used in bytes in the metadata. This is an optional
// advisory record for logging etc; wave dispatch actually uses the rsrc1
diff --git a/llvm/test/CodeGen/AMDGPU/amdpal-callable.ll b/llvm/test/CodeGen/AMDGPU/amdpal-callable.ll
index 20e9842d5e62b8..14bb9dc9da29e2 100644
--- a/llvm/test/CodeGen/AMDGPU/amdpal-callable.ll
+++ b/llvm/test/CodeGen/AMDGPU/amdpal-callable.ll
@@ -171,7 +171,7 @@ attributes #0 = { nounwind }
; GCN-NEXT: .stack_frame_size_in_bytes: 0{{$}}
; GCN-NEXT: .vgpr_count: 0x1{{$}}
; GCN-NEXT: no_stack_call:
-; GCN-NEXT: .backend_stack_size: 0x10{{$}}
+; GCN-NEXT: .backend_stack_size: 0x24{{$}}
; GCN-NEXT: .lds_size: 0{{$}}
; GCN-NEXT: .sgpr_count: 0x25{{$}}
; GCN-NEXT: .stack_frame_size_in_bytes: 0x10{{$}}
@@ -216,7 +216,7 @@ attributes #0 = { nounwind }
; GCN-NEXT: .stack_frame_size_in_bytes: 0x14{{$}}
; GCN-NEXT: .vgpr_count: 0x2{{$}}
; GCN-NEXT: simple_stack_call:
-; GCN-NEXT: .backend_stack_size: 0x20{{$}}
+; GCN-NEXT: .backend_stack_size: 0x34{{$}}
; GCN-NEXT: .lds_size: 0{{$}}
; GCN-NEXT: .sgpr_count: 0x25{{$}}
; GCN-NEXT: .stack_frame_size_in_bytes: 0x20{{$}}
diff --git a/llvm/test/CodeGen/AMDGPU/amdpal-chain-metadata.ll b/llvm/test/CodeGen/AMDGPU/amdpal-chain-metadata.ll
index d972dd7884902e..9d8da278ac79c8 100644
--- a/llvm/test/CodeGen/AMDGPU/amdpal-chain-metadata.ll
+++ b/llvm/test/CodeGen/AMDGPU/amdpal-chain-metadata.ll
@@ -1,4 +1,4 @@
-; RUN: llc -mtriple=amdgpu11.00--amdpal <%s | FileCheck %s
+; RUN: llc -mtriple=amdgpu11.00--amdpal < %s | FileCheck %s
; CHECK-LABEL: {{^}}amdgpu_cs_chain_func:
; CHECK: .set .Lamdgpu_cs_chain_func.private_seg_size, 16{{$}}
@@ -28,6 +28,19 @@
; CHECK: .set .Lchain_indirect_and_wwf.private_seg_size, 64+max(.Lwwf_alloca.private_seg_size){{$}}
; CHECK: .set .Lchain_indirect_and_wwf.has_indirect_call, 1{{$}}
+; CHECK-LABEL: {{^}}chain_indirect_gfx_and_wwf:
+; CHECK: .set .Lchain_indirect_gfx_and_wwf.private_seg_size, 64+max(.Lwwf_alloca.private_seg_size){{$}}
+; CHECK: .set .Lchain_indirect_gfx_and_wwf.has_indirect_call, 1{{$}}
+
+; CHECK-LABEL: {{^}}gfx_stack_only:
+; CHECK: .set .Lgfx_stack_only.private_seg_size, 20{{$}}
+
+; CHECK-LABEL: {{^}}wwf_callee_stack_only:
+; CHECK: .set .Lwwf_callee_stack_only.private_seg_size, 16+max(.Lgfx_stack_only.private_seg_size){{$}}
+
+; CHECK-LABEL: {{^}}chain_own_and_callee_stack:
+; CHECK: .set .Lchain_own_and_callee_stack.private_seg_size, 32+max(.Lwwf_callee_stack_only.private_seg_size){{$}}
+
; CHECK-LABEL: .amdgpu_pal_metadata
; CHECK-NEXT: ---
; CHECK-NEXT: amdpal.pipelines:
@@ -41,10 +54,10 @@
; CHECK: .backend_stack_size: 0x10{{$}}
; CHECK: .stack_frame_size_in_bytes: 0x10{{$}}
; CHECK-LABEL: chain_and_gfx:
-; CHECK: .backend_stack_size: 0x20{{$}}
+; CHECK: .backend_stack_size: 0x3c{{$}}
; CHECK: .stack_frame_size_in_bytes: 0x20{{$}}
; CHECK-LABEL: chain_and_wwf:
-; CHECK: .backend_stack_size: 0x50{{$}}
+; CHECK: .backend_stack_size: 0x6c{{$}}
; CHECK: .stack_frame_size_in_bytes: 0x50{{$}}
; CHECK-LABEL: chain_callee_larger_stack:
; CHECK: .backend_stack_size: 0x8{{$}}
@@ -53,11 +66,20 @@
; CHECK: .backend_stack_size: 0x24{{$}}
; CHECK: .stack_frame_size_in_bytes: 0x24{{$}}
; CHECK-LABEL: chain_indirect_and_wwf:
-; CHECK: .backend_stack_size: 0x40{{$}}
+; CHECK: .backend_stack_size: 0x5c{{$}}
+; CHECK: .stack_frame_size_in_bytes: 0x40{{$}}
+; CHECK-LABEL: chain_indirect_gfx_and_wwf:
+; CHECK: .backend_stack_size: 0x5c{{$}}
; CHECK: .stack_frame_size_in_bytes: 0x40{{$}}
+; CHECK-LABEL: chain_own_and_callee_stack:
+; CHECK: .backend_stack_size: 0x44{{$}}
+; CHECK: .stack_frame_size_in_bytes: 0x20{{$}}
; CHECK-LABEL: gfx_alloca:
; CHECK: .backend_stack_size: 0x1c{{$}}
; CHECK: .stack_frame_size_in_bytes: 0x1c{{$}}
+; CHECK-LABEL: gfx_stack_only:
+; CHECK: .backend_stack_size: 0x14{{$}}
+; CHECK: .stack_frame_size_in_bytes: 0x14{{$}}
; CHECK:amdpal.version:
; CHECK-NEXT: - 0x3
; CHECK-NEXT: - 0
@@ -137,11 +159,41 @@ define amdgpu_cs_chain void @chain_indirect_and_wwf(ptr inreg %callee, <40 x i32
unreachable
}
+define amdgpu_cs_chain void @chain_indirect_gfx_and_wwf(ptr inreg %callee) {
+.entry:
+ %a = alloca [12 x i32], addrspace(5)
+ store i32 11, ptr addrspace(5) %a
+ call void (ptr, ...) @llvm.amdgcn.call.whole.wave(ptr @wwf_alloca, ptr addrspace(5) %a)
+ call amdgpu_gfx void %callee()
+ ret void
+}
+
define amdgpu_cs_chain_preserve void @amdgpu_cs_chain_preserve_func() {
.entry:
ret void
}
+; Stack sizes accumulate transitively through non-chain callees, across calling
+; conventions.
+define amdgpu_gfx void @gfx_stack_only() {
+ %a = alloca [4 x i32], addrspace(5)
+ store volatile i32 12, ptr addrspace(5) %a
+ ret void
+}
+
+define amdgpu_gfx_whole_wave void @wwf_callee_stack_only(i1 %active) {
+ call amdgpu_gfx void @gfx_stack_only()
+ ret void
+}
+
+define amdgpu_cs_chain void @chain_own_and_callee_stack() {
+.entry:
+ %a = alloca [4 x i32], addrspace(5)
+ store volatile i32 13, ptr addrspace(5) %a
+ call void (ptr, ...) @llvm.amdgcn.call.whole.wave(ptr @wwf_callee_stack_only)
+ ret void
+}
+
declare void @llvm.amdgcn.call.whole.wave(ptr, ...)
declare void @llvm.amdgcn.cs.chain.p0.i32.s.v40i32(ptr, i32, {}, <40 x i32>, i32 immarg, ...)
diff --git a/llvm/test/CodeGen/AMDGPU/pal-metadata-3.0-callable-dvgpr.ll b/llvm/test/CodeGen/AMDGPU/pal-metadata-3.0-callable-dvgpr.ll
index 1c570d6d21358a..95bea92f331e96 100644
--- a/llvm/test/CodeGen/AMDGPU/pal-metadata-3.0-callable-dvgpr.ll
+++ b/llvm/test/CodeGen/AMDGPU/pal-metadata-3.0-callable-dvgpr.ll
@@ -91,7 +91,7 @@
; CHECK-NEXT: .stack_frame_size_in_bytes: 0
; CHECK-NEXT: .vgpr_count: 0x1
; CHECK-NEXT: no_stack_call:
-; CHECK-NEXT: .backend_stack_size: 0x10
+; CHECK-NEXT: .backend_stack_size: 0x24
; CHECK-NEXT: .lds_size: 0
; CHECK-NEXT: .sgpr_count: 0x22
; CHECK-NEXT: .stack_frame_size_in_bytes: 0x10
@@ -133,7 +133,7 @@
; CHECK-NEXT: .stack_frame_size_in_bytes: 0x14
; CHECK-NEXT: .vgpr_count: 0x2
; CHECK-NEXT: simple_stack_call:
-; CHECK-NEXT: .backend_stack_size: 0x20
+; CHECK-NEXT: .backend_stack_size: 0x34
; CHECK-NEXT: .lds_size: 0
; CHECK-NEXT: .sgpr_count: 0x22
; CHECK-NEXT: .stack_frame_size_in_bytes: 0x20
diff --git a/llvm/test/CodeGen/AMDGPU/pal-metadata-3.0-callable.ll b/llvm/test/CodeGen/AMDGPU/pal-metadata-3.0-callable.ll
index ca0941e8cebc84..9f86587cd7bfef 100644
--- a/llvm/test/CodeGen/AMDGPU/pal-metadata-3.0-callable.ll
+++ b/llvm/test/CodeGen/AMDGPU/pal-metadata-3.0-callable.ll
@@ -92,7 +92,7 @@
; CHECK-NEXT: .stack_frame_size_in_bytes: 0
; CHECK-NEXT: .vgpr_count: 0x1
; CHECK-NEXT: no_stack_call:
-; CHECK-NEXT: .backend_stack_size: 0x10
+; CHECK-NEXT: .backend_stack_size: 0x24
; CHECK-NEXT: .lds_size: 0
; CHECK-NEXT: .sgpr_count: 0x22
; CHECK-NEXT: .stack_frame_size_in_bytes: 0x10
@@ -137,7 +137,7 @@
; CHECK-NEXT: .stack_frame_size_in_bytes: 0x14
; CHECK-NEXT: .vgpr_count: 0x2
; CHECK-NEXT: simple_stack_call:
-; CHECK-NEXT: .backend_stack_size: 0x20
+; CHECK-NEXT: .backend_stack_size: 0x34
; CHECK-NEXT: .lds_size: 0
; CHECK-NEXT: .sgpr_count: 0x22
; CHECK-NEXT: .stack_frame_size_in_bytes: 0x20
``````````
</details>
https://github.com/llvm/llvm-project/pull/234522
More information about the llvm-branch-commits
mailing list