From d399c97984f5ff46001ae271cedfe9ece9332051 Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Thu, 10 Oct 2019 01:06:01 +0000 Subject: [PATCH] [codeview] Try to avoid emitting .cv_loc with line zero Summary: Visual Studio doesn't like it while stepping. It kicks you out of the source view of the file being stepped through and tries to fall back to the disassembly view. Fixes PR43530 The fix is incomplete, because it's possible to have a basic block with no source locations at all. In this case, we don't emit a .cv_loc, but that will result in wrong stepping behavior in the debugger if the layout predecessor of the location-less BB has an unrelated source location. We could try harder to find a valid location that dominates or post-dominates the current BB, but in general it's a dataflow problem, and one still might not exist. I left a FIXME about this. As an alternative, we might want to consider having the middle-end check if its emitting codeview and get it to stop using line zero. Reviewers: akhuang Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D68747 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@374267 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/AsmPrinter/CodeViewDebug.cpp | 16 ++++- test/DebugInfo/COFF/line-zero.ll | 77 ++++++++++++++++++++++++ test/DebugInfo/COFF/local-variables.ll | 2 +- 3 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 test/DebugInfo/COFF/line-zero.ll diff --git a/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp b/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp index 616b1368a7a..012da940f89 100644 --- a/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp +++ b/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp @@ -2858,6 +2858,14 @@ void CodeViewDebug::endFunctionImpl(const MachineFunction *MF) { CurFn = nullptr; } +// Usable locations are valid with non-zero line numbers. A line number of zero +// corresponds to optimized code that doesn't have a distinct source location. +// In this case, we try to use the previous or next source location depending on +// the context. +static bool isUsableDebugLoc(DebugLoc DL) { + return DL && DL.getLine() != 0; +} + void CodeViewDebug::beginInstruction(const MachineInstr *MI) { DebugHandlerBase::beginInstruction(MI); @@ -2869,19 +2877,21 @@ void CodeViewDebug::beginInstruction(const MachineInstr *MI) { // If the first instruction of a new MBB has no location, find the first // instruction with a location and use that. DebugLoc DL = MI->getDebugLoc(); - if (!DL && MI->getParent() != PrevInstBB) { + if (!isUsableDebugLoc(DL) && MI->getParent() != PrevInstBB) { for (const auto &NextMI : *MI->getParent()) { if (NextMI.isDebugInstr()) continue; DL = NextMI.getDebugLoc(); - if (DL) + if (isUsableDebugLoc(DL)) break; } + // FIXME: Handle the case where the BB has no valid locations. This would + // probably require doing a real dataflow analysis. } PrevInstBB = MI->getParent(); // If we still don't have a debug location, don't record a location. - if (!DL) + if (!isUsableDebugLoc(DL)) return; maybeRecordLocation(DL, Asm->MF); diff --git a/test/DebugInfo/COFF/line-zero.ll b/test/DebugInfo/COFF/line-zero.ll new file mode 100644 index 00000000000..3d343456451 --- /dev/null +++ b/test/DebugInfo/COFF/line-zero.ll @@ -0,0 +1,77 @@ +; RUN: llc < %s | FileCheck %s + +; C++ source to regenerate: +; int main() { +; volatile int x; +; x = 1; +; #line 0 +; x = 2; +; #line 7 +; x = 3; +; } + + +; CHECK-LABEL: main: # @main +; CHECK: .cv_loc 0 1 1 0 # t.cpp:1:0 +; CHECK: .cv_loc 0 1 3 0 # t.cpp:3:0 +; CHECK: movl $1, 4(%rsp) +; CHECK-NOT: .cv_loc {{.*}} t.cpp:0:0 +; CHECK: movl $2, 4(%rsp) +; CHECK: .cv_loc 0 1 7 0 # t.cpp:7:0 +; CHECK: movl $3, 4(%rsp) +; CHECK: .cv_loc 0 1 8 0 # t.cpp:8:0 +; CHECK: xorl %eax, %eax +; CHECK: retq + +; ModuleID = 't.cpp' +source_filename = "t.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.22.27905" + +; Function Attrs: norecurse nounwind uwtable +define dso_local i32 @main() local_unnamed_addr #0 !dbg !8 { +entry: + %x = alloca i32, align 4 + %x.0.x.0..sroa_cast = bitcast i32* %x to i8*, !dbg !15 + call void @llvm.dbg.declare(metadata i32* %x, metadata !13, metadata !DIExpression()), !dbg !15 + store volatile i32 1, i32* %x, align 4, !dbg !16, !tbaa !17 + store volatile i32 2, i32* %x, align 4, !dbg !21, !tbaa !17 + store volatile i32 3, i32* %x, align 4, !dbg !22, !tbaa !17 + ret i32 0, !dbg !23 +} + +; Function Attrs: nounwind readnone speculatable willreturn +declare void @llvm.dbg.declare(metadata, metadata, metadata) #2 + +attributes #0 = { norecurse nounwind uwtable } +attributes #1 = { argmemonly nounwind willreturn } +attributes #2 = { nounwind readnone speculatable willreturn } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5, !6} +!llvm.ident = !{!7} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None) +!1 = !DIFile(filename: "t.cpp", directory: "C:\5Csrc\5Cllvm-project\5Cbuild", checksumkind: CSK_MD5, checksum: "8b6d53b166e6fa660f115eff7beedf3b") +!2 = !{} +!3 = !{i32 2, !"CodeView", i32 1} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"wchar_size", i32 2} +!6 = !{i32 7, !"PIC Level", i32 2} +!7 = !{!"clang version 10.0.0"} +!8 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 1, type: !9, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !12) +!9 = !DISubroutineType(types: !10) +!10 = !{!11} +!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!12 = !{!13} +!13 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !14) +!14 = !DIDerivedType(tag: DW_TAG_volatile_type, baseType: !11) +!15 = !DILocation(line: 2, scope: !8) +!16 = !DILocation(line: 3, scope: !8) +!17 = !{!18, !18, i64 0} +!18 = !{!"int", !19, i64 0} +!19 = !{!"omnipotent char", !20, i64 0} +!20 = !{!"Simple C++ TBAA"} +!21 = !DILocation(line: 0, scope: !8) +!22 = !DILocation(line: 7, scope: !8) +!23 = !DILocation(line: 8, scope: !8) diff --git a/test/DebugInfo/COFF/local-variables.ll b/test/DebugInfo/COFF/local-variables.ll index e15152050d8..5e96160f189 100644 --- a/test/DebugInfo/COFF/local-variables.ll +++ b/test/DebugInfo/COFF/local-variables.ll @@ -60,7 +60,7 @@ ; ASM: leaq 36(%rsp), %rcx ; ASM: [[else_end:\.Ltmp.*]]: ; ASM: .LBB0_3: # %if.end -; ASM: .cv_loc 0 1 0 0 # t.cpp:0:0 +; ASM: .cv_loc 0 1 17 1 # t.cpp:17:1 ; ASM: callq capture ; ASM: nop ; ASM: addq $56, %rsp -- 2.40.0