@@ -1052,6 +1052,11 @@ CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
10521052 if (!RD->isExternallyVisible ())
10531053 return llvm::GlobalVariable::InternalLinkage;
10541054
1055+ // V-tables for non-template classes with an owning module are always
1056+ // uniquely emitted in that module.
1057+ if (RD->isInNamedModule ())
1058+ return llvm::GlobalVariable::ExternalLinkage;
1059+
10551060 // We're at the end of the translation unit, so the current key
10561061 // function is fully correct.
10571062 const CXXMethodDecl *keyFunction = Context.getCurrentKeyFunction (RD);
@@ -1186,6 +1191,21 @@ bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
11861191 TSK == TSK_ExplicitInstantiationDefinition)
11871192 return false ;
11881193
1194+ // Itanium C++ ABI [5.2.3]:
1195+ // Virtual tables for dynamic classes are emitted as follows:
1196+ //
1197+ // - If the class is templated, the tables are emitted in every object that
1198+ // references any of them.
1199+ // - Otherwise, if the class is attached to a module, the tables are uniquely
1200+ // emitted in the object for the module unit in which it is defined.
1201+ // - Otherwise, if the class has a key function (see below), the tables are
1202+ // emitted in the object for the translation unit containing the definition of
1203+ // the key function. This is unique if the key function is not inline.
1204+ // - Otherwise, the tables are emitted in every object that references any of
1205+ // them.
1206+ if (RD->isInNamedModule ())
1207+ return RD->shouldEmitInExternalSource ();
1208+
11891209 // Otherwise, if the class doesn't have a key function (possibly
11901210 // anymore), the vtable must be defined here.
11911211 const CXXMethodDecl *keyFunction = CGM.getContext ().getCurrentKeyFunction (RD);
@@ -1195,13 +1215,7 @@ bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
11951215 const FunctionDecl *Def;
11961216 // Otherwise, if we don't have a definition of the key function, the
11971217 // vtable must be defined somewhere else.
1198- if (!keyFunction->hasBody (Def))
1199- return true ;
1200-
1201- assert (Def && " The body of the key function is not assigned to Def?" );
1202- // If the non-inline key function comes from another module unit, the vtable
1203- // must be defined there.
1204- return Def->isInAnotherModuleUnit () && !Def->isInlineSpecified ();
1218+ return !keyFunction->hasBody (Def);
12051219}
12061220
12071221// / Given that we're currently at the end of the translation unit, and
0 commit comments