[clang] [lld] [llvm] Fix Windows EH IP2State tables (remove +1 bias) (PR #144745)

2025-07-21 Thread via cfe-commits

https://github.com/sivadeilra updated 
https://github.com/llvm/llvm-project/pull/144745

>From a7e33eb275d8a8b18b5f5bacda15dd931cce0434 Mon Sep 17 00:00:00 2001
From: Arlie Davis 
Date: Fri, 13 Jun 2025 12:45:34 -0700
Subject: [PATCH 1/2] Fix IP2State tables

style: revert one change

Adjust tests

adjust another test

fix llvm/test/CodeGen/XCore/exception.ll

PR feedback

PR feedback

update microsoft-abi-eh-async.cpp for tighter NOP handling

PR feedback: use INT3 instead of NOP for noreturn

update win64_frame.ll

Fix bug, plus more accurate NOP optimization

Fix win64-seh-epilogue-statepoint.ll

More accurate optimization for NOP insertion for EH

update tests

update test
---
 .../CodeGenCXX/microsoft-abi-eh-async.cpp | 209 +++
 .../CodeGenCXX/microsoft-abi-eh-disabled.cpp  | 136 ++
 .../CodeGenCXX/microsoft-abi-eh-ip2state.cpp  | 241 ++
 lld/test/COFF/lto-comdat.ll   |   4 +-
 llvm/include/llvm/CodeGen/AsmPrinter.h|   4 +
 llvm/include/llvm/CodeGen/AsmPrinterHandler.h |   4 +
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp|   4 +
 llvm/lib/CodeGen/AsmPrinter/WinException.cpp  |  16 +-
 llvm/lib/CodeGen/AsmPrinter/WinException.h|   3 +-
 llvm/lib/Target/X86/X86AsmPrinter.h   |   1 +
 llvm/lib/Target/X86/X86MCInstLower.cpp| 199 ---
 .../test/CodeGen/WinEH/wineh-noret-cleanup.ll |  14 +-
 .../CodeGen/X86/apx/push2-pop2-cfi-seh.ll |   3 -
 llvm/test/CodeGen/X86/avx512-intel-ocl.ll |   2 -
 llvm/test/CodeGen/X86/avx512-regcall-Mask.ll  |   3 -
 llvm/test/CodeGen/X86/call-rv-marker.ll   |   1 -
 .../CodeGen/X86/catchret-empty-fallthrough.ll |   2 +-
 .../CodeGen/X86/cfguard-x86-64-vectorcall.ll  |   1 -
 .../CodeGen/X86/conditional-tailcall-pgso.ll  |   1 -
 llvm/test/CodeGen/X86/conditional-tailcall.ll |   1 -
 llvm/test/CodeGen/X86/mixed-ptr-sizes.ll  |   1 -
 llvm/test/CodeGen/X86/no-sse-win64.ll |   2 -
 llvm/test/CodeGen/X86/noreturn-call-win64.ll  |  12 +
 .../CodeGen/X86/preserve_nonecc_call_win.ll   |   1 -
 llvm/test/CodeGen/X86/segmented-stacks.ll |   5 -
 llvm/test/CodeGen/X86/seh-catch-all.ll|   2 +-
 llvm/test/CodeGen/X86/seh-catchpad.ll |  10 +-
 llvm/test/CodeGen/X86/seh-except-finally.ll   |   6 +-
 llvm/test/CodeGen/X86/seh-finally.ll  |   2 +-
 llvm/test/CodeGen/X86/seh-safe-div.ll |   5 +-
 .../X86/seh-unwind-inline-asm-codegen.ll  |   4 +-
 llvm/test/CodeGen/X86/stack-coloring-wineh.ll |   6 +-
 .../CodeGen/X86/win-catchpad-nested-cxx.ll|   8 +-
 llvm/test/CodeGen/X86/win-catchpad.ll |   8 +-
 llvm/test/CodeGen/X86/win-cleanuppad.ll   |   2 +-
 .../X86/win-import-call-optimization.ll   |   1 -
 llvm/test/CodeGen/X86/win-smallparams.ll  |   1 -
 llvm/test/CodeGen/X86/win32-eh-states.ll  |  14 +-
 llvm/test/CodeGen/X86/win64-byval.ll  |   3 -
 .../X86/win64-seh-epilogue-statepoint.ll  |   1 -
 llvm/test/CodeGen/X86/win64_call_epi.ll   |   2 +-
 llvm/test/CodeGen/X86/win64_frame.ll  |   2 -
 llvm/test/CodeGen/X86/wineh-coreclr.ll|  14 +
 llvm/test/CodeGen/X86/wineh-no-ehpads.ll  |   1 -
 llvm/test/CodeGen/XCore/exception.ll  |   2 +-
 llvm/test/DebugInfo/COFF/local-variables.ll   |   3 +-
 .../Inputs/mips64_eh.ll.expected  |   6 +-
 47 files changed, 841 insertions(+), 132 deletions(-)
 create mode 100644 clang/test/CodeGenCXX/microsoft-abi-eh-async.cpp
 create mode 100644 clang/test/CodeGenCXX/microsoft-abi-eh-disabled.cpp
 create mode 100644 clang/test/CodeGenCXX/microsoft-abi-eh-ip2state.cpp

diff --git a/clang/test/CodeGenCXX/microsoft-abi-eh-async.cpp 
b/clang/test/CodeGenCXX/microsoft-abi-eh-async.cpp
new file mode 100644
index 0..b83173742a2a7
--- /dev/null
+++ b/clang/test/CodeGenCXX/microsoft-abi-eh-async.cpp
@@ -0,0 +1,209 @@
+// REQUIRES: x86-registered-target
+
+// RUN: %clang_cl -c --target=x86_64-windows-msvc /EHa -O2 /GS- \
+// RUN:   -Xclang=-import-call-optimization \
+// RUN:   /clang:-S /clang:-o- -- %s 2>&1 \
+// RUN:   | FileCheck %s
+
+#ifdef __clang__
+#define NO_TAIL __attribute((disable_tail_calls))
+#else
+#define NO_TAIL
+#endif
+
+void might_throw();
+void other_func(int x);
+
+void does_not_throw() noexcept(true);
+
+extern "C" void __declspec(dllimport) some_dll_import();
+
+class HasDtor {
+int x;
+char foo[40];
+
+public:
+explicit HasDtor(int x);
+~HasDtor();
+};
+
+class BadError {
+public:
+int errorCode;
+};
+
+void normal_has_regions() {
+// CHECK-LABEL: .def "?normal_has_regions@@YAXXZ"
+// CHECK: .seh_endprologue
+
+// <-- state -1 (none)
+{
+HasDtor hd{42};
+
+// <-- state goes from -1 to 0
+// because state changes, we expect the HasDtor::HasDtor() call to 
have a NOP
+// CHECK: call "??0HasDtor@@QEAA@H@Z"
+// CHECK-NEXT: nop
+
+might_throw();
+// CHECK: call "?might_throw@@YAXXZ"
+// CHECK-NEXT: no

[clang] [lld] [llvm] Fix Windows EH IP2State tables (remove +1 bias) (PR #144745)

2025-07-17 Thread Roland McGrath via cfe-commits

frobtech wrote:

> Actually, thinking about it, maybe we should just do the nop insertion for 
> UEFI targets. The UEFI environment is very similar to Windows. CC @Prabhuk .

I think that's an appropriately conservative place to start, anyway.  The 
uefi-* targets are still nascent and heretofore the Windows targets have been 
used for producing UEFI binaries.  So the approach we've taken is that for all 
miscellaneous code generation issues, we should start with the UEFI target 
matching the behavior of the corresponding Windows target exactly.  That seems 
like the right thing to do for now.  In the future as the uefi-* targets 
mature, we can reconsider things that are really only relevant to Windows ABIs 
and not to the UEFI ABI or the PE-COFF format.  The Windows EH ABI is not an 
aspect of the UEFI ABI, but the support for Windows EH does not interfere with 
UEFI so I think it's harmless enough to emit all the same stuff.  

https://github.com/llvm/llvm-project/pull/144745
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [lld] [llvm] Fix Windows EH IP2State tables (remove +1 bias) (PR #144745)

2025-07-16 Thread via cfe-commits

https://github.com/sivadeilra updated 
https://github.com/llvm/llvm-project/pull/144745

>From e7ccf87561e090a224021e2223e36599396f6335 Mon Sep 17 00:00:00 2001
From: Arlie Davis 
Date: Fri, 13 Jun 2025 12:45:34 -0700
Subject: [PATCH] Fix IP2State tables

style: revert one change

Adjust tests

adjust another test

fix llvm/test/CodeGen/XCore/exception.ll

PR feedback

PR feedback

update microsoft-abi-eh-async.cpp for tighter NOP handling

PR feedback: use INT3 instead of NOP for noreturn

update win64_frame.ll

Fix bug, plus more accurate NOP optimization

Fix win64-seh-epilogue-statepoint.ll

More accurate optimization for NOP insertion for EH

update tests

update test
---
 .../CodeGenCXX/microsoft-abi-eh-async.cpp | 209 +++
 .../CodeGenCXX/microsoft-abi-eh-disabled.cpp  | 136 ++
 .../CodeGenCXX/microsoft-abi-eh-ip2state.cpp  | 241 ++
 lld/test/COFF/lto-comdat.ll   |   4 +-
 llvm/include/llvm/CodeGen/AsmPrinter.h|   4 +
 llvm/include/llvm/CodeGen/AsmPrinterHandler.h |   4 +
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp|   4 +
 llvm/lib/CodeGen/AsmPrinter/WinException.cpp  |  16 +-
 llvm/lib/CodeGen/AsmPrinter/WinException.h|   3 +-
 llvm/lib/Target/X86/X86AsmPrinter.h   |   1 +
 llvm/lib/Target/X86/X86MCInstLower.cpp| 199 ---
 .../test/CodeGen/WinEH/wineh-noret-cleanup.ll |  14 +-
 .../CodeGen/X86/apx/push2-pop2-cfi-seh.ll |   3 -
 llvm/test/CodeGen/X86/avx512-intel-ocl.ll |   2 -
 llvm/test/CodeGen/X86/avx512-regcall-Mask.ll  |   3 -
 llvm/test/CodeGen/X86/call-rv-marker.ll   |   1 -
 .../CodeGen/X86/catchret-empty-fallthrough.ll |   2 +-
 .../CodeGen/X86/cfguard-x86-64-vectorcall.ll  |   1 -
 .../CodeGen/X86/conditional-tailcall-pgso.ll  |   1 -
 llvm/test/CodeGen/X86/conditional-tailcall.ll |   1 -
 llvm/test/CodeGen/X86/mixed-ptr-sizes.ll  |   1 -
 llvm/test/CodeGen/X86/no-sse-win64.ll |   2 -
 llvm/test/CodeGen/X86/noreturn-call-win64.ll  |  12 +
 .../CodeGen/X86/preserve_nonecc_call_win.ll   |   1 -
 llvm/test/CodeGen/X86/segmented-stacks.ll |   5 -
 llvm/test/CodeGen/X86/seh-catch-all.ll|   2 +-
 llvm/test/CodeGen/X86/seh-catchpad.ll |  10 +-
 llvm/test/CodeGen/X86/seh-except-finally.ll   |   6 +-
 llvm/test/CodeGen/X86/seh-finally.ll  |   2 +-
 llvm/test/CodeGen/X86/seh-safe-div.ll |   5 +-
 .../X86/seh-unwind-inline-asm-codegen.ll  |   4 +-
 llvm/test/CodeGen/X86/stack-coloring-wineh.ll |   6 +-
 .../CodeGen/X86/win-catchpad-nested-cxx.ll|   8 +-
 llvm/test/CodeGen/X86/win-catchpad.ll |   8 +-
 llvm/test/CodeGen/X86/win-cleanuppad.ll   |   2 +-
 .../X86/win-import-call-optimization.ll   |   1 -
 llvm/test/CodeGen/X86/win-smallparams.ll  |   1 -
 llvm/test/CodeGen/X86/win32-eh-states.ll  |  14 +-
 llvm/test/CodeGen/X86/win64-byval.ll  |   3 -
 .../X86/win64-seh-epilogue-statepoint.ll  |   1 -
 llvm/test/CodeGen/X86/win64_call_epi.ll   |   2 +-
 llvm/test/CodeGen/X86/win64_frame.ll  |   2 -
 llvm/test/CodeGen/X86/wineh-coreclr.ll|  14 +
 llvm/test/CodeGen/X86/wineh-no-ehpads.ll  |   1 -
 llvm/test/CodeGen/XCore/exception.ll  |   2 +-
 llvm/test/DebugInfo/COFF/local-variables.ll   |   3 +-
 .../Inputs/mips64_eh.ll.expected  |   6 +-
 47 files changed, 841 insertions(+), 132 deletions(-)
 create mode 100644 clang/test/CodeGenCXX/microsoft-abi-eh-async.cpp
 create mode 100644 clang/test/CodeGenCXX/microsoft-abi-eh-disabled.cpp
 create mode 100644 clang/test/CodeGenCXX/microsoft-abi-eh-ip2state.cpp

diff --git a/clang/test/CodeGenCXX/microsoft-abi-eh-async.cpp 
b/clang/test/CodeGenCXX/microsoft-abi-eh-async.cpp
new file mode 100644
index 0..b83173742a2a7
--- /dev/null
+++ b/clang/test/CodeGenCXX/microsoft-abi-eh-async.cpp
@@ -0,0 +1,209 @@
+// REQUIRES: x86-registered-target
+
+// RUN: %clang_cl -c --target=x86_64-windows-msvc /EHa -O2 /GS- \
+// RUN:   -Xclang=-import-call-optimization \
+// RUN:   /clang:-S /clang:-o- -- %s 2>&1 \
+// RUN:   | FileCheck %s
+
+#ifdef __clang__
+#define NO_TAIL __attribute((disable_tail_calls))
+#else
+#define NO_TAIL
+#endif
+
+void might_throw();
+void other_func(int x);
+
+void does_not_throw() noexcept(true);
+
+extern "C" void __declspec(dllimport) some_dll_import();
+
+class HasDtor {
+int x;
+char foo[40];
+
+public:
+explicit HasDtor(int x);
+~HasDtor();
+};
+
+class BadError {
+public:
+int errorCode;
+};
+
+void normal_has_regions() {
+// CHECK-LABEL: .def "?normal_has_regions@@YAXXZ"
+// CHECK: .seh_endprologue
+
+// <-- state -1 (none)
+{
+HasDtor hd{42};
+
+// <-- state goes from -1 to 0
+// because state changes, we expect the HasDtor::HasDtor() call to 
have a NOP
+// CHECK: call "??0HasDtor@@QEAA@H@Z"
+// CHECK-NEXT: nop
+
+might_throw();
+// CHECK: call "?might_throw@@YAXXZ"
+// CHECK-NEXT: nop
+