[llvm-dev] How the LLVM handle the debug location information of continue keyword and right brace(loop end location)?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[llvm-dev] How the LLVM handle the debug location information of continue keyword and right brace(loop end location)?

Hal Finkel via llvm-dev

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.



 


_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [llvm-dev] How the LLVM handle the debug location information of continue keyword and right brace(loop end location)?

Hal Finkel via llvm-dev

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:[hidden email]] On Behalf Of Frozen via llvm-dev
Sent: Friday, June 02, 2017 8:44 AM
To: [hidden email]
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.

 

 


_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [llvm-dev] How the LLVM handle the debug location information of continue keyword and right brace(loop end location)?

Hal Finkel via llvm-dev

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" <[hidden email]> 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:[hidden email]] On Behalf Of Frozen via llvm-dev
Sent: Friday, June 02, 2017 8:44 AM
To: [hidden email]
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.

 

 



 


_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [llvm-dev] How the LLVM handle the debug location information of continue keyword and right brace(loop end location)?

Hal Finkel via llvm-dev
On Jun 3, 2017 9:48 AM, "Frozen via llvm-dev" <[hidden email]> wrote:
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?

Simply because continue keyword is a part of the AST, while '}' is not. 

And you don't break on "natural" terminators. Also, would you expect a different behavior between:

for(i=0;i<N;++i)
    doStuff();

And:

