[llvm-branch-commits] [NFC][CrossDSOCFI] Move findCfiTypeIds to LowerTypeTests (PR #223582)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Sep 14 19:24:09 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Vitaly Buka (vitalybuka)

<details>
<summary>Changes</summary>

Move 64-bit numeric type identifier collection logic and extractNumericTypeId
from CrossDSOCFI.cpp into LowerTypeTests.cpp, exposing it as
lowertypetests::findCfiTypeIds in llvm/Transforms/IPO/LowerTypeTests.h.

Encapsulate Cross-DSO CFI type ID discovery so CrossDSOCFI no longer
directly parses cfi.functions metadata.

Metadata is accessed with:
- !type: global objects via GO.getMetadata(LLVMContext::MD_type)
- cfi.functions: operands 3..N containing !type MDNodes


---
Full diff: https://github.com/llvm/llvm-project/pull/223582.diff


3 Files Affected:

- (modified) llvm/include/llvm/Transforms/IPO/LowerTypeTests.h (+3) 
- (modified) llvm/lib/Transforms/IPO/CrossDSOCFI.cpp (+2-36) 
- (modified) llvm/lib/Transforms/IPO/LowerTypeTests.cpp (+40) 


``````````diff
diff --git a/llvm/include/llvm/Transforms/IPO/LowerTypeTests.h b/llvm/include/llvm/Transforms/IPO/LowerTypeTests.h
index 0ea74ff361f4c..9b86d7fe2d270 100644
--- a/llvm/include/llvm/Transforms/IPO/LowerTypeTests.h
+++ b/llvm/include/llvm/Transforms/IPO/LowerTypeTests.h
@@ -227,6 +227,9 @@ LLVM_ABI bool hasTypeMetadata(const GlobalObject *GO);
 /// entries.
 LLVM_ABI SetVector<GlobalValue *> findCfiFunctions(Module &M);
 
+/// Finds all 64-bit numeric type identifiers in \p M used for cross-DSO CFI.
+LLVM_ABI SetVector<uint64_t> findCfiTypeIds(const Module &M);
+
 /// Creates cfi.functions, aliases, and symvers named metadata in \p DestM
 /// for CFI functions in \p CfiFunctions from source module \p SrcM.
 LLVM_ABI void createCfiMetadata(Module &DestM, const Module &SrcM,
diff --git a/llvm/lib/Transforms/IPO/CrossDSOCFI.cpp b/llvm/lib/Transforms/IPO/CrossDSOCFI.cpp
index 9e940015902b4..90b374bef5462 100644
--- a/llvm/lib/Transforms/IPO/CrossDSOCFI.cpp
+++ b/llvm/lib/Transforms/IPO/CrossDSOCFI.cpp
@@ -23,6 +23,7 @@
 #include "llvm/IR/MDBuilder.h"
 #include "llvm/IR/Module.h"
 #include "llvm/TargetParser/Triple.h"
+#include "llvm/Transforms/IPO/LowerTypeTests.h"
 
 using namespace llvm;
 
@@ -35,52 +36,17 @@ namespace {
 struct CrossDSOCFI {
   MDNode *VeryLikelyWeights;
 
-  ConstantInt *extractNumericTypeId(MDNode *MD);
   void buildCFICheck(Module &M);
   bool runOnModule(Module &M);
 };
 
 } // anonymous namespace
 
-/// Extracts a numeric type identifier from an MDNode containing type metadata.
-ConstantInt *CrossDSOCFI::extractNumericTypeId(MDNode *MD) {
-  // This check excludes vtables for classes inside anonymous namespaces.
-  auto TM = dyn_cast<ValueAsMetadata>(MD->getOperand(1));
-  if (!TM)
-    return nullptr;
-  auto C = dyn_cast_or_null<ConstantInt>(TM->getValue());
-  if (!C) return nullptr;
-  // We are looking for i64 constants.
-  if (C->getBitWidth() != 64) return nullptr;
-
-  return C;
-}
-
 /// buildCFICheck - emits __cfi_check for the current module.
 void CrossDSOCFI::buildCFICheck(Module &M) {
   // FIXME: verify that __cfi_check ends up near the end of the code section,
   // but before the jump slots created in LowerTypeTests.
-  SetVector<uint64_t> TypeIds;
-  SmallVector<MDNode *, 2> Types;
-  for (GlobalObject &GO : M.global_objects()) {
-    Types.clear();
-    GO.getMetadata(LLVMContext::MD_type, Types);
-    for (MDNode *Type : Types)
-      if (ConstantInt *TypeId = extractNumericTypeId(Type))
-        TypeIds.insert(TypeId->getZExtValue());
-  }
-
-  NamedMDNode *CfiFunctionsMD = M.getNamedMetadata("cfi.functions");
-  if (CfiFunctionsMD) {
-    for (auto *Func : CfiFunctionsMD->operands()) {
-      assert(Func->getNumOperands() >= 3);
-      assert(isa<ConstantAsMetadata>(Func->getOperand(2)));
-      for (unsigned I = 3; I < Func->getNumOperands(); ++I)
-        if (ConstantInt *TypeId =
-                extractNumericTypeId(cast<MDNode>(Func->getOperand(I).get())))
-          TypeIds.insert(TypeId->getZExtValue());
-    }
-  }
+  SetVector<uint64_t> TypeIds = lowertypetests::findCfiTypeIds(M);
 
   LLVMContext &Ctx = M.getContext();
   FunctionCallee C = M.getOrInsertFunction(
diff --git a/llvm/lib/Transforms/IPO/LowerTypeTests.cpp b/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
index 3697972a87f79..aaf03ed56727c 100644
--- a/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
+++ b/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
@@ -316,6 +316,46 @@ SetVector<GlobalValue *> lowertypetests::findCfiFunctions(Module &M) {
   return CfiFunctions;
 }
 
+/// Extracts a numeric type identifier from an MDNode containing type metadata.
+static ConstantInt *extractNumericTypeId(MDNode *MD) {
+  // This check excludes vtables for classes inside anonymous namespaces.
+  auto TM = dyn_cast<ValueAsMetadata>(MD->getOperand(1));
+  if (!TM)
+    return nullptr;
+  auto C = dyn_cast_or_null<ConstantInt>(TM->getValue());
+  if (!C)
+    return nullptr;
+  // We are looking for i64 constants.
+  if (C->getBitWidth() != 64)
+    return nullptr;
+
+  return C;
+}
+
+SetVector<uint64_t> lowertypetests::findCfiTypeIds(const Module &M) {
+  SetVector<uint64_t> TypeIds;
+  SmallVector<MDNode *, 2> Types;
+  for (const GlobalObject &GO : M.global_objects()) {
+    Types.clear();
+    GO.getMetadata(LLVMContext::MD_type, Types);
+    for (MDNode *Type : Types)
+      if (ConstantInt *TypeId = extractNumericTypeId(Type))
+        TypeIds.insert(TypeId->getZExtValue());
+  }
+
+  if (NamedMDNode *CfiFunctionsMD = M.getNamedMetadata("cfi.functions")) {
+    for (auto *Func : CfiFunctionsMD->operands()) {
+      assert(Func->getNumOperands() >= 3);
+      assert(isa<ConstantAsMetadata>(Func->getOperand(2)));
+      for (unsigned I = 3; I < Func->getNumOperands(); ++I)
+        if (ConstantInt *TypeId =
+                extractNumericTypeId(cast<MDNode>(Func->getOperand(I).get())))
+          TypeIds.insert(TypeId->getZExtValue());
+    }
+  }
+  return TypeIds;
+}
+
 static void createCfiFunctionsMetadata(Module &DestM,
                                        ArrayRef<GlobalValue *> CfiFunctions) {
   auto &Ctx = DestM.getContext();

``````````

</details>


https://github.com/llvm/llvm-project/pull/223582


More information about the llvm-branch-commits mailing list