← 输入框|github.com|刷新
Skip to content

Initial debugging info implementation - #1240

Merged
certik merged 13 commits into
lcompilers:mainfrom
certik:debug
Oct 31, 2022
Merged

certik merged 13 commits into
lcompilers:mainfrom
certik:debug

Conversation

@certik

@certik certik commented Oct 28, 2022

Copy link
Copy Markdown
Contributor

Currently it shows:

$ lpython --show-llvm examples/expr2.py 
; ModuleID = 'LFortran'
source_filename = "LFortran"

@0 = private unnamed_addr constant [2 x i8] c" \00", align 1
@1 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@2 = private unnamed_addr constant [5 x i8] c"%d%s\00", align 1

define void @_lpython_main_program() !dbg !2 {
.entry:
  call void @main0()
  br label %return

return:                                           ; preds = %.entry
  ret void
}

define void @main0() !dbg !7 {
.entry:
  %x = alloca i32, align 4
  store i32 25, i32* %x, align 4, !dbg !9
  %0 = load i32, i32* %x, align 4, !dbg !9
  call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @2, i32 0, i32 0), i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @1, i32 0, i32 0)), !dbg !9
  br label %return, !dbg !9

return:                                           ; preds = %.entry
  ret void, !dbg !9
}

declare void @_lfortran_printf(i8*, ...)

define i32 @main() {
.entry:
  call void @_lpython_main_program(), !dbg !9
  ret i32 0, !dbg !9
}

!llvm.dbg.cu = !{!0}

!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "Kaleidoscope Compiler", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
!1 = !DIFile(filename: "xxexpr.py", directory: "/yy/")
!2 = distinct !DISubprogram(name: "_lpython_main_program", scope: !1, file: !1, type: !3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !6)
!3 = !DISubroutineType(types: !4)
!4 = !{!5, !5, !5}
!5 = !DIBasicType(name: "double", size: 64, encoding: DW_ATE_float)
!6 = <temporary!> !{}
!7 = distinct !DISubprogram(name: "main0", scope: !1, file: !1, type: !3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !8)
!8 = <temporary!> !{}
!9 = !DILocation(line: 28, column: 28, scope: !7)
code generation error: asr_to_llvm: module failed verification. Error:
Expected no forward declarations!
!6 = <temporary!> !{}
inlinable function call in a function with debug info must have a !dbg location
  call void @main0()
Expected no forward declarations!
!8 = <temporary!> !{}



Note: if any of the above error or warning messages are not clear or are lacking
context please report it to us (we consider that a bug that needs to be fixed).

@certik

certik commented Oct 28, 2022

Copy link
Copy Markdown
Contributor Author

Now it works:

$ lpython --show-llvm examples/expr2.py 
; ModuleID = 'LFortran'
source_filename = "LFortran"

@0 = private unnamed_addr constant [2 x i8] c" \00", align 1
@1 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@2 = private unnamed_addr constant [5 x i8] c"%d%s\00", align 1

define void @_lpython_main_program() !dbg !3 {
.entry:
  call void @main0(), !dbg !7
  br label %return, !dbg !7

return:                                           ; preds = %.entry
  ret void, !dbg !7
}

define void @main0() !dbg !8 {
.entry:
  %x = alloca i32, align 4, !dbg !9
  store i32 25, i32* %x, align 4, !dbg !10
  %0 = load i32, i32* %x, align 4, !dbg !10
  call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @2, i32 0, i32 0), i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @1, i32 0, i32 0)), !dbg !10
  br label %return, !dbg !10

return:                                           ; preds = %.entry
  ret void, !dbg !10
}

declare void @_lfortran_printf(i8*, ...)

define i32 @main() {
.entry:
  call void @_lpython_main_program(), !dbg !9
  ret i32 0, !dbg !9
}

!llvm.dbg.cu = !{!0}

