[llvm-branch-commits] [clang] release/23.x: [clang][KCFI] Skip the KCFIPass on Hexagon (#211716) (PR #216583)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sun Aug 16 10:35:42 PDT 2026


https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/216583

Backport 23a601dc9690

Requested by: @androm3da

>From 970772185a773aea248ed38d1160e7317f686013 Mon Sep 17 00:00:00 2001
From: Brian Cain <brian.cain at oss.qualcomm.com>
Date: Tue, 11 Aug 2026 07:19:57 -0500
Subject: [PATCH] [clang][KCFI] Skip the KCFIPass on Hexagon (#211716)

Hexagon implements KCFI operand-bundle lowering in the back end
HexagonTargetLowering::EmitKCFICheck emits a KCFI_CHECK pseudo, which
HexagonAsmPrinter::LowerKCFI_CHECK expands into a type-hash check and a
trap - like PS_crash.

Hexagon was never added to the list in addKCFIPass() of targets whose
back end lowers the bundles, so Clang kept running the middle-end
KCFIPass for it.

Add Hexagon to the addKCFIPass() early-return so the "kcfi" bundles
reach the back end, which then emits the trapping load that actually
blocks the call.

(cherry picked from commit 23a601dc9690d42a0b6e69b10279a6f002708ee6)
---
 clang/lib/CodeGen/BackendUtil.cpp |  3 ++-
 clang/test/CodeGen/kcfi-hexagon.c | 14 ++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/CodeGen/kcfi-hexagon.c

diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index 2b755fa916e55..e3ae446fbe81b 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -668,7 +668,8 @@ static void addKCFIPass(const Triple &TargetTriple, const LangOptions &LangOpts,
   // If the back-end supports KCFI operand bundle lowering, skip KCFIPass.
   if (TargetTriple.getArch() == llvm::Triple::x86_64 ||
       TargetTriple.isAArch64(64) || TargetTriple.isRISCV() ||
-      TargetTriple.isARM() || TargetTriple.isThumb())
+      TargetTriple.isARM() || TargetTriple.isThumb() ||
+      TargetTriple.getArch() == llvm::Triple::hexagon)
     return;
 
   // Ensure we lower KCFI operand bundles with -O0.
diff --git a/clang/test/CodeGen/kcfi-hexagon.c b/clang/test/CodeGen/kcfi-hexagon.c
new file mode 100644
index 0000000000000..b08758d4b81a4
--- /dev/null
+++ b/clang/test/CodeGen/kcfi-hexagon.c
@@ -0,0 +1,14 @@
+// Hexagon lowers KCFI operand bundles in the back end. Clang must leave the "kcfi"
+// operand bundles in place for the back end instead of running the middle-end
+// KCFIPass, which would rewrite them into a software llvm.debugtrap check.
+//
+// Verify the bundle survives the optimizer pipeline at both -O0 and -O2 and
+// is not lowered to debugtrap.
+//
+// RUN: %clang_cc1 -triple hexagon-unknown-linux-musl -O0 -fsanitize=kcfi -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple hexagon-unknown-linux-musl -O2 -fsanitize=kcfi -emit-llvm -o - %s | FileCheck %s
+
+// CHECK-LABEL: define {{.*}}void @call(
+// CHECK: call void %{{.*}}() {{.*}}[ "kcfi"(i32 {{-?[0-9]+}}) ]
+// CHECK-NOT: @llvm.debugtrap
+void call(void (*f)(void)) { f(); }



More information about the llvm-branch-commits mailing list