@@ -561,6 +561,8 @@ class LowerTypeTestsModule {
561561 return FunctionAnnotations.contains (V);
562562 }
563563
564+ void maybeReplaceComdat (Function *F, StringRef OriginalName);
565+
564566public:
565567 LowerTypeTestsModule (Module &M, ModuleAnalysisManager &AM,
566568 ModuleSummaryIndex *ExportSummary,
@@ -1082,6 +1084,23 @@ void LowerTypeTestsModule::importTypeTest(CallInst *CI) {
10821084 }
10831085}
10841086
1087+ void LowerTypeTestsModule::maybeReplaceComdat (Function *F,
1088+ StringRef OriginalName) {
1089+ // For COFF we should also rename the comdat if this function also
1090+ // happens to be the key function. Even if the comdat name changes, this
1091+ // should still be fine since comdat and symbol resolution happens
1092+ // before LTO, so all symbols which would prevail have been selected.
1093+ if (F->hasComdat () && ObjectFormat == Triple::COFF &&
1094+ F->getComdat ()->getName () == OriginalName) {
1095+ Comdat *OldComdat = F->getComdat ();
1096+ Comdat *NewComdat = M.getOrInsertComdat (F->getName ());
1097+ for (GlobalObject &GO : M.global_objects ()) {
1098+ if (GO.getComdat () == OldComdat)
1099+ GO.setComdat (NewComdat);
1100+ }
1101+ }
1102+ }
1103+
10851104// ThinLTO backend: the function F has a jump table entry; update this module
10861105// accordingly. isJumpTableCanonical describes the type of the jump table entry.
10871106void LowerTypeTestsModule::importFunction (
@@ -1115,6 +1134,7 @@ void LowerTypeTestsModule::importFunction(
11151134 FDecl->setVisibility (GlobalValue::HiddenVisibility);
11161135 } else {
11171136 F->setName (Name + " .cfi" );
1137+ maybeReplaceComdat (F, Name);
11181138 F->setLinkage (GlobalValue::ExternalLinkage);
11191139 FDecl = Function::Create (F->getFunctionType (), GlobalValue::ExternalLinkage,
11201140 F->getAddressSpace (), Name, &M);
@@ -1734,19 +1754,7 @@ void LowerTypeTestsModule::buildBitSetsFromFunctionsNative(
17341754 FAlias->takeName (F);
17351755 if (FAlias->hasName ()) {
17361756 F->setName (FAlias->getName () + " .cfi" );
1737- // For COFF we should also rename the comdat if this function also
1738- // happens to be the key function. Even if the comdat name changes, this
1739- // should still be fine since comdat and symbol resolution happens
1740- // before LTO, so all symbols which would prevail have been selected.
1741- if (F->hasComdat () && ObjectFormat == Triple::COFF &&
1742- F->getComdat ()->getName () == FAlias->getName ()) {
1743- Comdat *OldComdat = F->getComdat ();
1744- Comdat *NewComdat = M.getOrInsertComdat (F->getName ());
1745- for (GlobalObject &GO : M.global_objects ()) {
1746- if (GO.getComdat () == OldComdat)
1747- GO.setComdat (NewComdat);
1748- }
1749- }
1757+ maybeReplaceComdat (F, FAlias->getName ());
17501758 }
17511759 replaceCfiUses (F, FAlias, IsJumpTableCanonical);
17521760 if (!F->hasLocalLinkage ())
0 commit comments