@@ -1046,6 +1046,11 @@ CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
10461046 if (!RD->isExternallyVisible ())
10471047 return llvm::GlobalVariable::InternalLinkage;
10481048
1049+ // V-tables for non-template classes with an owning module are always
1050+ // uniquely emitted in that module.
1051+ if (Module *M = RD->getOwningModule (); M && M->isNamedModule ())
1052+ return llvm::GlobalVariable::ExternalLinkage;
1053+
10491054 // We're at the end of the translation unit, so the current key
10501055 // function is fully correct.
10511056 const CXXMethodDecl *keyFunction = Context.getCurrentKeyFunction (RD);
@@ -1180,6 +1185,21 @@ bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
11801185 TSK == TSK_ExplicitInstantiationDefinition)
11811186 return false ;
11821187
1188+ // Itanium C++ ABI [5.2.3]:
1189+ // Virtual tables for dynamic classes are emitted as follows:
1190+ //
1191+ // - If the class is templated, the tables are emitted in every object that
1192+ // references any of them.
1193+ // - Otherwise, if the class is attached to a module, the tables are uniquely
1194+ // emitted in the object for the module unit in which it is defined.
1195+ // - Otherwise, if the class has a key function (see below), the tables are
1196+ // emitted in the object for the translation unit containing the definition of
1197+ // the key function. This is unique if the key function is not inline.
1198+ // - Otherwise, the tables are emitted in every object that references any of
1199+ // them.
1200+ if (Module *M = RD->getOwningModule (); M && M->isNamedModule ())
1201+ return M != CGM.getContext ().getCurrentNamedModule ();
1202+
11831203 // Otherwise, if the class doesn't have a key function (possibly
11841204 // anymore), the vtable must be defined here.
11851205 const CXXMethodDecl *keyFunction = CGM.getContext ().getCurrentKeyFunction (RD);
@@ -1189,13 +1209,7 @@ bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
11891209 const FunctionDecl *Def;
11901210 // Otherwise, if we don't have a definition of the key function, the
11911211 // vtable must be defined somewhere else.
1192- if (!keyFunction->hasBody (Def))
1193- return true ;
1194-
1195- assert (Def && " The body of the key function is not assigned to Def?" );
1196- // If the non-inline key function comes from another module unit, the vtable
1197- // must be defined there.
1198- return Def->isInAnotherModuleUnit () && !Def->isInlineSpecified ();
1212+ return !keyFunction->hasBody (Def);
11991213}
12001214
12011215// / Given that we're currently at the end of the translation unit, and
0 commit comments