!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "Kaleidoscope Compiler", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
!1 = !DIFile(filename: "xxexpr.py", directory: "/yy/")
!2 = !{}
!3 = distinct !DISubprogram(name: "_lpython_main_program", scope: !1, file: !1, type: !4, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
!4 = !DISubroutineType(types: !5)
!5 = !{!6, !6, !6}
!6 = !DIBasicType(name: "double", size: 64, encoding: DW_ATE_float)
!7 = !DILocation(line: 54, column: 54, scope: !3)
!8 = distinct !DISubprogram(name: "main0", scope: !1, file: !1, type: !4, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
!9 = !DILocation(line: 0, scope: !8)
!10 = !DILocation(line: 28, column: 28, scope: !8)

@certik

certik commented Oct 28, 2022

Copy link
Copy Markdown
Contributor Author

Then:

dsymutil a.out
$ llvm-dwarfdump --debug-line a.out.dSYM
a.out.dSYM/Contents/Resources/DWARF/a.out:	file format Mach-O arm64

.debug_line contents:
debug_line[0x00000000]
Line table prologue:
    total_length: 0x00000049
          format: DWARF32
         version: 4
 prologue_length: 0x00000021
 min_inst_length: 1
max_ops_per_inst: 1
 default_is_stmt: 1
       line_base: -5
      line_range: 14
     opcode_base: 13
standard_opcode_lengths[DW_LNS_copy] = 0
standard_opcode_lengths[DW_LNS_advance_pc] = 1
standard_opcode_lengths[DW_LNS_advance_line] = 1
standard_opcode_lengths[DW_LNS_set_file] = 1
standard_opcode_lengths[DW_LNS_set_column] = 1
standard_opcode_lengths[DW_LNS_negate_stmt] = 0
standard_opcode_lengths[DW_LNS_set_basic_block] = 0
standard_opcode_lengths[DW_LNS_const_add_pc] = 0
standard_opcode_lengths[DW_LNS_fixed_advance_pc] = 1
standard_opcode_lengths[DW_LNS_set_prologue_end] = 0
standard_opcode_lengths[DW_LNS_set_epilogue_begin] = 0
standard_opcode_lengths[DW_LNS_set_isa] = 1
file_names[  1]:
           name: "xxexpr.py"
      dir_index: 0
       mod_time: 0x00000000
         length: 0x00000000

Address            Line   Column File   ISA Discriminator Flags
------------------ ------ ------ ------ --- ------------- -------------
0x0000000100003ec0      0      0      1   0             0  is_stmt
0x0000000100003ec4     54     54      1   0             0  is_stmt prologue_end
0x0000000100003ed0      0      0      1   0             0  is_stmt
0x0000000100003ed8     28     28      1   0             0  is_stmt prologue_end
0x0000000100003f04     28     28      1   0             0  is_stmt end_sequence

And it seems to show the correct info. Now we just postprocess it and we are done.

Comment thread src/libasr/codegen/asr_to_llvm.cpp Outdated

template <typename T>
void debug_emit_loc(const T &x) {
// TODO: convert the linear location to line and column

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would make this configurable with --debug-store-line-column. If off (default) it would just store the linear location, which is faster. If on, it would convert to line+column, which is slower, but other debugging tools could then work with it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Thirumalai-Shaktivel I thought about this and I think the best way is to just store the linear location information (as we have it in ASR) directly. It will not be directly usable in other debuggers, but it is the cleanest and most general solution, also the fastest. Will be easy to extract it from llvm-dwarf. We can also use the same approach with the WASM backend.

Comment thread src/libasr/codegen/asr_to_llvm.cpp Outdated
llvm::dwarf::DW_ATE_float);
el_types.push_back(DblTy);
el_types.push_back(DblTy);
el_types.push_back(DblTy);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't even care about this for stacktraces, so we can just use empty el_types (if it works).

@Thirumalai-Shaktivel Thirumalai-Shaktivel Oct 29, 2022 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I felt the same. So, replacing it with nullptr

llvm::DISubroutineType *dtype = DBuilder->createSubroutineType(
                    DBuilder->getOrCreateTypeArray(nullptr));

@Thirumalai-Shaktivel

Thirumalai-Shaktivel commented Oct 29, 2022 •

Copy link
Copy Markdown
Collaborator

From this output: #1240 (comment)

define void @main0() !dbg !8 {
.entry:
  %x = alloca i32, align 4, !dbg !9
  store i32 25, i32* %x, align 4, !dbg !10
  %0 = load i32, i32* %x, align 4, !dbg !10
  call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @2, i32 0, i32 0), i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @1, i32 0, i32 0)), !dbg !10
  br label %return, !dbg !10

