2017-01-25 18:46:41 +11:00
|
|
|
###############################################################################
|
|
|
|
if (__nc_optimisation)
|
|
|
|
return()
|
|
|
|
endif ()
|
|
|
|
|
|
|
|
set (__nc_optimisation TRUE)
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
2022-06-15 10:46:52 +10:00
|
|
|
include ("${CMAKE_CURRENT_LIST_DIR}/compile_flag.cmake")
|
|
|
|
include ("${CMAKE_CURRENT_LIST_DIR}/link_flag.cmake")
|
|
|
|
include ("${CMAKE_CURRENT_LIST_DIR}/canonical_host.cmake")
|
2017-01-17 19:20:12 +11:00
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
2017-01-25 18:46:23 +11:00
|
|
|
option(LTO "enable link-time optimisation" OFF)
|
2017-01-17 19:20:12 +11:00
|
|
|
|
|
|
|
|
|
|
|
##-----------------------------------------------------------------------------
|
2017-01-25 18:45:42 +11:00
|
|
|
## Enable link-time optimisation.
|
|
|
|
##
|
2017-05-12 15:57:30 +10:00
|
|
|
## The INTERPROCEDURAL_OPTIMISATION flag is silently ignored for almost all
|
|
|
|
## compilers. Fuck you too CMake. We have to make up for its deficiencies
|
|
|
|
## ourselves.
|
2017-01-25 18:46:23 +11:00
|
|
|
if (LTO)
|
2017-01-25 18:45:42 +11:00
|
|
|
# Add the linker flags first otherwise the linker may not recognise the
|
|
|
|
# object format
|
|
|
|
append_link_flag("-fuse-linker-plugin")
|
2022-08-23 13:36:01 +10:00
|
|
|
# HACK: we shouldn't use 'auto' as it will likely lead to oversubscription
|
|
|
|
# but I'm sick of holding GCC's hand today.
|
|
|
|
append_first_link_flag("-flto=auto" "-flto")
|
2017-01-25 18:45:42 +11:00
|
|
|
|
2019-01-16 14:32:07 +11:00
|
|
|
# Enable LTO on the compilation side, but try very hard to avoid
|
2017-01-25 18:45:42 +11:00
|
|
|
# situations where we may accidentally use regular/fat objects.
|
2022-08-23 13:36:01 +10:00
|
|
|
#
|
|
|
|
# GCC 12 will emit a warning about serial compilation of LTRANS jobs if an
|
|
|
|
# explicit parameter is not passed. It must be an integer, auto, or
|
|
|
|
# jobserver.
|
|
|
|
# Clang will only accept 'full', 'thin', or no argument.
|
|
|
|
#
|
|
|
|
# HACK: we shouldn't use 'auto' as it will likely lead to oversubscription
|
|
|
|
# but I'm sick of holding GCC's hand today.
|
|
|
|
append_first_compile_flag("-flto=auto" "-flto")
|
2017-01-17 19:20:12 +11:00
|
|
|
append_compile_flag("-fno-fat-lto-objects")
|
2020-10-19 08:46:42 +11:00
|
|
|
append_compile_flag("-flto-jobs=0")
|
2017-01-25 18:45:42 +11:00
|
|
|
|
2019-01-16 14:32:07 +11:00
|
|
|
# Try to squeeze out some more diagnostics via LTO
|
|
|
|
append_compile_flag("-flto-odr-type-merging")
|
|
|
|
|
|
|
|
# GCC: Attempt to use a more aggressive whole-program style of LTO
|
2020-10-21 07:08:20 +11:00
|
|
|
# NOTE: It's unclear if this _actually_ buys us performance or if it just
|
|
|
|
# slows down linking (by a substantial factor).
|
|
|
|
#append_compile_flag("-flto-partition=none")
|
2019-01-16 14:32:07 +11:00
|
|
|
|
2017-01-25 18:45:42 +11:00
|
|
|
# Throw in some optimisation flags that are LTO specific. We don't
|
|
|
|
# particularly care about checking Debug/Release here because LTO is
|
|
|
|
# pretty heavyweight anyway.
|
|
|
|
append_compile_flag("-fdevirtualize-at-ltrans")
|
2017-05-12 15:57:30 +10:00
|
|
|
|
|
|
|
# If we're using GCC we probably need to use gcc-{ar,nm,ranlib} so that
|
|
|
|
# plugin support works. Ideally we'd actually do some compilation tests,
|
|
|
|
# but... it's a royal PITA with CMake.
|
|
|
|
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
|
|
|
|
set (CMAKE_AR "gcc-ar")
|
|
|
|
set (CMAKE_NM "gcc-nm")
|
|
|
|
set (CMAKE_RANLIB "gcc-ranlib")
|
|
|
|
endif ()
|
2017-01-25 18:45:42 +11:00
|
|
|
endif ()
|
2017-01-17 19:20:12 +11:00
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
2017-01-25 18:46:23 +11:00
|
|
|
option(FRAMEPOINTER "retain the framepointer even if optimising" OFF)
|
2017-01-17 19:20:12 +11:00
|
|
|
|
|
|
|
|
|
|
|
##-----------------------------------------------------------------------------
|
2017-01-25 18:46:23 +11:00
|
|
|
if (FRAMEPOINTER)
|
|
|
|
append_compile_flag("-fno-omit-frame-pointer")
|
|
|
|
endif ()
|
2017-01-17 19:20:12 +11:00
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
canonical_host()
|
|
|
|
|
|
|
|
|
|
|
|
##-----------------------------------------------------------------------------
|
2017-01-18 21:43:30 +11:00
|
|
|
if (${host_cpu} STREQUAL "x86_64")
|
2018-09-13 14:53:06 +10:00
|
|
|
# Explicitly qualify the CPU as the minimum viable architecture, but tune
|
|
|
|
# for common platforms. This might prevent some invalid instructions being
|
|
|
|
# emitted.
|
|
|
|
append_compile_flag(-march=x86-64)
|
2017-01-17 19:20:12 +11:00
|
|
|
append_compile_flag(-mtune=generic)
|
2018-09-13 14:53:06 +10:00
|
|
|
|
|
|
|
append_compile_flag(-msse)
|
|
|
|
append_compile_flag(-msse2)
|
|
|
|
append_compile_flag(-mssse3)
|
2020-11-03 17:01:20 +11:00
|
|
|
append_compile_flag(-msse3)
|
|
|
|
append_compile_flag(-mavx)
|
2018-09-14 13:00:37 +10:00
|
|
|
|
2018-09-13 14:53:06 +10:00
|
|
|
append_compile_flag(-mno-avx2)
|
|
|
|
|
|
|
|
# These instructions should always be available on reasonable platforms.
|
|
|
|
# They're almost 20 years old at this point.
|
|
|
|
#append_compile_flag(-msse)
|
|
|
|
#append_compile_flag(-msse2)
|
|
|
|
# append_compile_flag(-mssse3)
|
|
|
|
# append_compile_flag(-msahf)
|
2017-01-18 21:43:30 +11:00
|
|
|
elseif (${host_cpu} STREQUAL "i686")
|
2017-01-17 19:20:12 +11:00
|
|
|
append_compile_flag(-march=prescott)
|
|
|
|
append_compile_flag(-mtune=generic)
|
|
|
|
append_compile_flag(-mcmov)
|
|
|
|
append_compile_flag(-mfpmath=sse)
|
|
|
|
else ()
|
2019-02-02 17:02:17 +11:00
|
|
|
message (WARNING "Unknown architecture. Not attempting performance options")
|
2017-01-17 19:20:12 +11:00
|
|
|
endif ()
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
2017-01-23 21:42:34 +11:00
|
|
|
if (CMAKE_BUILD_TYPE MATCHES Debug)
|
2017-01-17 19:20:12 +11:00
|
|
|
else()
|
2018-01-30 16:26:42 +11:00
|
|
|
# unsafe maths
|
|
|
|
# append_compile_flag(-funsafe-math-optimizations)
|
|
|
|
# append_compile_flag(-ffast-math)
|
|
|
|
# append_compile_flag(-fno-finite-math-only)
|
2017-01-17 19:20:12 +11:00
|
|
|
|
2018-01-30 16:26:42 +11:00
|
|
|
# vectorisation
|
|
|
|
append_compile_flag(-ftree-vectorize)
|
2017-01-17 19:20:12 +11:00
|
|
|
append_compile_flag(-fvectorize)
|
|
|
|
append_compile_flag(-fslp-vectorize)
|
|
|
|
|
|
|
|
# loop hosting/distribution
|
|
|
|
append_compile_flag(-floop-nest-optimize)
|
|
|
|
|
|
|
|
append_compile_flag(-ftree-loop-distribution)
|
|
|
|
append_compile_flag(-ftree-loop-distribute-patterns)
|
|
|
|
append_compile_flag(-ftree-loop-im)
|
|
|
|
append_compile_flag(-ftree-loop-if-convert-stores)
|
|
|
|
|
|
|
|
append_compile_flag(-fivopts)
|
|
|
|
|
|
|
|
append_compile_flag(-funsafe-loop-optimizations)
|
|
|
|
append_compile_flag(-floop-interchange)
|
|
|
|
|
|
|
|
append_compile_flag(-fpredictive-commoning)
|
|
|
|
append_compile_flag(-funswitch-loops)
|
|
|
|
|
2018-12-10 15:08:14 +11:00
|
|
|
# GCC 8.2 encounters an ICE in LTO linking with ipa-pta enabled
|
2019-02-22 12:41:41 +11:00
|
|
|
append_compile_flag (-fipa-pta)
|
2017-01-17 19:20:12 +11:00
|
|
|
|
|
|
|
# safety removal for performance
|
|
|
|
append_compile_flag(-fno-stack-protector)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
2017-01-23 21:42:34 +11:00
|
|
|
if (CMAKE_BUILD_TYPE MATCHES Debug)
|
2017-01-17 19:20:12 +11:00
|
|
|
else ()
|
|
|
|
append_compile_flag(-fdevirtualize)
|
|
|
|
append_compile_flag(-fdevirtualize-speculatively)
|
|
|
|
|
|
|
|
check_link_flag(TEST_GC_SECTIONS, "-Wl,--gc-sections")
|
|
|
|
if (TEST_LD_GC_SECTIONS)
|
|
|
|
append_compile_flag(-fdata-sections)
|
|
|
|
append_compile_flag(-ffunction-sections)
|
|
|
|
append_link_flag("-Wl,--gc-sections")
|
|
|
|
endif ()
|
|
|
|
endif ()
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
2022-08-18 13:13:02 +10:00
|
|
|
if (CMAKE_BUILD_TYPE MATCHES Debug)
|
|
|
|
set (_DEFAULT_RUNTIME_DEBUGGING ON)
|
|
|
|
else()
|
|
|
|
set (_DEFAULT_RUNTIME_DEBUGGING OFF)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
#message(FATAL_ERROR "RUNTIME DEBUGGING ${_DEFAULT_RUNTIME_DEBUGGING}")
|
|
|
|
option(
|
|
|
|
RUNTIME_DEBUGGING
|
|
|
|
"enable debugging features that impact runtime and ABI"
|
|
|
|
${_DEFAULT_RUNTIME_DEBUGGING}
|
|
|
|
)
|
|
|
|
|
|
|
|
if (RUNTIME_DEBUGGING)
|
|
|
|
if (CMAKE_BUILD_TYPE MATCHES Debug)
|
|
|
|
else()
|
|
|
|
message(FATAL_ERROR "RUNTIME_DEBUGGING is only supported under a DEBUG build")
|
|
|
|
endif()
|
|
|
|
endif ()
|
|
|
|
|
|
|
|
|
2017-01-23 21:42:34 +11:00
|
|
|
if (CMAKE_BUILD_TYPE MATCHES Debug)
|
2017-01-17 19:20:12 +11:00
|
|
|
add_definitions(-DENABLE_DEBUGGING)
|
2022-08-18 13:13:02 +10:00
|
|
|
|
|
|
|
if (RUNTIME_DEBUGGING)
|
2024-05-14 09:04:42 +10:00
|
|
|
if ("${STDLIB}" STREQUAL "libstdc++")
|
|
|
|
add_definitions(-D_GLIBCXX_DEBUG)
|
|
|
|
endif()
|
2022-08-18 13:13:02 +10:00
|
|
|
endif()
|
2017-01-17 19:20:12 +11:00
|
|
|
|
2018-01-24 17:33:15 +11:00
|
|
|
# Don't make any commits that use -O0 by default. Instead, either add an
|
|
|
|
# argument that allows one to choose, or temporarily switch the lines
|
|
|
|
# below locally. -O0 is just too slow for computationally heavy projects.
|
2018-09-05 13:17:50 +10:00
|
|
|
#append_compile_flag(-Og)
|
|
|
|
append_compile_flag(-O0)
|
2018-01-24 17:33:15 +11:00
|
|
|
|
2017-01-17 19:20:12 +11:00
|
|
|
append_compile_flag(-Werror)
|
2018-08-13 14:56:32 +10:00
|
|
|
|
|
|
|
# stack protection tends to cause segfaults in the generated binary
|
|
|
|
# occuring in the standard library under msys2 and related compilers.
|
|
|
|
#
|
|
|
|
# it's easily shown by simply creating a local std::string. rather than
|
|
|
|
# get to the root cause we just disable the protection under windows.
|
|
|
|
if (NOT WIN32)
|
|
|
|
append_compile_flag(-fstack-protector)
|
|
|
|
endif()
|
2017-01-17 19:20:12 +11:00
|
|
|
else ()
|
|
|
|
append_compile_flag(-O2)
|
|
|
|
append_compile_flag(-fno-rtti)
|
|
|
|
add_definitions(-DNO_RTTI)
|
|
|
|
add_definitions(-DNDEBUG)
|
|
|
|
endif ()
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
append_compile_flag(-g)
|
2018-01-18 11:54:11 +11:00
|
|
|
|
|
|
|
# gcc#: -ggdb tends to trigger an ICE in 'trunc_int_for_mode' under GCC with
|
|
|
|
# libcruft-util's coord code. It's not strictly necessary so disable it for
|
|
|
|
# now.
|
|
|
|
#append_compile_flag(-ggdb)
|