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

            Bug ID: 116767
           Summary: C const function pointer no code generated (or
                    optimized out)
           Product: gcc
           Version: 14.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: raffaellobertini at gmail dot com
  Target Milestone: ---

Created attachment 59139
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=59139&action=edit
sample C program

- gcc version: 14.2.1
- compiler flags: -Wall -Wextra
- OS: Fedora 40
- gcc has been installed from dnf default Fedora 40 repositories
- gcc output:
==============================================================================
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/14/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
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,m2,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 --enable-libstdcxx-backtrace
--with-libstdcxx-zoneinfo=/usr/share/zoneinfo --with-linker-hash-style=gnu
--enable-plugin --enable-initfini-array
--with-isl=/builddir/build/BUILD/gcc-14.2.1-20240801/obj-x86_64-redhat-linux/isl-install
--enable-offload-targets=nvptx-none,amdgcn-amdhsa --enable-offload-defaulted
--without-cuda-driver --enable-gnu-indirect-function --enable-cet
--with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
--with-build-config=bootstrap-lto --enable-link-serialization=1
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 14.2.1 20240801 (Red Hat 14.2.1-1) (GCC) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-mtune=generic'
'-march=x86-64' '-dumpdir' 'a-'
 /usr/libexec/gcc/x86_64-redhat-linux/14/cc1 -E -quiet -v audio_test.c
-mtune=generic -march=x86-64 -Wall -Wextra -fpch-preprocess -o a-audio_test.i
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-redhat-linux/14/include-fixed"
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-redhat-linux/14/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-redhat-linux/14/include
 /usr/local/include
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-mtune=generic'
'-march=x86-64' '-dumpdir' 'a-'
 /usr/libexec/gcc/x86_64-redhat-linux/14/cc1 -fpreprocessed a-audio_test.i
-quiet -dumpdir a- -dumpbase audio_test.c -dumpbase-ext .c -mtune=generic
-march=x86-64 -Wall -Wextra -version -o a-audio_test.s
GNU C17 (GCC) version 14.2.1 20240801 (Red Hat 14.2.1-1) (x86_64-redhat-linux)
        compiled by GNU C version 14.2.1 20240801 (Red Hat 14.2.1-1), GMP
version 6.2.1, MPFR version 4.2.1, MPC version 1.3.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 7983ab47815232989bed61515b77d1c7
funct_ptr_sample.c: In function ‘main’:
funct_ptr_sample.c:25:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
   25 | int main(int argc, char* argv[])
      |          ~~~~^~~~
funct_ptr_sample.c:25:26: warning: unused parameter ‘argv’ [-Wunused-parameter]
   25 | int main(int argc, char* argv[])
      |                    ~~~~~~^~~~~~
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-mtune=generic'
'-march=x86-64' '-dumpdir' 'a-'
 as -v --64 -o a-audio_test.o a-audio_test.s
GNU assembler version 2.41 (x86_64-redhat-linux) using BFD version version
2.41-37.fc40
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/14/:/usr/libexec/gcc/x86_64-redhat-linux/14/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/14/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/14/:/usr/lib/gcc/x86_64-redhat-linux/14/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/14/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-mtune=generic'
'-march=x86-64' '-dumpdir' 'a.'
 /usr/libexec/gcc/x86_64-redhat-linux/14/collect2 -plugin
/usr/libexec/gcc/x86_64-redhat-linux/14/liblto_plugin.so
-plugin-opt=/usr/libexec/gcc/x86_64-redhat-linux/14/lto-wrapper
-plugin-opt=-fresolution=a.res -plugin-opt=-pass-through=-lgcc
-plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc
-plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --build-id
--no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker
/lib64/ld-linux-x86-64.so.2
/usr/lib/gcc/x86_64-redhat-linux/14/../../../../lib64/crt1.o
/usr/lib/gcc/x86_64-redhat-linux/14/../../../../lib64/crti.o
/usr/lib/gcc/x86_64-redhat-linux/14/crtbegin.o
-L/usr/lib/gcc/x86_64-redhat-linux/14
-L/usr/lib/gcc/x86_64-redhat-linux/14/../../../../lib64 -L/lib/../lib64
-L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/14/../../..
a-audio_test.o -lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc
--push-state --as-needed -lgcc_s --pop-state
/usr/lib/gcc/x86_64-redhat-linux/14/crtend.o
/usr/lib/gcc/x86_64-redhat-linux/14/../../../../lib64/crtn.o
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-mtune=generic'
'-march=x86-64' '-dumpdir' 'a.'

==============================================================================
- 




Given this sample C program: (see attachment, containing the preprocessed files
too)


the generated code has optimized out the call to the function pointer.
in x86 asm terms the block of the `call` instruction is not generated at all.


if removing the const keyword at line 7:
const MyFunc* g_f = my_func; -----> MyFunc* g_f = my_func;


the resulting generated code is what is expected (calling the function
pointer).


In my perspective in this case rather than not generating the code that calls
the function pointer, gcc should discard the const keyword and generate a
warning telling to the user that that keyword is ignored as I don't see any
reason why someone would like to define a const function pointer when there is
no code generated for those statements.
As by mistake a const keyword could be put there and a warning telling
something can be catched up, the not generated asm block might be more
challenging to be found as a programming error.

Reply via email to