for(i=0;i<N;++i) {
    doStuff();

While they should be identical on AST level? 

Cheers, 
Marcin 

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [llvm-dev] How the LLVM handle the debug location information of continue keyword and right brace(loop end location)?

Hal Finkel via llvm-dev

Hi Marcin:
    I don't expect stop the right brace } and expect stop at continue keyword statement. My question is continue keyword statement is the same as right brace } statement in the LLVM IR except the !dbg!23 has different line number. I don't know how the LLVM backend distinguish it. 


在 2017-06-03 19:20:46,"Marcin Słowik" <[hidden email]> 写道:
On Jun 3, 2017 9:48 AM, "Frozen via llvm-dev" <[hidden email]> wrote:
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?

Simply because continue keyword is a part of the AST, while '}' is not. 

And you don't break on "natural" terminators. Also, would you expect a different behavior between:

for(i=0;i<N;++i)
    doStuff();

And:

for(i=0;i<N;++i) {
    doStuff();

While they should be identical on AST level? 

Cheers, 
Marcin 


 


_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [llvm-dev] How the LLVM handle the debug location information of continue keyword and right brace(loop end location)?

Hal Finkel via llvm-dev
If without any brace, the br should correspond to doStuff() in your case. However, maybe I don't list my concern and question very clearly, it is my mistake and I apologize for it. 

Let me show you more details:

1.int main()
2.{
3. int i;
4.
5.  for (i = 0; i < 256; i++) {
6.  }
7.}

; <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

!23 = !DILocation(line: 6, column: 1, scope: !24)

You can see that this br instruction corresponds to right brace(i.e. line 6).

Let us see:

1.int main()
2.{
3.  int i;
4.
5.  for (i = 0; i < 256; i++)  {
6.   continue;
7.    }
8. }

; <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

!23 = !DILocation(line: 6, column: 1, scope: !24)
You can see that this br instruction corresponds to continue statement (i.e. line 6).

But, the result is: the first case line 6 right brace will not generated(it make sense and just group), but the latter case is line 6 continue and will generate location information for debugger. My question is they are the same but will be treated differently, I do not know how LLVM backend treat these two IR. Maybe I think these two cases are not related with Clang.



At 2017-06-03 22:45:11, "Marcin Słowik" <[hidden email]> wrote:
If i'm not mistaken, it is not a matter of LLVM IR nor LLVM backend, but rather LLVM frontend i.e. clang in this case.
Also, there is a slight semantic difference between those two cases:
`}` is not a statement. `continue` is.
In the first case, `br` does not correspond to the `}` itself, but rather to a natural BB termination.
In the latter, `br` explicitly corresponds to the `continue`.
If you still wonder what is the difference, look at the IR in the third case: without any braces. Where would you expect the debug information to be?

Cheers,
Marcin

2017-06-03 15:46 GMT+02:00 Frozen <[hidden email]>:

Hi Marcin:
    I don't expect stop the right brace } and expect stop at continue keyword statement. My question is continue keyword statement is the same as right brace } statement in the LLVM IR except the !dbg!23 has different line number. I don't know how the LLVM backend distinguish it. 


在 2017-06-03 19:20:46,"Marcin Słowik" <[hidden email]> 写道:
On Jun 3, 2017 9:48 AM, "Frozen via llvm-dev" <[hidden email]> wrote:
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?

Simply because continue keyword is a part of the AST, while '}' is not. 

And you don't break on "natural" terminators. Also, would you expect a different behavior between:

for(i=0;i<N;++i)
    doStuff();

And:

for(i=0;i<N;++i) {
    doStuff();

While they should be identical on AST level? 

Cheers, 
Marcin 


 




--
Marcin Słowik


 


_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [llvm-dev] How the LLVM handle the debug location information of continue keyword and right brace(loop end location)?

Hal Finkel via llvm-dev
In reply to this post by Hal Finkel via llvm-dev

If we had a very naïve way of generating IR, in the 'continue' case you would actually see TWO branch instructions: one to implement the 'continue' statement, and one as part of the control flow of the 'for' loop.  The branch for the 'continue' statement would have the source location of the 'continue' and the branch for the control-flow of the 'for' loop would have the source location of the right brace.

Clang is smart enough that in the 'continue' example, it knows not to emit the second branch (it would be unreachable).

--paulr

 

From: Frozen [mailto:[hidden email]]
Sent: Saturday, June 03, 2017 8:13 AM
To: Marcin Słowik
Cc: [hidden email]; Robinson, Paul
Subject: Re:Re: Re: [llvm-dev] How the LLVM handle the debug location information of continue keyword and right brace(loop end location)?

 

If without any brace, the br should correspond to doStuff() in your case. However, maybe I don't list my concern and question very clearly, it is my mistake and I apologize for it. 

 

Let me show you more details:

 

1.int main()

2.{

3. int i;

4.

5.  for (i = 0; i < 256; i++) {

6.  }

7.}

 

; <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

 

!23 = !DILocation(line: 6, column: 1, scope: !24)

 

You can see that this br instruction corresponds to right brace(i.e. line 6).

 

Let us see:

 

1.int main()

2.{

3.  int i;

4.

5.  for (i = 0; i < 256; i++)  {

6.   continue;

7.    }

8. }

 

; <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

 

!23 = !DILocation(line: 6, column: 1, scope: !24)

You can see that this br instruction corresponds to continue statement (i.e. line 6).

 

But, the result is: the first case line 6 right brace will not generated(it make sense and just group), but the latter case is line 6 continue and will generate location information for debugger. My question is they are the same but will be treated differently, I do not know how LLVM backend treat these two IR. Maybe I think these two cases are not related with Clang.

 

 


At 2017-06-03 22:45:11, "Marcin Słowik" <[hidden email]> wrote:

If i'm not mistaken, it is not a matter of LLVM IR nor LLVM backend, but rather LLVM frontend i.e. clang in this case.

Also, there is a slight semantic difference between those two cases:

`}` is not a statement. `continue` is.

In the first case, `br` does not correspond to the `}` itself, but rather to a natural BB termination.

In the latter, `br` explicitly corresponds to the `continue`.

If you still wonder what is the difference, look at the IR in the third case: without any braces. Where would you expect the debug information to be?

 

Cheers,

Marcin

 

2017-06-03 15:46 GMT+02:00 Frozen <[hidden email]>:

 

Hi Marcin:

    I don't expect stop the right brace } and expect stop at continue keyword statement. My question is continue keyword statement is the same as right brace } statement in the LLVM IR except the !dbg!23 has different line number. I don't know how the LLVM backend distinguish it. 

 


2017-06-03 19:20:46"Marcin Słowik" <[hidden email]> 写道:

On Jun 3, 2017 9:48 AM, "Frozen via llvm-dev" <[hidden email]> wrote:

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?

 

Simply because continue keyword is a part of the AST, while '}' is not. 

 

And you don't break on "natural" terminators. Also, would you expect a different behavior between:

 

for(i=0;i<N;++i)

    doStuff();

 

And:

 

for(i=0;i<N;++i) {

    doStuff();

 

While they should be identical on AST level? 

 

Cheers, 

Marcin 

 

 



 

--

Marcin Słowik

 

 


_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [llvm-dev] How the LLVM handle the debug location information of continue keyword and right brace(loop end location)?

Hal Finkel via llvm-dev
This whole thread is confusing me.

Clang's AST certainly knows where end braces are:

$ cat scope.cpp
int main() {
  for (;;) {
  }
}
$ clang++ scope.cpp -Xclang -ast-dump
...
`-FunctionDecl 0xb914618 <scope.cpp:1:1, line:4:1> line:1:5 main 'int (void)'
  `-CompoundStmt 0xb914768 <col:12, line:4:1>
    `-ForStmt 0xb914730 <line:2:3, line:3:3>
      |-<<<NULL>>>
      |-<<<NULL>>>
      |-<<<NULL>>>
      |-<<<NULL>>>
      `-CompoundStmt 0xb914718 <line:2:12, line:3:3>

But that seems orthogonal to both the question and the motivation for the question.

Both GCC and Clang use the '}' of a function to place some instructions (at O0 at least) - so a function like:

1: int f(bool b) {
2:   if (b)
3:     return 3;
4:   return 4;
5: }

Will step 1, 2, 3, 5 or 1, 2, 4, 5.

Loops could be done the same way, I suppose, but aren't & I'm not really too fussed about that. If there's some specific issue/problem/need here, happy to hear about/discuss it.

I did have a thread/review at some point, long ago, about some issues with the scope locations specifically related to exception cleanups, but it doesn't seem to be relevant here.

On Mon, Jun 5, 2017 at 10:32 AM Robinson, Paul via llvm-dev <[hidden email]> wrote:

If we had a very naïve way of generating IR, in the 'continue' case you would actually see TWO branch instructions: one to implement the 'continue' statement, and one as part of the control flow of the 'for' loop.  The branch for the 'continue' statement would have the source location of the 'continue' and the branch for the control-flow of the 'for' loop would have the source location of the right brace.

Clang is smart enough that in the 'continue' example, it knows not to emit the second branch (it would be unreachable).

--paulr

 

From: Frozen [mailto:[hidden email]]
Sent: Saturday, June 03, 2017 8:13 AM
To: Marcin Słowik
Cc: [hidden email]; Robinson, Paul
Subject: Re:Re: Re: [llvm-dev] How the LLVM handle the debug location information of continue keyword and right brace(loop end location)?

 

If without any brace, the br should correspond to doStuff() in your case. However, maybe I don't list my concern and question very clearly, it is my mistake and I apologize for it. 

 

Let me show you more details:

 

1.int main()

2.{

3. int i;

4.

5.  for (i = 0; i < 256; i++) {

6.  }

7.}

 

; <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

 

!23 = !DILocation(line: 6, column: 1, scope: !24)

 

You can see that this br instruction corresponds to right brace(i.e. line 6).

 

Let us see:

 

1.int main()

2.{

3.  int i;

4.

5.  for (i = 0; i < 256; i++)  {

6.   continue;

7.    }

8. }

 

; <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

 

!23 = !DILocation(line: 6, column: 1, scope: !24)

You can see that this br instruction corresponds to continue statement (i.e. line 6).

 

But, the result is: the first case line 6 right brace will not generated(it make sense and just group), but the latter case is line 6 continue and will generate location information for debugger. My question is they are the same but will be treated differently, I do not know how LLVM backend treat these two IR. Maybe I think these two cases are not related with Clang.

 

 


At 2017-06-03 22:45:11, "Marcin Słowik" <[hidden email]> wrote:

If i'm not mistaken, it is not a matter of LLVM IR nor LLVM backend, but rather LLVM frontend i.e. clang in this case.

Also, there is a slight semantic difference between those two cases:

`}` is not a statement. `continue` is.

In the first case, `br` does not correspond to the `}` itself, but rather to a natural BB termination.

In the latter, `br` explicitly corresponds to the `continue`.

If you still wonder what is the difference, look at the IR in the third case: without any braces. Where would you expect the debug information to be?

 

Cheers,

Marcin

 

2017-06-03 15:46 GMT+02:00 Frozen <[hidden email]>:

 

Hi Marcin:

    I don't expect stop the right brace } and expect stop at continue keyword statement. My question is continue keyword statement is the same as right brace } statement in the LLVM IR except the !dbg!23 has different line number. I don't know how the LLVM backend distinguish it. 

 


2017-06-03 19:20:46"Marcin Słowik" <[hidden email]> 写道:

On Jun 3, 2017 9:48 AM, "Frozen via llvm-dev" <[hidden email]> wrote:

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?

 

Simply because continue keyword is a part of the AST, while '}' is not. 

 

And you don't break on "natural" terminators. Also, would you expect a different behavior between:

 

for(i=0;i<N;++i)

    doStuff();

 

And:

 

for(i=0;i<N;++i) {

    doStuff();

 

While they should be identical on AST level? 

 

Cheers, 

Marcin 

 

 



 

--

Marcin Słowik

 

 

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [llvm-dev] How the LLVM handle the debug location information of continue keyword and right brace(loop end location)?

Hal Finkel via llvm-dev
All right, let us put away the LLVM IR, just look at issue C code:

The continue statement issue like this:

1int main()
2.{
3  int i;
4
5 for (i = 0; i < 256; i++) {
6      i++;
7      continue;
8  }
9}

When to use clang -g, we will not stop at continue stmt.
(gdb) b main
Breakpoint 1 at 0x100000f8b: file a.c, line 5.
(gdb) r
5  for (i = 0; i < 256; i++) {
(gdb) n
6      i++;
(gdb) n
5  for (i = 0; i < 256; i++) {
(gdb) n
6      i++;
(gdb) n
5  for (i = 0; i < 256; i++) {
(gdb) n
6      i++;
(gdb) n
There is no continue statement. However, we should stop at line 7 when we execute line 6.

But if we don't have line 6
1int main()
2.{
3  int i;
4
5 for (i = 0; i < 256; i++) {
6      continue;
7  }
8}

(gdb) b main
Breakpoint 1 at 0x100000f8b: file a.c, line 5.
(gdb) r
5  for (i = 0; i < 256; i++) {
(gdb) n
6      continue;
(gdb) n
5  for (i = 0; i < 256; i++) {
(gdb) b
6      continue;
(gdb) n
5  for (i = 0; i < 256; i++) {
We can stop at line 6 continue statement and don't stop line 7 right brace.

The right brace statement issue like this:
1.int main()
2.{
3.  int i;
4.
5.  for (i = 0; i < 256; i++) {
6.  }
7.}
When we use clang -g to compile it:

(gdb) b main
Breakpoint 1 at 0x100000f8b: file a.c, line 5.
(gdb) r
5  for (i = 0; i < 256; i++) {
(gdb) n
6  }
(gdb) n
5  for (i = 0; i < 256; i++) {
(gdb) n
6  }
(gdb) n
5  for (i = 0; i < 256; i++) {
(gdb) n
6  }

We will stop at line 6 right brace, but it should not stop.

If we add one statement:
1.int main()
2.{
3.  int i;
4.
5.  for (i = 0; i < 256; i++) {
6.     i++;
7.  }
8.}

(gdb) b main
Breakpoint 1 at 0x100000f7b: file a.c, line 5.
(gdb) r
5  for (i = 0; i < 256; i++) {
(gdb) n
6      i++;
(gdb) n
5  for (i = 0; i < 256; i++) {
(gdb) n
6      i++;
(gdb) n
5  for (i = 0; i < 256; i++) {
(gdb) n
6      i++;

Everything is ok! we will not stop at line 7 right brace.


At 2017-06-06 05:37:15, "David Blaikie" <[hidden email]> wrote:
This whole thread is confusing me.

Clang's AST certainly knows where end braces are:

$ cat scope.cpp
int main() {
  for (;;) {
  }
}
$ clang++ scope.cpp -Xclang -ast-dump
...
`-FunctionDecl 0xb914618 <scope.cpp:1:1, line:4:1> line:1:5 main 'int (void)'
  `-CompoundStmt 0xb914768 <col:12, line:4:1>
    `-ForStmt 0xb914730 <line:2:3, line:3:3>
      |-<<<NULL>>>
      |-<<<NULL>>>
      |-<<<NULL>>>
      |-<<<NULL>>>
      `-CompoundStmt 0xb914718 <line:2:12, line:3:3>

But that seems orthogonal to both the question and the motivation for the question.

Both GCC and Clang use the '}' of a function to place some instructions (at O0 at least) - so a function like:

1: int f(bool b) {
2:   if (b)
3:     return 3;
4:   return 4;
5: }

Will step 1, 2, 3, 5 or 1, 2, 4, 5.

Loops could be done the same way, I suppose, but aren't & I'm not really too fussed about that. If there's some specific issue/problem/need here, happy to hear about/discuss it.

I did have a thread/review at some point, long ago, about some issues with the scope locations specifically related to exception cleanups, but it doesn't seem to be relevant here.

On Mon, Jun 5, 2017 at 10:32 AM Robinson, Paul via llvm-dev <[hidden email]> wrote:

If we had a very naïve way of generating IR, in the 'continue' case you would actually see TWO branch instructions: one to implement the 'continue' statement, and one as part of the control flow of the 'for' loop.  The branch for the 'continue' statement would have the source location of the 'continue' and the branch for the control-flow of the 'for' loop would have the source location of the right brace.

Clang is smart enough that in the 'continue' example, it knows not to emit the second branch (it would be unreachable).

--paulr

 

From: Frozen [mailto:[hidden email]]
Sent: Saturday, June 03, 2017 8:13 AM
To: Marcin Słowik
Cc: [hidden email]; Robinson, Paul
Subject: Re:Re: Re: [llvm-dev] How the LLVM handle the debug location information of continue keyword and right brace(loop end location)?

 

If without any brace, the br should correspond to doStuff() in your case. However, maybe I don't list my concern and question very clearly, it is my mistake and I apologize for it. 

 

Let me show you more details:

 

1.int main()

2.{

3. int i;

4.

5.  for (i = 0; i < 256; i++) {

6.  }

7.}

 

; <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

 

!23 = !DILocation(line: 6, column: 1, scope: !24)

 

You can see that this br instruction corresponds to right brace(i.e. line 6).

 

Let us see:

 

1.int main()

2.{

3.  int i;

4.

5.  for (i = 0; i < 256; i++)  {

6.   continue;

7.    }

8. }

 

; <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

 

!23 = !DILocation(line: 6, column: 1, scope: !24)

You can see that this br instruction corresponds to continue statement (i.e. line 6).

 

But, the result is: the first case line 6 right brace will not generated(it make sense and just group), but the latter case is line 6 continue and will generate location information for debugger. My question is they are the same but will be treated differently, I do not know how LLVM backend treat these two IR. Maybe I think these two cases are not related with Clang.

 

 


At 2017-06-03 22:45:11, "Marcin Słowik" <[hidden email]> wrote:

If i'm not mistaken, it is not a matter of LLVM IR nor LLVM backend, but rather LLVM frontend i.e. clang in this case.

Also, there is a slight semantic difference between those two cases:

`}` is not a statement. `continue` is.

In the first case, `br` does not correspond to the `}` itself, but rather to a natural BB termination.

In the latter, `br` explicitly corresponds to the `continue`.

If you still wonder what is the difference, look at the IR in the third case: without any braces. Where would you expect the debug information to be?

 

Cheers,

Marcin

 

2017-06-03 15:46 GMT+02:00 Frozen <[hidden email]>:

 

Hi Marcin:

    I don't expect stop the right brace } and expect stop at continue keyword statement. My question is continue keyword statement is the same as right brace } statement in the LLVM IR except the !dbg!23 has different line number. I don't know how the LLVM backend distinguish it. 

 


2017-06-03 19:20:46"Marcin Słowik" <[hidden email]> 写道:

On Jun 3, 2017 9:48 AM, "Frozen via llvm-dev" <[hidden email]> wrote:

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?

 

Simply because continue keyword is a part of the AST, while '}' is not. 

 

And you don't break on "natural" terminators. Also, would you expect a different behavior between:

 

for(i=0;i<N;++i)

    doStuff();

 

And:

 

for(i=0;i<N;++i) {

    doStuff();

 

While they should be identical on AST level? 

 

Cheers, 

Marcin 

 

 



 

--

Marcin Słowik

 

 

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev


 


_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [llvm-dev] How the LLVM handle the debug location information of continue keyword and right brace(loop end location)?

Hal Finkel via llvm-dev
In reply to this post by Hal Finkel via llvm-dev
Dear Frozen,

Please show me the patch kindly :)


diff --git a/lib/CodeGen/SelectionDAG/FastISel.cpp
b/lib/CodeGen/SelectionDAG/FastISel.cpp
index b2599b2e17f..ec96bbffd0b 100644
--- a/lib/CodeGen/SelectionDAG/FastISel.cpp
+++ b/lib/CodeGen/SelectionDAG/FastISel.cpp
@@ -1463,6 +1463,8 @@ void FastISel::fastEmitBranch(MachineBasicBlock
*MSucc,
                                const DebugLoc &DbgLoc) {
    if (FuncInfo.MBB->getBasicBlock()->size() > 1 &&
        FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
+    // TODO: Dear Frozen, please show me the patch kindly :)
+    // http://lists.llvm.org/pipermail/llvm-dev/2017-June/113631.html
      // For more accurate line information if this is the only instruction
      // in the block then emit it, otherwise we have the unconditional
      // fall-through case, which needs no instructions.


Thanks a lot!

--
Regards,
Leslie Zhai https://reviews.llvm.org/p/xiangzhai/


.
_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Loading...