Merge branch 'release-4-6'
[alexxy/gromacs.git] / cmake / gmxGCC44O3BugWorkaround.cmake
1 # Due to a bug, gcc 4.4.x crashes when compiling bondfree.c with -O3 and
2 # -fopenmp, but strangely it does not crash with -O2 + all additional options.
3 # -O3 uses. Therefore, for the affected files, when compiling in release mode,
4 # we override -O3 with -O2 and add the additional option.
5 #
6
7 # Considering compiler version and build configuration, check if the workaround
8 # is needed to avoid gcc crash.
9 macro(gmx_check_gcc44_bug_workaround_needed OUT_VAR)
10     if(CMAKE_COMPILER_IS_GNUCC AND
11     C_COMPILER_VERSION VERSION_GREATER "4.3.999" AND C_COMPILER_VERSION VERSION_LESS "4.4.999")
12
13         set(_gcc44_workaround FALSE)
14
15         # only apply the workaround if we are actually using -O3
16         string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
17         if ("${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${_build_type}}" MATCHES ".*-O3.*" AND
18             GMX_OPENMP)
19             if(GMX_DISABLE_GCC44_BUG_WORKAROUND)
20                 set(_msg "gcc ${C_COMPILER_VERSION} detected, using -O3, but workaround for optimization bug is disabled")
21             else()
22                 set(_msg "gcc ${C_COMPILER_VERSION} detected, using -O3, will apply workaround for optimization bug (disable with GMX_DISABLE_GCC44_BUG_WORKAROUND)")
23                 set(_gcc44_workaround TRUE)
24             endif()
25             # only issues message if the value has changed
26             if((NOT _gcc44_workaround AND ${OUT_VAR}) OR (_gcc44_workaround AND NOT ${OUT_VAR}))
27                 message(STATUS "${_msg}")
28             endif()
29         endif()
30
31         set(${OUT_VAR} ${_gcc44_workaround} CACHE INTERNAL "Use gcc 4.4.x O3 optimization bug workaround" FORCE)
32     endif()
33 endmacro()
34
35 # Apply workaround on the specified source file.
36 #
37 # This workaround does not seem to affect the performance in a measurable way.
38 macro(gmx_apply_gcc44_bug_workaround FILE_NAME)
39     set_source_files_properties(
40         ${FILE_NAME}
41         PROPERTIES
42         COMPILE_FLAGS "-O2 -finline-functions -funswitch-loops -fpredictive-commoning -fgcse-after-reload -ftree-vectorize -fipa-cp-clone"
43         )
44 endmacro()