[llvm] [AMDGPU] Fix excessive stack usage in SIInsertWaitcnts::run (PR #134835)
Jay Foad via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 8 03:43:36 PDT 2025
https://github.com/jayfoad created https://github.com/llvm/llvm-project/pull/134835
Noticed on Windows when running LLVM as part of a graphics driver, with
total stack usage limited to about 128 KB. In some cases this function
would overflow the stack.
On Linux this reduces stack usage in this function from about 32 KB to
about 0.5 KB.
>From e83b0e7c3848c4394035c5427bacbffcb233d8b3 Mon Sep 17 00:00:00 2001
From: Jay Foad <jay.foad at amd.com>
Date: Tue, 8 Apr 2025 11:32:59 +0100
Subject: [PATCH] [AMDGPU] Fix excessive stack usage in SIInsertWaitcnts::run
Noticed on Windows when running LLVM as part of a graphics driver, with
total stack usage limited to about 128 KB. In some cases this function
would overflow the stack.
On Linux this reduces stack usage in this function from about 32 KB to
about 0.5 KB.
---
llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp b/llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp
index 15965f2bac8aa..1ad60212c7d95 100644
--- a/llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp
+++ b/llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp
@@ -2623,12 +2623,17 @@ bool SIInsertWaitcnts::run(MachineFunction &MF) {
else
*Brackets = *BI.Incoming;
} else {
- if (!Brackets)
+ if (!Brackets) {
Brackets = std::make_unique<WaitcntBrackets>(
ST, MaxCounter, Limits, WaitEventMaskForInst, SmemAccessCounter);
- else
- *Brackets = WaitcntBrackets(ST, MaxCounter, Limits,
- WaitEventMaskForInst, SmemAccessCounter);
+ } else {
+ // Reinitialize in-place. N.B. do not do this by assigning from a
+ // temporary because the WaitcntBrackets class is large and it could
+ // cause this function to use an unreasonable amount of stack space.
+ Brackets->~WaitcntBrackets();
+ new (Brackets.get()) WaitcntBrackets(
+ ST, MaxCounter, Limits, WaitEventMaskForInst, SmemAccessCounter);
+ }
}
Modified |= insertWaitcntInBlock(MF, *MBB, *Brackets);
More information about the llvm-commits
mailing list