Intel® C++ Compiler
Support and discussions for creating C++ code that runs on platforms based on Intel® processors.
Announcements
This community is designed for sharing of public information. Please do not share Intel or third-party confidential information here.
7678 Discussions

variables value not showing correctly when compiling with -O3 option

yingdong
Beginner
647 Views

Hi

I'm using intel icc to compile a test program, and I find the value of variables not showing correctly when I compile the code with -O3 option.

 

here is my test code.  //test.c

#include <stdio.h>

void f(int argc, char* argv[])
{
int i = 0;
for(;i<argc; i++)
printf("%s\n", argv[i]);
}

int main(int argc, char* argv[])
{
f(argc,argv);
return 0;
}

 

when I use -O0 option, the variables of argv show correctly as below.

[root@icelake test]# icc -g -O0 -o test test.c
[root@icelake test]# gdb-ia --args test 1 2 3 4
GNU gdb (GDB) 8.2.1
Copyright (C) 2018 Free Software Foundation, Inc.; (C) 2019 Intel Corp.
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.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.

For information about how to find Technical Support, Product Updates,
User Forums, FAQs, tips and tricks, and other support information, please visit:
<http://www.intel.com/software/products/support/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...done.
(gdb) b main
Breakpoint 1 at 0x4005c3: file test.c, line 12.
(gdb) r
Starting program: /root/test/test 1 2 3 4

Breakpoint 1, main (argc=5, argv=0x7fffffffd188) at test.c:12
12 f(argc,argv);
(gdb) p argv[0]
$1 = 0x7fffffffd4fd "/root/test/test"

 

 

but when I use -O3 option, the variables of argv show NOT correctly as below.

[root@icelake test]# icc -g -O3 -o test test.c
[root@icelake test]# gdb-ia --args test 1 2 3 4
GNU gdb (GDB) 8.2.1
Copyright (C) 2018 Free Software Foundation, Inc.; (C) 2019 Intel Corp.
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.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.

For information about how to find Technical Support, Product Updates,
User Forums, FAQs, tips and tricks, and other support information, please visit:
<http://www.intel.com/software/products/support/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...done.
(gdb) b main
Breakpoint 1 at 0x400b59: file test.c, line 12.
(gdb) r
Starting program: /root/test/test 1 2 3 4

Breakpoint 1, main (argc=5, argv=0x3) at test.c:12
12 f(argc,argv);
(gdb) p argv[0]
Cannot access memory at address 0x3

 

when compiling with -O3,  the program can still run OK except the variable not showing correctly.

my intel icc version is :

[root@icelake test]# icc --version
icc (ICC) 19.0.4.235 20190416
Copyright (C) 1985-2019 Intel Corporation. All rights reserved.

 

[root@icelake test]# whereis icc
icc: /opt/intel/system_studio_2019/compilers_and_libraries_2019.4.235/linux/bin/intel64/icc /opt/intel/system_studio_2019/compilers_and_libraries_2019.4.235/linux/bin/intel64/icc.cfg

0 Kudos
1 Solution
Viet_H_Intel
Moderator
557 Views

Yes, but the address at 0x3 is invalid.

Anyway, the compiler version you have is old. I tried the new one (icx) and didn't see the issue.

Can you try Smart, Connected Device Development with Intel® oneAPI IoT Toolkit?


Thanks,


View solution in original post

8 Replies
ShanmukhS_Intel
Moderator
614 Views

Hi,

 

Thanks for providing the sample reproducer and the required details.

We are able to reproduce the issue at our end. We are working on it, we will get back to u soon.

Meanwhile, please refer the below link for more details about compiler optimization flags.

https://software.intel.com/content/www/us/en/develop/documentation/cpp-compiler-developer-guide-and-...

 

Best Regards,

Shanmukh.SS


Viet_H_Intel
Moderator
581 Views

Seems like when you compiled with -O3, the compiler optimizes it away.


$ icc -g -O3 -o test test.c

$ gdb-ia --args test 1 2 3 4

GNU gdb (GDB) 8.2.1

(gdb) b main

Breakpoint 1 at 0x400ac9: file test.c, line 14.

(gdb) r

Starting program: /tmp/test 1 2 3 4


Breakpoint 1, main (argc=5, argv=0x3) at test.c:14

14    f(argc,argv);

(gdb) s

f (argc=<optimized out>, argv=<optimized out>) at test.c:6

6    int i = 0;

(gdb) n

main (argc=5, argv=0x3) at test.c:12

12   {

(gdb) s

14    f(argc,argv);

(gdb) s

f (argc=<optimized out>, argv=<optimized out>) at test.c:7

7    for(;i<argc; i++)

(gdb) p argv[0]

value has been optimized out

(gdb)



yingdong
Beginner
568 Views

Hi Viet, 

 

thanks for your help.  actually, I find it's a little strange that the argv of main function is not showing optimized,  only the argv of f function is.

 

(gdb) b main
Breakpoint 1 at 0x400b59: file test.c, line 12.
(gdb) r
Starting program: /root/test 1 2 3 4

Breakpoint 1, main (argc=5, argv=0x3) at test.c:12
12 f(argc,argv);
(gdb) s
f (argc=<optimized out>, argv=<optimized out>) at test.c:5
5 int i = 0;
(gdb) p argv[0]
value has been optimized out
(gdb) bt
#0 f (argc=<optimized out>, argv=<optimized out>) at test.c:5
#1 main (argc=5, argv=0x3) at test.c:12
(gdb) f 1
#1 main (argc=5, argv=0x3) at test.c:12
12 f(argc,argv);
(gdb) p argv[0]
Cannot access memory at address 0x3
(gdb)

Viet_H_Intel
Moderator
558 Views

Yes, but the address at 0x3 is invalid.

Anyway, the compiler version you have is old. I tried the new one (icx) and didn't see the issue.

Can you try Smart, Connected Device Development with Intel® oneAPI IoT Toolkit?


Thanks,


yingdong
Beginner
541 Views

ok, I switched the compiler to icx.  as you said, the argv variable of main function can be showed correctly now even I compile with -O3 option.

thank you.

 

(gdb) bt
#0 f (argc=<optimized out>, argv=<optimized out>) at test.c:6
#1 main (argc=5, argv=0x7fffffffd1f8) at test.c:12
(gdb) p argv[0]
value has been optimized out
(gdb) f 1
#1 main (argc=5, argv=0x7fffffffd1f8) at test.c:12
12 in test.c
(gdb) p argv[0]
$1 = 0x7fffffffd56b "/root/testicx"
(gdb)

Viet_H_Intel
Moderator
526 Views

Great,

Can we close this thread?

Thanks,


yingdong
Beginner
510 Views
Viet_H_Intel
Moderator
493 Views

This issue has been resolved and we will no longer respond to this thread. If you require additional assistance from Intel, please start a new thread. Any further interaction in this thread will be considered community only.

Thanks,


Reply