Skip to content

Conversation

@dpaoliello
Copy link
Contributor

While attempting to enable Windows x64 unwind v2, compilation failed with the following error:

fatal error: error in backend: Windows x64 Unwind v2 is required, but LLVM has generated incompatible code in function '<redacted>': Cannot pop registers before the stack allocation has been deallocated

I traced this down to an optimization in X86FrameLowering:

while (Offset) {
uint64_t ThisVal = std::min(Offset, Chunk);
if (ThisVal == SlotSize) {
// Use push / pop for slot sized adjustments as a size optimization. We
// need to find a dead register when using pop.
unsigned Reg = isSub ? (unsigned)(Is64Bit ? X86::RAX : X86::EAX)
: TRI->findDeadCallerSavedReg(MBB, MBBI);
if (Reg) {
unsigned Opc = isSub ? (Is64Bit ? X86::PUSH64r : X86::PUSH32r)
: (Is64Bit ? X86::POP64r : X86::POP32r);
BuildMI(MBB, MBBI, DL, TII.get(Opc))
.addReg(Reg, getDefRegState(!isSub) | getUndefRegState(isSub))
.setMIFlag(Flag);
Offset -= ThisVal;
continue;
}
}

Technically, using push/pop to adjust the stack is permitted under unwind v2: the requirement for a "canonical" epilog is that the stack is fully adjusted before the registers listed as pushed in the unwind table are popped. So, as long as the .seh_unwindv2start pseudo is after the pops that adjust the stack, then everything will work correctly.

One other side effect of this change is that the stack is now allowed to be adjusted across multiple instructions, which would be needed for extremely large stack frames.

@llvmbot
Copy link
Member

llvmbot commented Aug 14, 2025

@llvm/pr-subscribers-backend-x86

Author: Daniel Paoliello (dpaoliello)

Changes

While attempting to enable Windows x64 unwind v2, compilation failed with the following error:

fatal error: error in backend: Windows x64 Unwind v2 is required, but LLVM has generated incompatible code in function '&lt;redacted&gt;': Cannot pop registers before the stack allocation has been deallocated

I traced this down to an optimization in X86FrameLowering:
<

while (Offset) {
uint64_t ThisVal = std::min(Offset, Chunk);
if (ThisVal == SlotSize) {
// Use push / pop for slot sized adjustments as a size optimization. We
// need to find a dead register when using pop.
unsigned Reg = isSub ? (unsigned)(Is64Bit ? X86::RAX : X86::EAX)
: TRI->findDeadCallerSavedReg(MBB, MBBI);
if (Reg) {
unsigned Opc = isSub ? (Is64Bit ? X86::PUSH64r : X86::PUSH32r)
: (Is64Bit ? X86::POP64r : X86::POP32r);
BuildMI(MBB, MBBI, DL, TII.get(Opc))
.addReg(Reg, getDefRegState(!isSub) | getUndefRegState(isSub))
.setMIFlag(Flag);
Offset -= ThisVal;
continue;
}
}
>

Technically, using push/pop to adjust the stack is permitted under unwind v2: the requirement for a "canonical" epilog is that the stack is fully adjusted before the registers listed as pushed in the unwind table are popped. So, as long as the .seh_unwindv2start pseudo is after the pops that adjust the stack, then everything will work correctly.

One other side effect of this change is that the stack is now allowed to be adjusted across multiple instructions, which would be needed for extremely large stack frames.


Full diff: https://github.com/llvm/llvm-project/pull/153621.diff

2 Files Affected:

  • (modified) llvm/lib/Target/X86/X86WinEHUnwindV2.cpp (+35-32)
  • (added) llvm/test/CodeGen/X86/win64-eh-unwindv2-push-pop-stack-alloc.ll (+80)
diff --git a/llvm/lib/Target/X86/X86WinEHUnwindV2.cpp b/llvm/lib/Target/X86/X86WinEHUnwindV2.cpp
index e9081a4ae4e72..8efb3bf6ed8a5 100644
--- a/llvm/lib/Target/X86/X86WinEHUnwindV2.cpp
+++ b/llvm/lib/Target/X86/X86WinEHUnwindV2.cpp
@@ -201,11 +201,6 @@ bool X86WinEHUnwindV2::runOnMachineFunction(MachineFunction &MF) {
                 "The epilog is deallocating a stack "
                 "allocation, but the prolog did "
                 "not allocate one");
-          if (HasStackDealloc)
-            return rejectCurrentFunctionInternalError(
-                MF, Mode,
-                "The epilog is deallocating the stack "
-                "allocation more than once");
           if (PoppedRegCount > 0)
             llvm_unreachable(
                 "Should have raised an error: either popping before "
@@ -219,33 +214,41 @@ bool X86WinEHUnwindV2::runOnMachineFunction(MachineFunction &MF) {
 
       case X86::POP64r:
         if (State == FunctionState::InEpilog) {
-          // After the stack pointer has been adjusted, the epilog must
-          // POP each register in reverse order of the PUSHes in the prolog.
-          PoppedRegCount++;
-          if (HasStackAlloc != HasStackDealloc)
-            return rejectCurrentFunctionInternalError(
-                MF, Mode,
-                "Cannot pop registers before the stack "
-                "allocation has been deallocated");
-          if (PoppedRegCount > PushedRegs.size())
-            return rejectCurrentFunctionInternalError(
-                MF, Mode,
-                "The epilog is popping more registers than the prolog pushed");
-          if (PushedRegs[PushedRegs.size() - PoppedRegCount] !=
-              MI.getOperand(0).getReg())
-            return rejectCurrentFunctionInternalError(
-                MF, Mode,
-                "The epilog is popping a registers in "
-                "a different order than the "
-                "prolog pushed them");
-
-          // Unwind v2 records the size of the epilog not from where we place
-          // SEH_BeginEpilogue (as that contains the instruction to adjust the
-          // stack pointer) but from the first POP instruction (if there is
-          // one).
-          if (!UnwindV2StartLocation) {
-            assert(PoppedRegCount == 1);
-            UnwindV2StartLocation = &MI;
+          Register Reg = MI.getOperand(0).getReg();
+          if (HasStackAlloc && (PoppedRegCount == 0) &&
+              !llvm::is_contained(PushedRegs, Reg)) {
+            // If this is a pop that doesn't correspond to the set of pushed
+            // registers, then assume it was used to adjust the stack pointer.
+            HasStackDealloc = true;
+          } else {
+            // After the stack pointer has been adjusted, the epilog must
+            // POP each register in reverse order of the PUSHes in the prolog.
+            PoppedRegCount++;
+            if (HasStackAlloc != HasStackDealloc)
+              return rejectCurrentFunctionInternalError(
+                  MF, Mode,
+                  "Cannot pop registers before the stack "
+                  "allocation has been deallocated");
+            if (PoppedRegCount > PushedRegs.size())
+              return rejectCurrentFunctionInternalError(
+                  MF, Mode,
+                  "The epilog is popping more registers than the prolog "
+                  "pushed");
+            if (PushedRegs[PushedRegs.size() - PoppedRegCount] != Reg)
+              return rejectCurrentFunctionInternalError(
+                  MF, Mode,
+                  "The epilog is popping a registers in "
+                  "a different order than the "
+                  "prolog pushed them");
+
+            // Unwind v2 records the size of the epilog not from where we place
+            // SEH_BeginEpilogue (as that contains the instruction to adjust the
+            // stack pointer) but from the first POP instruction (if there is
+            // one).
+            if (!UnwindV2StartLocation) {
+              assert(PoppedRegCount == 1);
+              UnwindV2StartLocation = &MI;
+            }
           }
         } else if (State == FunctionState::FinishedEpilog)
           // Unexpected instruction after the epilog.
diff --git a/llvm/test/CodeGen/X86/win64-eh-unwindv2-push-pop-stack-alloc.ll b/llvm/test/CodeGen/X86/win64-eh-unwindv2-push-pop-stack-alloc.ll
new file mode 100644
index 0000000000000..c6f76d00ce4f1
--- /dev/null
+++ b/llvm/test/CodeGen/X86/win64-eh-unwindv2-push-pop-stack-alloc.ll
@@ -0,0 +1,80 @@
+; RUN: llc -mtriple=x86_64-unknown-windows-msvc -o - %s | FileCheck %s
+
+; Regression test for Win x64 unwind v2: in some cases it is better to use
+; push+pop to adjust the stack, rather than sub+add. This is permitted with
+; unwind v2 as the requirement is that the epilog finishes adjusting the stack
+; before popping the registers listed in the unwind table.
+
+; Pushes and pops the same register.
+define void @push_pop_same() {
+  %small_alloca = alloca i32
+  ret void
+}
+; CHECK-LABEL:  push_pop_same:
+; CHECK:        .seh_unwindversion 2
+; CHECK-NEXT:   pushq   %rax
+; CHECK-NEXT:   .seh_stackalloc 8
+; CHECK-NEXT:   .seh_endprologue
+; CHECK-NEXT:   .seh_startepilogue
+; CHECK-NEXT:   popq    %rax
+; CHECK-NEXT:   .seh_unwindv2start
+; CHECK-NEXT:   .seh_endepilogue
+; CHECK-NEXT:   retq
+; CHECK-NEXT:   .seh_endproc
+
+; Pushes and pops a different register.
+define i32 @push_pop_different(i32 %x, i32 %y) {
+  %small_alloca = alloca i32
+  %sum = add i32 %x, %y
+  ret i32 %sum
+}
+; CHECK-LABEL:  push_pop_different:
+; CHECK:        .seh_unwindversion 2
+; CHECK-NEXT:   pushq   %rax
+; CHECK-NEXT:   .seh_stackalloc 8
+; CHECK-NEXT:   .seh_endprologue
+; CHECK:       .seh_startepilogue
+; CHECK-NEXT:   popq    %rcx
+; CHECK-NEXT:   .seh_unwindv2start
+; CHECK-NEXT:   .seh_endepilogue
+; CHECK-NEXT:   retq
+; CHECK-NEXT:   .seh_endproc
+
+; Pushes in the prolog, adds in the epilog.
+define void @push_add(ptr %a, ptr %b, ptr %out) {
+  %small_alloca = alloca i32
+  %av = load i256, ptr %a
+  %bv = load i256, ptr %b
+  %r = mul i256 %av, %bv
+  store i256 %r, ptr %out
+  ret void
+}
+; CHECK-LABEL:  push_add:
+; CHECK:        .seh_unwindversion 2
+; CHECK-NEXT:   pushq   %r15
+; CHECK-NEXT:   .seh_pushreg %r15
+; CHECK-NEXT:   pushq   %r14
+; CHECK-NEXT:   .seh_pushreg %r14
+; CHECK-NEXT:   pushq   %rsi
+; CHECK-NEXT:   .seh_pushreg %rsi
+; CHECK-NEXT:   pushq   %rdi
+; CHECK-NEXT:   .seh_pushreg %rdi
+; CHECK-NEXT:   pushq   %rbx
+; CHECK-NEXT:   .seh_pushreg %rbx
+; CHECK-NEXT:   pushq   %rax
+; CHECK-NEXT:   .seh_stackalloc 8
+; CHECK-NEXT:   .seh_endprologue
+; CHECK:       .seh_startepilogue
+; CHECK-NEXT:  addq    $8, %rsp
+; CHECK-NEXT:  .seh_unwindv2start
+; CHECK-NEXT:  popq    %rbx
+; CHECK-NEXT:  popq    %rdi
+; CHECK-NEXT:  popq    %rsi
+; CHECK-NEXT:  popq    %r14
+; CHECK-NEXT:  popq    %r15
+; CHECK-NEXT:  .seh_endepilogue
+; CHECK-NEXT:  retq
+; CHECK-NEXT:  .seh_endproc
+
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"winx64-eh-unwindv2", i32 2}

@dpaoliello dpaoliello force-pushed the fixunwindv2 branch 2 times, most recently from 9da36d2 to f578766 Compare August 14, 2025 17:31
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this, the remaining use of HasStackDealloc is the HasStackAlloc != HasStackDealloc checks. Which is maybe fine? I guess we don't care if the number of allocation and deallocation instructions matches.

Do we need to change the llvm_unreachable in the PoppedRegCount > 0 check to rejectCurrentFunctionInternalError?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to leave the HasStackAlloc != HasStackDealloc checks: we should see some sort of stack adjustment before popping registers and the end of the function.

The PoppedRegCount > 0 check is still correct: we only increment PoppedRegCount if it's popping one of the SEH_PushReg pushed registers, not the stack adjustment pops.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Say you have a push and a stackalloc in the prologue. Then you have a stackdealloc, a pop, and another stackdealloc in the epilogue. We want to rejectCurrentFunctionInternalError in that case. (Not sure how likely that is, but could happen with a callee-pop calling convention.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see what you mean now. Switch it to be an error and added tests.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably less likely to need future changes if you make it an MIR test, since you don't care what the original IR looks like here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea

Copy link
Collaborator

@efriedma-quic efriedma-quic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dpaoliello dpaoliello merged commit 1d1e52e into llvm:main Aug 15, 2025
9 checks passed
@dpaoliello dpaoliello deleted the fixunwindv2 branch August 15, 2025 16:03
sivadeilra pushed a commit to sivadeilra/llvm-project that referenced this pull request Nov 13, 2025
While attempting to enable Windows x64 unwind v2, compilation failed
with the following error:

```
fatal error: error in backend: Windows x64 Unwind v2 is required, but LLVM has generated incompatible code in function '<redacted>': Cannot pop registers before the stack allocation has been deallocated
```

I traced this down to an optimization in `X86FrameLowering`:

<https://github.com/llvm/llvm-project/blob/6961139ce9154d03c88b8d46c8742a1eaa569cd9/llvm/lib/Target/X86/X86FrameLowering.cpp#L324-L340>

Technically, using `push`/`pop` to adjust the stack is permitted under
unwind v2: the requirement for a "canonical" epilog is that the stack is
fully adjusted before the registers listed as pushed in the unwind table
are popped. So, as long as the `.seh_unwindv2start` pseudo is after the
pops that adjust the stack, then everything will work correctly.

One other side effect of this change is that the stack is now allowed to
be adjusted across multiple instructions, which would be needed for
extremely large stack frames.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants