https://github.com/eleviant created 
https://github.com/llvm/llvm-project/pull/196550

Patch lowers unreachable to the trap (ud2) when `-fms-kernel` is provided to 
ensure kernel trap handler is invoked in case something goes wrong, instead of 
falling through.

>From 619a097bf225a6de75ef18411520cec49677864f Mon Sep 17 00:00:00 2001
From: Evgeny Leviant <[email protected]>
Date: Wed, 15 Apr 2026 19:51:57 +0200
Subject: [PATCH] [clang][X86] Always emit trap for noreturn functions with
 -fms-kernel

Patch lowers unreachable to the trap (ud2) when `-fms-kernel` is
provided to ensure kernel trap handler is invoked in case something
goes wrong, instead of falling through.
---
 clang/lib/CodeGen/BackendUtil.cpp      |  3 +++
 clang/test/CodeGen/MSKernel/noreturn.c | 13 +++++++++++++
 2 files changed, 16 insertions(+)
 create mode 100644 clang/test/CodeGen/MSKernel/noreturn.c

diff --git a/clang/lib/CodeGen/BackendUtil.cpp 
b/clang/lib/CodeGen/BackendUtil.cpp
index a46a25c4492f2..6b2a81f100d7a 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -477,6 +477,9 @@ static bool initTargetOptions(const CompilerInstance &CI,
   Options.VecLib =
       convertDriverVectorLibraryToVectorLibrary(CodeGenOpts.getVecLib());
 
+  if (LangOpts.Kernel)
+    Options.TrapUnreachable = true;
+
   switch (CodeGenOpts.getSwiftAsyncFramePointer()) {
   case CodeGenOptions::SwiftAsyncFramePointerKind::Auto:
     Options.SwiftAsyncFramePointer =
diff --git a/clang/test/CodeGen/MSKernel/noreturn.c 
b/clang/test/CodeGen/MSKernel/noreturn.c
new file mode 100644
index 0000000000000..4a5eae3472b48
--- /dev/null
+++ b/clang/test/CodeGen/MSKernel/noreturn.c
@@ -0,0 +1,13 @@
+// REQUIRES: x86-registered-target
+// RUN: %clang_cc1 -fms-kernel -fms-extensions -triple x86_64-windows-msvc -O2 
-S %s -o - | FileCheck %s
+
+// CHECK-LABEL: my_noreturn_func:
+// CHECK:       movl $0, (%rax)
+// CHECK-NEXT:  ud2
+
+extern long volatile *gtrap;
+
+__declspec(noreturn) void my_noreturn_func(void) {
+    *gtrap = 0;
+}
+

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to