[llvm] [x64][win] Windows x64 unwind v3: Use tail-relative epilog offsets and add size-based splitting (PR #203108)

Jaydeep Chauhan via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 18 10:05:18 PDT 2026


================
@@ -232,22 +276,71 @@ bool X86WinEHUnwindV3::runOnMachineFunction(MachineFunction &MF) {
       return true;
     }
 
-    if (Info.EpilogCount > MaxV3Epilogs) {
-      const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
-      unsigned Count = 0;
-      for (MachineInstr *BeginEpilog : Info.EpilogBegins) {
-        Count++;
-        if (Count > MaxV3Epilogs) {
-          MachineBasicBlock *MBB = BeginEpilog->getParent();
-          BuildMI(*MBB, BeginEpilog, BeginEpilog->getDebugLoc(),
-                  TII->get(X86::SEH_SplitChained));
-          BuildMI(*MBB, BeginEpilog, BeginEpilog->getDebugLoc(),
-                  TII->get(X86::SEH_EndPrologue));
-          SubFragmentSplits++;
-          Count = 1;
+    // Split the funclet into chained sub-fragments so that each fragment's
+    // UNWIND_INFO stays within the V3 capacity limits: at most 7 epilogs per
+    // fragment, and every epilog close enough to its fragment tail that the
+    // tail-relative EpilogOffset fits in the signed 16-bit field.
+    //
+    // A SEH_SplitChainedAtEndOfBlock inserted at the start of an epilog's
+    // block makes the AsmPrinter emit the actual .seh_splitchained at the
+    // *end* of that block, so the epilog becomes the last epilog of the
+    // earlier fragment, immediately followed by the new chained fragment. A
+    // long tail after the last epilog is pushed into its own epilog-free
+    // chained fragment.
+    const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
+    auto SplitAfter = [&](const EpilogSplitPoint &Epilog) {
+      MachineBasicBlock *MBB = Epilog.BeginEpilog->getParent();
+      BuildMI(*MBB, MBB->begin(), Epilog.BeginEpilog->getDebugLoc(),
+              TII->get(X86::SEH_SplitChainedAtEndOfBlock));
+      SubFragmentSplits++;
+      Changed = true;
+    };
+
+    unsigned FragmentFirstPos = 0;
+    unsigned EpilogsInFragment = 0;
+    const EpilogSplitPoint *LastEpilog = nullptr;
+    unsigned LastEpilogIdx = 0;
+    for (unsigned Idx = 0; Idx < Info.Epilogs.size(); ++Idx) {
+      const EpilogSplitPoint &Epilog = Info.Epilogs[Idx];
+      // If adding this epilog would exceed a fragment limit, end the current
+      // fragment after the previous epilog and start a new one here.
+      if (EpilogsInFragment > 0) {
+        bool ExceedsEpilogCount = EpilogsInFragment >= MaxV3Epilogs;
+        bool ExceedsDistance =
+            Epilog.ApproxInstrPos - FragmentFirstPos >= EpilogDistanceThreshold;
----------------
JaydeepChauhan14 wrote:

This can be too ...?
```suggestion
            Epilog.ApproxInstrPos - LastEpilog->ApproxInstrPos >= EpilogDistanceThreshold;
```

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


More information about the llvm-commits mailing list