Skip to content

Commit

Permalink
build: Add local build option on Linux; compiler fixups (#1756)
Browse files Browse the repository at this point in the history
> [!IMPORTANT]
> This alters default behavior for configuration on Linux that
maintainers should be aware of.

Introduces `ARES_BUILD_LOCAL` on Linux, enabled by default, that will
pass `-march=native` if enabled. Maintainers will probably want to
disable this. Also adds `ARES_ENABLE_MINIMUM_CPU`, `ON` by default, that
compiles with the previously enforced `x86-64-v2` minimum on x86_64.
Maintainers should do whatever they want with this option.

(@bsdcode @Mastergatto)

These changes are intended to give Linux users building from source a
better experience. Enabling x86-64-v2 compared to compiling without any
SSE functionality is a 10%+ performance loss locally in the N64 core,
and may be higher on actual x86_64 machines.

> [!NOTE]
> It is worth noting that even though we do not explicitly define a
minimum SSE functionality on arm64, in practice nall will still [define
`__SSE4_1__`](https://github.com/ares-emulator/ares/blob/e808d2c5ab5be91d3e1035063892e8c5115d25e2/nall/intrinsics.hpp#L232)
and take advantage of SIMD on that platform via sse2neon.

### Other changes

* Adds `-march=native` on macOS builds under the control of
`ARES_BUILD_LOCAL`, and fixes `ARES_BUILD_LOCAL` on Windows so that
`-march=native` is added under gcc and not just clang.

* Removes warning in the N64 core related to the issue caused by strict
aliasing under GCC.

* Fixed an issue where macOS would always generate for arm64 rather than
the host architecture.

* Ran a formatter pass that was previously missed on the CMake code, so
there are a couple miscellaneous formatting changes.
  • Loading branch information
jcm93 authored Jan 11, 2025
1 parent 05898a6 commit b4b026d
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 44 deletions.
7 changes: 0 additions & 7 deletions ares/n64/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ elseif(OS_LINUX OR OS_FREEBSD OR OS_OPENBSD)
include(cmake/os-linux.cmake)
endif()

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
message(WARNING "
Building N64 with optimizations under GCC currently results in broken graphical output, see \
https://github.com/ares-emulator/ares/issues/1737 . Building with clang instead will work around this issue. \
")
endif()

ares_add_sources(
CORE #
n64
Expand Down
6 changes: 1 addition & 5 deletions cmake/common/compiler_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,7 @@ set(
-Wno-delete-non-abstract-non-virtual-dtor
)

set(
_ares_gcc_common_options
-fwrapv
-fno-strict-aliasing
)
set(_ares_gcc_common_options -fwrapv -fno-strict-aliasing -Wno-unused-result)

if(NOT DEFINED CMAKE_COMPILE_WARNING_AS_ERROR)
set(CMAKE_COMPILE_WARNING_AS_ERROR OFF)
Expand Down
26 changes: 23 additions & 3 deletions cmake/linux/compilerconfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ include_guard(GLOBAL)
option(ENABLE_COMPILER_TRACE "Enable clang time-trace" OFF)
mark_as_advanced(ENABLE_COMPILER_TRACE)

option(
ARES_BUILD_LOCAL
"Allows the compiler to generate code optimized for the target machine; increases performance."
ON
)
option(
ARES_ENABLE_MINIMUM_CPU
"Defines a project minimum instruction set version in order to expose advanced SSE functionality to the compiler. Currently x86-64-v2 on x86_64; not defined on arm64."
ON
)

include(ccache)
include(compiler_common)

Expand All @@ -14,7 +25,16 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
-fwrapv
)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options(
${_ares_gcc_common_options}
)
add_compile_options(${_ares_gcc_common_options})
endif()

if(ARES_BUILD_LOCAL)
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-march=native>)
else()
if(ARES_ENABLE_MINIMUM_CPU)
string(TOLOWER ${CMAKE_SYSTEM_PROCESSOR} LOWERCASE_CMAKE_SYSTEM_PROCESSOR)
if(LOWERCASE_CMAKE_SYSTEM_PROCESSOR MATCHES "(i[3-6]86|x86|x64|x86_64|amd64|e2k)")
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-march=x86-64-v2>)
endif()
endif()
endif()
18 changes: 17 additions & 1 deletion cmake/macos/compilerconfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ include_guard(GLOBAL)
option(ENABLE_COMPILER_TRACE "Enable clang time-trace" OFF)
mark_as_advanced(ENABLE_COMPILER_TRACE)

option(
ARES_BUILD_LOCAL
"Allows the compiler to generate code optimized for the target machine; increases performance. Also disables certain entitlements and runtime options that interfere with debugging but are required for notarization and distribution."
ON
)

include(ccache)
include(compiler_common)

# Enable selection between arm64 and x86_64 targets
if(NOT CMAKE_OSX_ARCHITECTURES)
set(CMAKE_OSX_ARCHITECTURES arm64 CACHE STRING "Build architectures for macOS" FORCE)
set(CMAKE_OSX_ARCHITECTURES ${CMAKE_SYSTEM_PROCESSOR} CACHE STRING "Build architectures for macOS" FORCE)
endif()
set_property(CACHE CMAKE_OSX_ARCHITECTURES PROPERTY STRINGS arm64 x86_64)

Expand Down Expand Up @@ -38,6 +44,16 @@ string(APPEND CMAKE_CXX_FLAGS_RELEASE " -g")
string(APPEND CMAKE_OBJC_FLAGS_RELEASE " -g")
string(APPEND CMAKE_OBJCXX_FLAGS_RELEASE " -g")

if(ARES_BUILD_LOCAL)
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-march=native>)
else()
if(CMAKE_OSX_ARCHITECTURES MATCHES arm64)
#
else()
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-march=x86-64-v2>)
endif()
endif()

