[PATCH] D131393: [MC] [Win64EH] Fix the calculation of the end of epilogs

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 8 06:01:17 PDT 2022


mstorsjo created this revision.
mstorsjo added reviewers: zzheng, efriedma.
Herald added a subscriber: hiraditya.
Herald added a project: All.
mstorsjo requested review of this revision.
Herald added a project: LLVM.

Exclude the terminating end opcode from the epilog - it doesn't
correspond to an actual instruction that is included in the epilog
itself (within the .seh_startepilogue/.seh_endepilogue range).

In most (all?) cases, an epilog is followed by a matching terminating
instruction though (a ret or a branch to a tail call), but it's not
strictly within the .seh_startepilogue/.seh_endepilogue range.

This fixes a number of failed asserts in cases where the codegen
has incorrectly reoredered SEH opcodes so they don't match up
exactly with their instructions.

However this still just avoids failing the assertion; the root cause
of generating unexpected epilogs is still present (and fixing that is
a less obvious issue).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D131393

Files:
  llvm/lib/MC/MCWin64EH.cpp
  llvm/test/MC/AArch64/seh-multi-epilog.s


Index: llvm/test/MC/AArch64/seh-multi-epilog.s
===================================================================
--- /dev/null
+++ llvm/test/MC/AArch64/seh-multi-epilog.s
@@ -0,0 +1,62 @@
+// This test checks that we emit allow multiple consecutive epilogs without
+// triggering failed asserts.unwind info correctly for epilogs that:
+// RUN: llvm-mc -triple aarch64-pc-win32 -filetype=obj %s -o %t.o
+// RUN: llvm-readobj -u %t.o | FileCheck %s
+
+// CHECK-LABEL:UnwindInformation [
+// CHECK-NEXT:  RuntimeFunction {
+// CHECK-NEXT:    Function: multi_epilog (0x0)
+// CHECK-NEXT:    ExceptionRecord: .xdata (0x0)
+// CHECK-NEXT:    ExceptionData {
+// CHECK-NEXT:      FunctionLength: 20
+// CHECK-NEXT:      Version: 0
+// CHECK-NEXT:      ExceptionData: No
+// CHECK-NEXT:      EpiloguePacked: No
+// CHECK-NEXT:      EpilogueScopes: 2
+// CHECK-NEXT:      ByteCodeLength: 4
+// CHECK-NEXT:      Prologue [
+// CHECK-NEXT:        0x81                ; stp x29, x30, [sp, #-16]!
+// CHECK-NEXT:        0xe4                ; end
+// CHECK-NEXT:      ]
+// CHECK-NEXT:      EpilogueScopes [
+// CHECK-NEXT:        EpilogueScope {
+// CHECK-NEXT:          StartOffset: 2
+// CHECK-NEXT:          EpilogueStartIndex: 0
+// CHECK-NEXT:          Opcodes [
+// CHECK-NEXT:            0x81                ; ldp x29, x30, [sp], #16
+// CHECK-NEXT:            0xe4                ; end
+// CHECK-NEXT:          ]
+// CHECK-NEXT:        }
+// CHECK-NEXT:        EpilogueScope {
+// CHECK-NEXT:          StartOffset: 3
+// CHECK-NEXT:          EpilogueStartIndex: 0
+// CHECK-NEXT:          Opcodes [
+// CHECK-NEXT:            0x81                ; ldp x29, x30, [sp], #16
+// CHECK-NEXT:            0xe4                ; end
+// CHECK-NEXT:          ]
+// CHECK-NEXT:        }
+// CHECK-NEXT:      ]
+// CHECK-NEXT:    }
+// CHECK-NEXT:  }
+// CHECK-NEXT:]
+
+	.text
+	.global	multi_epilog
+	.p2align	2
+	.seh_proc multi_epilog
+multi_epilog:
+	stp	x29, lr, [sp, #-16]!
+	.seh_save_fplr_x 16
+	.seh_endprologue
+        nop
+	.seh_startepilogue
+	ldp	x29, lr, [sp], #16
+	.seh_save_fplr_x 16
+	.seh_endepilogue
+	.seh_startepilogue
+	ldp	x29, lr, [sp], #16
+	.seh_save_fplr_x 16
+	.seh_endepilogue
+	ret
+	.seh_endfunclet
+	.seh_endproc
Index: llvm/lib/MC/MCWin64EH.cpp
===================================================================
--- llvm/lib/MC/MCWin64EH.cpp
+++ llvm/lib/MC/MCWin64EH.cpp
@@ -997,7 +997,9 @@
     int64_t Offset = GetAbsDifference(streamer, Start, info->Begin);
     assert((Epilogs.size() == 0 || Offset >= Epilogs.back().End) &&
            "Epilogs should be monotonically ordered");
-    Epilogs.push_back({Start, Offset, Offset + (int64_t)Instrs.size() * 4});
+    // Exclue the end opcode from Instrs.size() when calculating the end of the
+    // epilog.
+    Epilogs.push_back({Start, Offset, Offset + (int64_t)(Instrs.size() - 1) * 4});
   }
 
   unsigned E = 0;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131393.450776.patch
Type: text/x-patch
Size: 2895 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220808/716f7d99/attachment.bin>


More information about the llvm-commits mailing list