Index: llvm/trunk/lib/CodeGen/AsmPrinter/CodeViewDebug.h =================================================================== --- llvm/trunk/lib/CodeGen/AsmPrinter/CodeViewDebug.h +++ llvm/trunk/lib/CodeGen/AsmPrinter/CodeViewDebug.h @@ -101,6 +101,18 @@ }; FunctionInfo *CurFn; + /// The set of comdat .debug$S sections that we've seen so far. Each section + /// must start with a magic version number that must only be emitted once. + /// This set tracks which sections we've already opened. + DenseSet ComdatDebugSections; + + /// Switch to the appropriate .debug$S section for GVSym. If GVSym, the symbol + /// of an emitted global value, is in a comdat COFF section, this will switch + /// to a new .debug$S section in that comdat. This method ensures that the + /// section starts with the magic version number on first use. If GVSym is + /// null, uses the main .debug$S section. + void switchToDebugSectionForSymbol(const MCSymbol *GVSym); + /// The next available function index for use with our .cv_* directives. Not /// to be confused with type indices for LF_FUNC_ID records. unsigned NextFuncId = 0; @@ -148,9 +160,13 @@ FileToFilepathMap.clear(); } + /// Emit the magic version number at the start of a CodeView type or symbol + /// section. Appears at the front of every .debug$S or .debug$T section. + void emitCodeViewMagicVersion(); + void emitTypeInformation(); - void emitInlineeFuncIdsAndLines(); + void emitInlineeLinesSubsection(); void emitDebugInfoForFunction(const Function *GV, FunctionInfo &FI); Index: llvm/trunk/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp =================================================================== --- llvm/trunk/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp +++ llvm/trunk/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp @@ -18,6 +18,7 @@ #include "llvm/DebugInfo/CodeView/TypeIndex.h" #include "llvm/DebugInfo/CodeView/TypeRecord.h" #include "llvm/MC/MCExpr.h" +#include "llvm/MC/MCSectionCOFF.h" #include "llvm/MC/MCSymbol.h" #include "llvm/Support/COFF.h" #include "llvm/Target/TargetSubtargetInfo.h" @@ -217,19 +218,19 @@ /*IsStmt=*/false, DL->getFilename()); } +void CodeViewDebug::emitCodeViewMagicVersion() { + OS.EmitValueToAlignment(4); + OS.AddComment("Debug section magic"); + OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4); +} + void CodeViewDebug::endModule() { if (FnDebugInfo.empty()) return; emitTypeInformation(); - // FIXME: For functions that are comdat, we should emit separate .debug$S - // sections that are comdat associative with the main function instead of - // having one big .debug$S section. assert(Asm != nullptr); - OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugSymbolsSection()); - OS.AddComment("Debug section magic"); - OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4); // The COFF .debug$S section consists of several subsections, each starting // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length @@ -237,12 +238,16 @@ // aligned. // Make a subsection for all the inlined subprograms. - emitInlineeFuncIdsAndLines(); + emitInlineeLinesSubsection(); // Emit per-function debug information. for (auto &P : FnDebugInfo) emitDebugInfoForFunction(P.first, P.second); + // Switch back to the generic .debug$S section after potentially processing + // comdat symbol sections. + switchToDebugSectionForSymbol(nullptr); + // This subsection holds a file index to offset in string table table. OS.AddComment("File index to string table offset subsection"); OS.EmitCVFileChecksumsDirective(); @@ -275,9 +280,7 @@ // Start the .debug$T section with 0x4. OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection()); - OS.AddComment("Debug section magic"); - OS.EmitValueToAlignment(4); - OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4); + emitCodeViewMagicVersion(); TypeTable.ForEachRecord( [&](TypeIndex Index, const MemoryTypeTableBuilder::Record *R) { @@ -298,10 +301,13 @@ }); } -void CodeViewDebug::emitInlineeFuncIdsAndLines() { +void CodeViewDebug::emitInlineeLinesSubsection() { if (InlinedSubprograms.empty()) return; + // Use the generic .debug$S section. + switchToDebugSectionForSymbol(nullptr); + MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(), *InlineEnd = MMI->getContext().createTempSymbol(); @@ -401,6 +407,26 @@ OS.EmitIntValue(SymbolKind::S_INLINESITE_END, 2); // RecordKind } +void CodeViewDebug::switchToDebugSectionForSymbol(const MCSymbol *GVSym) { + // If we have a symbol, it may be in a section that is COMDAT. If so, find the + // comdat key. A section may be comdat because of -ffunction-sections or + // because it is comdat in the IR. + MCSectionCOFF *GVSec = + GVSym ? dyn_cast(&GVSym->getSection()) : nullptr; + const MCSymbol *KeySym = GVSec ? GVSec->getCOMDATSymbol() : nullptr; + + MCSectionCOFF *DebugSec = cast( + Asm->getObjFileLowering().getCOFFDebugSymbolsSection()); + DebugSec = OS.getContext().getAssociativeCOFFSection(DebugSec, KeySym); + + OS.SwitchSection(DebugSec); + + // Emit the magic version number if this is the first time we've switched to + // this section. + if (ComdatDebugSections.insert(DebugSec).second) + emitCodeViewMagicVersion(); +} + void CodeViewDebug::emitDebugInfoForFunction(const Function *GV, FunctionInfo &FI) { // For each function there is a separate subsection @@ -408,6 +434,9 @@ const MCSymbol *Fn = Asm->getSymbol(GV); assert(Fn); + // Switch to the to a comdat section, if appropriate. + switchToDebugSectionForSymbol(Fn); + StringRef FuncName; if (auto *SP = GV->getSubprogram()) FuncName = SP->getDisplayName(); Index: llvm/trunk/test/DebugInfo/COFF/asm.ll =================================================================== --- llvm/trunk/test/DebugInfo/COFF/asm.ll +++ llvm/trunk/test/DebugInfo/COFF/asm.ll @@ -22,6 +22,7 @@ ; X86: [[END_OF_F:.?Lfunc_end.*]]: ; ; X86-LABEL: .section .debug$S,"dr" +; X86-NEXT: .p2align 2 ; X86-NEXT: .long 4 ; Symbol subsection ; X86-NEXT: .long 241 @@ -114,6 +115,7 @@ ; X64: [[END_OF_F:.?Lfunc_end.*]]: ; ; X64-LABEL: .section .debug$S,"dr" +; X64-NEXT: .p2align 2 ; X64-NEXT: .long 4 ; Symbol subsection ; X64-NEXT: .long 241 Index: llvm/trunk/test/DebugInfo/COFF/comdat.ll =================================================================== --- llvm/trunk/test/DebugInfo/COFF/comdat.ll +++ llvm/trunk/test/DebugInfo/COFF/comdat.ll @@ -0,0 +1,196 @@ +; RUN: llc < %s | FileCheck %s + +; Verify that we get *two* .debug$S sections, the main one describing bar and +; main, and one for f and fin$f, which is comdat with f. + +; Start in the main symbol section describing bar and main. + +; CHECK: .section .debug$S,"dr"{{$}} +; CHECK: .long 4 # Debug section magic +; CHECK: # Symbol subsection for bar +; CHECK-NOT: Debug section magic +; CHECK: # Symbol subsection for main + +; Emit symbol info for f and its associated code in a separate associated +; section. + +; CHECK: .section .debug$S,"dr",associative,f{{$}} +; CHECK: .long 4 # Debug section magic +; CHECK: # Symbol subsection for f +; CHECK-NOT: Debug section magic +; CHECK: # Symbol subsection for ?fin$0@0@f@@ + +; Switch back to the main section for the shared file checksum table and string +; table. + +; CHECK: .section .debug$S,"dr"{{$}} +; CHECK-NOT: Debug section magic +; CHECK: .cv_filechecksums +; CHECK: .cv_stringtable +; CHECK-NOT: .section .debug$S, + +; Generated with this C++ source: +; void foo(); +; void bar(); +; extern volatile int x; +; inline void __declspec(noinline) f(bool c) { +; x++; +; if (c) { +; __try { +; foo(); +; } __finally { +; x++; +; } +; } else +; bar(); +; x++; +; } +; void bar() { +; x++; +; } +; int main() { +; f(true); +; } + +; ModuleID = 't.cpp' +source_filename = "t.cpp" +target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-pc-windows-msvc19.0.23918" + +$f = comdat any + +@x = external global i32, align 4 + +; Function Attrs: norecurse nounwind uwtable +define void @bar() #0 !dbg !7 { +entry: + %0 = load volatile i32, i32* @x, align 4, !dbg !10, !tbaa !11 + %inc = add nsw i32 %0, 1, !dbg !10 + store volatile i32 %inc, i32* @x, align 4, !dbg !10, !tbaa !11 + ret void, !dbg !15 +} + +; Function Attrs: nounwind uwtable +define i32 @main() #1 !dbg !16 { +entry: + tail call void @f(i32 1), !dbg !20 + ret i32 0, !dbg !21 +} + +; Function Attrs: inlinehint noinline nounwind uwtable +define linkonce_odr void @f(i32 %c) #2 comdat personality i8* bitcast (i32 (...)* @__C_specific_handler to i8*) !dbg !22 { +entry: + tail call void @llvm.dbg.value(metadata i32 %c, i64 0, metadata !26, metadata !27), !dbg !28 + %0 = load volatile i32, i32* @x, align 4, !dbg !29, !tbaa !11 + %inc = add nsw i32 %0, 1, !dbg !29 + store volatile i32 %inc, i32* @x, align 4, !dbg !29, !tbaa !11 + %tobool = icmp eq i32 %c, 0, !dbg !30 + br i1 %tobool, label %if.else, label %if.then, !dbg !32 + +if.then: ; preds = %entry + invoke void bitcast (void (...)* @foo to void ()*)() #6 + to label %invoke.cont unwind label %ehcleanup, !dbg !33 + +invoke.cont: ; preds = %if.then + tail call fastcc void @"\01?fin$0@0@f@@"() #7, !dbg !36 + br label %if.end, !dbg !37 + +ehcleanup: ; preds = %if.then + %1 = cleanuppad within none [], !dbg !36 + tail call fastcc void @"\01?fin$0@0@f@@"() #7 [ "funclet"(token %1) ], !dbg !36 + cleanupret from %1 unwind to caller, !dbg !36 + +if.else: ; preds = %entry + tail call void @bar(), !dbg !38 + br label %if.end + +if.end: ; preds = %if.else, %invoke.cont + %2 = load volatile i32, i32* @x, align 4, !dbg !39, !tbaa !11 + %inc1 = add nsw i32 %2, 1, !dbg !39 + store volatile i32 %inc1, i32* @x, align 4, !dbg !39, !tbaa !11 + ret void, !dbg !40 +} + +; Function Attrs: nounwind +define internal fastcc void @"\01?fin$0@0@f@@"() unnamed_addr #3 comdat($f) !dbg !41 { +entry: + tail call void @llvm.dbg.value(metadata i8* null, i64 0, metadata !44, metadata !27), !dbg !48 + tail call void @llvm.dbg.value(metadata i8 0, i64 0, metadata !46, metadata !27), !dbg !48 + %0 = load volatile i32, i32* @x, align 4, !dbg !49, !tbaa !11 + %inc = add nsw i32 %0, 1, !dbg !49 + store volatile i32 %inc, i32* @x, align 4, !dbg !49, !tbaa !11 + ret void, !dbg !51 +} + +declare void @foo(...) #4 + +declare i32 @__C_specific_handler(...) + +; Function Attrs: nounwind readnone +declare void @llvm.dbg.value(metadata, i64, metadata, metadata) #5 + +attributes #0 = { norecurse nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #2 = { inlinehint noinline nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #3 = { nounwind "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #4 = { "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #5 = { nounwind readnone } +attributes #6 = { noinline } +attributes #7 = { nounwind } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5} +!llvm.ident = !{!6} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.9.0 ", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) +!1 = !DIFile(filename: "t.cpp", directory: "D:\5Csrc\5Cllvm\5Cbuild") +!2 = !{} +!3 = !{i32 2, !"CodeView", i32 1} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"PIC Level", i32 2} +!6 = !{!"clang version 3.9.0 "} +!7 = distinct !DISubprogram(name: "bar", scope: !1, file: !1, line: 19, type: !8, isLocal: false, isDefinition: true, scopeLine: 19, isOptimized: true, unit: !0, variables: !2) +!8 = !DISubroutineType(types: !9) +!9 = !{null} +!10 = !DILocation(line: 20, column: 4, scope: !7) +!11 = !{!12, !12, i64 0} +!12 = !{!"int", !13, i64 0} +!13 = !{!"omnipotent char", !14, i64 0} +!14 = !{!"Simple C/C++ TBAA"} +!15 = !DILocation(line: 21, column: 1, scope: !7) +!16 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 22, type: !17, isLocal: false, isDefinition: true, scopeLine: 22, isOptimized: true, unit: !0, variables: !2) +!17 = !DISubroutineType(types: !18) +!18 = !{!19} +!19 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed) +!20 = !DILocation(line: 23, column: 3, scope: !16) +!21 = !DILocation(line: 24, column: 1, scope: !16) +!22 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 5, type: !23, isLocal: false, isDefinition: true, scopeLine: 5, flags: DIFlagPrototyped, isOptimized: true, unit: !0, variables: !25) +!23 = !DISubroutineType(types: !24) +!24 = !{null, !19} +!25 = !{!26} +!26 = !DILocalVariable(name: "c", arg: 1, scope: !22, file: !1, line: 5, type: !19) +!27 = !DIExpression() +!28 = !DILocation(line: 5, column: 40, scope: !22) +!29 = !DILocation(line: 6, column: 4, scope: !22) +!30 = !DILocation(line: 7, column: 7, scope: !31) +!31 = distinct !DILexicalBlock(scope: !22, file: !1, line: 7, column: 7) +!32 = !DILocation(line: 7, column: 7, scope: !22) +!33 = !DILocation(line: 9, column: 7, scope: !34) +!34 = distinct !DILexicalBlock(scope: !35, file: !1, line: 8, column: 11) +!35 = distinct !DILexicalBlock(scope: !31, file: !1, line: 7, column: 10) +!36 = !DILocation(line: 10, column: 5, scope: !34) +!37 = !DILocation(line: 13, column: 3, scope: !35) +!38 = !DILocation(line: 14, column: 5, scope: !31) +!39 = !DILocation(line: 15, column: 4, scope: !22) +!40 = !DILocation(line: 16, column: 1, scope: !22) +!41 = distinct !DISubprogram(linkageName: "\01?fin$0@0@f@@", scope: !1, file: !1, line: 10, type: !42, isLocal: true, isDefinition: true, scopeLine: 10, flags: DIFlagArtificial, isOptimized: true, unit: !0, variables: !43) +!42 = !DISubroutineType(types: !2) +!43 = !{!44, !46} +!44 = !DILocalVariable(name: "frame_pointer", arg: 2, scope: !41, type: !45, flags: DIFlagArtificial) +!45 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64, align: 64) +!46 = !DILocalVariable(name: "abnormal_termination", arg: 1, scope: !41, type: !47, flags: DIFlagArtificial | DIFlagObjectPointer) +!47 = !DIBasicType(name: "unsigned char", size: 8, align: 8, encoding: DW_ATE_unsigned_char) +!48 = !DILocation(line: 0, scope: !41) +!49 = !DILocation(line: 11, column: 8, scope: !50) +!50 = distinct !DILexicalBlock(scope: !41, file: !1, line: 10, column: 17) +!51 = !DILocation(line: 12, column: 5, scope: !41) Index: llvm/trunk/test/DebugInfo/COFF/multifile.ll =================================================================== --- llvm/trunk/test/DebugInfo/COFF/multifile.ll +++ llvm/trunk/test/DebugInfo/COFF/multifile.ll @@ -31,6 +31,7 @@ ; X86: [[END_OF_F:.?Lfunc_end.*]]: ; ; X86-LABEL: .section .debug$S,"dr" +; X86-NEXT: .p2align 2 ; X86-NEXT: .long 4 ; Symbol subsection ; X86-NEXT: .long 241 @@ -138,6 +139,7 @@ ; X64: [[END_OF_F:.?Lfunc_end.*]]: ; ; X64-LABEL: .section .debug$S,"dr" +; X64-NEXT: .p2align 2 ; X64-NEXT: .long 4 ; Symbol subsection ; X64-NEXT: .long 241 Index: llvm/trunk/test/DebugInfo/COFF/multifunction.ll =================================================================== --- llvm/trunk/test/DebugInfo/COFF/multifunction.ll +++ llvm/trunk/test/DebugInfo/COFF/multifunction.ll @@ -52,6 +52,7 @@ ; X86: [[END_OF_F:.?Lfunc_end.*]]: ; ; X86-LABEL: .section .debug$S,"dr" +; X86-NEXT: .p2align 2 ; X86-NEXT: .long 4 ; Symbol subsection for x ; X86-NEXT: .long 241 @@ -315,6 +316,7 @@ ; X64: [[END_OF_F:.?Lfunc_end.*]]: ; ; X64-LABEL: .section .debug$S,"dr" +; X64-NEXT: .p2align 2 ; X64-NEXT: .long 4 ; Symbol subsection for x ; X64-NEXT: .long 241 Index: llvm/trunk/test/DebugInfo/COFF/simple.ll =================================================================== --- llvm/trunk/test/DebugInfo/COFF/simple.ll +++ llvm/trunk/test/DebugInfo/COFF/simple.ll @@ -21,6 +21,7 @@ ; X86: [[END_OF_F:Lfunc_end.*]]: ; ; X86-LABEL: .section .debug$S,"dr" +; X86-NEXT: .p2align 2 ; X86-NEXT: .long 4 ; Symbol subsection ; X86-NEXT: .long 241 @@ -110,6 +111,7 @@ ; X64: [[END_OF_F:.?Lfunc_end.*]]: ; ; X64-LABEL: .section .debug$S,"dr" +; X64-NEXT: .p2align 2 ; X64-NEXT: .long 4 ; Symbol subsection ; X64-NEXT: .long 241