diff --git a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp --- a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp @@ -2944,6 +2944,13 @@ if (!Asm || !CurFn || MI->isDebugInstr() || MI->getFlag(MachineInstr::FrameSetup)) return; + // The codeview line table format requires functions are contiguous, the + // non-primary basic block sections are not contiguous with primary basic + // block sectoin, don't emit relocation for non-prmiary (and non-contiguous) + // basic block sections. + if (MI->getParent()->getParent()->hasBBSections() && + !MI->getParent()->isEntryBlock()) + return; // If the first instruction of a new MBB has no location, find the first // instruction with a location and use that. diff --git a/llvm/test/DebugInfo/COFF/basic-block-sections.ll b/llvm/test/DebugInfo/COFF/basic-block-sections.ll new file mode 100644 --- /dev/null +++ b/llvm/test/DebugInfo/COFF/basic-block-sections.ll @@ -0,0 +1,205 @@ +; RUN: llc -O0 %s -function-sections -basic-block-sections=all -unique-basic-block-section-names -mtriple=i686-pc-win32 -filetype=asm -frame-pointer=all -o - | FileCheck --check-prefix=X86 %s +; RUN: llc -O0 %s -basic-block-sections=all -unique-basic-block-section-names -mtriple=i686-pc-win32 -filetype=obj -frame-pointer=all -o - | llvm-readobj -S --sr --codeview - | FileCheck --check-prefix=OBJ32 %s +; RUN: llc -O0 %s -function-sections -basic-block-sections=all -unique-basic-block-section-names -mtriple=x86_64-pc-win32 -filetype=asm -frame-pointer=all -o - | FileCheck --check-prefix=X64 %s +; RUN: llc -O0 %s -basic-block-sections=all -unique-basic-block-section-names -mtriple=x86_64-pc-win32 -filetype=obj -frame-pointer=all -o - | llvm-readobj -S --sr --codeview - | FileCheck --check-prefix=OBJ64 %s + +;; Generated with below C++ source: +;; int volatile x; +;; static inline void bar() { +;; ++x; +;; } +;; void baz(bool b) { +;; if (b) +;; bar(); +;; } + +; X86: .section .text,"xr",one_only,"?baz@@YAX_N@Z" +; X86: [[BEGIN_OF_BAZ:.?Lfunc_begin.*]]: +; X86: .cv_file 1 "/media/sdb/llvm/basic-block-sections.cpp" +; X86: .cv_loc 0 1 5 0 +; X86: .cv_loc 0 1 6 0 +; X86-LABEL: Ltmp0: +; X86: .section .text,"xr",associative,"?baz@@YAX_N@Z.__part.1" +; X86-LABEL: "?baz@@YAX_N@Z.__part.1": +; X86: .section .text,"xr",associative,"?baz@@YAX_N@Z.__part.2" +; X86-LABEL: "?baz@@YAX_N@Z.__part.2": +; X86: retl +; X86: .section .text,"xr",one_only,"?baz@@YAX_N@Z" +; X86: [[END_OF_BAZ:.?Lfunc_end.*]]: + +; Line table +; X86: .cv_linetable 0, "?baz@@YAX_N@Z", [[END_OF_BAZ]] +; X86-NEXT: .section .debug$S,"dr" +; File index to string table offset subsection +; X86: .cv_filechecksums +; String table +; X86-NEXT: .cv_stringtable + +; OBJ32: Section { +; OBJ32: Name: .debug$S +; OBJ32: Characteristics [ (0x42300040) +; OBJ32: ] +; OBJ32: Section { +; OBJ32: Name: .debug$S +; OBJ32: Characteristics [ (0x42301040) +; OBJ32: ] +; OBJ32: Relocations [ +; OBJ32: 0xC IMAGE_REL_I386_DIR32NB ?baz@@YAX_N@Z +; OBJ32: 0x58 IMAGE_REL_I386_SECREL ?baz@@YAX_N@Z +; OBJ32: 0x5C IMAGE_REL_I386_SECTION ?baz@@YAX_N@Z +; OBJ32: 0x9C IMAGE_REL_I386_SECREL ?baz@@YAX_N@Z +; OBJ32: 0xA0 IMAGE_REL_I386_SECTION ?baz@@YAX_N@Z +; OBJ32: ] +; OBJ32: CodeViewDebugInfo [ +; OBJ32: Subsection [ +; OBJ32-NEXT: SubSectionType: Symbols (0xF1) +; OBJ32: {{.*}}Proc{{.*}}Sym { +; OBJ32: CodeSize: 0x16 +; OBJ32: DisplayName: baz +; OBJ32: LinkageName: ?baz@@YAX_N@Z +; OBJ32: } +; OBJ32: FunctionLineTable [ +; OBJ32-NEXT: LinkageName: ?baz@@YAX_N@Z +; OBJ32-NEXT: Flags: 0x0 +; OBJ32-NEXT: CodeSize: 0x16 +; OBJ32-NEXT: FilenameSegment [ +; OBJ32-NEXT: Filename: /media/sdb/llvm/basic-block-sections.cpp +; OBJ32-NEXT: +0x0 [ +; OBJ32-NEXT: LineNumberStart: 5 +; OBJ32-NEXT: LineNumberEndDelta: 0 +; OBJ32-NEXT: IsStatement: No +; OBJ32-NEXT: ] +; OBJ32-NEXT: +0x0 [ +; OBJ32-NEXT: LineNumberStart: 6 +; OBJ32-NEXT: LineNumberEndDelta: 0 +; OBJ32-NEXT: IsStatement: No +; OBJ32-NEXT: ] +; OBJ32-NEXT: ] +; OBJ32-NEXT: ] +; OBJ32-NEXT: ] +; X64: .section .text,"xr",one_only,"?baz@@YAX_N@Z" +; X64: [[BEGIN_OF_BAZ:.?Lfunc_begin.*]]: +; X64: .cv_file 1 "/media/sdb/llvm/basic-block-sections.cpp" +; X64: .cv_loc 0 1 6 0 +; X64: .section .text,"xr",associative,"?baz@@YAX_N@Z.__part.1" +; X64-LABEL: "?baz@@YAX_N@Z.__part.1": +; X64: .section .text,"xr",associative,"?baz@@YAX_N@Z.__part.2" +; X64-LABEL: "?baz@@YAX_N@Z.__part.2": +; X64: retq +; X64: .section .text,"xr",one_only,"?baz@@YAX_N@Z" +; X64: [[END_OF_BAZ:.?Lfunc_end.*]]: + +; Line table +; X64: .cv_linetable 0, "?baz@@YAX_N@Z", [[END_OF_BAZ]] +; X64-NEXT: .section .debug$S,"dr" +; File index to string table offset subsection +; X64: .cv_filechecksums +; String table +; X64-NEXT: .cv_stringtable + +; OBJ64: Section { +; OBJ64: Name: .debug$S +; OBJ64: Characteristics [ (0x42300040) +; OBJ64: ] +; OBJ64: Relocations [ +; OBJ64-NEXT: 0xA0 IMAGE_REL_AMD64_SECREL ?x@@3HC +; OBJ64-NEXT: 0xA4 IMAGE_REL_AMD64_SECTION ?x@@3HC +; OBJ64-NEXT: ] +; OBJ64: Section { +; OBJ64: Name: .debug$S +; OBJ64: Characteristics [ (0x42301040) +; OBJ64: ] +; OBJ64: Relocations [ +; OBJ64-NEXT: 0x2C IMAGE_REL_AMD64_SECREL ?baz@@YAX_N@Z +; OBJ64-NEXT: 0x30 IMAGE_REL_AMD64_SECTION ?baz@@YAX_N@Z +; OBJ64-NEXT: 0x70 IMAGE_REL_AMD64_SECREL ?baz@@YAX_N@Z +; OBJ64-NEXT: 0x74 IMAGE_REL_AMD64_SECTION ?baz@@YAX_N@Z +; OBJ64-NEXT: ] +; OBJ64: CodeViewDebugInfo [ +; OBJ64: Subsection [ +; OBJ64-NEXT: SubSectionType: Symbols (0xF1) +; OBJ64: {{.*}}Proc{{.*}}Sym { +; OBJ64: CodeSize: 0x13 +; OBJ64: DisplayName: baz +; OBJ64: LinkageName: ?baz@@YAX_N@Z +; OBJ64: } +; OBJ64: FunctionLineTable [ +; OBJ64-NEXT: LinkageName: ?baz@@YAX_N@Z +; OBJ64-NEXT: Flags: 0x0 +; OBJ64-NEXT: CodeSize: 0x13 +; OBJ64-NEXT: FilenameSegment [ +; OBJ64-NEXT: Filename: /media/sdb/llvm/basic-block-sections.cpp +; OBJ64-NEXT: +0x0 [ +; OBJ64-NEXT: LineNumberStart: 6 +; OBJ64-NEXT: LineNumberEndDelta: 0 +; OBJ64-NEXT: IsStatement: No +; OBJ64-NEXT: ] +; OBJ64-NEXT: ] +; OBJ64-NEXT: ] +; OBJ64-NEXT: ] + +; ModuleID = 'basic-block-sections.cpp' +source_filename = "basic-block-sections.cpp" +target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-pc-windows-msvc19.0.24245" + +@"?x@@3HC" = dso_local global i32 0, align 4, !dbg !0 + +; Function Attrs: nofree norecurse nounwind uwtable willreturn mustprogress +define dso_local void @"?baz@@YAX_N@Z"(i1 zeroext %0) local_unnamed_addr #0 !dbg !13 { + call void @llvm.dbg.value(metadata i1 %0, metadata !18, metadata !DIExpression(DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 8, DW_ATE_unsigned, DW_OP_stack_value)), !dbg !19 + br i1 %0, label %2, label %5, !dbg !20 + +2: ; preds = %1 + %3 = load volatile i32, i32* @"?x@@3HC", align 4, !dbg !21, !tbaa !27 + %4 = add nsw i32 %3, 1, !dbg !21 + store volatile i32 %4, i32* @"?x@@3HC", align 4, !dbg !21, !tbaa !27 + br label %5, !dbg !31 + +5: ; preds = %2, %1 + ret void, !dbg !32 +} + +; Function Attrs: nofree nosync nounwind readnone speculatable willreturn +declare void @llvm.dbg.value(metadata, metadata, metadata) #1 + +attributes #0 = { nofree norecurse nounwind uwtable willreturn mustprogress "frame-pointer"="none" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +attributes #1 = { nofree nosync nounwind readnone speculatable willreturn } + +!llvm.dbg.cu = !{!2} +!llvm.module.flags = !{!8, !9, !10, !11} +!llvm.ident = !{!12} + +!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression()) +!1 = distinct !DIGlobalVariable(name: "x", linkageName: "?x@@3HC", scope: !2, file: !3, line: 1, type: !6, isLocal: false, isDefinition: true) +!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "clang version 13.0.0 (https://github.com/llvm/llvm-project.git 8e0e1773b6442feaef2f1be11b039984f61aa51c)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, splitDebugInlining: false, nameTableKind: None) +!3 = !DIFile(filename: "basic-block-sections.cpp", directory: "/media/sdb/llvm", checksumkind: CSK_MD5, checksum: "c5a8f2e2d863d9146247c422b76d1ab1") +!4 = !{} +!5 = !{!0} +!6 = !DIDerivedType(tag: DW_TAG_volatile_type, baseType: !7) +!7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!8 = !{i32 2, !"CodeView", i32 1} +!9 = !{i32 2, !"Debug Info Version", i32 3} +!10 = !{i32 1, !"wchar_size", i32 2} +!11 = !{i32 7, !"uwtable", i32 1} +!12 = !{!"clang version 13.0.0 (https://github.com/llvm/llvm-project.git 8e0e1773b6442feaef2f1be11b039984f61aa51c)"} +!13 = distinct !DISubprogram(name: "baz", linkageName: "?baz@@YAX_N@Z", scope: !3, file: !3, line: 5, type: !14, scopeLine: 5, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !17) +!14 = !DISubroutineType(types: !15) +!15 = !{null, !16} +!16 = !DIBasicType(name: "bool", size: 8, encoding: DW_ATE_boolean) +!17 = !{!18} +!18 = !DILocalVariable(name: "b", arg: 1, scope: !13, file: !3, line: 5, type: !16) +!19 = !DILocation(line: 0, scope: !13) +!20 = !DILocation(line: 6, scope: !13) +!21 = !DILocation(line: 3, scope: !22, inlinedAt: !25) +!22 = distinct !DISubprogram(name: "bar", linkageName: "?bar@@YAXXZ", scope: !3, file: !3, line: 2, type: !23, scopeLine: 2, flags: DIFlagPrototyped, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !4) +!23 = !DISubroutineType(types: !24) +!24 = !{null} +!25 = distinct !DILocation(line: 7, scope: !26) +!26 = distinct !DILexicalBlock(scope: !13, file: !3, line: 6) +!27 = !{!28, !28, i64 0} +!28 = !{!"int", !29, i64 0} +!29 = !{!"omnipotent char", !30, i64 0} +!30 = !{!"Simple C++ TBAA"} +!31 = !DILocation(line: 7, scope: !26) +!32 = !DILocation(line: 8, scope: !13)