if(ENABLE_COMPILER_TRACE)
add_compile_options(-ftime-trace)
add_link_options(LINKER:-print_statistics)
Expand Down
1 change: 1 addition & 0 deletions cmake/macos/defaults.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

include_guard(GLOBAL)

# Required to avoid us finding a system SDL2.framework before our provided SDL2.dylib
set(CMAKE_FIND_FRAMEWORK LAST)

# Set empty codesigning team if not specified as cache variable
Expand Down
56 changes: 36 additions & 20 deletions cmake/windows/compilerconfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ endif()

include_guard(GLOBAL)

option(
ARES_BUILD_LOCAL
"Allows the compiler to generate code optimized for the target machine; increases performance."
ON
)

include(ccache)
include(compiler_common)

Expand Down Expand Up @@ -53,7 +59,6 @@ set(

set(
_ares_clang_cl_c_cxx_options
-Wno-unused-function
-Wno-reorder-ctor
-Wno-missing-braces
-Wno-char-subscripts
Expand All @@ -69,30 +74,36 @@ if(MSVC)
string(REPLACE "/Ob1" "/Ob2" CMAKE_C_FLAGS_RELWITHDEBINFO ${CMAKE_C_FLAGS_RELWITHDEBINFO})
endif()

# add compiler flags
# add general compiler flags and optimizations
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# we are on either msys2/mingw clang, or clang-cl
# add common options
add_compile_options(
"$<$<COMPILE_LANGUAGE:C>:${_ares_clang_c_options}>"
"$<$<COMPILE_LANGUAGE:CXX>:${_ares_clang_cxx_options}>"
"$<$<COMPILE_LANGUAGE:C,CXX>:${_ares_clang_cl_c_cxx_options}>"
)
if(NOT MSVC)
# we are on msys2 clang
# statically link libc++
add_link_options(-static-libstdc++)

# msys2/mingw-specific invocations to make clang emit debug symbols
set(_ares_mingw_clang_debug_compile_options -g -gcodeview)
set(_ares_mingw_clang_debug_link_options -fuse-ld=lld -g -Wl,--pdb=)
add_compile_options("$<$<CONFIG:Debug,RelWithDebInfo>:${_ares_mingw_clang_debug_compile_options}>")
add_link_options("$<$<CONFIG:Debug,RelWithDebInfo>:${_ares_mingw_clang_debug_link_options}>")
# clang-cl does not understand -fwrapv, but we do want it on msys2 clang

add_compile_options(-fwrapv)
else()
# we are on clang-cl
# generate PDBs rather than embed debug symbols
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT ProgramDatabase)
add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:${_ares_msvc_cxx_options}>")

add_compile_options(
"$<$<COMPILE_LANGUAGE:CXX>:${_ares_msvc_cxx_options}>"
"$<$<COMPILE_LANGUAGE:C,CXX>:${_ares_clang_cl_c_cxx_options}>"
)

# work around https://gitlab.kitware.com/cmake/cmake/-/issues/26559
add_compile_options($<$<AND:$<BOOL:${ENABLE_IPO}>,$<NOT:$<CONFIG:Debug>>>:-flto=thin>)
add_link_options(
Expand All @@ -102,29 +113,34 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
$<$<NOT:$<CONFIG:Debug>>:/OPT:REF>
$<$<NOT:$<CONFIG:Debug>>:/OPT:ICF>
)
# add -fwrapv
add_compile_options(
"$<$<COMPILE_LANGUAGE:C,CXX>:/clang:-fwrapv>"
)
endif()

# optimizations
if(ARES_BUILD_LOCAL)
add_compile_options(-march=native)
else()
if(${arch} STREQUAL x64)
add_compile_options("$<$<COMPILE_LANGUAGE:C,CXX>:-march=x86-64-v2>")
endif()
# add -fwrapv
add_compile_options("$<$<COMPILE_LANGUAGE:C,CXX>:/clang:-fwrapv>")
endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
add_compile_options("$<$<COMPILE_LANGUAGE:C,CXX>:${_ares_msvc_cxx_options}>")
if(CMAKE_COMPILE_WARNING_AS_ERROR)
add_link_options(/WX)
endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options(
${_ares_gcc_common_options}
)
add_compile_options(${_ares_gcc_common_options})
endif()

# arch/machine-specific optimizations
if(ARES_BUILD_LOCAL)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-march=native>)
else()
# todo: arch optimizations on msvc
endif()
else()
if(${arch} STREQUAL x64)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_compile_options("$<$<COMPILE_LANGUAGE:C,CXX>:-march=x86-64-v2>")
endif()
else()
# todo: arm64 arch baseline
endif()
endif()

if(NOT MINGW)
Expand Down
5 changes: 1 addition & 4 deletions desktop-ui/cmake/os-windows.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ target_sources(desktop-ui PRIVATE resource/ares.rc resource/ares.Manifest)
set_property(DIRECTORY ${CMAKE_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT desktop-ui)

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_link_libraries(
desktop-ui
PRIVATE mingw32
)
target_link_libraries(desktop-ui PRIVATE mingw32)
endif()

if(ARES_ENABLE_LIBRASHADER)
Expand Down
5 changes: 1 addition & 4 deletions mia/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,7 @@ if(ARES_BUILD_OPTIONAL_TARGETS)

if(OS_WINDOWS)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_link_libraries(
mia-ui
PRIVATE nall
)
target_link_libraries(mia-ui PRIVATE nall)
endif()
endif()

Expand Down

0 comments on commit b4b026d

Please sign in to comment.