Skip to content
Open
Show file tree
Hide file tree
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
91 changes: 89 additions & 2 deletions cmake/option.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## https://en.wikipedia.org/wiki/List_of_Intel_CPU_microarchitectures
## https://en.wikipedia.org/wiki/List_of_AMD_CPU_microarchitectures
## https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html
## https://gcc.gnu.org/onlinedocs/gcc/RISC-V-Options.html

## Intel Microarchitectures
option(ENABLE_NEHALEM "Enable Intel Nehalem CPU microarchitecture" OFF)
Expand Down Expand Up @@ -30,6 +31,11 @@ option(ENABLE_ARMV8.4A "Enable ARMv8.4-a architecture" OFF)
option(ENABLE_ARMV8.5A "Enable ARMv8.5-a architecture" OFF)
option(ENABLE_ARMV8.6A "Enable ARMv8.6-a architecture" OFF)

## RISC-V architectures
option(ENABLE_RISCV64 "Enable RISC-V 64-bit base architecture" OFF)
option(ENABLE_RISCV_VECTOR "Enable RISC-V Vector extension" OFF)
option(ENABLE_RISCV_ZVFH "Enable RISC-V Zvfh extension" OFF)

## OpenMP option
option(ENABLE_OPENMP "Enable OpenMP support" OFF)

Expand All @@ -39,6 +45,7 @@ set(ARCH_OPTIONS
ENABLE_ZEN1 ENABLE_ZEN2 ENABLE_ZEN3
ENABLE_ARMV8A ENABLE_ARMV8.1A ENABLE_ARMV8.2A ENABLE_ARMV8.3A ENABLE_ARMV8.4A
ENABLE_ARMV8.5A ENABLE_ARMV8.6A
ENABLE_RISCV64 ENABLE_RISCV_VECTOR ENABLE_RISCV_ZVFH
ENABLE_NATIVE
)

Expand All @@ -51,6 +58,10 @@ foreach(opt IN LISTS ARCH_OPTIONS)
endif()
endforeach()

if((ENABLE_RISCV_VECTOR OR ENABLE_RISCV_ZVFH) AND NOT ENABLE_RISCV64)
message(FATAL_ERROR "ENABLE_RISCV_VECTOR and ENABLE_RISCV_ZVFH require ENABLE_RISCV64")
endif()

include(CheckCCompilerFlag)

function(_AppendFlags _RESULT _FLAG)
Expand Down Expand Up @@ -90,6 +101,60 @@ macro(add_arch_flag FLAG VAR_NAME OPTION_NAME)
endif()
endmacro()

function(_build_riscv64_march RESULT_VAR)
set(_march "rv64gc")
if(ENABLE_RISCV_VECTOR)
string(APPEND _march "v")
endif()
if(ENABLE_RISCV_ZVFH)
if(NOT ENABLE_RISCV_VECTOR)
message(FATAL_ERROR "ENABLE_RISCV_ZVFH requires ENABLE_RISCV_VECTOR")
endif()
string(APPEND _march "_zvfh")
endif()
set(${RESULT_VAR} "${_march}" PARENT_SCOPE)
endfunction()

function(_build_riscv64_rvv_march RESULT_VAR)
set(_march "rv64gcv")
if(ENABLE_RISCV_ZVFH)
string(APPEND _march "_zvfh")
endif()
set(${RESULT_VAR} "${_march}" PARENT_SCOPE)
endfunction()

function(_setup_riscv64_march)
_build_riscv64_march(_riscv_march)
set(CMAKE_REQUIRED_FLAGS "-mabi=lp64d")
check_c_compiler_flag("-march=${_riscv_march}" _COMP_SUPP_riscv64)
unset(CMAKE_REQUIRED_FLAGS)

if(_COMP_SUPP_riscv64)
_AppendFlags(CMAKE_C_FLAGS "-march=${_riscv_march}")
_AppendFlags(CMAKE_C_FLAGS "-mabi=lp64d")
_AppendFlags(CMAKE_CXX_FLAGS "-march=${_riscv_march}")
_AppendFlags(CMAKE_CXX_FLAGS "-mabi=lp64d")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}" PARENT_SCOPE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" PARENT_SCOPE)
message(STATUS "RISC-V: enabled -march=${_riscv_march} -mabi=lp64d")
else()
message(WARNING "Compiler does not support -march=${_riscv_march} -mabi=lp64d")
endif()
endfunction()

function(setup_compiler_march_for_riscv64_rvv RESULT_VAR)
_build_riscv64_rvv_march(_riscv_rvv_march)
set(CMAKE_REQUIRED_FLAGS "-mabi=lp64d")
check_c_compiler_flag("-march=${_riscv_rvv_march}" _COMP_SUPP_riscv64_rvv)
unset(CMAKE_REQUIRED_FLAGS)

if(_COMP_SUPP_riscv64_rvv)
set(${RESULT_VAR} "-march=${_riscv_rvv_march} -mabi=lp64d" PARENT_SCOPE)
else()
set(${RESULT_VAR} "" PARENT_SCOPE)
endif()
endfunction()

