[llvm] [MergeFunctions] Fix merging functions with different KCFI type identifiers (PR #217665)

via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 20 08:56:48 PDT 2026


https://github.com/Lane0218 updated https://github.com/llvm/llvm-project/pull/217665

>From 4d139ea6357d19bc23ce99e731ad6ed16ec8b36e Mon Sep 17 00:00:00 2001
From: Lane0218 <laneljc at qq.com>
Date: Thu, 20 Aug 2026 23:26:33 +0800
Subject: [PATCH] [MergeFunctions] Fix merging functions with different KCFI
 type identifiers

---
 .../Transforms/Utils/FunctionComparator.cpp   |  4 +++
 .../MergeFunc/kcfi-function-merging.ll        | 28 +++++++++++++++++++
 2 files changed, 32 insertions(+)
 create mode 100644 llvm/test/Transforms/MergeFunc/kcfi-function-merging.ll

diff --git a/llvm/lib/Transforms/Utils/FunctionComparator.cpp b/llvm/lib/Transforms/Utils/FunctionComparator.cpp
index 05c24af6efb39..9e84c78087303 100644
--- a/llvm/lib/Transforms/Utils/FunctionComparator.cpp
+++ b/llvm/lib/Transforms/Utils/FunctionComparator.cpp
@@ -990,6 +990,10 @@ int FunctionComparator::compareSignature() const {
   if (int Res = cmpAttrs(FnL->getAttributes(), FnR->getAttributes()))
     return Res;
 
+  if (int Res = cmpMDNode(FnL->getMetadata(LLVMContext::MD_kcfi_type),
+                          FnR->getMetadata(LLVMContext::MD_kcfi_type)))
+    return Res;
+
   if (int Res = cmpNumbers(FnL->hasGC(), FnR->hasGC()))
     return Res;
 
diff --git a/llvm/test/Transforms/MergeFunc/kcfi-function-merging.ll b/llvm/test/Transforms/MergeFunc/kcfi-function-merging.ll
new file mode 100644
index 0000000000000..ad62c5f6d5f2e
--- /dev/null
+++ b/llvm/test/Transforms/MergeFunc/kcfi-function-merging.ll
@@ -0,0 +1,28 @@
+; RUN: opt -S -passes=mergefunc < %s | FileCheck %s
+
+; Functions with different KCFI type identifiers must not be merged.
+
+define internal i32 @a() unnamed_addr !kcfi_type !0 {
+entry:
+  ret i32 0
+}
+
+define internal i32 @b() unnamed_addr !kcfi_type !1 {
+entry:
+  ret i32 0
+}
+
+define i32 @caller() {
+; CHECK-LABEL: define i32 @caller()
+; CHECK-NEXT: entry:
+; CHECK-NEXT: %x = call i32 @a()
+; CHECK-NEXT: %y = call i32 @b()
+; CHECK-NEXT: ret i32 %y
+entry:
+  %x = call i32 @a()
+  %y = call i32 @b()
+  ret i32 %y
+}
+
+!0 = !{i32 1234}
+!1 = !{i32 6789}



More information about the llvm-commits mailing list