[clang] [llvm] [win][x64] Add support for Windows x64 unwind v3 (PR #200249)

via llvm-commits llvm-commits at lists.llvm.org
Thu May 28 12:02:12 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-mc

@llvm/pr-subscribers-backend-x86

Author: Daniel Paoliello (dpaoliello)

<details>
<summary>Changes</summary>

The extended registers in Intel APX cannot be described in the current Windows x64 unwind information, this has made it neccesary to introduce a new version. While designing this new version, improvements have been made from the lessons learnt from the existing unwind information formats (both x64 and AArch64).

Documentation for unwind v3 is available at:
<https://learn.microsoft.com/en-us/cpp/build/x64-unwind-information-v3>

This change:
* Implements encoding unwind v3 information in MC. This includes support for non-mirror epilogs, WOD pool sharing and using large infos if required.
* Add support for encoding push2/pop2 SEH info: a new `SEH_Push2Regs` pseudo-instruction and `.seh_push2regs` assembly directive.
* Changes the prolog/epilog codegen to support unwind v3. Specifically, adding pseudos into the epilog (as non-mirror epilogs are permitted) and placing the pseudos in the prolog before the instruction instead of after (as v3 measures its offsets from the start rather than the end of an instruction).
* Add an unwind v3 pass to chain unwind info if there are too many epilogs in a function and raise a fatal error if there are too many instructions in a prolog or epilog.
* Changes the module flag and code gen enums to allow selecting "Default" vs "v1" vs "v2 best effort" vs "v2 required" vs "v3" unwind info version.
* Adds new Clang and cc1 flags for selecting the unwind info version and deprecates the old v2 flag. The default unwind info version stays as v1 UNLESS the EGPR target feature is enabled (then it will use v3). Trying to enable EGPR when v1/v2 is explicitly enabled will result in an error.

---

Patch is 174.76 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/200249.diff


46 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+4) 
- (modified) clang/include/clang/Basic/CodeGenOptions.def (+3-4) 
- (modified) clang/include/clang/Basic/CodeGenOptions.h (+1-1) 
- (modified) clang/include/clang/Options/Options.td (+13-7) 
- (modified) clang/lib/CodeGen/CodeGenModule.cpp (+26-5) 
- (modified) clang/lib/Driver/ToolChains/Clang.cpp (+22-5) 
- (modified) clang/test/CodeGen/epilog-unwind.c (+9-7) 
- (added) clang/test/CodeGen/winx64-eh-unwind-egpr.c (+31) 
- (modified) clang/test/Driver/cl-options.c (+11-2) 
- (added) clang/test/Driver/winx64-eh-unwind.c (+22) 
- (modified) llvm/docs/ReleaseNotes.md (+6) 
- (modified) llvm/include/llvm/IR/Module.h (+2-3) 
- (modified) llvm/include/llvm/MC/MCStreamer.h (+10) 
- (modified) llvm/include/llvm/MC/MCWin64EH.h (+6) 
- (modified) llvm/include/llvm/MC/MCWinEH.h (+9-4) 
- (modified) llvm/include/llvm/Support/CodeGen.h (+8-7) 
- (modified) llvm/lib/IR/Module.cpp (+12-5) 
- (modified) llvm/lib/MC/MCAsmStreamer.cpp (+13) 
- (modified) llvm/lib/MC/MCStreamer.cpp (+90-12) 
- (modified) llvm/lib/MC/MCWin64EH.cpp (+553) 
- (modified) llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp (+24) 
- (modified) llvm/lib/Target/X86/CMakeLists.txt (+1) 
- (modified) llvm/lib/Target/X86/X86.h (+4) 
- (modified) llvm/lib/Target/X86/X86AsmPrinter.cpp (+4) 
- (modified) llvm/lib/Target/X86/X86FrameLowering.cpp (+217-56) 
- (modified) llvm/lib/Target/X86/X86InstrCompiler.td (+4) 
- (modified) llvm/lib/Target/X86/X86MCInstLower.cpp (+12) 
- (modified) llvm/lib/Target/X86/X86TargetMachine.cpp (+4-1) 
- (modified) llvm/lib/Target/X86/X86WinEHUnwindV2.cpp (+9-7) 
- (added) llvm/lib/Target/X86/X86WinEHUnwindV3.cpp (+203) 
- (modified) llvm/test/CodeGen/X86/apx/push2-pop2-cfi-seh.ll (+1-1) 
- (modified) llvm/test/CodeGen/X86/machine-block-hash.mir (+6-6) 
- (added) llvm/test/CodeGen/X86/win64-eh-unwindv3-egpr-required.ll (+9) 
- (added) llvm/test/CodeGen/X86/win64-eh-unwindv3-funclet-prolog.ll (+165) 
- (added) llvm/test/CodeGen/X86/win64-eh-unwindv3-push2pop2.ll (+48) 
- (added) llvm/test/CodeGen/X86/win64-eh-unwindv3-split.ll (+67) 
- (added) llvm/test/CodeGen/X86/win64-eh-unwindv3.ll (+411) 
- (modified) llvm/test/MC/AsmParser/seh-directive-errors.s (+23-1) 
- (added) llvm/test/MC/COFF/seh-unwindv3-error.s (+50) 
- (added) llvm/test/MC/COFF/seh-unwindv3-inheritance.s (+137) 
- (added) llvm/test/MC/COFF/seh-unwindv3-large.s (+197) 
- (added) llvm/test/MC/COFF/seh-unwindv3-nonmirror.s (+327) 
- (added) llvm/test/MC/COFF/seh-unwindv3-pool-sharing.s (+153) 
- (added) llvm/test/MC/COFF/seh-unwindv3.s (+758) 
- (modified) llvm/unittests/MC/CMakeLists.txt (+1) 
- (added) llvm/unittests/MC/WODRoundTripTest.cpp (+206) 


