Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions cpp/cmake/arch.cmake
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
if(WASM)
# Disable SLP vectorization on WASM as it's brokenly slow. To give an idea, with this off it still takes
# 2m:18s to compile scalar_multiplication.cpp, and with it on I estimate it's 50-100 times longer. I never
# had the patience to wait it out...
# 🛡️ ARCHITECTURE DETECTION: Rely on CMake's built-in variables for robustness.
# 'EMSCRIPTEN' is automatically set by the emscripten toolchain file.
if(EMSCRIPTEN OR CMAKE_SYSTEM_NAME MATCHES "Emscripten")
# 🚀 WASM OPTIMIZATION
# Disable SLP vectorization on WASM as it causes massive compile-time regressions.
# (As noted: scalar_multiplication.cpp compile time explodes without this).
# -fno-exceptions: Standard for high-perf WASM/Crypto to reduce code size and overhead.
add_compile_options(-fno-exceptions -fno-slp-vectorize)
endif()

if(NOT WASM AND NOT APPLE AND NOT ARM)
add_compile_options(-march=skylake)
else()
# 🖥️ NATIVE OPTIMIZATION (Linux/macOS/Windows)

# Check if we are specifically on x86_64 architecture.
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64|AMD64")

# 🍎 APPLE SILICON / INTEL MAC HANDLING
# Apple's Clang usually handles architecture selection well via CMAKE_OSX_ARCHITECTURES.
# We explicitly avoid overriding flags on Apple unless necessary to prevent conflicts.
if(NOT APPLE)
# 🛡️ PORTABILITY FIX: Changed '-march=skylake' to '-march=native'.
# '-march=native': Tells the compiler to use all instructions available on THIS machine
# (AVX2, BMI2, ADX, etc.). It works on both Intel and AMD.
#
# NOTE: If building Docker images for distribution, consider using '-march=x86-64-v3'
# instead of 'native' to ensure binaries work on diverse servers.
add_compile_options(-march=native)
endif()

endif()

# Note: No explicit 'else' needed for ARM (M1/M2/M3 etc.) as '-march=native'
# is implied or handled by the compiler defaults on those platforms usually,
# or CMAKE_OSX_ARCHITECTURES handles it for macOS.
endif()