Skip to content

Commit d7acd1f

Browse files
authored
Set LTO option only if it's supported and the build is not a debug build (#1002)
Previously, the CMakeLists.txt files added `-flto` to some of the compilation targets unconditionally. The addition should only be done if the compiler supports LTO, and it shouldn't be done if the cmake configuration is the debug configuration. This PR moves the detection of availability of LTO to the top-level CMakeLists.txt file, and defines a CMake macro to set it in the sub-CMakeLists.txt files. The macro avoids repeating some gnarly CMake syntax all over the place.
1 parent 14cf87d commit d7acd1f

6 files changed

Lines changed: 35 additions & 17 deletions

File tree

CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ else()
7070
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-O3>)
7171
endif()
7272

73+
find_package(OpenMP REQUIRED)
74+
75+
# Check for LTO support (which in CMake is lumped in with IPO).
76+
include(CheckIPOSupported)
77+
check_ipo_supported(RESULT HAVE_IPO)
78+
79+
# Helper function for setting LTO flag (but only if the config is not debug).
80+
function(enable_lto target_name)
81+
if(HAVE_IPO)
82+
set_property(TARGET ${target_name} PROPERTY
83+
INTERPROCEDURAL_OPTIMIZATION $<NOT:$<CONFIG:Debug>>
84+
)
85+
endif()
86+
endfunction()
87+
7388
# Always build the basic part.
7489
add_subdirectory(pybind_interface/basic)
7590
add_subdirectory(pybind_interface/decide)

Makefile

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,10 @@ HIPCCFLAGS := $(BASE_HIPCCFLAGS) $(HIPCCFLAGS)
4848

4949
LTO_FLAGS := -flto=auto
5050
USING_CLANG := $(shell $(CXX) --version | grep -isq clang && echo "true")
51-
ifeq ($(USING_CLANG),"true")
51+
ifeq ($(USING_CLANG),true)
5252
LTO_FLAGS := -flto
5353
endif
5454

55-
# Test if OpenMP header files are available and we can link with the library.
56-
OMP_CHECK_CMD := echo "int main() { return 0; }" | \
57-
$(CXX) -fopenmp -x c++ - -o /dev/null 2>/dev/null
58-
HAVE_OPENMP := $(shell $(OMP_CHECK_CMD) && echo "true")
59-
ifeq ($(HAVE_OPENMP),true)
60-
OPENMP_FLAGS := -fopenmp
61-
endif
62-
6355
ifdef DEBUG
6456
DEBUG_FLAGS := -g -O0
6557
CXXFLAGS += $(DEBUG_FLAGS)

pybind_interface/avx2/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ project(qsim)
1717

1818
if(MSVC)
1919
add_compile_options(/arch:AVX2 /openmp)
20+
# Add /O2 to any configuration that is NOT Debug.
21+
# This prevents a conflict with /RTC1 in DEBUG builds.
22+
add_compile_options($<$<NOT:$<CONFIG:Debug>>:/O2>)
2023
else()
21-
add_compile_options(-mavx2 -mfma -flto=auto)
24+
add_compile_options(-mavx2 -mfma -O3)
2225
execute_process(
2326
COMMAND bash --noprofile -c "grep -qs bmi2 /proc/cpuinfo"
2427
RESULT_VARIABLE _EXIT_CODE
@@ -46,4 +49,6 @@ endif()
4649
include(../GetPybind11.cmake)
4750
pybind11_add_module(qsim_avx2 pybind_main_avx2.cpp)
4851

49-
target_link_libraries(qsim_avx2 PRIVATE qsim_openmp_config)
52+
target_link_libraries(qsim_avx2 PUBLIC OpenMP::OpenMP_CXX)
53+
54+
enable_lto(qsim_avx2)

pybind_interface/avx512/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ project(qsim)
1818
if(MSVC)
1919
add_compile_options(/arch:AVX512 /openmp)
2020
else()
21-
add_compile_options(-mavx512f -mbmi2 -flto=auto)
21+
add_compile_options(-mavx512f -mbmi2 -O3)
2222
endif()
2323

2424
if(APPLE)
@@ -39,4 +39,6 @@ endif()
3939
include(../GetPybind11.cmake)
4040
pybind11_add_module(qsim_avx512 pybind_main_avx512.cpp)
4141

42-
target_link_libraries(qsim_avx512 PRIVATE qsim_openmp_config)
42+
target_link_libraries(qsim_avx512 PUBLIC OpenMP::OpenMP_CXX)
43+
44+
enable_lto(qsim_avx512)

pybind_interface/basic/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ project(qsim)
1818
if(MSVC)
1919
add_compile_options(/openmp)
2020
else()
21-
add_compile_options(-flto=auto)
21+
add_compile_options(-O3)
2222
endif()
2323

2424
if(APPLE)
@@ -39,4 +39,6 @@ endif()
3939
include(../GetPybind11.cmake)
4040
pybind11_add_module(qsim_basic pybind_main_basic.cpp)
4141

42-
target_link_libraries(qsim_basic PRIVATE qsim_openmp_config)
42+
target_link_libraries(qsim_basic PUBLIC OpenMP::OpenMP_CXX)
43+
44+
enable_lto(qsim_basic)

pybind_interface/sse/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ project(qsim)
1818
if(MSVC)
1919
add_compile_options(/openmp)
2020
else()
21-
add_compile_options(-msse4 -flto=auto)
21+
add_compile_options(-msse4 -O3)
2222
endif()
2323

2424
if(APPLE)
@@ -39,4 +39,6 @@ endif()
3939
include(../GetPybind11.cmake)
4040
pybind11_add_module(qsim_sse pybind_main_sse.cpp)
4141

42-
target_link_libraries(qsim_sse PRIVATE qsim_openmp_config)
42+
target_link_libraries(qsim_sse PUBLIC OpenMP::OpenMP_CXX)
43+
44+
enable_lto(qsim_sse)

0 commit comments

Comments
 (0)