From 1813fcd4892551065f9715111f7c1c444e32e688 Mon Sep 17 00:00:00 2001 From: Thorsten Hater <24411438+thorstenhater@users.noreply.github.com> Date: Mon, 5 Aug 2024 14:29:31 +0200 Subject: [PATCH] Use `-march` correctly (#2302) Selecting tuning flags depends on compilers and architectures. Account for this complication which became more apparent w/ proliferation of Arm64 based chips. Following from: https://maskray.me/blog/2022-08-28-march-mcpu-mtune Also fix a new --- sensible --- warning. --- arbor/morph/place_pwlin.cpp | 3 ++- cmake/CompilerOptions.cmake | 27 ++++++++++++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/arbor/morph/place_pwlin.cpp b/arbor/morph/place_pwlin.cpp index 8e49094ca3..a9dd7a2bb9 100644 --- a/arbor/morph/place_pwlin.cpp +++ b/arbor/morph/place_pwlin.cpp @@ -146,7 +146,8 @@ place_pwlin::place_pwlin(const arb::morphology& m, const isometry& iso) { arb_assert(!segments.empty()); seg_pos.reserve(segments.size()+1); - seg_pos = {0}; + seg_pos.clear(); + seg_pos.push_back(0); for (auto& seg: segments) { seg_pos.push_back(seg_pos.back()+distance(seg.prox, seg.dist)); } diff --git a/cmake/CompilerOptions.cmake b/cmake/CompilerOptions.cmake index e9ff5b6ec6..912c6104b5 100644 --- a/cmake/CompilerOptions.cmake +++ b/cmake/CompilerOptions.cmake @@ -110,18 +110,27 @@ function(set_arch_target optvar optvar_cuda_guarded arch) endforeach(line) string(REGEX REPLACE "-.*" "" target_model "${target}") - # Use -mcpu for all supported targets _except_ for x86 and Apple arm64, where it should be -march. - + # Figure out which flags to pass to compiler to tune for concrete + # architecture. + # See clang / gcc manuals and: + # https://maskray.me/blog/2022-08-28-march-mcpu-mtune if (CMAKE_CXX_COMPILER_ID MATCHES "AppleClang" AND CMAKE_CXX_COMPILER_VERSION LESS 15) set(arch_opt "") - else() - if("${target}" MATCHES "aarch64-apple-darwin" OR "${target}" MATCHES "arm64-apple-darwin") - set(arch_opt "-march=${arch} -mtune=${arch}") - elseif(target_model MATCHES "x86|i[3456]86" OR target_model MATCHES "amd64" OR target_model MATCHES "aarch64") - set(arch_opt "-march=${arch} -mtune=${arch}") + elseif (CMAKE_CXX_COMPILER_ID MATCHES "GNU") + if ("${target}" MATCHES "(arm64|aarch64)-.*") + # on AArch64, this is correct, ... + set(arch_opt "-mcpu=${arch} -mtune=${arch}") else() - set(arch_opt "-mcpu=${arch}") - endif() + # ... however on x86 mcpu _is_ mtune _and_ deprecated (since 2003!), but ... + set(arch_opt "-march=${arch}") + endif () + elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + # ... clang likes march (and possibly mtune) + # See https://discourse.llvm.org/t/when-to-use-mcpu-versus-march/47953/9 + set(arch_opt "-march=${arch} -mtune=${arch}") + else () + message(STATUS "Falling back to -march=${arch} for compiler ${CMAKE_CXX_COMPILER_ID}") + set(arch_opt "-march=${arch}") endif() endif()