https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97783
Bug ID: 97783 Summary: Optimizer assumes global static variable cannot be updated by external function, even though function is passed address of local functions Product: gcc Version: 10.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization Assignee: unassigned at gcc dot gnu.org Reporter: quentin at armitage dot org.uk Target Milestone: --- Created attachment 49541 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49541&action=edit The preprocessed glob_err.i file The attached code, when compiled with -O1 outputs: glob(/tmp/zz{a,b}.conf) returned 3, missing files 0 glob(/tmp/zz{a,b}.conf) returned 3, missing files 2 When compiled with -O0, the following is output: glob(/tmp/zz{a,b}.conf) returned 3, missing files 2 glob(/tmp/zz{a,b}.conf) returned 3, missing files 2 Since the two fprintf statements (lines 40 and 41) generating the two lines of output are consecutive statements, and no variables are altered in the calls to fprintf, the output when using -O1 is incorrect. It appears that when compiled with -O1 or higher levels of optimisation, if the missing_files variable is accessed prior to another function call following the return from the call to open_and_check_glob(), then the value used is 0; if there is any intermediate function call before missing_files is accessed, then the correct value is used. If missing_files is not declared static, or if it is declared static volatile, then the problem does not occur. The problem also does not occur if the statement (at line 36) missing_files = 0; is deleted. The versions of gcc given below are using the latest version I have installed. If have also tested this on Fedora 31 x86_64 with gcc version 9.3.1, Debian 10 (Buster) 32 bit armv7l, and Debian 10 aarch64 (both with gcc version 8.3.0), and all exhibit exactly the same behaviour. the exact version of GCC the system type the options given when GCC was configured/built ================================================ This is from a Fedora 33 x86_64 system: Output of gcc -v: Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/10/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-redhat-linux Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 10.2.1 20201016 (Red Hat 10.2.1-6) (GCC) the complete command line that triggers the bug =============================================== Ensure that /tmp/azz.conf and /tmp/bzz.conf do not exist: ./glob_err "/tmp/{a,b}zz.conf" the compiler output (error messages, warnings, etc.) ==================================================== None the preprocessed file (*.i*) that triggers the bug ================================================== Attached