https://github.com/Prabhuk created 
https://github.com/llvm/llvm-project/pull/210194

Internal linkage symbols were excluded from call graph section as the
type metdata generation for these symbols were not correct. This patch
cleans up metadata creation for MD_callgraph by correctly emitting
generalized type metadata to be used for this purpose.


>From 9b20b015588080640352fbb7970899cf616ec45b Mon Sep 17 00:00:00 2001
From: prabhukr <[email protected]>
Date: Thu, 16 Jul 2026 21:48:02 +0000
Subject: [PATCH] [clang] Emit call graph type metadata for internal linkage
 symbols

Internal linkage symbols were excluded from call graph section as the
type metdata generation for these symbols were not correct. This patch
cleans up metadata creation for MD_callgraph by correctly emitting
generalized type metadata to be used for this purpose.
---
 clang/lib/CodeGen/CodeGenModule.cpp           | 34 ++++++++++---------
 clang/lib/CodeGen/CodeGenModule.h             |  9 ++++-
 .../CodeGen/call-graph-section-internal.cpp   | 11 +++---
 3 files changed, 32 insertions(+), 22 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index 78627047b19ad..8f96c994161e0 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -3459,10 +3459,11 @@ void CodeGenModule::createIndirectFunctionTypeMD(const 
FunctionDecl *FD,
       F->getFunction().hasAddressTaken(nullptr, /*IgnoreCallbackUses=*/true,
                                        /*IgnoreAssumeLikeCalls=*/true,
                                        /*IgnoreLLVMUsed=*/false)) {
-    F->addMetadata(llvm::LLVMContext::MD_callgraph,
-                   *llvm::MDTuple::get(
-                       getLLVMContext(),
-                       {CreateMetadataIdentifierGeneralized(FD->getType())}));
+    F->addMetadata(
+        llvm::LLVMContext::MD_callgraph,
+        *llvm::MDTuple::get(
+            getLLVMContext(),
+            {CreateMetadataIdentifierForCallGraphType(FD->getType())}));
   }
 }
 