``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 11cce36a0906c..244a40a9a5a97 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -791,6 +791,10 @@ Windows Support
 - ``-fmacro-prefix-map=`` (``-ffile-prefix-map=``) now affects an anonymous namespace hash generation
   for the MSVC targets and allows deterministic symbol mangling for reproducible builds.
 
+- When targeting Windows x64 with EGPR (`-mapx-features=egpr`), Clang now
+  automatically enables V3 unwind info (`-fwinx64-eh-unwind=v3`) if no
+  explicit unwind version was specified.
+
 LoongArch Support
 ^^^^^^^^^^^^^^^^^
 
diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def
index aa36de6edecbf..519fe120b4cab 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -520,10 +520,9 @@ CODEGENOPT(ResMayAlias, 1, 0, Benign)
 /// Assume that all resources are bound if enabled
 CODEGENOPT(AllResourcesBound, 1, 0, Benign)
 
-/// Controls how unwind v2 (epilog) information should be generated for x64
-/// Windows.
-ENUM_CODEGENOPT(WinX64EHUnwindV2, WinX64EHUnwindV2Mode,
-                2, WinX64EHUnwindV2Mode::Disabled, Benign)
+/// Controls the x64 Windows unwind info version.
+ENUM_CODEGENOPT(WinX64EHUnwind, WinX64EHUnwindMode, 3,
+                WinX64EHUnwindMode::Default, Benign)
 
 /// Controls the mechanism used for Control Flow Guard (CFG) on Windows.
 ENUM_CODEGENOPT(WinControlFlowGuardMechanism, ControlFlowGuardMechanism,
diff --git a/clang/include/clang/Basic/CodeGenOptions.h b/clang/include/clang/Basic/CodeGenOptions.h
index e43112b4bb98b..835ea728e9c56 100644
--- a/clang/include/clang/Basic/CodeGenOptions.h
+++ b/clang/include/clang/Basic/CodeGenOptions.h
@@ -66,7 +66,7 @@ class CodeGenOptionsBase {
   using AsanDtorKind = llvm::AsanDtorKind;
   using VectorLibrary = llvm::driver::VectorLibrary;
   using ZeroCallUsedRegsKind = llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind;
-  using WinX64EHUnwindV2Mode = llvm::WinX64EHUnwindV2Mode;
+  using WinX64EHUnwindMode = llvm::WinX64EHUnwindMode;
   using ControlFlowGuardMechanism = llvm::ControlFlowGuardMechanism;
 
   using DebugCompressionType = llvm::DebugCompressionType;
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index 6fc8806ba683c..40bc859a71b29 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -2337,14 +2337,20 @@ defm assume_nothrow_exception_dtor: BoolFOption<"assume-nothrow-exception-dtor",
   LangOpts<"AssumeNothrowExceptionDtor">, DefaultFalse,
   PosFlag<SetTrue, [], [ClangOption, CC1Option], "Assume that exception objects' destructors are non-throwing">,
   NegFlag<SetFalse>>;
-def winx64_eh_unwindv2
-    : Joined<["-"], "fwinx64-eh-unwindv2=">, Group<f_Group>,
+def winx64_eh_unwind_EQ
+    : Joined<["-"], "fwinx64-eh-unwind=">, Group<f_Group>,
     Visibility<[ClangOption, CC1Option]>,
-      HelpText<"Generate unwind v2 (epilog) information for x64 Windows">,
-      Values<"disabled,best-effort,required">,
-      NormalizedValues<["Disabled", "BestEffort", "Required"]>,
-      NormalizedValuesScope<"llvm::WinX64EHUnwindV2Mode">,
-      MarshallingInfoEnum<CodeGenOpts<"WinX64EHUnwindV2">, "Disabled">;
+      HelpText<"Set x64 Windows unwind info version">,
+      Values<"v1,v2-best-effort,v2-required,v3">,
+      NormalizedValues<["V1", "V2BestEffort", "V2Required", "V3"]>,
+      NormalizedValuesScope<"llvm::WinX64EHUnwindMode">,
+      MarshallingInfoEnum<CodeGenOpts<"WinX64EHUnwind">, "Default">;
+// Legacy flag — driver only, not passed to cc1. The driver translates it.
+def winx64_eh_unwindv2_EQ
+    : Joined<["-"], "fwinx64-eh-unwindv2=">, Group<f_Group>,
+    Visibility<[ClangOption]>,
+      HelpText<"(Legacy) Generate unwind v2 (epilog) information for x64 Windows">,
+      Values<"disabled,best-effort,required">;
 def win_cfg_mechanism
     : Joined<["-"], "fwin-cfg-mechanism=">, Group<f_Group>,
     Visibility<[ClangOption, CC1Option]>,
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index b8841f75a5c19..38a694d2d58b6 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -1590,11 +1590,32 @@ void CodeGenModule::Release() {
     getModule().addModuleFlag(llvm::Module::Warning, "import-call-optimization",
                               1);
 
-  // Enable unwind v2 (epilog).
-  if (CodeGenOpts.getWinX64EHUnwindV2() != llvm::WinX64EHUnwindV2Mode::Disabled)
-    getModule().addModuleFlag(
-        llvm::Module::Warning, "winx64-eh-unwindv2",
-        static_cast<unsigned>(CodeGenOpts.getWinX64EHUnwindV2()));
+  // Enable unwind v2/v3.
+  // EGPR (R16-R31) requires V3 unwind because V1/V2 cannot encode extended
+  // register numbers. Auto-promote to V3 when no explicit mode was requested.
+
+  auto UnwindMode = CodeGenOpts.getWinX64EHUnwind();
+  bool HasEGPR = T.isOSWindows() && T.isX86_64() &&
+                 Context.getTargetInfo().hasFeature("egpr");
+
+  // Check for EGPR incompatibility with explicitly requested V1 or V2
+  if (HasEGPR && UnwindMode != llvm::WinX64EHUnwindMode::Default &&
+      UnwindMode != llvm::WinX64EHUnwindMode::V3) {
+    unsigned DiagID =
+        Diags.getCustomDiagID(DiagnosticsEngine::Error,
+                              "EGPR target feature requires unwind version 3");
+    Diags.Report(DiagID);
+  }
+
+  if (UnwindMode == llvm::WinX64EHUnwindMode::Default) {
+    if (HasEGPR)
+      UnwindMode = llvm::WinX64EHUnwindMode::V3;
+    else
+      UnwindMode = llvm::WinX64EHUnwindMode::V1;
+  }
+  if (UnwindMode != llvm::WinX64EHUnwindMode::V1)
+    getModule().addModuleFlag(llvm::Module::Warning, "winx64-eh-unwind",
+                              static_cast<unsigned>(UnwindMode));
 
   // Indicate whether this Module was compiled with -fopenmp
   if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd)
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 0f915e9f222f7..7bc520fe10cf4 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -7704,8 +7704,24 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
     }
   }
 
