diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp --- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp @@ -2087,12 +2087,22 @@ static DebugLoc findPrologueEndLoc(const MachineFunction *MF) { // First known non-DBG_VALUE and non-frame setup location marks // the beginning of the function body. - for (const auto &MBB : *MF) - for (const auto &MI : MBB) + DebugLoc LineZeroLoc; + for (const auto &MBB : *MF) { + for (const auto &MI : MBB) { if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) && - MI.getDebugLoc()) - return MI.getDebugLoc(); - return DebugLoc(); + MI.getDebugLoc()) { + // Scan forward to try to find a non-zero line number. The prologue_end + // marks the first breakpoint in the function after the frame setup, and + // a compiler-generated line 0 location is not a meaningful breakpoint. + // If none is found, return the first location after the frame setup. + if (MI.getDebugLoc().getLine()) + return MI.getDebugLoc(); + LineZeroLoc = MI.getDebugLoc(); + } + } + } + return LineZeroLoc; } /// Register a source line with debug info. Returns the unique label that was diff --git a/llvm/test/CodeGen/X86/line-zero-prologue-end.ll b/llvm/test/CodeGen/X86/line-zero-prologue-end.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/X86/line-zero-prologue-end.ll @@ -0,0 +1,25 @@ +; RUN: llc -filetype=asm -mtriple=x86_64-apple-macosx12.0.0 -O0 %s -o - | FileCheck %s +; CHECK: Lfunc_begin0: +; CHECK-NEXT: .file{{.+}} +; CHECK-NEXT: .loc 1 2 0 ## test/test.c:2:0{{$}} +; CHECK-NEXT: .cfi_startproc +; CHECK-NEXT: ## %bb.{{[0-9]+}}: +; CHECK-NEXT: .loc 1 0 5 {{is_stmt [0-9]+}} ## test/test.c:0:5{{$}} +@x = common global i32 0, align 4 +define void @test() #0 !dbg !9 { + store i32 1, i32* @x, align 4, !dbg !12 + ret void, !dbg !14 +} +!llvm.module.flags = !{!0,!2,!4} +!llvm.dbg.cu = !{!5} +!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 12, i32 0]} +!2 = !{i32 2, !"Debug Info Version", i32 3} +!4 = !{i32 7, !"PIC Level", i32 2} +!5 = distinct !DICompileUnit(language: DW_LANG_C99, file: !6, producer: "Apple clang version 13.0.0 (clang-1300.0.29.3)", isOptimized: false, runtimeVersion: 0, emissionKind: LineTablesOnly, enums: !7, nameTableKind: None, sysroot: "/Library/Developer/CommandLineTools/SDKs/MacOSX12.0.sdk", sdk: "MacOSX12.0.sdk") +!6 = !DIFile(filename: "/Users/shubham/Development/test/test.c", directory: "/Users/shubham/Development/deltaTest") +!7 = !{} +!9 = distinct !DISubprogram(name: "test", scope: !10, file: !10, line: 2, type: !11, scopeLine: 2, spFlags: DISPFlagDefinition, unit: !5, retainedNodes: !7) +!10 = !DIFile(filename: "test/test.c", directory: "/Users/shubham/Development") +!11 = !DISubroutineType(types: !7) +!12 = !DILocation(line: 0, column: 5, scope: !9) +!14 = !DILocation(line: 3, column: 1, scope: !9) \ No newline at end of file diff --git a/llvm/test/CodeGen/X86/no-non-zero-debug-loc-prologue.ll b/llvm/test/CodeGen/X86/no-non-zero-debug-loc-prologue.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/X86/no-non-zero-debug-loc-prologue.ll @@ -0,0 +1,22 @@ +; RUN: llc -filetype=asm -mtriple=x86_64-apple-macosx12.0.0 -O0 %s -o - | FileCheck %s +; CHECK: Lfunc_begin0: +; CHECK-NEXT: .file{{.+}} +; CHECK-NEXT: .loc 1 1 0 ## test-small.c:1:0{{$}} +; CHECK-NEXT: .cfi_startproc +; CHECK-NEXT: ## %bb.{{[0-9]+}}: +; CHECK-NEXT: .loc 1 0 1 prologue_end{{.*}} +define void @test() #0 !dbg !9 { + ret void, !dbg !12 +} +!llvm.module.flags = !{!0, !2, !4} +!llvm.dbg.cu = !{!5} +!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 12, i32 0]} +!2 = !{i32 2, !"Debug Info Version", i32 3} +!4 = !{i32 7, !"PIC Level", i32 2} +!5 = distinct !DICompileUnit(language: DW_LANG_C99, file: !6, producer: "Apple clang version 13.0.0 (clang-1300.0.29.3)", isOptimized: false, runtimeVersion: 0, emissionKind: LineTablesOnly, enums: !7, nameTableKind: None, sysroot: "/Library/Developer/CommandLineTools/SDKs/MacOSX12.0.sdk", sdk: "MacOSX12.0.sdk") +!6 = !DIFile(filename: "/Users/shubham/Development/test/test-small.c", directory: "/Users/shubham/Development/test") +!7 = !{} +!9 = distinct !DISubprogram(name: "test", scope: !10, file: !10, line: 1, type: !11, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !5, retainedNodes: !7) +!10 = !DIFile(filename: "test-small.c", directory: "/Users/shubham/Development/test") +!11 = !DISubroutineType(types: !7) +!12 = !DILocation(line: 0, column: 1, scope: !9) \ No newline at end of file diff --git a/llvm/test/DebugInfo/MIR/X86/debug-loc-0.mir b/llvm/test/DebugInfo/MIR/X86/debug-loc-0.mir --- a/llvm/test/DebugInfo/MIR/X86/debug-loc-0.mir +++ b/llvm/test/DebugInfo/MIR/X86/debug-loc-0.mir @@ -3,9 +3,9 @@ # RUN: llc -start-before=machine-cp -O2 -filetype=asm -mtriple=x86_64-apple-macosx10.9.0 -o - %s | FileCheck %s # CHECK: Ltmp0: -# CHECK: .loc 1 0 0 prologue_end +# CHECK: .loc 1 0 0 # CHECK-NOT: .loc 1 0 0 -# CHECK: .loc 1 37 1 +# CHECK: .loc 1 37 1 prologue_end --- | ; ModuleID = '' diff --git a/llvm/test/DebugInfo/X86/dbg-prolog-end.ll b/llvm/test/DebugInfo/X86/dbg-prolog-end.ll --- a/llvm/test/DebugInfo/X86/dbg-prolog-end.ll +++ b/llvm/test/DebugInfo/X86/dbg-prolog-end.ll @@ -26,7 +26,7 @@ declare void @llvm.dbg.declare(metadata, metadata, metadata) nounwind readnone ;CHECK-LABEL: main: -;CHECK: .loc 1 0 0 prologue_end +;CHECK: .loc 1 8 2 prologue_end define i32 @main() nounwind ssp !dbg !6 { entry: