public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug debug/104549] New: Missing variable at O2/O3 likely caused by -fearly-inlining
@ 2022-02-15 15:46 assaiante at diag dot uniroma1.it
  2022-02-15 17:30 ` [Bug debug/104549] " jakub at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: assaiante at diag dot uniroma1.it @ 2022-02-15 15:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104549

            Bug ID: 104549
           Summary: Missing variable at O2/O3 likely caused by
                    -fearly-inlining
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: debug
          Assignee: unassigned at gcc dot gnu.org
          Reporter: assaiante at diag dot uniroma1.it
  Target Milestone: ---

In the following testing code, local variable i, which gets passed as argument
to a function defined in an external module, is not visible upon the call when
debugging. This behavior only occurs when the optimization level is O2 or O3.


$ cat a.c
void a()
{
    int i = 0;
    for (; i ; ) {}
    test(i)
     ;
}
int main()
{
    a();
}

$ cat lib.c
#include <stdio.h>

void nop() {
    printf("\n");
}

void test(int i) {
    printf("%d", i);
}


GCC and GDB version (GCC commit id: 500d3f0a302):
$ gcc --version
gcc (GCC) 12.0.0 20211227 (experimental)
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gdb --version
GNU gdb (GDB) 11.2
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.


GDB trace:
$ gcc -O2 -g a.c lib.c -o opt # <- same exact with O3
$ gdb -q opt
Reading symbols from opt...
(gdb) b 5
Breakpoint 1 at 0x400460: /home/stepping/66/reduce/a.c:5. (2 locations)
(gdb) r
Starting program: /home/stepping/66/reduce/opt 

Breakpoint 1, main () at a.c:5
5           test(i)
(gdb) info locals
No locals.


Through some testing we found out that the optimization flag that introduces
the bug is likely -fearly-inlining, which makes main() call the test() function
directly. Indeed, if we add the flag -fno-early-inlining to the compilation
command line used above, the variable i will appear in the current frame with
its value correctly available.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug debug/104549] Missing variable at O2/O3 likely caused by -fearly-inlining
  2022-02-15 15:46 [Bug debug/104549] New: Missing variable at O2/O3 likely caused by -fearly-inlining assaiante at diag dot uniroma1.it
@ 2022-02-15 17:30 ` jakub at gcc dot gnu.org
  2022-02-16 13:01 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-02-15 17:30 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104549

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
I don't understand what problem you complain about.
The i variable is certainly in the debug info:
        .uleb128 0xc    # (DIE (0xa4) DW_TAG_variable)
        .ascii "i\0"    # DW_AT_name
        .byte   0x1     # DW_AT_decl_file (pr104549.c)
        .byte   0x3     # DW_AT_decl_line
        .byte   0x9     # DW_AT_decl_column
        .long   0x3c    # DW_AT_type
and has the correct value:
        .uleb128 0x2    # (DIE (0x7e) DW_TAG_variable)
        .long   0xa4    # DW_AT_abstract_origin
                        # DW_AT_const_value (0)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug debug/104549] Missing variable at O2/O3 likely caused by -fearly-inlining
  2022-02-15 15:46 [Bug debug/104549] New: Missing variable at O2/O3 likely caused by -fearly-inlining assaiante at diag dot uniroma1.it
  2022-02-15 17:30 ` [Bug debug/104549] " jakub at gcc dot gnu.org
@ 2022-02-16 13:01 ` jakub at gcc dot gnu.org
  2022-02-17 12:38 ` assaiante at diag dot uniroma1.it
  2022-02-17 13:48 ` assaiante at diag dot uniroma1.it
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-02-16 13:01 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104549

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |aoliva at gcc dot gnu.org

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
The assembly of main is:
main:
.LFB1:
        # pr104549.c:9:1
        .loc 1 9 1
        .cfi_startproc
# BLOCK 2, count:1073741824 (estimated locally) seq:0
# PRED: ENTRY [always]  count:1073741824 (estimated locally) (FALLTHRU)
        # pr104549.c:10:5
        .loc 1 10 5
.LBB4:
.LBB5:
        # pr104549.c:3:5
        .loc 1 3 5
.LVL2:
        # DEBUG i => 0
        # pr104549.c:4:5
        .loc 1 4 5
        # pr104549.c:4:19
        .loc 1 4 19
        # pr104549.c:4:12
        .loc 1 4 12
        # pr104549.c:5:5
        .loc 1 5 5