-  // Unwind v2 (epilog) information for x64 Windows.
-  Args.AddLastArg(CmdArgs, options::OPT_winx64_eh_unwindv2);
+  // Unwind information version for x64 Windows.
+  // Forward the new unified flag if present, otherwise translate legacy flags.
+  if (const Arg *A = Args.getLastArg(options::OPT_winx64_eh_unwind_EQ)) {
+    A->claim();
+    CmdArgs.push_back(
+        Args.MakeArgString(Twine("-fwinx64-eh-unwind=") + A->getValue()));
+  } else if (const Arg *A =
+                 Args.getLastArg(options::OPT_winx64_eh_unwindv2_EQ)) {
+    A->claim();
+    StringRef Val = A->getValue();
+    if (Val == "best-effort")
+      CmdArgs.push_back("-fwinx64-eh-unwind=v2-best-effort");
+    else if (Val == "required")
+      CmdArgs.push_back("-fwinx64-eh-unwind=v2-required");
+    // "disabled" maps to v1 default, nothing to forward.
+    else if (Val != "disabled")
+      D.Diag(diag::err_drv_invalid_value) << A->getAsString(Args) << Val;
+  }
 
   // Control Flow Guard mechanism for Windows.
   Args.AddLastArg(CmdArgs, options::OPT_win_cfg_mechanism);
