[Bug debug/104549] New: Missing variable at O2/O3 likely caused by -fearly-inlining

assaiante at diag dot uniroma1.it gcc-bugzilla@gcc.gnu.org
Tue Feb 15 15:46:39 GMT 2022


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.


More information about the Gcc-bugs mailing list