Frozen via llvm-dev
2017-Jun-02 15:44 UTC
[llvm-dev] How the LLVM handle the debug location information of continue keyword and right brace(loop end location)?
Let me show you the following simple C code:
int main()
{
int i;
for (i = 0; i < 256; i++)
{
i++;
}
}
In this simple C code, if we use Clang to compile it and debug it: We will get
something like this:
(gdb) b main
Breakpoint 1 at 0x100000f7b: file a.c, line 5.
(gdb) r
Starting program: a.out
[New Thread 0x1403 of process 23435]
warning: unhandled dyld version (15)
Thread 2 hit Breakpoint 1, main () at a.c:5
5 for (i = 0; i < 256; i++)
(gdb) n
7 i++;
(gdb)
5 for (i = 0; i < 256; i++)
(gdb)
7 i++;
That is to say, the right brace of location LLVM doesn't emit. However if we
have the continue keyword:
int main()
{
int i;
for (i = 0; i < 256; i++)
{
continue;
i++;
}
}
Then we compile and debug it:
Thread 2 hit Breakpoint 1, main () at a.c:5
5 for (i = 0; i < 256; i++)
(gdb) n
7 continue;
(gdb)
5 for (i = 0; i < 256; i++)
(gdb)
7 continue;
(gdb)
5 for (i = 0; i < 256; i++)
(gdb)
7 continue;
We will stop the line of continue. But if we compare the LLVM IR between them:
The right brace and continue keyword both are the following the br instruction
and !dbg !23. Except that the line number of !dbg !23 not the same.
; :6: ; preds = %3 br label %7, !dbg !23
The question is how LLVM know whether to generate the debug location(generate
for the continue keyword line, but not for the loop's right brace)? Because
they are the same br instruction and others are the same.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20170602/758186d1/attachment.html>
Robinson, Paul via llvm-dev
2017-Jun-02 18:50 UTC
[llvm-dev] How the LLVM handle the debug location information of continue keyword and right brace(loop end location)?
The braces around the body of the 'for' statement indicate grouping, but
don't have any other semantic significance. If you look at the abstract
syntax tree (AST) constructed for this by any compiler, it is highly unlikely to
have an explicit representation of the braces, because the structure of the AST
already describes the grouping. On the other hand, a 'continue'
statement will be explicitly represented in the AST because it is a statement in
its own right.
If I modify your for-loop body to this:
{
i++;
#ifdef CONTINUE
continue;
#endif
}
and compile it both ways, then I see the same thing you do: The generated IR is
the same for both cases except for the source-location of the branch. This
makes sense to me as follows.
A 'for' loop has four parts: initialization, condition, increment, and
body. Clang emits the condition and increment parts in their own basic-blocks,
for convenience. This means it implicitly needs to add a branch at the end of
the "body" block to the "increment" block. However, if the
"body" block already ends with an explicit branch (of any kind), then
it can omit the implicit branch. The 'continue' statement will
obviously be generated as an explicit branch to the "increment" block.
The difference in debug-info is that the explicit branch is associated with the
'continue' statement, while the implicit branch is associated with the
'for' statement, and these statements have different source locations.
Even though the sequence of instructions is the same, the *reason* they were
emitted is different, and the debug info reflects that difference.
--paulr
From: llvm-dev [mailto:llvm-dev-bounces at lists.llvm.org] On Behalf Of Frozen
via llvm-dev
Sent: Friday, June 02, 2017 8:44 AM
To: llvm-dev at lists.llvm.org
Subject: [llvm-dev] How the LLVM handle the debug location information of
continue keyword and right brace(loop end location)?
Let me show you the following simple C code:
int main()
{
int i;
for (i = 0; i < 256; i++)
{
i++;
}
}
In this simple C code, if we use Clang to compile it and debug it: We will get
something like this:
(gdb) b main
Breakpoint 1 at 0x100000f7b: file a.c, line 5.
(gdb) r
Starting program: a.out
[New Thread 0x1403 of process 23435]
warning: unhandled dyld version (15)
Thread 2 hit Breakpoint 1, main () at a.c:5
5 for (i = 0; i < 256; i++)
(gdb) n
7 i++;
(gdb)
5 for (i = 0; i < 256; i++)
(gdb)
7 i++;
That is to say, the right brace of location LLVM doesn't emit. However if we
have the continue keyword:
int main()
{
int i;
for (i = 0; i < 256; i++)
{
continue;
i++;
}
}
Then we compile and debug it:
Thread 2 hit Breakpoint 1, main () at a.c:5
5 for (i = 0; i < 256; i++)
(gdb) n
7 continue;
(gdb)
5 for (i = 0; i < 256; i++)
(gdb)
7 continue;
(gdb)
5 for (i = 0; i < 256; i++)
(gdb)
7 continue;
We will stop the line of continue. But if we compare the LLVM IR between them:
The right brace and continue keyword both are the following the br instruction
and !dbg !23. Except that the line number of !dbg !23 not the same.
; :6: ; preds = %3 br label %7, !dbg !23
The question is how LLVM know whether to generate the debug location(generate
for the continue keyword line, but not for the loop's right brace)? Because
they are the same br instruction and others are the same.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20170602/6d6d2358/attachment-0001.html>
Frozen via llvm-dev
2017-Jun-03 07:48 UTC
[llvm-dev] How the LLVM handle the debug location information of continue keyword and right brace(loop end location)?
Hi paulr:
Thanks for your kindly response. Maybe I don't describe my question
cleanly, let me show more information. From my side, I notice that whether we
are in the continue keyword mode or we are in the right brace mode, the target
of br instruction is the same, i.e. for loop
Continue Keyword Mode:
; <label>:6: ; preds = %3
br label %7, !dbg !23
; <label>:7: ; preds = %6
%8 = load i32, i32* %2, align 4, !dbg !25
%9 = add nsw i32 %8, 1, !dbg !25
store i32 %9, i32* %2, align 4, !dbg !25
br label %3, !dbg !27, !llvm.loop !28
Right Brace Mode:
; <label>:6: ; preds = %3
br label %7, !dbg !23
; <label>:7: ; preds = %6
%8 = load i32, i32* %2, align 4, !dbg !25
%9 = add nsw i32 %8, 1, !dbg !25
store i32 %9, i32* %2, align 4, !dbg !25
br label %3, !dbg !27, !llvm.loop !28
There are only different line number of !dbg!23. But my concern and question is:
Why continue keyword can be emitted but right brace won't be emitted, and
debbuger can stop at continue keyword statement but won't stop at right
brace statement? I know left / right brace is just to group and right brace
statement should not be emitted debug location information. But the br
instruction is the same and only has different line number information in the
debug metadata, how the llvm distinguish this is meaningful statement and not
right brace?
At 2017-06-03 02:50:13, "Robinson, Paul" <paul.robinson at
sony.com> wrote:
The braces around the body of the 'for' statement indicate grouping, but
don't have any other semantic significance. If you look at the abstract
syntax tree (AST) constructed for this by any compiler, it is highly unlikely to
have an explicit representation of the braces, because the structure of the AST
already describes the grouping. On the other hand, a 'continue'
statement will be explicitly represented in the AST because it is a statement in
its own right.
If I modify your for-loop body to this:
{
i++;
#ifdef CONTINUE
continue;
#endif
}
and compile it both ways, then I see the same thing you do: The generated IR is
the same for both cases except for the source-location of the branch. This
makes sense to me as follows.
A 'for' loop has four parts: initialization, condition, increment, and
body. Clang emits the condition and increment parts in their own basic-blocks,
for convenience. This means it implicitly needs to add a branch at the end of
the "body" block to the "increment" block. However, if the
"body" block already ends with an explicit branch (of any kind), then
it can omit the implicit branch. The 'continue' statement will
obviously be generated as an explicit branch to the "increment" block.
The difference in debug-info is that the explicit branch is associated with the
'continue' statement, while the implicit branch is associated with the
'for' statement, and these statements have different source locations.
Even though the sequence of instructions is the same, the *reason* they were
emitted is different, and the debug info reflects that difference.
--paulr
From: llvm-dev [mailto:llvm-dev-bounces at lists.llvm.org] On Behalf Of Frozen
via llvm-dev
Sent: Friday, June 02, 2017 8:44 AM
To:llvm-dev at lists.llvm.org
Subject: [llvm-dev] How the LLVM handle the debug location information of
continue keyword and right brace(loop end location)?
Let me show you the following simple C code:
int main()
{
int i;
for (i = 0; i < 256; i++)
{
i++;
}
}
In this simple C code, if we use Clang to compile it and debug it: We will get
something like this:
(gdb) b main
Breakpoint 1 at 0x100000f7b: file a.c, line 5.
(gdb) r
Starting program: a.out
[New Thread 0x1403 of process 23435]
warning: unhandled dyld version (15)
Thread 2 hit Breakpoint 1, main () at a.c:5
5 for (i = 0; i < 256; i++)
(gdb) n
7 i++;
(gdb)
5 for (i = 0; i < 256; i++)
(gdb)
7 i++;
That is to say, the right brace of location LLVM doesn't emit. However if we
have the continue keyword:
int main()
{
int i;
for (i = 0; i < 256; i++)
{
continue;
i++;
}
}
Then we compile and debug it:
Thread 2 hit Breakpoint 1, main () at a.c:5
5 for (i = 0; i < 256; i++)
(gdb) n
7 continue;
(gdb)
5 for (i = 0; i < 256; i++)
(gdb)
7 continue;
(gdb)
5 for (i = 0; i < 256; i++)
(gdb)
7 continue;
We will stop the line of continue. But if we compare the LLVM IR between them:
The right brace and continue keyword both are the following the br instruction
and !dbg !23. Except that the line number of !dbg !23 not the same.
; :6: ; preds = %3 br label %7, !dbg !23
The question is how LLVM know whether to generate the debug location(generate
for the continue keyword line, but not for the loop's right brace)? Because
they are the same br instruction and others are the same.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20170603/e96ee50e/attachment.html>
Reasonably Related Threads
- How the LLVM handle the debug location information of continue keyword and right brace(loop end location)?
- How the LLVM handle the debug location information of continue keyword and right brace(loop end location)?
- RFC: Unify debug and optimized variable locations with llvm.dbg.addr [was: DW_OP_LLVM_memory]
- RFC: Introduce DW_OP_LLVM_memory to describe variables in memory with dbg.value
- RFC: Introduce DW_OP_LLVM_memory to describe variables in memory with dbg.value