@@ -8810,11 +8826,12 @@ void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType,
   if (Args.hasArg(options::OPT__SLASH_kernel))
     CmdArgs.push_back("-fms-kernel");
 
-  // Unwind v2 (epilog) information for x64 Windows.
+  // Unwind v2 (epilog) information for x64 Windows. MSVC's behavior is not
+  // order-dependent: /d2epilogunwindrequirev2 always wins over /d2epilogunwind.
   if (Args.hasArg(options::OPT__SLASH_d2epilogunwindrequirev2))
-    CmdArgs.push_back("-fwinx64-eh-unwindv2=required");
+    CmdArgs.push_back("-fwinx64-eh-unwind=v2-required");
   else if (Args.hasArg(options::OPT__SLASH_d2epilogunwind))
-    CmdArgs.push_back("-fwinx64-eh-unwindv2=best-effort");
+    CmdArgs.push_back("-fwinx64-eh-unwind=v2-best-effort");
 
   // Handle the various /guard options. We don't immediately push back clang
   // args since there are /d2 args that can modify the behavior of /guard:cf.
diff --git a/clang/test/CodeGen/epilog-unwind.c b/clang/test/CodeGen/epilog-unwind.c
index b2f7497b455b6..55c22a55ad374 100644
--- a/clang/test/CodeGen/epilog-unwind.c
+++ b/clang/test/CodeGen/epilog-unwind.c
@@ -1,11 +1,13 @@
 // RUN: %clang_cc1 -emit-llvm %s -o - | FileCheck %s -check-prefix=DISABLED