.LBE5:
.LBE4:
        # pr104549.c:9:1
        .loc 1 9 1 is_stmt 0
        subq    $8, %rsp
        .cfi_def_cfa_offset 16
.LBB7:
.LBB6:
        # pr104549.c:5:5
        .loc 1 5 5
        xorl    %edi, %edi
        xorl    %eax, %eax
        call    test
.LVL3:
.LBE6:
.LBE7:
        # pr104549.c:11:1
        .loc 1 11 1
        xorl    %eax, %eax
        addq    $8, %rsp
        .cfi_def_cfa_offset 8
# SUCC: EXIT [always]  count:1073741824 (estimated locally)
        ret
with -O2 -g -dA, the inlined a is in the .LBB4...LBE4 and .LBB7...LBE7 ranges
but the first range is empty (no insns in it), the
very first insn in main doesn't belong to the inline function and .loc for it
is line 9, perhaps the debugger chooses some other one because of the is_stmt
0?
With -gno-statement-frontiers the debugger reports main's { as the first
location.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug debug/104549] Missing variable at O2/O3 likely caused by -fearly-inlining
  2022-02-15 15:46 [Bug debug/104549] New: Missing variable at O2/O3 likely caused by -fearly-inlining assaiante at diag dot uniroma1.it
  2022-02-15 17:30 ` [Bug debug/104549] " jakub at gcc dot gnu.org
  2022-02-16 13:01 ` jakub at gcc dot gnu.org
@ 2022-02-17 12:38 ` assaiante at diag dot uniroma1.it
  2022-02-17 13:48 ` assaiante at diag dot uniroma1.it
  3 siblings, 0 replies; 5+ messages in thread
From: assaiante at diag dot uniroma1.it @ 2022-02-17 12:38 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104549

--- Comment #3 from Cristian Assaiante <assaiante at diag dot uniroma1.it> ---
I have tested multiple older gcc versions (4.3, 7.5, 9.4, 11.2) and apparently
the variable is visible in 4.3 and 7.5. 
The asm for the main function of the binary from gcc7.5:
main:
.LFB1:
        # a.c:9
        .loc 1 9 0
        .cfi_startproc
# BLOCK 2 freq:10000 seq:0
# PRED: ENTRY [100.0%]  (FALLTHRU)
.LVL2:
        subq    $8, %rsp
        .cfi_def_cfa_offset 16
.LBB4:
.LBB5:
        # a.c:5
        .loc 1 5 0
        xorl    %edi, %edi
        xorl    %eax, %eax
        call    test@PLT
.LVL3:
.LBE5:
.LBE4:
        # a.c:11
        .loc 1 11 0
        xorl    %eax, %eax
        addq    $8, %rsp
        .cfi_def_cfa_offset 8
# SUCC: EXIT [100.0%] 
        ret

Differently from more recent versions, the inlined a is in the LBB5...LBE5
range only. So there is no empty range associated to it and the debug info in
correctly visualized in gdb.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug debug/104549] Missing variable at O2/O3 likely caused by -fearly-inlining
  2022-02-15 15:46 [Bug debug/104549] New: Missing variable at O2/O3 likely caused by -fearly-inlining assaiante at diag dot uniroma1.it
                   ` (2 preceding siblings ...)
  2022-02-17 12:38 ` assaiante at diag dot uniroma1.it
@ 2022-02-17 13:48 ` assaiante at diag dot uniroma1.it
  3 siblings, 0 replies; 5+ messages in thread
From: assaiante at diag dot uniroma1.it @ 2022-02-17 13:48 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104549

--- Comment #4 from Cristian Assaiante <assaiante at diag dot uniroma1.it> ---
We have tested also multiple versions of gdb. 
With versions 9.2 and 11.2 we have the same behavior: variable i is visible for
gcc4 and gcc7 and not visible for more recent gcc versions.
With version 10.2 we have that variable i is marked as optimized out for gcc4
and gcc7 and not visible for more recent gcc versions.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-02-17 13:48 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-15 15:46 [Bug debug/104549] New: Missing variable at O2/O3 likely caused by -fearly-inlining assaiante at diag dot uniroma1.it
2022-02-15 17:30 ` [Bug debug/104549] " jakub at gcc dot gnu.org
2022-02-16 13:01 ` jakub at gcc dot gnu.org
2022-02-17 12:38 ` assaiante at diag dot uniroma1.it
2022-02-17 13:48 ` assaiante at diag dot uniroma1.it

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).