diff --git a/CMakeLists.txt b/CMakeLists.txt index 05de6455..24a2a338 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,6 +70,21 @@ else() add_compile_options($<$>:-O3>) endif() +find_package(OpenMP REQUIRED) + +# Check for LTO support (which in CMake is lumped in with IPO). +include(CheckIPOSupported) +check_ipo_supported(RESULT HAVE_IPO) + +# Helper function for setting LTO flag (but only if the config is not debug). +function(enable_lto target_name) + if(HAVE_IPO) + set_property(TARGET ${target_name} PROPERTY + INTERPROCEDURAL_OPTIMIZATION $> + ) + endif() +endfunction() + # Always build the basic part. add_subdirectory(pybind_interface/basic) add_subdirectory(pybind_interface/decide) diff --git a/Makefile b/Makefile index 8946fa1d..c17988f1 100644 --- a/Makefile +++ b/Makefile @@ -48,18 +48,10 @@ HIPCCFLAGS := $(BASE_HIPCCFLAGS) $(HIPCCFLAGS) LTO_FLAGS := -flto=auto USING_CLANG := $(shell $(CXX) --version | grep -isq clang && echo "true") -ifeq ($(USING_CLANG),"true") +ifeq ($(USING_CLANG),true) LTO_FLAGS := -flto endif -# Test if OpenMP header files are available and we can link with the library. -OMP_CHECK_CMD := echo "int main() { return 0; }" | \ - $(CXX) -fopenmp -x c++ - -o /dev/null 2>/dev/null -HAVE_OPENMP := $(shell $(OMP_CHECK_CMD) && echo "true") -ifeq ($(HAVE_OPENMP),true) - OPENMP_FLAGS := -fopenmp -endif - ifdef DEBUG DEBUG_FLAGS := -g -O0 CXXFLAGS += $(DEBUG_FLAGS) diff --git a/pybind_interface/avx2/CMakeLists.txt b/pybind_interface/avx2/CMakeLists.txt index 3a4f9b0a..f79bc073 100644 --- a/pybind_interface/avx2/CMakeLists.txt +++ b/pybind_interface/avx2/CMakeLists.txt @@ -17,8 +17,11 @@ project(qsim) if(MSVC) add_compile_options(/arch:AVX2 /openmp) + # Add /O2 to any configuration that is NOT Debug. + # This prevents a conflict with /RTC1 in DEBUG builds. + add_compile_options($<$>:/O2>) else() - add_compile_options(-mavx2 -mfma -flto=auto) + add_compile_options(-mavx2 -mfma -O3) execute_process( COMMAND bash --noprofile -c "grep -qs bmi2 /proc/cpuinfo" RESULT_VARIABLE _EXIT_CODE @@ -46,4 +49,6 @@ endif() include(../GetPybind11.cmake) pybind11_add_module(qsim_avx2 pybind_main_avx2.cpp) -target_link_libraries(qsim_avx2 PRIVATE qsim_openmp_config) +target_link_libraries(qsim_avx2 PUBLIC OpenMP::OpenMP_CXX) + +enable_lto(qsim_avx2) diff --git a/pybind_interface/avx512/CMakeLists.txt b/pybind_interface/avx512/CMakeLists.txt index 22d4b2dc..2b4a000d 100644 --- a/pybind_interface/avx512/CMakeLists.txt +++ b/pybind_interface/avx512/CMakeLists.txt @@ -18,7 +18,7 @@ project(qsim) if(MSVC) add_compile_options(/arch:AVX512 /openmp) else() - add_compile_options(-mavx512f -mbmi2 -flto=auto) + add_compile_options(-mavx512f -mbmi2 -O3) endif() if(APPLE) @@ -39,4 +39,6 @@ endif() include(../GetPybind11.cmake) pybind11_add_module(qsim_avx512 pybind_main_avx512.cpp) -target_link_libraries(qsim_avx512 PRIVATE qsim_openmp_config) +target_link_libraries(qsim_avx512 PUBLIC OpenMP::OpenMP_CXX) + +enable_lto(qsim_avx512) diff --git a/pybind_interface/basic/CMakeLists.txt b/pybind_interface/basic/CMakeLists.txt index 5d1e1979..b83c1b67 100644 --- a/pybind_interface/basic/CMakeLists.txt +++ b/pybind_interface/basic/CMakeLists.txt @@ -18,7 +18,7 @@ project(qsim) if(MSVC) add_compile_options(/openmp) else() - add_compile_options(-flto=auto) + add_compile_options(-O3) endif() if(APPLE) @@ -39,4 +39,6 @@ endif() include(../GetPybind11.cmake) pybind11_add_module(qsim_basic pybind_main_basic.cpp) -target_link_libraries(qsim_basic PRIVATE qsim_openmp_config) +target_link_libraries(qsim_basic PUBLIC OpenMP::OpenMP_CXX) + +enable_lto(qsim_basic) diff --git a/pybind_interface/sse/CMakeLists.txt b/pybind_interface/sse/CMakeLists.txt index 187805dc..dd474504 100644 --- a/pybind_interface/sse/CMakeLists.txt +++ b/pybind_interface/sse/CMakeLists.txt @@ -18,7 +18,7 @@ project(qsim) if(MSVC) add_compile_options(/openmp) else() - add_compile_options(-msse4 -flto=auto) + add_compile_options(-msse4 -O3) endif() if(APPLE) @@ -39,4 +39,6 @@ endif() include(../GetPybind11.cmake) pybind11_add_module(qsim_sse pybind_main_sse.cpp) -target_link_libraries(qsim_sse PRIVATE qsim_openmp_config) +target_link_libraries(qsim_sse PUBLIC OpenMP::OpenMP_CXX) + +enable_lto(qsim_sse)