-// RUN: %clang_cc1 -fwinx64-eh-unwindv2=disabled -emit-llvm %s -o - | FileCheck %s -check-prefix=DISABLED
-// RUN: %clang_cc1 -fwinx64-eh-unwindv2=best-effort -emit-llvm %s -o - | FileCheck %s -check-prefix=BESTEFFORT
-// RUN: %clang_cc1 -fwinx64-eh-unwindv2=required -emit-llvm %s -o - | FileCheck %s -check-prefix=REQUIRED
-// RUN: %clang -fwinx64-eh-unwindv2=best-effort -S -emit-llvm %s -o - | FileCheck %s -check-prefix=BESTEFFORT
+// RUN: %clang_cc1 -fwinx64-eh-unwind=v1 -emit-llvm %s -o - | FileCheck %s -check-prefix=DISABLED
+// RUN: %clang_cc1 -fwinx64-eh-unwind=v2-best-effort -emit-llvm %s -o - | FileCheck %s -check-prefix=BESTEFFORT
+// RUN: %clang_cc1 -fwinx64-eh-unwind=v2-required -emit-llvm %s -o - | FileCheck %s -check-prefix=REQUIRED
+// RUN: %clang_cc1 -fwinx64-eh-unwind=v3 -emit-llvm %s -o - | FileCheck %s -check-prefix=V3
+// RUN: %clang -fwinx64-eh-unwind=v2-best-effort -S -emit-llvm %s -o - | FileCheck %s -check-prefix=BESTEFFORT
 
 void f(void) {}
 
-// BESTEFFORT: !"winx64-eh-unwindv2", i32 1}
-// REQUIRED: !"winx64-eh-unwindv2", i32 2}
-// DISABLED-NOT: "winx64-eh-unwindv2"
+// BESTEFFORT: !"winx64-eh-unwind", i32 1}
+// REQUIRED: !"winx64-eh-unwind", i32 2}
+// V3: !"winx64-eh-unwind", i32 3}
+// DISABLED-NOT: "winx64-eh-unwind"
diff --git a/clang/test/CodeGen/winx64-eh-unwind-egpr.c b/clang/test/CodeGen/winx64-eh-unwind-egpr.c
new file mode 100644
index 0000000000000..fd15d8a6b99f6
--- /dev/null
+++ b/clang/test/CodeGen/winx64-eh-unwind-egpr.c
@@ -0,0 +1,31 @@
+// REQUIRES: x86-registered-target
+// RUN: %clang_cc1 -triple x86_64-windows-msvc -target-feature +egpr -emit-llvm %s -o - | FileCheck %s -check-prefix=EGPR-DEFAULT
+// RUN: %clang_cc1 -triple x86_64-windows-msvc -target-feature +egpr -fwinx64-eh-unwind=v3 -emit-llvm %s -o - | FileCheck %s -check-prefix=EGPR-V3
+// RUN: not %clang_cc1 -triple x86_64-windows-msvc -target-feature +egpr -fwinx64-eh-unwind=v1 -emit-llvm %s -o - 2>&1 | FileCheck %s -check-prefix=EGPR-V1-ERROR
+// RUN: not %clang_cc1 -triple x86_64-windows-msvc -target-feature +egpr -fwinx64-eh-unwind=v2-best-effort -emit-llvm %s -o - 2>&1 | FileCheck %s -check-prefix=EGPR-V2-ERROR
+// RUN: not %clang_cc1 -triple x86_64-windows-msvc -target-feature +egpr -fwinx64-eh-unwind=v2-required -emit-llvm %s -o - 2>&1 | FileCheck %s -check-prefix=EGPR-V2-ERROR
+// RUN: %clang_cc1 -triple x86_64-windows-msvc -emit-llvm %s -o - | FileCheck %s -check-prefix=NO-EGPR
+// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -target-feature +egpr -emit-llvm %s -o - | FileCheck %s -check-prefix=EGPR-LINUX
+
+void f(void) {}
+
+// EGPR on Windows x64 with no explicit unwind mode should auto-promote to V3.
+// EGPR-DEFAULT: !"winx64-eh-unwind", i32 3}
+
+// EGPR on Windows x64 with explicit V3 should still emit V3.
+// EGPR-V3: !"winx64-eh-unwind", i32 3}
+
+// EGPR on Windows x64 with explicit V1 should produce an error.
+// EGPR-V1-ERROR: error: EGPR target feature requires unwind version 3
+
+// EGPR on Windows x64 with explicit V2 should produce an error.
+// EGPR-V2-ERROR: error: EGPR target feature requires unwind version 3
+
+// EGPR on Windows x64 with explicit V1 should respect the user's choice.
+// EGPR-EXPLICIT-V1-NOT: "winx64-eh-unwind"
+
+// Without EGPR on Windows x64, default should be V1 (no flag emitted).
+// NO-EGPR-NOT: "winx64-eh-unwind"
+
+// EGPR on non-Windows should not emit the flag.
+// EGPR-LINUX-NOT: "winx64-eh-unwind"
diff --git a/clang/test/Driver/cl-options.c b/clang/test/Driver/cl-options.c
index c0f57ae768252..63a423f5b3114 100644
--- a/clang/test/Driver/cl-options.c
+++ b/clang/test/Driver/cl-options.c
@@ -856,11 +856,20 @@
 // ARM64EC_OVERRIDE: warning: /arm64EC has been overridden by specified target: x86_64-pc-windows-msvc; option ignored
 
 // RUN: %clang_cl /d2epilogunwind /c -### -- %s 2>&1 | FileCheck %s --check-prefix=EPILOGUNWIND
