Index: lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp =================================================================== --- lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp +++ lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp @@ -69,15 +69,42 @@ // Promote each local-linkage entity defined by ExportM and used by ImportM by // changing visibility and appending the given ModuleId. void promoteInternals(Module &ExportM, Module &ImportM, StringRef ModuleId) { + DenseMap> ComdatMap; + DenseSet ComdatsToDelete; for (auto &ExportGV : ExportM.global_values()) { + Comdat *C = ExportGV.getComdat(); + if (C) { + if (auto *GO = dyn_cast(&ExportGV)) { + if (ComdatsToDelete.count(C->getName())) + GO->setComdat(nullptr); + else if (C->getName() != ExportGV.getName()) + ComdatMap[C->getName()].push_back(GO); + } + } + if (!ExportGV.hasLocalLinkage()) continue; - GlobalValue *ImportGV = ImportM.getNamedValue(ExportGV.getName()); + StringRef Name = ExportGV.getName(); + GlobalValue *ImportGV = ImportM.getNamedValue(Name); if (!ImportGV || ImportGV->use_empty()) continue; - std::string NewName = (ExportGV.getName() + ModuleId).str(); + std::string NewName = (Name + ModuleId).str(); + + // COFF requires that every COMDAT contain a symbol with the same name + // as the COMDAT. If renaming this value would break that requirement, + // delete the COMDAT. This is safe, because this COMDAT is not going to + // be merged anyway. + if (C && C->getName() == Name) { + if (auto *GO = dyn_cast(&ExportGV)) + GO->setComdat(nullptr); + ComdatsToDelete.insert(Name); + auto I = ComdatMap.find(Name); + if (I != ComdatMap.end()) + for (auto &O : I->second) + O->setComdat(nullptr); + } ExportGV.setName(NewName); ExportGV.setLinkage(GlobalValue::ExternalLinkage); @@ -86,6 +113,10 @@ ImportGV->setName(NewName); ImportGV->setVisibility(GlobalValue::HiddenVisibility); } + + auto &Comdats = ExportM.getComdatSymbolTable(); + for (auto C : ComdatsToDelete) + Comdats.erase(C); } // Promote all internal (i.e. distinct) type ids used by the module by replacing Index: test/Transforms/ThinLTOBitcodeWriter/comdat.ll =================================================================== --- /dev/null +++ test/Transforms/ThinLTOBitcodeWriter/comdat.ll @@ -0,0 +1,25 @@ +; RUN: opt -thinlto-bc -o %t %s +; RUN: llvm-modextract -n 0 -o - %t | llvm-dis | FileCheck --check-prefix=CHECK0 %s +; RUN: llvm-modextract -n 1 -o - %t | llvm-dis | FileCheck --check-prefix=CHECK1 %s + +; foo should have been renamed, and there should no longer be a comdat +; with the original name. +; CHECK0: {{@"?foo[^ ]+}} = external hidden global +; CHECK1-NOT: $foo = comdat +; CHECK1: {{@"?foo[^ ]+}} = hidden + +target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-pc-windows-msvc19.0.24215" + +$foo = comdat any + +@anon = private unnamed_addr constant { [1 x i8*] } { [1 x i8*] [i8* null] }, comdat($foo), !type !0 + +@foo = internal unnamed_addr alias i8*, getelementptr inbounds ({ [1 x i8*] }, { [1 x i8*] }* @anon, i32 0, i32 0, i32 1) + +define i8* @bar() { + %1 = load i8*, i8** @foo + ret i8* %1 +} + +!0 = !{i64 8, !"?AVA@@"}