@@ -3494,15 +3495,11 @@ void 
CodeGenModule::createFunctionTypeMetadataForIcall(const FunctionDecl *FD,
 
 void CodeGenModule::createCalleeTypeMetadataForIcall(const QualType &QT,
                                                      llvm::CallBase *CB) {
-  // Only if needed for call graph section and only for indirect calls that are
-  // visible externally.
-  // TODO: Handle local linkage symbols so they are not left out of call graph
-  // reducing precision.
-  if (!CodeGenOpts.CallGraphSection || !CB->isIndirectCall() ||
-      !isExternallyVisible(QT->getLinkage()))
+  // Only if needed for call graph section and only for indirect calls
+  if (!CodeGenOpts.CallGraphSection || !CB->isIndirectCall())
     return;
 
-  llvm::Metadata *TypeIdMD = CreateMetadataIdentifierGeneralized(QT);
+  llvm::Metadata *TypeIdMD = CreateMetadataIdentifierForCallGraphType(QT);
   llvm::MDTuple *TypeTuple = llvm::MDTuple::get(getLLVMContext(), {TypeIdMD});
   llvm::MDTuple *MDN = llvm::MDNode::get(getLLVMContext(), {TypeTuple});
   CB->setMetadata(llvm::LLVMContext::MD_callee_type, MDN);
@@ -8575,9 +8572,8 @@ void CodeGenModule::EmitOMPThreadPrivateDecl(const 
OMPThreadPrivateDecl *D) {
   }
 }
 
-llvm::Metadata *
-CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
-                                            StringRef Suffix) {
+llvm::Metadata *CodeGenModule::CreateMetadataIdentifierImpl(
+    QualType T, MetadataTypeMap &Map, StringRef Suffix, bool ForceString) {
   if (auto *FnType = T->getAs<FunctionProtoType>())
     T = getContext().getFunctionType(
         FnType->getReturnType(), FnType->getParamTypes(),
@@ -8587,7 +8583,7 @@ CodeGenModule::CreateMetadataIdentifierImpl(QualType T, 
MetadataTypeMap &Map,
   if (InternalId)
     return InternalId;
 
-  if (isExternallyVisible(T->getLinkage())) {
+  if (ForceString || isExternallyVisible(T->getLinkage())) {
     std::string OutName;
     llvm::raw_string_ostream Out(OutName);
     getCXXABI().getMangleContext().mangleCanonicalTypeName(
@@ -8627,7 +8623,13 @@ 
CodeGenModule::CreateMetadataIdentifierForVirtualMemPtrType(QualType T) {
 
 llvm::Metadata *CodeGenModule::CreateMetadataIdentifierGeneralized(QualType T) 
{
   return CreateMetadataIdentifierImpl(T, GeneralizedMetadataIdMap,
-                                      ".generalized");
+                                      ".generalized", /*ForceString=*/false);
+}
+
+llvm::Metadata *
+CodeGenModule::CreateMetadataIdentifierForCallGraphType(QualType T) {
+  return CreateMetadataIdentifierImpl(T, CallGraphMetadataIdMap, 
".generalized",
+                                      /*ForceString=*/true);
 }
 
 /// Returns whether this module needs the "all-vtables" type identifier.
diff --git a/clang/lib/CodeGen/CodeGenModule.h 
b/clang/lib/CodeGen/CodeGenModule.h
index 0abd75ccb0551..aa2a1f5eb25aa 100644
--- a/clang/lib/CodeGen/CodeGenModule.h
+++ b/clang/lib/CodeGen/CodeGenModule.h
@@ -708,6 +708,7 @@ class CodeGenModule : public CodeGenTypeCache {
   MetadataTypeMap MetadataIdMap;
   MetadataTypeMap VirtualMetadataIdMap;
   MetadataTypeMap GeneralizedMetadataIdMap;
+  MetadataTypeMap CallGraphMetadataIdMap;
 
   // Helps squashing blocks of TopLevelStmtDecl into a single llvm::Function
   // when used with -fincremental-extensions.
@@ -1753,6 +1754,11 @@ class CodeGenModule : public CodeGenTypeCache {
   /// internal identifiers).
   llvm::Metadata *CreateMetadataIdentifierForType(QualType T);
 
+  /// Create a metadata identifier for the Call Graph Section.
+  /// This is a generalized type identifier that is guaranteed to be an
+  /// MDString.
+  llvm::Metadata *CreateMetadataIdentifierForCallGraphType(QualType T);
+
   /// Create a metadata identifier that is intended to be used to check virtual
   /// calls via a member function pointer.
   llvm::Metadata *CreateMetadataIdentifierForVirtualMemPtrType(QualType T);
@@ -2192,7 +2198,8 @@ class CodeGenModule : public CodeGenTypeCache {
                                     llvm::AttrBuilder &FuncAttrs);
 
   llvm::Metadata *CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap 
&Map,
-                                               StringRef Suffix);
+                                               StringRef Suffix,
+                                               bool ForceString = false);
 
   /// Emit deactivation symbols for any PFP fields whose offset is taken with
   /// offsetof.
diff --git a/clang/test/CodeGen/call-graph-section-internal.cpp 
b/clang/test/CodeGen/call-graph-section-internal.cpp
index 923cb4ed053b0..d27bf92518726 100644
--- a/clang/test/CodeGen/call-graph-section-internal.cpp
+++ b/clang/test/CodeGen/call-graph-section-internal.cpp
@@ -1,9 +1,8 @@
 // RUN: %clang_cc1 -triple x86_64-unknown-linux 
-fexperimental-call-graph-section -disable-llvm-passes -emit-llvm -o - %s | 
FileCheck %s
 
-// Check that we do not generate callee_type metadata for indirect calls
+// Check that we generate callee_type metadata for indirect calls
 // to functions with internal linkage (e.g., types in anonymous namespaces),
-// as their callgraph metadata identifiers are distinct MDNodes instead of 
-// generalized strings, which would fail the LLVM Verifier.
+// using the generalized type metadata form.
 
 namespace {
 class a;
@@ -32,6 +31,8 @@ void test() {
 // CHECK-LABEL: define {{.*}} void @{{.*}}1a1dEv
 // CHECK:   %[[VFN:.*]] = getelementptr inbounds ptr, ptr %{{.*}}, i{{[0-9]+}} 0
 // CHECK:   %[[FP:.*]] = load ptr, ptr %[[VFN]], align {{[0-9]+}}
-// CHECK:   call void %[[FP]]({{.*}})
-// CHECK-NOT: !callee_type
+// CHECK:   call void %[[FP]]({{.*}}), !callee_type [[HEX_TYPE:![0-9]+]]
 // CHECK:   ret void
+
+// CHECK: [[HEX_TYPE]] = !{[[HEX_TYPE_INNER:![0-9]+]]}
+// CHECK: [[HEX_TYPE_INNER]] = !{!"_ZTSFvN12_GLOBAL__N_11aEE.generalized"}

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

Reply via email to