-// EPILOGUNWIND: -fwinx64-eh-unwindv2=best-effort
+// EPILOGUNWIND: -fwinx64-eh-unwind=v2-best-effort
 
 // RUN: %clang_cl /d2epilogunwindrequirev2 /c -### -- %s 2>&1 | FileCheck %s --check-prefix=EPILOGUNWINDREQUIREV2
+// /d2epilogunwindrequirev2 always wins over /d2epilogunwind, regardless of
+// the order they appear on the command line (matches MSVC).
+// RUN: %clang_cl /d2epilogunwind /d2epilogunwindrequirev2 /c -### -- %s 2>&1 | FileCheck %s --check-prefix=EPILOGUNWINDREQUIREV2
 // RUN: %clang_cl /d2epilogunwindrequirev2 /d2epilogunwind /c -### -- %s 2>&1 | FileCheck %s --check-prefix=EPILOGUNWINDREQUIREV2
-// EPILOGUNWINDREQUIREV2: -fwinx64-eh-unwindv2=require
+// EPILOGUNWINDREQUIREV2: -fwinx64-eh-unwind=v2-required
+
+// RUN: not %clang --target=x86_64-windows-msvc -fsyntax-only -fwinx64-eh-unwind=invalid %s 2>&1 | FileCheck %s --check-prefix=UNWIND_INVALID
+// UNWIND_INVALID: error: invalid value 'invalid' in '-fwinx64-eh-unwind=invalid'
+
+// RUN: not %clang --target=x86_64-windows-msvc -fwinx64-eh-unwindv2=invalid -### %s 2>&1 | FileCheck %s --check-prefix=UNWINDV2_INVALID
+// UNWINDV2_INVALID: error: invalid value 'invalid' in '-fwinx64-eh-unwindv2=invalid'
 
 // RUN: %clang_cl /funcoverride:override_me1 /funcoverride:override_me2 /c -### -- %s 2>&1 | FileCheck %s --check-prefix=FUNCOVERRIDE
 // FUNCOVERRIDE: -loader-replaceable-function=override_me1
