Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[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.
  • Loading branch information
AbdallahRashed committed Apr 23, 2026
commit cc061f77e0fd1daf4e87f972fc7fb0a75ee10d67
16 changes: 11 additions & 5 deletions clang/lib/CIR/CodeGen/CIRGenVTables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The llvm_unreachable on line 375 relates to this same kind. It shouldn't be unreachable. That isn't implemented in the incubator, but it is in classic codegen (doing basically what you have here). Can you add that in this PR also. It will require an additional test case to reach that code.

// 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;
Expand Down
27 changes: 27 additions & 0 deletions clang/test/CIR/CodeGen/vtable-linkage-explicit-instantiation.cpp
Original file line number Diff line number Diff line change
@@ -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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add RUN lines to compile without -fclangir? This is useful for verifying that we do the same thing as classic codegen.

// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s

// Test that explicit instantiation declarations don't trigger
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment isn't useful. I'd delete it.

// "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