Index: include/llvm/Transforms/Utils/ValueMapper.h =================================================================== --- include/llvm/Transforms/Utils/ValueMapper.h +++ include/llvm/Transforms/Utils/ValueMapper.h @@ -56,6 +56,11 @@ virtual Metadata *mapTemporaryMetadata(Metadata *MD) { return nullptr; } virtual void replaceTemporaryMetadata(const Metadata *OrigMD, Metadata *NewMD) {} + + /// The client should implement this method if some metadata need + /// not be mapped, for example DISubprogram metadata for functions not + /// linked into the destination module. + virtual bool isMetadataNeeded(Metadata *MD) { return true; } }; /// RemapFlags - These are flags that the value mapping APIs allow. Index: lib/Linker/LinkModules.cpp =================================================================== --- lib/Linker/LinkModules.cpp +++ lib/Linker/LinkModules.cpp @@ -391,6 +391,11 @@ /// Used when metadata linking as a postpass for function importing. void replaceTemporaryMetadata(const Metadata *OrigMD, Metadata *NewMD) override; + + /// Invoke ModuleLinker callback to indicate whether we need to map the + /// given metadata into the destination module. Used to prevent linking of + /// metadata only needed by functions not linked into the dest module. + bool isMetadataNeeded(Metadata *MD) override; }; class LinkDiagnosticInfo : public DiagnosticInfo { @@ -471,6 +476,11 @@ /// importing and consumed during the metadata linking postpass. DenseMap *ValIDToTempMDMap; + /// Set of subprogram metadata that does not need to be linked into the + /// destination module, because the functions were not imported directly + /// or via an inlined body in an imported function. + SmallPtrSet *UnneededSubprograms; + public: ModuleLinker(Module *dstM, Linker::IdentifiedStructTypeSet &Set, Module *srcM, DiagnosticHandlerFunction DiagnosticHandler, unsigned Flags, @@ -482,7 +492,8 @@ DiagnosticHandler(DiagnosticHandler), Flags(Flags), ValueMapperFlags(RF_MoveDistinctMDs), ImportIndex(Index), ImportFunction(FuncToImport), HasExportedFunctions(false), - DoneLinkingBodies(false), ValIDToTempMDMap(ValIDToTempMDMap) { + DoneLinkingBodies(false), ValIDToTempMDMap(ValIDToTempMDMap), + UnneededSubprograms(nullptr) { assert((ImportIndex || !ImportFunction) && "Expect a FunctionInfoIndex when importing"); // If we have a FunctionInfoIndex but no function to import, @@ -544,6 +555,11 @@ /// for function importing. void replaceTemporaryMetadata(const Metadata *OrigMD, Metadata *NewMD); + /// Indicates whether we need to map the given metadata into the destination + /// module. Used to prevent linking of metadata only needed by functions not + /// linked into the dest module. + bool isMetadataNeeded(Metadata *MD); + private: bool shouldLinkFromSource(bool &LinkFromSrc, const GlobalValue &Dest, const GlobalValue &Src); @@ -649,6 +665,16 @@ const GlobalValue *DGV = nullptr); void linkNamedMDNodes(); + + /// Populate the UnneededSubprograms set with the DISubprogram metadata + /// from the source module that we don't need to link into the dest module, + /// because the functions were not imported directly or via an inlined body + /// in an imported function. + void findNeededSubprograms(ValueToValueMapTy &ValueMap); + + /// The value mapper leaves nulls in the list of subprograms for any + /// in the UnneededSubprograms map. Strip those out after metadata linking. + void stripNullSubprograms(); }; } @@ -991,6 +1017,10 @@ ModLinker->replaceTemporaryMetadata(OrigMD, NewMD); } +bool ValueMaterializerTy::isMetadataNeeded(Metadata *MD) { + return ModLinker->isMetadataNeeded(MD); +} + Metadata *ModuleLinker::mapTemporaryMetadata(Metadata *MD) { MDNode *Node = cast(MD); assert(Node->isTemporary()); @@ -1033,6 +1063,19 @@ } } +bool ModuleLinker::isMetadataNeeded(Metadata *MD) { + // Currently only DISubprogram metadata is marked as being unneeded. + if (!UnneededSubprograms) + return true; + MDNode *Node = dyn_cast(MD); + if (!Node) + return true; + DISubprogram *SP = getDISubprogram(Node); + if (!SP) + return true; + return !UnneededSubprograms->count(SP); +} + bool ModuleLinker::getComdatLeader(Module *M, StringRef ComdatName, const GlobalVariable *&GVar) { const GlobalValue *GVal = M->getNamedValue(ComdatName); @@ -1741,8 +1784,71 @@ return false; } +void ModuleLinker::findNeededSubprograms(ValueToValueMapTy &ValueMap) { + // Track unneeded nodes to make it simpler to handle the case + // where we are checking if an already-mapped SP is needed. + NamedMDNode *CompileUnits = SrcM->getNamedMetadata("llvm.dbg.cu"); + if (!CompileUnits) + return; + UnneededSubprograms = new SmallPtrSet(); + for (unsigned I = 0, E = CompileUnits->getNumOperands(); I != E; ++I) { + auto *CU = cast(CompileUnits->getOperand(I)); + assert(CU && "Expected valid compile unit"); + for (const Metadata *Op : CU->getSubprograms()->operands()) { + // Unless we were doing function importing and deferred metadata linking, + // any needed SPs should have been mapped as they would be reached + // from the function linked in (either on the function itself for linked + // function bodies, or from DILocation on inlined instructions). + assert(!(ValueMap.MD()[Op] && isMetadataLinkingPostpass()) && + "DISubprogram shouldn't be mapped yet"); + if (!ValueMap.MD()[Op]) + UnneededSubprograms->insert(Op); + } + } + if (!isMetadataLinkingPostpass()) + return; + // In the case of metadata linking as a postpass (e.g. for function + // importing), see which DISubprogram MD from the source has an associated + // temporary metadata node, which means the SP was needed by an imported + // function. + for (auto MDI : MDValueToValIDMap) { + const MDNode *Node = dyn_cast(MDI.first); + if (!Node) + continue; + DISubprogram *SP = getDISubprogram(Node); + if (!SP || !ValIDToTempMDMap->count(MDI.second)) + continue; + UnneededSubprograms->erase(SP); + } +} + +// Squash null subprograms from compile unit subprogram lists. +void ModuleLinker::stripNullSubprograms() { + NamedMDNode *CompileUnits = DstM->getNamedMetadata("llvm.dbg.cu"); + if (CompileUnits) { + for (unsigned I = 0, E = CompileUnits->getNumOperands(); I != E; ++I) { + auto *CU = cast(CompileUnits->getOperand(I)); + assert(CU && "Expected valid compile unit"); + + SmallVector NewSPs; + NewSPs.reserve(CU->getSubprograms().size()); + bool FoundNull = false; + for (DISubprogram *SP : CU->getSubprograms()) { + if (!SP) { + FoundNull = true; + continue; + } + NewSPs.push_back(SP); + } + if (FoundNull) + CU->replaceSubprograms(MDTuple::get(CU->getContext(), NewSPs)); + } + } +} + /// Insert all of the named MDNodes in Src into the Dest module. void ModuleLinker::linkNamedMDNodes() { + findNeededSubprograms(ValueMap); const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata(); for (const NamedMDNode &NMD : SrcM->named_metadata()) { // Don't link module flags here. Do them separately. @@ -1755,6 +1861,7 @@ op, ValueMap, ValueMapperFlags | RF_NullMapMissingGlobalValues, &TypeMap, &ValMaterializer)); } + stripNullSubprograms(); } /// Merge the linker flags in Src into the Dest module. Index: lib/Transforms/Utils/ValueMapper.cpp =================================================================== --- lib/Transforms/Utils/ValueMapper.cpp +++ lib/Transforms/Utils/ValueMapper.cpp @@ -189,6 +189,10 @@ ValueMaterializer *Materializer) { if (!Op) return nullptr; + + if (Materializer && !Materializer->isMetadataNeeded(Op)) + return nullptr; + if (Metadata *MappedOp = MapMetadataImpl(Op, DistinctWorklist, VM, Flags, TypeMapper, Materializer)) return MappedOp; Index: test/Linker/Inputs/only-needed-debug-metadata.ll =================================================================== --- /dev/null +++ test/Linker/Inputs/only-needed-debug-metadata.ll @@ -0,0 +1,27 @@ +@X = external global i32 + +declare i32 @foo() + +define void @bar() !dbg !4 { + load i32, i32* @X, !dbg !10 + call i32 @foo(), !dbg !11 + ret void, !dbg !12 +} + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!7, !8} +!llvm.ident = !{!9} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.8.0 (trunk 251407) (llvm/trunk 251401)", isOptimized: true, runtimeVersion: 0, emissionKind: 1, enums: !2, subprograms: !3) +!1 = !DIFile(filename: "linkused.b.c", directory: ".") +!2 = !{} +!3 = !{!4} +!4 = distinct !DISubprogram(name: "bar", scope: !1, file: !1, line: 5, type: !5, isLocal: false, isDefinition: true, scopeLine: 5, isOptimized: true, variables: !2) +!5 = !DISubroutineType(types: !6) +!6 = !{null} +!7 = !{i32 2, !"Dwarf Version", i32 4} +!8 = !{i32 2, !"Debug Info Version", i32 3} +!9 = !{!"clang version 3.8.0 (trunk 251407) (llvm/trunk 251401)"} +!10 = !DILocation(line: 6, column: 7, scope: !4) +!11 = !DILocation(line: 6, column: 3, scope: !4) +!12 = !DILocation(line: 7, column: 1, scope: !4) Index: test/Linker/only-needed-debug-metadata.ll =================================================================== --- /dev/null +++ test/Linker/only-needed-debug-metadata.ll @@ -0,0 +1,49 @@ +; RUN: llvm-as %s -o %t.bc +; RUN: llvm-as %p/Inputs/only-needed-debug-metadata.ll -o %t2.bc + +; Without -only-needed, we need to link in both DISubprogram. +; RUN: llvm-link -S %t2.bc %t.bc | FileCheck %s +; CHECK: distinct !DISubprogram(name: "foo" +; CHECK: distinct !DISubprogram(name: "unused" + +; With -only-needed, we only need to link in foo's DISubprogram. +; RUN: llvm-link -S -only-needed %t2.bc %t.bc | FileCheck %s -check-prefix=ONLYNEEDED +; ONLYNEEDED: distinct !DISubprogram(name: "foo" +; ONLYNEEDED-NOT: distinct !DISubprogram(name: "unused" + +@X = global i32 5 +@U = global i32 6 +@U_linkonce = linkonce_odr hidden global i32 6 +define i32 @foo() !dbg !4 { + ret i32 7, !dbg !20 +} +define i32 @unused() !dbg !10 { + ret i32 8, !dbg !21 +} + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!16, !17} +!llvm.ident = !{!18} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.8.0 (trunk 251407) (llvm/trunk 251401)", isOptimized: true, runtimeVersion: 0, emissionKind: 1, enums: !2, subprograms: !3, globals: !13) +!1 = !DIFile(filename: "linkused2.c", directory: "/usr/local/google/home/tejohnson/llvm/tmp") +!2 = !{} +!3 = !{!4, !10} +!4 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 4, type: !5, isLocal: false, isDefinition: true, scopeLine: 4, flags: DIFlagPrototyped, isOptimized: true, variables: !8) +!5 = !DISubroutineType(types: !6) +!6 = !{!7, !7} +!7 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed) +!8 = !{!9} +!9 = !DILocalVariable(name: "x", arg: 1, scope: !4, file: !1, line: 4, type: !7) +!10 = distinct !DISubprogram(name: "unused", scope: !1, file: !1, line: 8, type: !11, isLocal: false, isDefinition: true, scopeLine: 8, isOptimized: true, variables: !2) +!11 = !DISubroutineType(types: !12) +!12 = !{!7} +!13 = !{!14, !15} +!14 = !DIGlobalVariable(name: "X", scope: !0, file: !1, line: 1, type: !7, isLocal: false, isDefinition: true, variable: i32* @X) +!15 = !DIGlobalVariable(name: "U", scope: !0, file: !1, line: 2, type: !7, isLocal: false, isDefinition: true, variable: i32* @U) +!16 = !{i32 2, !"Dwarf Version", i32 4} +!17 = !{i32 2, !"Debug Info Version", i32 3} +!18 = !{!"clang version 3.8.0 (trunk 251407) (llvm/trunk 251401)"} +!19 = !DIExpression() +!20 = !DILocation(line: 4, column: 13, scope: !4) +!21 = !DILocation(line: 9, column: 3, scope: !10) Index: test/Linker/thinlto_funcimport_debug.ll =================================================================== --- test/Linker/thinlto_funcimport_debug.ll +++ test/Linker/thinlto_funcimport_debug.ll @@ -9,7 +9,7 @@ ; CHECK: declare i32 @func2 ; CHECK: define available_externally i32 @func1 ; CHECK: distinct !DISubprogram(name: "func1" -; CHECK: distinct !DISubprogram(name: "func2" +; CHECK-NOT: distinct !DISubprogram(name: "func2" ; ModuleID = 'dbg.o' target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" Index: test/tools/gold/X86/Inputs/linkonce-weak.ll =================================================================== --- test/tools/gold/X86/Inputs/linkonce-weak.ll +++ test/tools/gold/X86/Inputs/linkonce-weak.ll @@ -1,3 +1,19 @@ -define weak_odr void @f() { - ret void +define weak_odr void @f() !dbg !4 { + ret void, !dbg !10 } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!7, !8} +!llvm.ident = !{!9} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.8.0 (trunk 251407) (llvm/trunk 251401)", isOptimized: true, runtimeVersion: 0, emissionKind: 1, enums: !2, subprograms: !3) +!1 = !DIFile(filename: "linkonce-weak.c", directory: ".") +!2 = !{} +!3 = !{!4} +!4 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !5, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: true, variables: !2) +!5 = !DISubroutineType(types: !6) +!6 = !{null} +!7 = !{i32 2, !"Dwarf Version", i32 4} +!8 = !{i32 2, !"Debug Info Version", i32 3} +!9 = !{!"clang version 3.8.0 (trunk 251407) (llvm/trunk 251401)"} +!10 = !DILocation(line: 2, column: 1, scope: !4) Index: test/tools/gold/X86/linkonce-weak.ll =================================================================== --- test/tools/gold/X86/linkonce-weak.ll +++ test/tools/gold/X86/linkonce-weak.ll @@ -11,9 +11,29 @@ ; RUN: -shared %t2.o %t.o -o %t3.o ; RUN: llvm-dis %t3.o -o - | FileCheck %s -define linkonce_odr void @f() { - ret void +define linkonce_odr void @f() !dbg !4 { + ret void, !dbg !10 } ; Test that we get a weak_odr regardless of the order of the files -; CHECK: define weak_odr void @f() { +; CHECK: define weak_odr void @f() + +; Test that we only get a single DISubprogram for @f +; CHECK: !DISubprogram(name: "f" +; CHECK-NOT: !DISubprogram(name: "f" + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!7, !8} +!llvm.ident = !{!9} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.8.0 (trunk 251407) (llvm/trunk 251401)", isOptimized: true, runtimeVersion: 0, emissionKind: 1, enums: !2, subprograms: !3) +!1 = !DIFile(filename: "linkonce-weak.c", directory: ".") +!2 = !{} +!3 = !{!4} +!4 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !5, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: true, variables: !2) +!5 = !DISubroutineType(types: !6) +!6 = !{null} +!7 = !{i32 2, !"Dwarf Version", i32 4} +!8 = !{i32 2, !"Debug Info Version", i32 3} +!9 = !{!"clang version 3.8.0 (trunk 251407) (llvm/trunk 251401)"} +!10 = !DILocation(line: 2, column: 1, scope: !4)