diff --git a/clang/test/Driver/winx64-eh-unwind.c b/clang/test/Driver/winx64-eh-unwind.c
new file mode 100644
index 0000000000000..036c44aeed87c
--- /dev/null
+++ b/clang/test/Driver/winx64-eh-unwind.c
@@ -0,0 +1,22 @@
+// RUN: %clang -### --target=x86_64-windows-msvc -fwinx64-eh-unwind=v1 %s 2>&1 | FileCheck --check-prefix=V1 %s
+// RUN: %clang -### --target=x86_64-windows-msvc -fwinx64-eh-unwind=v2-best-effort %s 2>&1 | FileCheck --check-prefix=V2BE %s
+// RUN: %clang -### --target=x86_64-windows-msvc -fwinx64-eh-unwind=v2-required %s 2>&1 | FileCheck --check-prefix=V2REQ %s
+// RUN: %clang -### --target=x86_64-windows-msvc -fwinx64-eh-unwind=v3 %s 2>&1 | FileCheck --check-prefix=V3 %s
+
+// Legacy -fwinx64-eh-unwindv2= translation.
+// RUN: %clang -### --target=x86_64-windows-msvc -fwinx64-eh-unwindv2=best-effort %s 2>&1 | FileCheck --check-prefix=V2BE %s
+// RUN: %clang -### --target=x86_64-windows-msvc -fwinx64-eh-unwindv2=required %s 2>&1 | FileCheck --check-prefix=V2REQ %s
+// Legacy disabled maps to v1 default — no flag should be forwarded.
+// RUN: %clang -### --target=x86_64-windows-msvc -fwinx64-eh-unwindv2=disabled %s 2>&1 | FileCheck --check-prefix=V1DISABLED %s
+
+// MSVC compatibility flags.
+// RUN: %clang_cl -### --target=x86_64-windows-msvc /d2epilogunwind -- %s 2>&1 | FileCheck --check-prefix=V2BE %s
+// RUN: %clang_cl -### --target=x86_64-windows-msvc /d2epilogunwindrequirev2 -- %s 2>&1 | FileCheck --check-prefix=V2REQ %s
+
+// V1:          "-fwinx64-eh-unwind=v1"
+// V1DISABLED-NOT: "-fwinx64-eh-unwind=
+// V2BE:        "-fwinx64-eh-unwind=v2-best-effort"
+// V2REQ:       "-fwinx64-eh-unwind=v2-required"
+// V3:          "-fwinx64-eh-unwind=v3"
+
+void f(void) {}
diff --git a/llvm/docs/ReleaseNotes.md b/llvm/docs/ReleaseNotes.md
index 013b5d0a52067..158fad7907a3e 100644
--- a/llvm/docs/ReleaseNotes.md
+++ b/llvm/docs/ReleaseNotes.md
@@ -241,6 +241,12 @@ Makes programs 10x faster by doing Special New Thing.
   in use. This matches the behaviour of Intel syntax and aids with
   compatibility when changing the default Clang syntax to the Intel syntax.
 
+* Added ``.seh_push2regs`` assembly directive for explicitly encoding a
+  two-register push in Windows x64 V3 unwind info. The directive takes two
+  register operands: ``.seh_push2regs %r12, %r13``.
+* EGPR (R16-R31) now requires V3 unwind info on Windows x64. Using EGPR
+  without V3 unwind produces a fatal error.
+
 ### Changes to the OCaml bindings
 
 ### Changes to the Python bindings
