[llvm] 4c9a33a - [WinEH] Only emit the async-EH nop after EH_LABEL on x86 (#219275)

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 16 15:51:10 PDT 2026


Author: Jonas Heinle
Date: 2026-09-16T22:51:04Z
New Revision: 4c9a33aaee2cd827935a547603e449abf3ef734d

URL: https://github.com/llvm/llvm-project/commit/4c9a33aaee2cd827935a547603e449abf3ef734d
DIFF: https://github.com/llvm/llvm-project/commit/4c9a33aaee2cd827935a547603e449abf3ef734d.diff

LOG: [WinEH] Only emit the async-EH nop after EH_LABEL on x86 (#219275)

Under `-fasync-exceptions`, `AsmPrinter::emitFunctionBody()` emits a nop
after an `EH_LABEL` whose next instruction can fault, so that the
faulting instruction opening an EH region does not share an address with
the return address of the call before it.

`EH_LABEL` is a meta-instruction and reports a size of zero, so that nop
is invisible to `getInstSizeInBytes()` and to everything that sums it.
On AArch64 that undercounts block offsets, `AArch64CompressJumpTables`
picks a one-byte jump table entry that does not fit, and the assembler
rejects it with `value evaluated as 258 is out of range`. That is how
this was found, building OpenCV's bundled protobuf for
`aarch64-pc-windows-msvc` with clang-cl `/EHa`. `BranchRelaxation` sums
the same sizes and has the same hole.

Only x86 needs the padding. On Arm64 the unwinder already backs the PC
up over a call when it knows there was one, which is exactly the case
the nop protects against, So gate the nop on x86 triples. That also
stops emitting it on armv7-windows, same size mismatch on #221687.

That in turn lets the `EH_LABEL` exception added to
`TargetInstrInfo::getInstSizeVerifyMode()` in #223641 go. x86 still
emits the nop, but x86 does not implement `getInstSizeInBytes()`, so
the base class returns `~0U` and the `AllowOverEstimate` comparison is
vacuous there. Every target that does implement it has stopped emitting
the nop.

Assisted-by: Claude (Anthropic)

Added: 
    llvm/test/CodeGen/WinEH/wineh-eha-nop.ll

Modified: 
    llvm/include/llvm/CodeGen/TargetInstrInfo.h
    llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Removed: 
    llvm/test/CodeGen/AArch64/wineh-async-eh-pad-instr-size.ll


################################################################################
diff  --git a/llvm/include/llvm/CodeGen/TargetInstrInfo.h b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
index 260cf794c731f..eff16348a8bc0 100644
--- a/llvm/include/llvm/CodeGen/TargetInstrInfo.h
+++ b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
@@ -450,9 +450,6 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
   /// getInstSizeInBytes() should be verified.
   virtual InstSizeVerifyMode
   getInstSizeVerifyMode(const MachineInstr &MI) const {
-    // FIXME: Incorrect size reported if nop inserted for async exceptions.
-    if (MI.getOpcode() == TargetOpcode::EH_LABEL)
-      return InstSizeVerifyMode::NoVerify;
     return InstSizeVerifyMode::AllowOverEstimate;
   }
 

diff  --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index fe034ac8c97c5..52f8f148f7487 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -2097,7 +2097,9 @@ void AsmPrinter::emitFunctionBody() {
   // Print out code for the function.
   bool HasAnyRealCode = false;
   int NumInstsInFunction = 0;
-  bool IsEHa = MMI->getModule()->getModuleFlag("eh-asynch");
+  // Only x86 needs this padding; the Arm unwinders back the PC up themselves.
+  bool NeedsEHaNops = MMI->getModule()->getModuleFlag("eh-asynch") &&
+                      TM.getTargetTriple().isX86();
 
   const MCSubtargetInfo *STI = nullptr;
   if (this->MF)
@@ -2190,7 +2192,7 @@ void AsmPrinter::emitFunctionBody() {
         //  an EH region as it must be led by at least a Load
         {
           auto MI2 = std::next(MI.getIterator());
-          if (IsEHa && MI2 != MBB.end() &&
+          if (NeedsEHaNops && MI2 != MBB.end() &&
               (MI2->mayLoadOrStore() || MI2->mayRaiseFPException()))
             emitNops(1);
         }

diff  --git a/llvm/test/CodeGen/AArch64/wineh-async-eh-pad-instr-size.ll b/llvm/test/CodeGen/AArch64/wineh-async-eh-pad-instr-size.ll
deleted file mode 100644
index d6eca0b3a6a99..0000000000000
--- a/llvm/test/CodeGen/AArch64/wineh-async-eh-pad-instr-size.ll
+++ /dev/null
@@ -1,34 +0,0 @@
-; RUN: llc -mtriple=aarch64-unknown-windows-msvc -filetype=obj < %s | llvm-objdump -d - | FileCheck %s
-
-; Check that there is no instruction size verification error when a nop has
-; to be inserted for an EH_LABEL.
-
-; CHECK: nop
-define void @test() personality ptr @__C_specific_handler {
-entry:
-  %a = alloca i32, align 4
-  invoke void @llvm.seh.try.begin()
-          to label %invoke.cont unwind label %catch.dispatch
-
-invoke.cont:
-  %a.val = load volatile i32, ptr %a, align 4
-  invoke void @llvm.seh.try.end()
-          to label %exit unwind label %catch.dispatch
-
-catch.dispatch:
-  %cs = catchswitch within none [label %__except.ret] unwind to caller
-
-__except.ret:
-  %cp = catchpad within %cs [ptr @filt]
-  catchret from %cp to label %exit
-
-exit:
-  ret void
-}
-
-declare i32 @filt(ptr %exception_pointers, ptr %frame_pointer)
-
-declare i32 @__C_specific_handler(...)
-
-!llvm.module.flags = !{!1}
-!1 = !{i32 2, !"eh-asynch", i32 1}

diff  --git a/llvm/test/CodeGen/WinEH/wineh-eha-nop.ll b/llvm/test/CodeGen/WinEH/wineh-eha-nop.ll
new file mode 100644
index 0000000000000..d221cde28f106
--- /dev/null
+++ b/llvm/test/CodeGen/WinEH/wineh-eha-nop.ll
@@ -0,0 +1,41 @@
+; RUN: llc -mtriple=x86_64-pc-windows-msvc < %s | FileCheck %s --check-prefix=X64
+; RUN: %if aarch64-registered-target %{ llc -mtriple=aarch64-pc-windows-msvc < %s | FileCheck %s --check-prefix=ARM64 %}
+
+; x86 pads a faulting instruction that opens an EH region away from the return
+; of the call before it. The Arm64 unwinder backs the PC up itself, so it does
+; not need that nop.
+
+declare i32 @__CxxFrameHandler3(...)
+declare void @llvm.seh.scope.begin()
+declare void @llvm.seh.scope.end()
+declare void @dtor(ptr)
+
+define void @f(ptr %p) personality ptr @__CxxFrameHandler3 {
+entry:
+  invoke void @llvm.seh.scope.begin()
+          to label %cont unwind label %cleanup
+
+cont:
+  store volatile i32 0, ptr %p, align 4
+  invoke void @llvm.seh.scope.end()
+          to label %done unwind label %cleanup
+
+done:
+  ret void
+
+cleanup:
+  %cp = cleanuppad within none []
+  call void @dtor(ptr %p) [ "funclet"(token %cp) ]
+  cleanupret from %cp unwind to caller
+}
+
+; X64-LABEL: f:
+; X64:        # EH_LABEL
+; X64-NEXT:   nop
+
+; ARM64-LABEL: f:
+; ARM64:       // EH_LABEL
+; ARM64-NOT:   nop
+
+!llvm.module.flags = !{!0}
+!0 = !{i32 2, !"eh-asynch", i32 1}


        


More information about the llvm-commits mailing list