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