diff --git a/llvm/include/llvm/IR/Module.h b/llvm/include/llvm/IR/Module.h
index e71dd36ba5155..de042562a5f0a 100644
--- a/llvm/include/llvm/IR/Module.h
+++ b/llvm/include/llvm/IR/Module.h
@@ -1084,9 +1084,8 @@ class LLVM_ABI Module {
   /// Returns target-abi from MDString, null if target-abi is absent.
   StringRef getTargetABIFromMD();
 
-  /// Get how unwind v2 (epilog) information should be generated for x64
-  /// Windows.
-  WinX64EHUnwindV2Mode getWinX64EHUnwindV2Mode() const;
+  /// Get how unwind information should be generated for x64 Windows.
+  WinX64EHUnwindMode getWinX64EHUnwindMode() const;
 
   /// Gets the Control Flow Guard mode.
   ControlFlowGuardMode getControlFlowGuardMode() const;
diff --git a/llvm/include/llvm/MC/MCStreamer.h b/llvm/include/llvm/MC/MCStreamer.h
index 3d249408d07a8..750e3269cc0e3 100644
--- a/llvm/include/llvm/MC/MCStreamer.h
+++ b/llvm/include/llvm/MC/MCStreamer.h
@@ -236,6 +236,9 @@ class LLVM_ABI MCStreamer {
   WinEH::FrameInfo *CurrentWinFrameInfo;
   size_t CurrentProcWinFrameInfoStartIndex;
 
+  /// Default unwind version for new WinCFI frames.
+  uint8_t DefaultWinCFIUnwindVersion = 1;
+
   /// This is stack of current and previous section values saved by
   /// pushSection.
   SmallVector<std::pair<MCSectionSubPair, MCSectionSubPair>, 4> SectionStack;
@@ -1065,6 +1068,8 @@ class LLVM_ABI MCStreamer {
   virtual void emitWinCFIFuncletOrFuncEnd(SMLoc Loc = SMLoc());
   virtual void emitWinCFISplitChained(SMLoc Loc = SMLoc());
   virtual void emitWinCFIPushReg(MCRegister Register, SMLoc Loc = SMLoc());
+  virtual void emitWinCFIPush2Regs(MCRegister Reg1, MCRegister Reg2,
+                                   SMLoc Loc = SMLoc());
   virtual void emitWinCFISetFrame(MCRegister Register, unsigned Offset,
                                   SMLoc Loc = SMLoc());
   virtual void emitWinCFIAllocStack(unsigned Size, SMLoc Loc = SMLoc());
@@ -1078,6 +1083,11 @@ class LLVM_ABI MCStreamer {
   virtual void emitWinCFIEndEpilogue(SMLoc Loc = SMLoc());
   virtual void emitWinCFIUnwindV2Start(SMLoc Loc = SMLoc());
   virtual void emitWinCFIUnwindVersion(uint8_t Version, SMLoc Loc = SMLoc());
+
+  /// Set the default unwind version for new WinCFI frames.
+  void setDefaultWinCFIUnwindVersion(uint8_t V) {
+    DefaultWinCFIUnwindVersion = V;
+  }
   virtual void emitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except,
                                 SMLoc Loc = SMLoc());
   virtual void emitWinEHHandlerData(SMLoc Loc = SMLoc());
diff --git a/llvm/include/llvm/MC/MCWin64EH.h b/llvm/include/llvm/MC/MCWin64EH.h
index f9af9857d6867..dcd6c6497d111 100644
--- a/llvm/include/llvm/MC/MCWin64EH.h
+++ b/llvm/include/llvm/MC/MCWin64EH.h
@@ -26,6 +26,9 @@ struct Instruction {
   static WinEH::Instruction PushNonVol(MCSymbol *L, unsigned Reg) {
     return WinEH::Instruction(Win64EH::UOP_PushNonVol, L, Reg, -1);
   }
+  static WinEH::Instruction Push2(MCSymbol *L, unsigned Reg1, unsigned Reg2) {
+    return WinEH::Instruction(Win64EH::UOP_Push2, L, Reg1, Reg2, -1);
+  }
   static WinEH::Instruction Alloc(MCSymbol *L, unsigned Size) {
     return WinEH::Instruction(Size > 128 ? UOP_AllocLarge : UOP_AllocSmall, L,
                               -1, Size);
@@ -70,6 +73,9 @@ class ARM64UnwindEmitter : public WinEH::UnwindEmitter {
   void EmitUnwindInfo(MCStreamer &Streamer, WinEH::FrameInfo *FI,
                       bool HandlerData) const override;
 };
+/// Encode a single WinEH::Instruction as V3 WOD bytes.
+/// Appends encoded bytes to Out.
+void EncodeWOD(const WinEH::Instruction &Inst, SmallVectorImpl<uint8_t> &Out);
 } // namespace Win64EH
 } // namespace llvm
 
diff --git a/llvm/include/llvm/MC/MCWinEH.h b/llvm/include/llvm/MC/MCWinEH.h
index 9fbfd34da8e64..3d96f1d842fae 100644
--- a/llvm/include/llvm/MC/MCWinEH.h
+++ b/llvm/include/llvm/MC/MCWinEH.h
@@ -23,17 +23,22 @@ namespace WinEH {
 struct Instruction {
   const MCSymbol *Label;
   unsigned Offset;
-  unsigned Register;
-  unsigned Operation;
+  uint16_t Register;
+  uint16_t Register2; // For 2-register ops (e.g. PUSH2)
+  uint8_t Operation;
 
   Instruction(...
[truncated]

``````````

</details>


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


More information about the llvm-commits mailing list