[llvm] [X86][WinEH] Avoid stale RSP after Win64 dynalloca invokes (PR #191616)

via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 16 19:58:12 PDT 2026


================
@@ -297,6 +413,25 @@ bool X86DynAllocaExpander::run(MachineFunction &MF) {
   SlotSize = TRI->getSlotSize();
   StackProbeSize = STI->getTargetLowering()->getStackProbeSize(MF);
   NoStackArgProbe = MF.getFunction().hasFnAttribute("no-stack-arg-probe");
+  auto *X86FI = MF.getInfo<X86MachineFunctionInfo>();
+
+  unsigned Win64MSVCDynAllocaCallFrameSize = 0;
+  if (STI->isTargetWin64() && STI->isTargetWindowsMSVC() &&
+      !X86FI->getHasPushSequences() && !X86FI->hasPreallocatedCall()) {
+    Align StackAlign = STI->getFrameLowering()->getStackAlign();
+    for (const MachineBasicBlock &MBB : MF) {
+      for (const MachineInstr &MI : MBB) {
+        if (MI.getOpcode() == X86::ADJCALLSTACKDOWN64 ||
+            MI.getOpcode() == X86::ADJCALLSTACKUP64) {
+          Win64MSVCDynAllocaCallFrameSize = std::max<unsigned>(
+              Win64MSVCDynAllocaCallFrameSize,
+              alignTo(MI.getOperand(0).getImm(), StackAlign));
+        }
+      }
+    }
+  }
+  X86FI->setWin64MSVCDynAllocaCallFrameSize(Win64MSVCDynAllocaCallFrameSize);
----------------
taigacon wrote:

Good point. I was too focused on the stack layout and missed that this should probably just use the existing computeMaxCallFrameSize() machinery instead.

https://github.com/llvm/llvm-project/pull/191616


More information about the llvm-commits mailing list