Search code examples
c++gcccompiler-optimizationicc

How it's possible a binary is not changed when increasing optimization flag?


I've compiled the same program for different optimization flags: -O0, -O1, -O2, and -O3. I've used both gcc and icc. Below you can see a fragment of the make file:

build-gcc-O3: CXX = g++
build-gcc-O3: BIN_POST_NAME = -gcc-O3
build-gcc-O3: OPT_FLAGS = -O3
build-gcc-O3: fluidsGL

build-icc-O0: CXX = $(INTEL_ICPC)
build-icc-O0: BIN_POST_NAME = -icc-O0
build-icc-O0: OPT_FLAGS = -O0
build-icc-O0: fluidsGL

fluidsGL: fluidsGL.o fluidsGL_cpu.o bilinear_interpolation.o defines.o
   $(CXX) $(CXXFLAGS) $(BINARY_DIR)/defines.o $(BINARY_DIR)/bilinear_interpolation.o $(BINARY_DIR)/fluidsGL_cpu.o $(BINARY_DIR)/fluidsGL.o -o $(BINARY_DIR)/$@$(BIN_POST_NAME)$(DBG_NAME_APPEND) $(OPT_FLAGS) -lGL -lGLU -lGLEW -lglut -lfftw3f

After running make, the output looks fine. For example, this is the make resultant command for icc with -O3:

"/opt/intel/compilers_and_libraries/linux/bin/intel64/icpc" -Wall bin/defines.o bin/bilinear_interpolation.o bin/fluidsGL_cpu.o bin/fluidsGL.o -o bin/fluidsGL-icc-O3 -O3 -lGL -lGLU -lGLEW -lglut -lfftw3f

After compiling, fluidsGL-gcc-O0 and fluidsGL-gcc-O3 (and fluidsGL-icc-O0 vs fluidsGL-icc-O3) binaries need the same disk space, this already seems strange for me:

$ ls -la bin/ 
total 728
drwxrwxr-x 3 jesus jesus  4096 ene 18 09:56 .
drwxrwxr-x 7 jesus jesus  4096 ene 18 09:31 ..
-rw-rw-r-- 1 jesus jesus  4200 ene 18 09:56 bilinear_interpolation.o
-rw-rw-r-- 1 jesus jesus  5608 ene 18 09:56 defines.o
-rw-rw-r-- 1 jesus jesus 14952 ene 18 09:56 fluidsGL_cpu.o
-rwxrwxr-x 1 jesus jesus 30780 ene 18 09:56 fluidsGL-gcc-O0
-rwxrwxr-x 1 jesus jesus 43940 ene 18 09:56 fluidsGL-gcc-O0-dbg
-rwxrwxr-x 1 jesus jesus 44272 ene 18 09:56 fluidsGL-gcc-O0-dbg-gprof
-rwxrwxr-x 1 jesus jesus 30780 ene 18 09:56 fluidsGL-gcc-O1
-rwxrwxr-x 1 jesus jesus 30780 ene 18 09:56 fluidsGL-gcc-O2
-rwxrwxr-x 1 jesus jesus 30780 ene 18 09:56 fluidsGL-gcc-O3
-rwxrwxr-x 1 jesus jesus 71151 ene 18 09:56 fluidsGL-icc-O0
-rwxrwxr-x 1 jesus jesus 71151 ene 18 09:56 fluidsGL-icc-O1
-rwxrwxr-x 1 jesus jesus 71151 ene 18 09:56 fluidsGL-icc-O2
-rwxrwxr-x 1 jesus jesus 71151 ene 18 09:56 fluidsGL-icc-O3
-rw-rw-r-- 1 jesus jesus 34664 ene 18 09:56 fluidsGL.o

But even after using diff to compare the files no output is displayed, meaning the binaries are the same exact binaries:

$ diff bin/fluidsGL-gcc-O0 bin/fluidsGL-gcc-O3
$ diff bin/fluidsGL-icc-O0 bin/fluidsGL-icc-O3
$

Is this a normal behaviour of the compiler or am I making a building mistake?


Solution

  • The -Ox flags (where x is the optimization level) are for compiler optimizations, but you're not actually re-compiling. You're just re-linking the already compiled object (.o) files.

    Make sure to add the -Ox flag to the compilation commands for the object files, and do a full clean rebuild.