[clang] [llvm] [NFC][CrossDSOCFI] Move findCfiTypeIds to LowerTypeTests (PR #223582)
Vitaly Buka via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 14 22:53:22 PDT 2026
https://github.com/vitalybuka updated https://github.com/llvm/llvm-project/pull/223582
>From 289f3d90150f95a488907c23d3c341d6d56b615b Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Mon, 14 Sep 2026 19:23:20 -0700
Subject: [PATCH 1/4] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20ch?=
=?UTF-8?q?anges=20to=20main=20this=20commit=20is=20based=20on?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.7
[skip ci]
---
.../include/llvm/Analysis/TypeMetadataUtils.h | 7 --
.../llvm/Transforms/IPO/LowerTypeTests.h | 36 +++++-
llvm/lib/Transforms/IPO/LowerTypeTests.cpp | 107 ++++++++++++++++++
.../Transforms/IPO/ThinLTOBitcodeWriter.cpp | 99 +---------------
4 files changed, 143 insertions(+), 106 deletions(-)
diff --git a/llvm/include/llvm/Analysis/TypeMetadataUtils.h b/llvm/include/llvm/Analysis/TypeMetadataUtils.h
index d6f4c8c0e8b54..9e0776e60a038 100644
--- a/llvm/include/llvm/Analysis/TypeMetadataUtils.h
+++ b/llvm/include/llvm/Analysis/TypeMetadataUtils.h
@@ -30,13 +30,6 @@ class GlobalVariable;
class Instruction;
class Module;
-/// The type of CFI jumptable needed for a function.
-enum CfiFunctionLinkage {
- CFL_Definition = 0,
- CFL_Declaration = 1,
- CFL_WeakDeclaration = 2
-};
-
/// A call site that could be devirtualized.
struct DevirtCallSite {
/// The offset from the address point to the virtual function.
diff --git a/llvm/include/llvm/Transforms/IPO/LowerTypeTests.h b/llvm/include/llvm/Transforms/IPO/LowerTypeTests.h
index b3c275001a9a7..0ea74ff361f4c 100644
--- a/llvm/include/llvm/Transforms/IPO/LowerTypeTests.h
+++ b/llvm/include/llvm/Transforms/IPO/LowerTypeTests.h
@@ -14,19 +14,25 @@
#ifndef LLVM_TRANSFORMS_IPO_LOWERTYPETESTS_H
#define LLVM_TRANSFORMS_IPO_LOWERTYPETESTS_H
-#include "llvm/ADT/FunctionExtras.h"
-#include "llvm/ADT/STLFunctionalExtras.h"
-#include "llvm/ADT/SmallVector.h"
-#include "llvm/IR/PassManager.h"
-#include "llvm/Support/Compiler.h"
#include <cstdint>
#include <cstring>
#include <limits>
#include <set>
#include <vector>
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/FunctionExtras.h"
+#include "llvm/ADT/STLFunctionalExtras.h"
+#include "llvm/ADT/SetVector.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/IR/PassManager.h"
+#include "llvm/Support/Compiler.h"
+
namespace llvm {
+class Function;
+class GlobalObject;
+class GlobalValue;
class Module;
class ModuleSummaryIndex;
class raw_ostream;
@@ -204,8 +210,28 @@ struct ByteArrayBuilder {
uint64_t &AllocByteOffset, uint8_t &AllocMask);
};
+/// The type of CFI jumptable needed for a function.
+enum CfiFunctionLinkage {
+ CFL_Definition = 0,
+ CFL_Declaration = 1,
+ CFL_WeakDeclaration = 2
+};
+
LLVM_ABI bool isJumpTableCanonical(Function *F);
+/// Returns whether a global or its associated global has attached type
+/// metadata.
+LLVM_ABI bool hasTypeMetadata(const GlobalObject *GO);
+
+/// Finds all functions and aliases in \p M that may need CFI jump table
+/// entries.
+LLVM_ABI SetVector<GlobalValue *> findCfiFunctions(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,
+ ArrayRef<GlobalValue *> CfiFunctions);
+
/// Specifies how to drop type tests.
enum class DropTestKind {
Assume, /// Drop only llvm.assumes using type test value.
diff --git a/llvm/lib/Transforms/IPO/LowerTypeTests.cpp b/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
index 35ec5d795495f..3697972a87f79 100644
--- a/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
+++ b/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
@@ -18,6 +18,7 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/EquivalenceClasses.h"
+#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SetVector.h"
@@ -64,6 +65,7 @@
#include "llvm/IR/Use.h"
#include "llvm/IR/User.h"
#include "llvm/IR/Value.h"
+#include "llvm/Object/ModuleSymbolTable.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
@@ -293,6 +295,111 @@ bool lowertypetests::isJumpTableCanonical(Function *F) {
return F->hasFnAttribute("cfi-canonical-jump-table");
}
+bool lowertypetests::hasTypeMetadata(const GlobalObject *GO) {
+ if (MDNode *MD = GO->getMetadata(LLVMContext::MD_associated))
+ if (auto *AssocVM = dyn_cast_or_null<ValueAsMetadata>(MD->getOperand(0)))
+ if (auto *AssocGO = dyn_cast<GlobalObject>(AssocVM->getValue()))
+ if (AssocGO->hasMetadata(LLVMContext::MD_type))
+ return true;
+ return GO->hasMetadata(LLVMContext::MD_type);
+}
+
+SetVector<GlobalValue *> lowertypetests::findCfiFunctions(Module &M) {
+ SetVector<GlobalValue *> CfiFunctions;
+ for (auto &F : M)
+ if ((!F.hasLocalLinkage() || F.hasAddressTaken()) && hasTypeMetadata(&F))
+ CfiFunctions.insert(&F);
+ for (auto &A : M.aliases())
+ if (auto *F = dyn_cast<Function>(A.getAliasee()))
+ if (hasTypeMetadata(F))
+ CfiFunctions.insert(&A);
+ return CfiFunctions;
+}
+
+static void createCfiFunctionsMetadata(Module &DestM,
+ ArrayRef<GlobalValue *> CfiFunctions) {
+ auto &Ctx = DestM.getContext();
+ SmallVector<MDNode *, 8> CfiFunctionMDs;
+ for (auto *V : CfiFunctions) {
+ Function &F = *cast<Function>(V->getAliaseeObject());
+ SmallVector<MDNode *, 2> Types;
+ F.getMetadata(LLVMContext::MD_type, Types);
+
+ SmallVector<Metadata *, 4> Elts;
+ Elts.push_back(MDString::get(Ctx, V->getName()));
+ CfiFunctionLinkage Linkage;
+ if (lowertypetests::isJumpTableCanonical(&F))
+ Linkage = CFL_Definition;
+ else if (F.hasExternalWeakLinkage())
+ Linkage = CFL_WeakDeclaration;
+ else
+ Linkage = CFL_Declaration;
+ Elts.push_back(ConstantAsMetadata::get(
+ llvm::ConstantInt::get(Type::getInt8Ty(Ctx), Linkage)));
+ GlobalValue::GUID GUID = V->getGUID();
+ Elts.push_back(ConstantAsMetadata::get(
+ llvm::ConstantInt::get(Type::getInt64Ty(Ctx), GUID)));
+ append_range(Elts, Types);
+ CfiFunctionMDs.push_back(MDTuple::get(Ctx, Elts));
+ }
+
+ if (!CfiFunctionMDs.empty()) {
+ NamedMDNode *NMD = DestM.getOrInsertNamedMetadata("cfi.functions");
+ for (auto *MD : CfiFunctionMDs)
+ NMD->addOperand(MD);
+ }
+}
+
+static void createCfiAliasesMetadata(Module &DestM, const Module &SrcM) {
+ auto &Ctx = DestM.getContext();
+ MapVector<const Function *, std::vector<const GlobalAlias *>> FunctionAliases;
+ for (const auto &A : SrcM.aliases()) {
+ if (!isa<Function>(A.getAliasee()))
+ continue;
+
+ const auto *F = cast<Function>(A.getAliasee());
+ FunctionAliases[F].push_back(&A);
+ }
+
+ if (!FunctionAliases.empty()) {
+ NamedMDNode *NMD = DestM.getOrInsertNamedMetadata("aliases");
+ for (auto &Alias : FunctionAliases) {
+ SmallVector<Metadata *> Elts;
+ Elts.push_back(MDString::get(Ctx, Alias.first->getName()));
+ for (auto *A : Alias.second)
+ Elts.push_back(MDString::get(Ctx, A->getName()));
+ NMD->addOperand(MDTuple::get(Ctx, Elts));
+ }
+ }
+}
+
+static void createCfiSymversMetadata(Module &DestM, const Module &SrcM) {
+ auto &Ctx = DestM.getContext();
+ SmallVector<MDNode *, 8> Symvers;
+ ModuleSymbolTable::CollectAsmSymvers(
+ SrcM, [&](StringRef Name, StringRef Alias) {
+ const Function *F = SrcM.getFunction(Name);
+ if (!F || F->use_empty())
+ return;
+
+ Symvers.push_back(MDTuple::get(
+ Ctx, {MDString::get(Ctx, Name), MDString::get(Ctx, Alias)}));
+ });
+
+ if (!Symvers.empty()) {
+ NamedMDNode *NMD = DestM.getOrInsertNamedMetadata("symvers");
+ for (auto *MD : Symvers)
+ NMD->addOperand(MD);
+ }
+}
+
+void lowertypetests::createCfiMetadata(Module &DestM, const Module &SrcM,
+ ArrayRef<GlobalValue *> CfiFunctions) {
+ createCfiFunctionsMetadata(DestM, CfiFunctions);
+ createCfiAliasesMetadata(DestM, SrcM);
+ createCfiSymversMetadata(DestM, SrcM);
+}
+
namespace {
struct ByteArrayInfo {
diff --git a/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp b/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
index cccc49384f921..399f7f3186b8c 100644
--- a/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
+++ b/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
@@ -10,7 +10,6 @@
#include "llvm/Analysis/BasicAliasAnalysis.h"
#include "llvm/Analysis/ModuleSummaryAnalysis.h"
#include "llvm/Analysis/ProfileSummaryInfo.h"
-#include "llvm/Analysis/TypeMetadataUtils.h"
#include "llvm/Bitcode/BitcodeWriter.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfo.h"
@@ -18,7 +17,6 @@
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
-#include "llvm/Object/ModuleSymbolTable.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/IPO/FunctionAttrs.h"
@@ -322,21 +320,6 @@ void splitAndWriteThinLTOBitcode(
promoteTypeIds(M, ModuleId);
- // Returns whether a global or its associated global has attached type
- // metadata. The former may participate in CFI or whole-program
- // devirtualization, so they need to appear in the merged module instead of
- // the thin LTO module. Similarly, globals that are associated with globals
- // with type metadata need to appear in the merged module because they will
- // reference the global's section directly.
- auto HasTypeMetadata = [](const GlobalObject *GO) {
- if (MDNode *MD = GO->getMetadata(LLVMContext::MD_associated))
- if (auto *AssocVM = dyn_cast_or_null<ValueAsMetadata>(MD->getOperand(0)))
- if (auto *AssocGO = dyn_cast<GlobalObject>(AssocVM->getValue()))
- if (AssocGO->hasMetadata(LLVMContext::MD_type))
- return true;
- return GO->hasMetadata(LLVMContext::MD_type);
- };
-
// Collect the set of virtual functions that are eligible for virtual constant
// propagation. Each eligible function must not access memory, must return
// an integer of width <=64 bits, must take at least one argument, must not
@@ -354,7 +337,7 @@ void splitAndWriteThinLTOBitcode(
// comdat in MergedM to keep the comdat together.
DenseSet<const Comdat *> MergedMComdats;
for (GlobalVariable &GV : M.globals())
- if (!GV.isDeclaration() && HasTypeMetadata(&GV)) {
+ if (!GV.isDeclaration() && lowertypetests::hasTypeMetadata(&GV)) {
if (const auto *C = GV.getComdat())
MergedMComdats.insert(C);
forEachVirtualFunction(GV.getInitializer(), [&](Function *F) {
@@ -386,7 +369,7 @@ void splitAndWriteThinLTOBitcode(
return EligibleVirtualFns.count(F);
if (auto *GVar =
dyn_cast_or_null<GlobalVariable>(GV->getAliaseeObject()))
- return HasTypeMetadata(GVar);
+ return lowertypetests::hasTypeMetadata(GVar);
return false;
}));
StripDebugInfo(*MergedM);
@@ -406,20 +389,13 @@ void splitAndWriteThinLTOBitcode(
F.setComdat(nullptr);
}
- SetVector<GlobalValue *> CfiFunctions;
- for (auto &F : M)
- if ((!F.hasLocalLinkage() || F.hasAddressTaken()) && HasTypeMetadata(&F))
- CfiFunctions.insert(&F);
- for (auto &A : M.aliases())
- if (auto *F = dyn_cast<Function>(A.getAliasee()))
- if (HasTypeMetadata(F))
- CfiFunctions.insert(&A);
+ SetVector<GlobalValue *> CfiFunctions = lowertypetests::findCfiFunctions(M);
// Remove all globals with type metadata, globals with comdats that live in
// MergedM, and aliases pointing to such globals from the thin LTO module.
filterModule(&M, [&](const GlobalValue *GV) {
if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getAliaseeObject()))
- if (HasTypeMetadata(GVar))
+ if (lowertypetests::hasTypeMetadata(GVar))
return false;
if (const auto *C = GV->getComdat())
if (MergedMComdats.count(C))
@@ -436,72 +412,7 @@ void splitAndWriteThinLTOBitcode(
promoteInternals(*MergedM, M, ModuleId, {});
promoteInternals(M, *MergedM, ModuleId, CfiFunctions);
- auto &Ctx = MergedM->getContext();
- SmallVector<MDNode *, 8> CfiFunctionMDs;
- for (auto *V : CfiFunctions) {
- Function &F = *cast<Function>(V->getAliaseeObject());
- SmallVector<MDNode *, 2> Types;
- F.getMetadata(LLVMContext::MD_type, Types);
-
- SmallVector<Metadata *, 4> Elts;
- Elts.push_back(MDString::get(Ctx, V->getName()));
- CfiFunctionLinkage Linkage;
- if (lowertypetests::isJumpTableCanonical(&F))
- Linkage = CFL_Definition;
- else if (F.hasExternalWeakLinkage())
- Linkage = CFL_WeakDeclaration;
- else
- Linkage = CFL_Declaration;
- Elts.push_back(ConstantAsMetadata::get(
- llvm::ConstantInt::get(Type::getInt8Ty(Ctx), Linkage)));
- GlobalValue::GUID GUID = V->getGUID();
- Elts.push_back(ConstantAsMetadata::get(
- llvm::ConstantInt::get(Type::getInt64Ty(Ctx), GUID)));
- append_range(Elts, Types);
- CfiFunctionMDs.push_back(MDTuple::get(Ctx, Elts));
- }
-
- if(!CfiFunctionMDs.empty()) {
- NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("cfi.functions");
- for (auto *MD : CfiFunctionMDs)
- NMD->addOperand(MD);
- }
-
- MapVector<Function *, std::vector<GlobalAlias *>> FunctionAliases;
- for (auto &A : M.aliases()) {
- if (!isa<Function>(A.getAliasee()))
- continue;
-
- auto *F = cast<Function>(A.getAliasee());
- FunctionAliases[F].push_back(&A);
- }
-
- if (!FunctionAliases.empty()) {
- NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("aliases");
- for (auto &Alias : FunctionAliases) {
- SmallVector<Metadata *> Elts;
- Elts.push_back(MDString::get(Ctx, Alias.first->getName()));
- for (auto *A : Alias.second)
- Elts.push_back(MDString::get(Ctx, A->getName()));
- NMD->addOperand(MDTuple::get(Ctx, Elts));
- }
- }
-
- SmallVector<MDNode *, 8> Symvers;
- ModuleSymbolTable::CollectAsmSymvers(M, [&](StringRef Name, StringRef Alias) {
- Function *F = M.getFunction(Name);
- if (!F || F->use_empty())
- return;
-
- Symvers.push_back(MDTuple::get(
- Ctx, {MDString::get(Ctx, Name), MDString::get(Ctx, Alias)}));
- });
-
- if (!Symvers.empty()) {
- NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("symvers");
- for (auto *MD : Symvers)
- NMD->addOperand(MD);
- }
+ lowertypetests::createCfiMetadata(*MergedM, M, CfiFunctions.getArrayRef());
simplifyExternals(*MergedM);
>From 614fff664d53f7a2f69fbf30dd338bf51868d02d Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Mon, 14 Sep 2026 19:38:09 -0700
Subject: [PATCH 2/4] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20ch?=
=?UTF-8?q?anges=20introduced=20through=20rebase?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.7
[skip ci]
---
clang/test/Driver/modules-driver-depscan-log.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/Driver/modules-driver-depscan-log.cpp b/clang/test/Driver/modules-driver-depscan-log.cpp
index 50a5319f17ef1..af7e816f98b01 100644
--- a/clang/test/Driver/modules-driver-depscan-log.cpp
+++ b/clang/test/Driver/modules-driver-depscan-log.cpp
@@ -5,7 +5,7 @@
// RUN: split-file %s %t
// RUN: %clang -c -std=c++23 -fmodules-driver -fdepscan-log-path=%t/scan.log \
-// RUN: %t/A.cppm -o %t/A.o
+// RUN: %t/A.cppm -fsyntax-only
// RUN: FileCheck %s --input-file %t/scan.log
// CHECK: logging_start
>From 7d9bbe817813314f5374262d20e44bb053453c51 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Mon, 14 Sep 2026 21:45:51 -0700
Subject: [PATCH 3/4] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20ch?=
=?UTF-8?q?anges=20introduced=20through=20rebase?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.7
[skip ci]
---
.../llvm/Transforms/IPO/LowerTypeTests.h | 9 +---
llvm/lib/Transforms/IPO/LowerTypeTests.cpp | 46 ++++++++++++-------
.../Transforms/IPO/ThinLTOBitcodeWriter.cpp | 6 +--
3 files changed, 33 insertions(+), 28 deletions(-)
diff --git a/llvm/include/llvm/Transforms/IPO/LowerTypeTests.h b/llvm/include/llvm/Transforms/IPO/LowerTypeTests.h
index 0ea74ff361f4c..97a7fc80aa890 100644
--- a/llvm/include/llvm/Transforms/IPO/LowerTypeTests.h
+++ b/llvm/include/llvm/Transforms/IPO/LowerTypeTests.h
@@ -210,18 +210,11 @@ struct ByteArrayBuilder {
uint64_t &AllocByteOffset, uint8_t &AllocMask);
};
-/// The type of CFI jumptable needed for a function.
-enum CfiFunctionLinkage {
- CFL_Definition = 0,
- CFL_Declaration = 1,
- CFL_WeakDeclaration = 2
-};
-
LLVM_ABI bool isJumpTableCanonical(Function *F);
/// Returns whether a global or its associated global has attached type
/// metadata.
-LLVM_ABI bool hasTypeMetadata(const GlobalObject *GO);
+LLVM_ABI bool hasTypeMetadata(const GlobalObject &GO);
/// Finds all functions and aliases in \p M that may need CFI jump table
/// entries.
diff --git a/llvm/lib/Transforms/IPO/LowerTypeTests.cpp b/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
index 3697972a87f79..21172e6c43371 100644
--- a/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
+++ b/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
@@ -295,27 +295,38 @@ bool lowertypetests::isJumpTableCanonical(Function *F) {
return F->hasFnAttribute("cfi-canonical-jump-table");
}
-bool lowertypetests::hasTypeMetadata(const GlobalObject *GO) {
- if (MDNode *MD = GO->getMetadata(LLVMContext::MD_associated))
+bool lowertypetests::hasTypeMetadata(const GlobalObject &GO) {
+ if (MDNode *MD = GO.getMetadata(LLVMContext::MD_associated))
if (auto *AssocVM = dyn_cast_or_null<ValueAsMetadata>(MD->getOperand(0)))
if (auto *AssocGO = dyn_cast<GlobalObject>(AssocVM->getValue()))
if (AssocGO->hasMetadata(LLVMContext::MD_type))
return true;
- return GO->hasMetadata(LLVMContext::MD_type);
+ return GO.hasMetadata(LLVMContext::MD_type);
}
SetVector<GlobalValue *> lowertypetests::findCfiFunctions(Module &M) {
SetVector<GlobalValue *> CfiFunctions;
for (auto &F : M)
- if ((!F.hasLocalLinkage() || F.hasAddressTaken()) && hasTypeMetadata(&F))
+ if ((!F.hasLocalLinkage() || F.hasAddressTaken()) && hasTypeMetadata(F))
CfiFunctions.insert(&F);
for (auto &A : M.aliases())
if (auto *F = dyn_cast<Function>(A.getAliasee()))
- if (hasTypeMetadata(F))
+ if (hasTypeMetadata(*F))
CfiFunctions.insert(&A);
return CfiFunctions;
}
+namespace {
+
+/// The type of CFI jumptable needed for a function.
+enum class CfiFunctionLinkage : uint8_t {
+ Definition = 0,
+ Declaration = 1,
+ WeakDeclaration = 2,
+};
+
+} // namespace
+
static void createCfiFunctionsMetadata(Module &DestM,
ArrayRef<GlobalValue *> CfiFunctions) {
auto &Ctx = DestM.getContext();
@@ -327,15 +338,13 @@ static void createCfiFunctionsMetadata(Module &DestM,
SmallVector<Metadata *, 4> Elts;
Elts.push_back(MDString::get(Ctx, V->getName()));
- CfiFunctionLinkage Linkage;
+ CfiFunctionLinkage Linkage = CfiFunctionLinkage::Declaration;
if (lowertypetests::isJumpTableCanonical(&F))
- Linkage = CFL_Definition;
+ Linkage = CfiFunctionLinkage::Definition;
else if (F.hasExternalWeakLinkage())
- Linkage = CFL_WeakDeclaration;
- else
- Linkage = CFL_Declaration;
- Elts.push_back(ConstantAsMetadata::get(
- llvm::ConstantInt::get(Type::getInt8Ty(Ctx), Linkage)));
+ Linkage = CfiFunctionLinkage::WeakDeclaration;
+ Elts.push_back(ConstantAsMetadata::get(llvm::ConstantInt::get(
+ Type::getInt8Ty(Ctx), static_cast<uint8_t>(Linkage))));
GlobalValue::GUID GUID = V->getGUID();
Elts.push_back(ConstantAsMetadata::get(
llvm::ConstantInt::get(Type::getInt64Ty(Ctx), GUID)));
@@ -2404,7 +2413,7 @@ bool LowerTypeTestsModule::lower() {
if (!ExportSummary->isGUIDLive(GUID))
continue;
if (!IsAddressTaken(GUID)) {
- if (!CrossDsoCfi || Linkage != CFL_Definition)
+ if (!CrossDsoCfi || Linkage != CfiFunctionLinkage::Definition)
continue;
bool Exported = false;
@@ -2417,7 +2426,8 @@ bool LowerTypeTestsModule::lower() {
continue;
}
auto P = ExportedFunctions.insert({FunctionName, {Linkage, FuncMD}});
- if (!P.second && P.first->second.Linkage != CFL_Definition)
+ if (!P.second &&
+ P.first->second.Linkage != CfiFunctionLinkage::Definition)
P.first->second = {Linkage, FuncMD};
}
@@ -2473,7 +2483,8 @@ bool LowerTypeTestsModule::lower() {
// Update the linkage for extern_weak declarations when a definition
// exists.
- if (Linkage == CFL_Definition && F->hasExternalWeakLinkage())
+ if (Linkage == CfiFunctionLinkage::Definition &&
+ F->hasExternalWeakLinkage())
F->setLinkage(GlobalValue::ExternalLinkage);
// If the function in the full LTO module is a declaration, replace its
@@ -2481,7 +2492,7 @@ bool LowerTypeTestsModule::lower() {
// metadata is presumed to be more accurate than the metadata attached
// to the declaration.
if (F->isDeclaration()) {
- if (Linkage == CFL_WeakDeclaration)
+ if (Linkage == CfiFunctionLinkage::WeakDeclaration)
F->setLinkage(GlobalValue::ExternalWeakLinkage);
F->eraseMetadata(LLVMContext::MD_type);
@@ -2544,7 +2555,8 @@ bool LowerTypeTestsModule::lower() {
IsJumpTableCanonical = isJumpTableCanonical(F);
if (auto It = ExportedFunctions.find(F->getName());
It != ExportedFunctions.end()) {
- IsJumpTableCanonical |= It->second.Linkage == CFL_Definition;
+ IsJumpTableCanonical |=
+ It->second.Linkage == CfiFunctionLinkage::Definition;
IsExported = true;
// TODO: The logic here checks only that the function is address taken,
// not that the address takers are live. This can be updated to check
diff --git a/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp b/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
index 399f7f3186b8c..2aed5f06db0b9 100644
--- a/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
+++ b/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
@@ -337,7 +337,7 @@ void splitAndWriteThinLTOBitcode(
// comdat in MergedM to keep the comdat together.
DenseSet<const Comdat *> MergedMComdats;
for (GlobalVariable &GV : M.globals())
- if (!GV.isDeclaration() && lowertypetests::hasTypeMetadata(&GV)) {
+ if (!GV.isDeclaration() && lowertypetests::hasTypeMetadata(GV)) {
if (const auto *C = GV.getComdat())
MergedMComdats.insert(C);
forEachVirtualFunction(GV.getInitializer(), [&](Function *F) {
@@ -369,7 +369,7 @@ void splitAndWriteThinLTOBitcode(
return EligibleVirtualFns.count(F);
if (auto *GVar =
dyn_cast_or_null<GlobalVariable>(GV->getAliaseeObject()))
- return lowertypetests::hasTypeMetadata(GVar);
+ return lowertypetests::hasTypeMetadata(*GVar);
return false;
}));
StripDebugInfo(*MergedM);
@@ -395,7 +395,7 @@ void splitAndWriteThinLTOBitcode(
// MergedM, and aliases pointing to such globals from the thin LTO module.
filterModule(&M, [&](const GlobalValue *GV) {
if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getAliaseeObject()))
- if (lowertypetests::hasTypeMetadata(GVar))
+ if (lowertypetests::hasTypeMetadata(*GVar))
return false;
if (const auto *C = GV->getComdat())
if (MergedMComdats.count(C))
>From 5a58d482db13a32b3c26fa757e8216798820f8a9 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Mon, 14 Sep 2026 22:53:09 -0700
Subject: [PATCH 4/4] &
Created using spr 1.3.7
---
llvm/lib/Transforms/IPO/LowerTypeTests.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Transforms/IPO/LowerTypeTests.cpp b/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
index 8564ed82f8422..5e1ada488e849 100644
--- a/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
+++ b/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
@@ -317,9 +317,9 @@ SetVector<GlobalValue *> lowertypetests::findCfiFunctions(Module &M) {
}
/// Extracts a numeric type identifier from an MDNode containing type metadata.
-static ConstantInt *extractNumericTypeId(MDNode *MD) {
+static ConstantInt *extractNumericTypeId(MDNode &MD) {
// This check excludes vtables for classes inside anonymous namespaces.
- auto TM = dyn_cast<ValueAsMetadata>(MD->getOperand(1));
+ auto TM = dyn_cast<ValueAsMetadata>(MD.getOperand(1));
if (!TM)
return nullptr;
auto C = dyn_cast_or_null<ConstantInt>(TM->getValue());
@@ -339,7 +339,7 @@ SetVector<uint64_t> lowertypetests::findCfiTypeIds(const Module &M) {
Types.clear();
GO.getMetadata(LLVMContext::MD_type, Types);
for (MDNode *Type : Types)
- if (ConstantInt *TypeId = extractNumericTypeId(Type))
+ if (ConstantInt *TypeId = extractNumericTypeId(*Type))
TypeIds.insert(TypeId->getZExtValue());
}
@@ -349,7 +349,7 @@ SetVector<uint64_t> lowertypetests::findCfiTypeIds(const Module &M) {
assert(isa<ConstantAsMetadata>(Func->getOperand(2)));
for (unsigned I = 3; I < Func->getNumOperands(); ++I)
if (ConstantInt *TypeId =
- extractNumericTypeId(cast<MDNode>(Func->getOperand(I).get())))
+ extractNumericTypeId(*cast<MDNode>(Func->getOperand(I))))
TypeIds.insert(TypeId->getZExtValue());
}
}
More information about the llvm-commits
mailing list