function(_setup_armv8_march)
if(MSVC)
return()
Expand Down Expand Up @@ -247,8 +312,7 @@ if(NOT AUTO_DETECT_ARCH)
add_arch_flag("-march=nehalem" NEHALEM ENABLE_NEHALEM)
endif()

# ARM (newest first — allow multiple? usually only one)
# But GCC allows only one -march=, so honor highest enabled
# ARM (newest first)
if(ENABLE_ARMV8.6A)
add_arch_flag("-march=armv8.6-a" ARMV86A ENABLE_ARMV8.6A)
endif()
Expand All @@ -271,6 +335,25 @@ if(NOT AUTO_DETECT_ARCH)
add_arch_flag("-march=armv8-a" ARMV8A ENABLE_ARMV8A)
endif()

# RISC-V 64-bit
if(ENABLE_RISCV64)
_build_riscv64_march(_riscv_march)
set(CMAKE_REQUIRED_FLAGS "-mabi=lp64d")
check_c_compiler_flag("-march=${_riscv_march}" COMPILER_SUPPORT_RISCV64)
unset(CMAKE_REQUIRED_FLAGS)
if(COMPILER_SUPPORT_RISCV64)
_AppendFlags(CMAKE_C_FLAGS "-march=${_riscv_march}")
_AppendFlags(CMAKE_C_FLAGS "-mabi=lp64d")
_AppendFlags(CMAKE_CXX_FLAGS "-march=${_riscv_march}")
_AppendFlags(CMAKE_CXX_FLAGS "-mabi=lp64d")
message(STATUS "RISC-V: enabled -march=${_riscv_march} -mabi=lp64d")
else()
message(FATAL_ERROR
"Compiler does not support required flags: "
"-march=${_riscv_march} -mabi=lp64d for ENABLE_RISCV64")
endif()
endif()

else()
# AUTO DETECT
# Heuristic: detect host architecture and probe appropriate flags
Expand All @@ -284,6 +367,8 @@ else()
set(HOST_ARCH arm64)
elseif(SYSTEM_PROC_LOWER MATCHES "^(arm|armv7|armv7-a|armv7l)$")
set(HOST_ARCH arm)
elseif(SYSTEM_PROC_LOWER MATCHES "^(riscv64)$")
set(HOST_ARCH riscv64)
else()
set(HOST_ARCH unknown)
message(WARNING "unknown host arch: ${CMAKE_SYSTEM_PROCESSOR}")
Expand All @@ -294,6 +379,8 @@ else()
_setup_armv8_march()
elseif (HOST_ARCH MATCHES "^(x86|x64)$")
_setup_x86_march()
elseif (HOST_ARCH STREQUAL "riscv64")
_setup_riscv64_march()
else ()
message(WARNING "unknown host arch - no -march set")
endif ()
Expand Down
37 changes: 30 additions & 7 deletions src/ailego/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ if(NOT ANDROID AND AUTO_DETECT_ARCH)
endforeach()

foreach(MATH_FILE ${MATH_FILES_AVX512FP16})
set_source_files_properties(
${MATH_FILE}
PROPERTIES
COMPILE_FLAGS "${MATH_MARCH_FLAG_AVX512FP16}"
)
endforeach()
elseif (HOST_ARCH MATCHES "^(arm|arm64)$")
set_source_files_properties(
${MATH_FILE}
PROPERTIES
COMPILE_FLAGS "${MATH_MARCH_FLAG_AVX512FP16}"
)
endforeach()

elseif(HOST_ARCH MATCHES "^(arm|arm64)$")
if(MSVC)
return()
endif()
Expand All @@ -114,6 +115,28 @@ if(NOT ANDROID AND AUTO_DETECT_ARCH)
COMPILE_FLAGS "${MATH_MARCH_FLAG_NEON}"
)
endforeach()

elseif(HOST_ARCH STREQUAL "riscv64")
setup_compiler_march_for_riscv64_rvv(MATH_MARCH_FLAG_RVV)

if(MATH_MARCH_FLAG_RVV)
file(GLOB_RECURSE MATH_FILES_RVV
${CMAKE_CURRENT_SOURCE_DIR}/math/*_rvv.cc
${CMAKE_CURRENT_SOURCE_DIR}/math/*_rvv.c
${CMAKE_CURRENT_SOURCE_DIR}/math_batch/*_rvv.cc
${CMAKE_CURRENT_SOURCE_DIR}/math_batch/*_rvv.c
)

foreach(MATH_FILE ${MATH_FILES_RVV})
set_source_files_properties(
${MATH_FILE}
PROPERTIES
COMPILE_FLAGS "${MATH_MARCH_FLAG_RVV}"
)
endforeach()
else()
message(STATUS "RISC-V RVV-specific march is not supported by the compiler; skipping *_rvv sources")
endif()
endif()
endif()

Expand Down
Loading