return:                                           ; preds = %.entry
  ret void, !dbg !10
}

The debug metadata must be unique for each instruction

store i32 25, i32* %x, align 4, !dbg !10

Here !dbg !10 metadata points to the DILocation of x = (2+3) * 5. But, the ret void, !dbg !10 must not have the same metadata, right?
My point is that we have to emit new DILocation for each instruction in the IR.

@Thirumalai-Shaktivel

Thirumalai-Shaktivel commented Oct 29, 2022 •

Copy link
Copy Markdown
Collaborator

Here is the LLVM IR for C code:

#include <stdio.h>
int main() {
    int x;
    x = 1;
}
; ModuleID = 'a.c'
source_filename = "a.c"
...

; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 @main() #0 !dbg !10 {
  %1 = alloca i32, align 4
  call void @llvm.dbg.declare(metadata i32* %1, metadata !15, metadata !DIExpression()), !dbg !16
  store i32 1, i32* %1, align 4, !dbg !17
  ret i32 0, !dbg !18
}

declare void @llvm.dbg.declare(metadata, metadata, metadata) #1

!llvm.dbg.cu = !{!0}
...
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "Ubuntu clang version 14.0.0-1ubuntu1", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
!1 = !DIFile(filename: "a.c", directory: "/home/thirumalai/Open_Source/lpython", checksumkind: CSK_MD5, checksum: "59bcf6376b4ef3b2a3d4fa38b6d3b8b0")
...
!10 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 2, type: !11, scopeLine: 2, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !14)
!11 = !DISubroutineType(types: !12)
!12 = !{!13}
!13 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!14 = !{}
!15 = !DILocalVariable(name: "x", scope: !10, file: !1, line: 3, type: !13)
!16 = !DILocation(line: 3, column: 9, scope: !10)
!17 = !DILocation(line: 4, column: 7, scope: !10)
!18 = !DILocation(line: 5, column: 1, scope: !10)

@Thirumalai-Shaktivel

Copy link
Copy Markdown
Collaborator

This PR is ready to go in, @certik kindly review it.

@Thirumalai-Shaktivel
Thirumalai-Shaktivel marked this pull request as ready for review October 29, 2022 10:43
@Thirumalai-Shaktivel Thirumalai-Shaktivel added the ready for review PRs that are ready for review label Oct 29, 2022
@Thirumalai-Shaktivel

Copy link
Copy Markdown
Collaborator

Now I will work on llvm.dbg.declare()
So, For each new alloca (variable declaration or something similar), I need to call llvm.dbg.declare

  int x;
  %1 = alloca i32, align 4
  call void @llvm.dbg.declare(metadata i32* %1, metadata !15, metadata !DIExpression()), !dbg !16

@certik certik left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks great. It says I can't approve my own PR. :)

Comment thread src/libasr/utils.h
bool implicit_interface = false;
std::string target = "";
std::string arg_o = "";
bool arg_g = false;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call it emit_debug_info or something like that. It can be done in a subsequent PR.

@certik
certik merged commit 6241a98 into lcompilers:main Oct 31, 2022
@certik
certik deleted the debug branch October 31, 2022 23:22
@certik

certik commented Oct 31, 2022

Copy link
Copy Markdown
Contributor Author

Thanks @Thirumalai-Shaktivel for finishing it. It looks great.

There are two orthogonal paths forward:

  • Finish LLVM style debugging information so that one can use a debugger such as lldb with a binary generated by LPython. For that, we should convert the linear location info into line+column.
  • Finish stacktraces. For that I think we don't need any debug info for variables etc., just basic location.

I think we can use an option like --debug-line-column (there's probably a better name), that way we can do both. Ultimately we need both approaches, so any work you do towards any of the two approaches or both will be great.

@Thirumalai-Shaktivel

Copy link
Copy Markdown
Collaborator

Thank you for reviewing and merging this PR!
Yup, we have to store the line+column information in the debugging info.
I'm working on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready for review PRs that are ready for review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants