[clang] [CIR] Handle explicit instantiation declaration in getVTableLinkage (PR #193809)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 23 10:46:07 PDT 2026
https://github.com/AbdallahRashed created https://github.com/llvm/llvm-project/pull/193809
Replace errorNYI for TSK_ExplicitInstantiationDeclaration with the correct linkage logic: use discardable ODR linkage for MSVC, and for Itanium choose between AvailableExternallyLinkage (when the vtable can be speculatively emitted) or ExternalLinkage.
Fixes point 3 of #192330.
>From cc061f77e0fd1daf4e87f972fc7fb0a75ee10d67 Mon Sep 17 00:00:00 2001
From: AbdallahRashed <abdallah.mrashed at gmail.com>
Date: Thu, 23 Apr 2026 19:45:12 +0200
Subject: [PATCH] [CIR] Handle explicit instantiation declaration in
getVTableLinkage
Replace errorNYI for TSK_ExplicitInstantiationDeclaration with the
correct linkage logic: use discardable ODR linkage for MSVC, and
for Itanium choose between AvailableExternallyLinkage (when the
vtable can be speculatively emitted) or ExternalLinkage.
Fixes point 3 of #192330.
---
clang/lib/CIR/CodeGen/CIRGenVTables.cpp | 16 +++++++----
.../vtable-linkage-explicit-instantiation.cpp | 27 +++++++++++++++++++
2 files changed, 38 insertions(+), 5 deletions(-)
create mode 100644 clang/test/CIR/CodeGen/vtable-linkage-explicit-instantiation.cpp
diff --git a/clang/lib/CIR/CodeGen/CIRGenVTables.cpp b/clang/lib/CIR/CodeGen/CIRGenVTables.cpp
index 6e1a80926f679..ca83d7a355408 100644
--- a/clang/lib/CIR/CodeGen/CIRGenVTables.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenVTables.cpp
@@ -326,6 +326,9 @@ cir::GlobalOp CIRGenVTables::generateConstructionVTable(
return vtable;
}
+static bool shouldEmitAvailableExternallyVTable(const CIRGenModule &cgm,
+ const CXXRecordDecl *rd);
+
/// Compute the required linkage of the vtable for the given class.
///
/// Note that we only call this at the end of the translation unit.
@@ -395,11 +398,14 @@ cir::GlobalLinkageKind CIRGenModule::getVTableLinkage(const CXXRecordDecl *rd) {
case TSK_ImplicitInstantiation:
return discardableODRLinkage;
- case TSK_ExplicitInstantiationDeclaration: {
- errorNYI(rd->getSourceRange(),
- "getVTableLinkage: explicit instantiation declaration");
- return cir::GlobalLinkageKind::ExternalLinkage;
- }
+ case TSK_ExplicitInstantiationDeclaration:
+ // Explicit instantiations in MSVC do not provide vtables, so we must emit
+ // our own.
+ if (getTarget().getCXXABI().isMicrosoft())
+ return discardableODRLinkage;
+ return shouldEmitAvailableExternallyVTable(*this, rd)
+ ? cir::GlobalLinkageKind::AvailableExternallyLinkage
+ : cir::GlobalLinkageKind::ExternalLinkage;
case TSK_ExplicitInstantiationDefinition:
return nonDiscardableODRLinkage;
diff --git a/clang/test/CIR/CodeGen/vtable-linkage-explicit-instantiation.cpp b/clang/test/CIR/CodeGen/vtable-linkage-explicit-instantiation.cpp
new file mode 100644
index 0000000000000..6fc4cc4d51fbf
--- /dev/null
+++ b/clang/test/CIR/CodeGen/vtable-linkage-explicit-instantiation.cpp
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s
+
+// Test that explicit instantiation declarations don't trigger
+// "getVTableLinkage: explicit instantiation declaration" errorNYI.
+
+template <typename T>
+struct Base {
+ virtual ~Base() {}
+ virtual void foo() {}
+ T val;
+};
+
+extern template class Base<int>;
+
+void use(Base<int> *p) {
+ p->foo();
+}
+
+// Verify the virtual call goes through the vtable.
+// CHECK: cir.func {{.*}} @_Z3useP4BaseIiE
+// CHECK: cir.vtable.get_vptr
+// CHECK: cir.vtable.get_virtual_fn_addr
+
+// LLVM: define {{.*}} @_Z3useP4BaseIiE
More information about the cfe-commits
mailing list