Repository navigation
Initial debugging info implementation - #1240
Conversation
|
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) |
|
Then: And it seems to show the correct info. Now we just postprocess it and we are done. |
|
|
||
| template <typename T> | ||
| void debug_emit_loc(const T &x) { | ||
| // TODO: convert the linear location to line and column |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@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.
| llvm::dwarf::DW_ATE_float); | ||
| el_types.push_back(DblTy); | ||
| el_types.push_back(DblTy); | ||
| el_types.push_back(DblTy); |
There was a problem hiding this comment.
I think we don't even care about this for stacktraces, so we can just use empty el_types (if it works).
There was a problem hiding this comment.
Yup, I felt the same. So, replacing it with nullptr
llvm::DISubroutineType *dtype = DBuilder->createSubroutineType(
DBuilder->getOrCreateTypeArray(nullptr));2206e6e to
7ecf51c
Compare
|
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 !10Here |
|
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) |
7ecf51c to
5fb0c4f
Compare
|
This PR is ready to go in, @certik kindly review it. |
|
Now I will work on int x; %1 = alloca i32, align 4
call void @llvm.dbg.declare(metadata i32* %1, metadata !15, metadata !DIExpression()), !dbg !16 |
certik
left a comment
There was a problem hiding this comment.
I think this looks great. It says I can't approve my own PR. :)
| bool implicit_interface = false; | ||
| std::string target = ""; | ||
| std::string arg_o = ""; | ||
| bool arg_g = false; |
There was a problem hiding this comment.
I would call it emit_debug_info or something like that. It can be done in a subsequent PR.
|
Thanks @Thirumalai-Shaktivel for finishing it. It looks great. There are two orthogonal paths forward:
I think we can use an option like |
|
Thank you for reviewing and merging this PR! |
Currently it shows: