Index: lib/Linker/LinkModules.cpp =================================================================== --- lib/Linker/LinkModules.cpp +++ lib/Linker/LinkModules.cpp @@ -1624,15 +1624,6 @@ linkGlobalValueBody(Src); } - // Remap all of the named MDNodes in Src into the DstM module. We do this - // after linking GlobalValues so that MDNodes that reference GlobalValues - // are properly remapped. - linkNamedMDNodes(); - - // Merge the module flags into the DstM module. - if (linkModuleFlagsMetadata()) - return true; - // Update the initializers in the DstM module now that all globals that may // be referenced are in DstM. for (GlobalVariable &Src : SrcM->globals()) { @@ -1657,6 +1648,15 @@ return true; } + // Remap all of the named MDNodes in Src into the DstM module. We do this + // after linking GlobalValues so that MDNodes that reference GlobalValues + // are properly remapped. + linkNamedMDNodes(); + + // Merge the module flags into the DstM module. + if (linkModuleFlagsMetadata()) + return true; + return false; } Index: test/Linker/Inputs/linkage_debug.b.ll =================================================================== --- /dev/null +++ test/Linker/Inputs/linkage_debug.b.ll @@ -0,0 +1,27 @@ +@X = external global i32 + +declare i32 @foo() + +define void @bar() { + 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: "linkage.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, function: void ()* @bar, 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/Inputs/linkage_debug.c.ll =================================================================== --- /dev/null +++ test/Linker/Inputs/linkage_debug.c.ll @@ -0,0 +1,29 @@ +@X = global i32 5 +@U = global i32 6 +define i32 @foo() { ret i32 7, !dbg !20 } +define i32 @unused() { 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: "linkage.c.c", directory: ".") +!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, function: i32 ()* @foo, variables: !8) +!5 = !DISubroutineType(types: !6) +!6 = !{!7, !7} +!7 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed) +!8 = !{} +!10 = distinct !DISubprogram(name: "unused", scope: !1, file: !1, line: 8, type: !11, isLocal: false, isDefinition: true, scopeLine: 8, isOptimized: true, function: i32 ()* @unused, 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)"} +!20 = !DILocation(line: 4, column: 13, scope: !4) +!21 = !DILocation(line: 9, column: 3, scope: !10) Index: test/Linker/link-flags-debug.ll =================================================================== --- /dev/null +++ test/Linker/link-flags-debug.ll @@ -0,0 +1,15 @@ +; RUN: llvm-as %S/Inputs/linkage_debug.b.ll -o %t.b.bc +; RUN: llvm-as %S/Inputs/linkage_debug.c.ll -o %t.c.bc +; RUN: llvm-link -S -only-needed %t.b.bc %t.c.bc | FileCheck %s -check-prefix=B -check-prefix=C -check-prefix=CN +; RUN: llvm-link -S -internalize -only-needed %t.b.bc %t.c.bc | FileCheck %s -check-prefix=B -check-prefix=CI -check-prefix=CN + +C-LABEL: @X = global i32 5 +CI-LABEL: @X = internal global i32 5 +CN-LABEL:@U = external global i32 + +B-LABEL: define void @bar() { + +C-LABEL: define i32 @foo() +CI-LABEL: define internal i32 @foo() + +CN-LABEL:declare i32 @unused()