Skip to content

Commit

Permalink
Fix some additional compile options for the FAMSA code
Browse files Browse the repository at this point in the history
  • Loading branch information
althonos committed Nov 5, 2024
1 parent 290d8a7 commit 16612fc
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 3 deletions.
23 changes: 20 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,39 @@ project(${SKBUILD_PROJECT_NAME} VERSION ${SKBUILD_PROJECT_VERSION} LANGUAGES C C

set(BUILD_SHARED_LIBS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_CXX_STANDARD 20)

set_property(GLOBAL PROPERTY PYTHON_EXTENSIONS_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

message(STATUS "Building ${SKBUILD_PROJECT_NAME} for ${CMAKE_SYSTEM_PROCESSOR}")

# --- Setup include folders ----------------------------------------------------

set_property(GLOBAL PROPERTY PYTHON_EXTENSIONS_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(CYTHON_HEADERS_DIR ${CMAKE_CURRENT_LIST_DIR}/include)

# --- Add global C++ flags -----------------------------------------------------

include(CheckCXXCompilerFlag)

check_cxx_compiler_flag(-funroll-loops HAVE_UNROLL_LOOPS)
if(HAVE_UNROLL_LOOPS)
add_compile_options(-funroll-loops)
endif()

add_definitions(-DOLD_ATOMIC_FLAG -DNO_PROFILE_PAR)

if(WIN32)
add_definitions(-D_WIN32)
endif()

# --- Add scripts for Cython ---------------------------------------------------

# include("src/scripts/cmake/FindAVX1.cmake")
include("src/scripts/cmake/FindAVX1.cmake")
include("src/scripts/cmake/FindAVX2.cmake")
# include("src/scripts/cmake/FindAVX512.cmake")
include("src/scripts/cmake/FindNEON.cmake")
include("src/scripts/cmake/FindPOPCNT.cmake")
include("src/scripts/cmake/FindSSE2.cmake")
include("src/scripts/cmake/FindSSE4.cmake")
include("src/scripts/cmake/CythonExtension.cmake")
Expand Down
4 changes: 4 additions & 0 deletions src/FAMSA/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ else()
target_compile_definitions(famsa_lcs PRIVATE -DSIMD=0)
endif()

if(HAVE_POPCNT)
target_compile_options(famsa_lcs PRIVATE ${POPCNT_C_FLAGS})
endif()


target_link_libraries(famsa PUBLIC famsa_lcs)

103 changes: 103 additions & 0 deletions src/scripts/cmake/FindAVX1.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#.rst:
# FindAVX1
# --------
#
# Finds AVX1 support
#
# This module can be used to detect AVX1 support in a C compiler. If
# the compiler supports AVX1, the flags required to compile with
# AVX1 support are returned in variables for the different languages.
# The variables may be empty if the compiler does not need a special
# flag to support AVX1.
#
# The following variables are set:
#
# ::
#
# AVX1_C_FLAGS - flags to add to the C compiler for AVX1 support
# AVX1_FOUND - true if AVX1 is detected
#
#=============================================================================

set(_AVX1_REQUIRED_VARS)
set(CMAKE_REQUIRED_QUIET_SAVE ${CMAKE_REQUIRED_QUIET})
set(CMAKE_REQUIRED_QUIET ${AVX1_FIND_QUIETLY})

# sample AVX1 source code to test
set(AVX1_C_TEST_SOURCE
"
#include <immintrin.h>
void parasail_memset___m256i(__m256i *b, __m256i c, size_t len)
{
size_t i;
for (i=0; i<len; ++i) {
_mm256_store_si256(&b[i], c);
}
}
int foo() {
__m256i vOne = _mm256_set1_epi16(1);
return _mm256_extract_epi16(vOne, 0);
}
int main(void) { return (int)foo(); }
")

# if these are set then do not try to find them again,
# by avoiding any try_compiles for the flags
if((DEFINED AVX1_C_FLAGS) OR (DEFINED HAVE_AVX1))
else()
if(WIN32)
# MSVC can compile AVX intrinsics without the arch flag, however it
# will detect that AVX code is found and "consider using /arch:AVX".
set(AVX1_C_FLAG_CANDIDATES
"/arch:AVX"
)
else()
set(AVX1_C_FLAG_CANDIDATES
#Empty, if compiler automatically accepts AVX1
" "
#clang
"-mavx"
#GNU, Intel
"-march=core-avx-i"
)
endif()

include(CheckCSourceCompiles)

foreach(FLAG IN LISTS AVX1_C_FLAG_CANDIDATES)
set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
set(CMAKE_REQUIRED_FLAGS "${FLAG}")
unset(HAVE_AVX1 CACHE)
if(NOT CMAKE_REQUIRED_QUIET)
message(STATUS "Try AVX1 C flag = [${FLAG}]")
endif()
check_c_source_compiles("${AVX1_C_TEST_SOURCE}" HAVE_AVX1)
set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
if(HAVE_AVX1)
set(AVX1_C_FLAGS_INTERNAL "${FLAG}")
break()
endif()
endforeach()

unset(AVX1_C_FLAG_CANDIDATES)

set(AVX1_C_FLAGS "${AVX1_C_FLAGS_INTERNAL}"
CACHE STRING "C compiler flags for AVX1 intrinsics")
endif()

list(APPEND _AVX1_REQUIRED_VARS AVX1_C_FLAGS)

set(CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_SAVE})

if(_AVX1_REQUIRED_VARS)
include(FindPackageHandleStandardArgs)

find_package_handle_standard_args(AVX1
REQUIRED_VARS ${_AVX1_REQUIRED_VARS})

mark_as_advanced(${_AVX1_REQUIRED_VARS})

unset(_AVX1_REQUIRED_VARS)
else()
message(SEND_ERROR "FindAVX1 requires C or CXX language to be enabled")
endif()
92 changes: 92 additions & 0 deletions src/scripts/cmake/FindPOPCNT.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#.rst:
# FindPOPCNT
# --------
#
# Finds POPCNT support
#
# This module can be used to detect POPCNT support in a C compiler. If
# the compiler supports POPCNT, the flags required to compile with
# POPCNT support are returned in variables for the different languages.
# The variables may be empty if the compiler does not need a special
# flag to support POPCNT.
#
# The following variables are set:
#
# ::
#
# POPCNT_C_FLAGS - flags to add to the C compiler for POPCNT support
# POPCNT_FOUND - true if POPCNT is detected
#
#=============================================================================

set(_POPCNT_REQUIRED_VARS)
set(CMAKE_REQUIRED_QUIET_SAVE ${CMAKE_REQUIRED_QUIET})
set(CMAKE_REQUIRED_QUIET ${POPCNT_FIND_QUIETLY})

# sample POPCNT source code to test
set(POPCNT_C_TEST_SOURCE
"
#include <immintrin.h>
int foo(long long int a)
{
return _popcnt64(a);
}
int main(void) { return (int)foo(0); }
")

# if these are set then do not try to find them again,
# by avoiding any try_compiles for the flags
if((DEFINED POPCNT_C_FLAGS) OR (DEFINED HAVE_POPCNT))
else()
if(WIN32)
# MSVC can compile AVX intrinsics without the arch flag, however it
# will detect that AVX code is found and "consider using /arch:AVX".
set(POPCNT_C_FLAG_CANDIDATES
"/arch:SSE4.2"
)
else()
set(POPCNT_C_FLAG_CANDIDATES
#clang,GNU,Intel
"-mpopcnt"
)
endif()

include(CheckCSourceCompiles)

foreach(FLAG IN LISTS POPCNT_C_FLAG_CANDIDATES)
set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
set(CMAKE_REQUIRED_FLAGS "${FLAG}")
unset(HAVE_POPCNT CACHE)
if(NOT CMAKE_REQUIRED_QUIET)
message(STATUS "Try POPCNT C flag = [${FLAG}]")
endif()
check_c_source_compiles("${POPCNT_C_TEST_SOURCE}" HAVE_POPCNT)
set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
if(HAVE_POPCNT)
set(POPCNT_C_FLAGS_INTERNAL "${FLAG}")
break()
endif()
endforeach()

unset(POPCNT_C_FLAG_CANDIDATES)

set(POPCNT_C_FLAGS "${POPCNT_C_FLAGS_INTERNAL}"
CACHE STRING "C compiler flags for POPCNT intrinsics")
endif()

list(APPEND _POPCNT_REQUIRED_VARS POPCNT_C_FLAGS)

set(CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_SAVE})

if(_POPCNT_REQUIRED_VARS)
include(FindPackageHandleStandardArgs)

find_package_handle_standard_args(POPCNT
REQUIRED_VARS ${_POPCNT_REQUIRED_VARS})

mark_as_advanced(${_POPCNT_REQUIRED_VARS})

unset(_POPCNT_REQUIRED_VARS)
else()
message(SEND_ERROR "FindPOPCNT requires C or CXX language to be enabled")
endif()

0 comments on commit 16612fc

Please sign in to comment.