[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:48:25 PDT 2026


================
@@ -204,79 +249,150 @@ static unsigned getSubOpcode(bool Is64Bit) {
   return X86::SUB32ri;
 }
 
+Register X86DynAllocaExpander::materializeWinAllocaAmount(
+    MachineInstr *MI, MachineBasicBlock::iterator I, const DebugLoc &DL) {
+  assert(MI->getOpcode() == X86::WIN_ALLOCA_64);
+
+  MachineBasicBlock *MBB = MI->getParent();
+  unsigned StackAlign = STI->getFrameLowering()->getStackAlign().value();
+  unsigned Alignment = MI->getOperand(2).getImm();
+  Register RawAmountReg = MI->getOperand(1).getReg();
+
+  uint64_t Addend = StackAlign - 1;
+  if (Alignment > StackAlign)
+    Addend += Alignment - 1;
----------------
taigacon wrote:

This looks weird, but it isn’t a different semantic.

It’s just the constant-size formula written in expanded form. The constant path computes

alignTo(RawAmount + (Alignment > StackAlign ? Alignment - 1 : 0), StackAlign)

and alignTo(X, StackAlign) expands to (X + StackAlign - 1) & -StackAlign.

So in the variable-size path, where we materialize (RawAmount + Addend) & -StackAlign, the addend has to include both the extra alignment slack and the usual StackAlign - 1 rounding bias. I agree the current spelling is confusing, so I’ll rewrite it to make that correspondence clearer.

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


More information about the llvm-commits mailing list