Description
Description
README.md instructs users to control compiler flags in CMake builds like this (e.g. to get "-O3"):
export FFLAGS="-O3"
cmake -B build -G Ninja -DCMAKE_MAXIMUM_RANK:String=7 -DCMAKE_INSTALL_PREFIX=$HOME/.local
However this does not have the intended effect; by default there are other options that override this optimization.
To see that this is the case, we can insert the line set( CMAKE_VERBOSE_MAKEFILE on )
near the top of CMakeLists.txt
, and build with the above commands. Then the compiler commands are printed out. Copying one (for illustration) we have:
/usr/bin/gfortran -I/home/gareth/Code_Experiments/fortran/stdlib/my_branch/stdlib/build/src/mod_files -O3 -O2 -g -DNDEBUG -Jmod_files/ -fimplicit-none -ffree-line-length-132 -Wall -Wextra -Wimplicit-procedure -pedantic-errors -std=f2018 -c /home/gareth/Code_Experiments/fortran/stdlib/my_branch/stdlib/build/src/stdlib_bitsets_64.f90 -o CMakeFiles/fortran_stdlib.dir/stdlib_bitsets_64.f90.o
Notice how right after the "-O3"
, we have "-O2 -g -DNDEBUG"
. This is overriding the "-O3"
optimization which was intended. Our documentation doesn't make this clear, and this has confused some efforts to try optimising code.
Expected Behaviour
I expected the code to have been compiled with -O3
, not -O2
.
The reason I noticed this is that my code was slower when linking to Cmake builds, vs Makefile builds (and hence I was getting weird performance results).
Version of stdlib
Platform and Architecture
All
Additional Information
It seems that by default, stdlib
is having CMake add the flags for a CMAKE_BUILD_TYPE=RelWithDebInfo
build.
One way to work-around this issue is to have the user make up some ad-hoc BUILD_TYPE (so that the defaults are not invoked). However presumably there are some options that we really do want by default -- so the questions are:
- What should we suggest in README.md?
- Should any of the CMake defaults be changed?
I think it's a good idea to use CMAKE_VERBOSE_MAKEFILE=On (or suggest that option to users in the documentation). This prints out the compiler commands (so users will likely notice if they differ from what was intended).
export FFLAGS="-O3 -fno-range-check"
cmake -B build -DCMAKE_MAXIMUM_RANK=7 -DCMAKE_INSTALL_PREFIX=/home/gareth/Code_Experiments/fortran/stdlib/my_branch/stdlib/local_install_gfortran/ -DCMAKE_BUILD_TYPE=MyCustomBuild -DCMAKE_VERBOSE_MAKEFILE=On