[llvm] 008c875 - [AMDGPU] Fix excessive stack usage in SIInsertWaitcnts::run (#134835)

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 8 06:08:46 PDT 2025


Author: Jay Foad
Date: 2025-04-08T14:08:42+01:00
New Revision: 008c875be85732f72c4df4671167f5be79f449eb

URL: https://github.com/llvm/llvm-project/commit/008c875be85732f72c4df4671167f5be79f449eb
DIFF: https://github.com/llvm/llvm-project/commit/008c875be85732f72c4df4671167f5be79f449eb.diff

LOG: [AMDGPU] Fix excessive stack usage in SIInsertWaitcnts::run (#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.

Added: 
    

Modified: 
    llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp b/llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp
index 49943265500b1..ccbafa6a1f887 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