Index: llvm/lib/Object/IRSymtab.cpp =================================================================== --- llvm/lib/Object/IRSymtab.cpp +++ llvm/lib/Object/IRSymtab.cpp @@ -72,7 +72,7 @@ BumpPtrAllocator &Alloc) : Symtab(Symtab), StrtabBuilder(StrtabBuilder), Saver(Alloc) {} - DenseMap ComdatMap; + DenseMap ComdatMap; Mangler Mang; Triple TT; @@ -97,6 +97,8 @@ reinterpret_cast(Objs.data() + Objs.size())); } + Expected getComdatIndex(const Comdat *C, const Module *M); + Error addModule(Module *M); Error addSymbol(const ModuleSymbolTable &Msymtab, const SmallPtrSet &Used, @@ -140,6 +142,35 @@ return Error::success(); } +Expected Builder::getComdatIndex(const Comdat *C, const Module *M) { + auto P = ComdatMap.insert(std::make_pair(C, Comdats.size())); + if (P.second) { + std::string Name; + if (TT.isOSBinFormatCOFF()) { + const GlobalValue *GV = M->getNamedValue(C->getName()); + if (!GV) + return make_error("Could not find leader", + inconvertibleErrorCode()); + // Internal leaders do not affect symbol resolution, therefore they do not + // appear in the symbol table. + if (GV->hasLocalLinkage()) { + P.first->second = -1; + return -1; + } + llvm::raw_string_ostream OS(Name); + Mang.getNameWithPrefix(OS, GV, false); + } else { + Name = C->getName(); + } + + storage::Comdat Comdat; + setStr(Comdat.Name, Saver.save(Name)); + Comdats.push_back(Comdat); + } + + return P.first->second; +} + Error Builder::addSymbol(const ModuleSymbolTable &Msymtab, const SmallPtrSet &Used, ModuleSymbolTable::Symbol Msym) { @@ -216,14 +247,10 @@ return make_error("Unable to determine comdat of alias!", inconvertibleErrorCode()); if (const Comdat *C = Base->getComdat()) { - auto P = ComdatMap.insert(std::make_pair(C, Comdats.size())); - Sym.ComdatIndex = P.first->second; - - if (P.second) { - storage::Comdat Comdat; - setStr(Comdat.Name, C->getName()); - Comdats.push_back(Comdat); - } + Expected ComdatIndexOrErr = getComdatIndex(C, GV->getParent()); + if (!ComdatIndexOrErr) + return ComdatIndexOrErr.takeError(); + Sym.ComdatIndex = *ComdatIndexOrErr; } if (TT.isOSBinFormatCOFF()) { Index: llvm/test/LTO/Resolution/X86/symtab.ll =================================================================== --- llvm/test/LTO/Resolution/X86/symtab.ll +++ llvm/test/LTO/Resolution/X86/symtab.ll @@ -17,6 +17,15 @@ ret i32 0 } +; CHECK: D------X @fun2@8 +; CHECK-NEXT: comdat @fun2@8 +$fun2 = comdat any +define x86_fastcallcc i32 @fun2(i32 inreg %a, i32 inreg %b) comdat { +entry: + %add = add nsw i32 %b, %a + ret i32 %add +} + ; CHECK: H------- _g1 @g1 = hidden global i32 0 @@ -43,11 +52,19 @@ @g8 = common global i32 0, align 8 ; CHECK: D------- _g9 -; CHECK-NEXT: comdat g9 +; CHECK-NEXT: comdat _g9 $g9 = comdat any @g9 = global i32 0, comdat -; CHECK: D--WI--- _g10 -; CHECK-NEXT: comdat g9 +; CHECK-NOT: _g10 +$g10 = comdat any +@g10 = internal global i32 0, comdat + +; CHECK: D------- _g11 +; CHECK-NOT: comdat +@g11 = global i32 0, comdat($g10) + +; CHECK: D--WI--- _a1 +; CHECK-NEXT: comdat _g9 ; CHECK-NEXT: fallback _g9 -@g10 = weak alias i32, i32* @g9 +@a1 = weak alias i32, i32* @g9