Skip to content

Commit 6a10b95

Browse files
authored
Move setting of compiler optimization flag to top-level CMakeLists.txt (#1000)
Setting `-O3` or `/O2` does not have to be done in each sub-CMakeLists.txt file. It can be done once in the top-level one.
1 parent bdb414a commit 6a10b95

10 files changed

Lines changed: 28 additions & 46 deletions

File tree

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ else()
5252
endif()
5353
endif()
5454

55+
# Add optimization flags to any configuration that is NOT Debug. Note: CMake
56+
# will automatically use the appropriate debug flags (e.g., /Od and /Zi for
57+
# MSVC, or -O0 -g for GCC/Clang), so there's no need to do that part.
58+
if(MSVC)
59+
add_compile_options($<$<NOT:$<CONFIG:Debug>>:/O2>)
60+
else()
61+
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-O3>)
62+
endif()
63+
5564
find_package(OpenMP REQUIRED)
5665

5766
# Always build the basic part.

pybind_interface/avx2/CMakeLists.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@
1515
cmake_minimum_required(VERSION 3.28)
1616
project(qsim)
1717

18-
if(WIN32)
18+
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>)
23-
elseif(LINUX)
24-
add_compile_options(-mavx2 -mfma -O3 -flto=auto)
20+
else()
21+
add_compile_options(-mavx2 -mfma -flto=auto)
2522
execute_process(
2623
COMMAND bash --noprofile -c "grep -qs bmi2 /proc/cpuinfo"
2724
RESULT_VARIABLE _EXIT_CODE

pybind_interface/avx512/CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@
1515
cmake_minimum_required(VERSION 3.28)
1616
project(qsim)
1717

18-
if(WIN32)
18+
if(MSVC)
1919
add_compile_options(/arch:AVX512 /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>)
2320
else()
24-
add_compile_options(-mavx512f -mbmi2 -O3 -flto=auto)
21+
add_compile_options(-mavx512f -mbmi2 -flto=auto)
2522
endif()
2623

2724
if(APPLE)

pybind_interface/basic/CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@
1515
cmake_minimum_required(VERSION 3.28)
1616
project(qsim)
1717

18-
if(WIN32)
18+
if(MSVC)
1919
add_compile_options(/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>)
2320
else()
24-
add_compile_options(-O3 -flto=auto)
21+
add_compile_options(-flto=auto)
2522
endif()
2623

2724
if(APPLE)

pybind_interface/cuda/CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@
1515
cmake_minimum_required(VERSION 3.28)
1616
project(qsim LANGUAGES CXX CUDA)
1717

18-
if(WIN32)
18+
if(MSVC)
1919
# Always apply AVX2 and openmp on Windows
2020
add_compile_options(/openmp)
21-
# Add /O2 to any configuration that is NOT Debug.
22-
# This prevents a conflict with /RTC1 in DEBUG builds.
23-
add_compile_options($<$<NOT:$<CONFIG:Debug>>:/O2>)
2421
else()
25-
add_compile_options(-O3 -fno-lto)
22+
add_compile_options(-fno-lto)
2623
endif()
2724

2825
if(APPLE)

pybind_interface/custatevec/CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@
1515
cmake_minimum_required(VERSION 3.28)
1616
project(qsim LANGUAGES CXX CUDA)
1717

18-
if(WIN32)
18+
if(MSVC)
1919
add_compile_options(/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>)
2320
else()
24-
add_compile_options(-O3 -fno-lto)
21+
add_compile_options(-fno-lto)
2522
endif()
2623

2724
if(APPLE)

pybind_interface/custatevecex/CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@
1515
cmake_minimum_required(VERSION 3.28)
1616
project(qsim LANGUAGES CXX CUDA)
1717

18-
if(WIN32)
18+
if(MSVC)
1919
add_compile_options(/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>)
2320
else()
24-
add_compile_options(-O3 -fno-lto)
21+
add_compile_options(-fno-lto)
2522
endif()
2623

2724
if(APPLE)

pybind_interface/decide/CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@ project(qsim LANGUAGES CXX)
1818
include(CheckLanguage)
1919
check_language(CUDA)
2020

21-
if(WIN32)
21+
if(MSVC)
2222
add_compile_options(/openmp)
23-
# Add /O2 to any configuration that is NOT Debug.
24-
# This prevents a conflict with /RTC1 in DEBUG builds.
25-
add_compile_options($<$<NOT:$<CONFIG:Debug>>:/O2>)
2623
else()
27-
add_compile_options(-O3 -fno-lto)
24+
add_compile_options(-fno-lto)
2825
endif()
2926

3027
if(APPLE)

pybind_interface/hip/CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@
1515
cmake_minimum_required(VERSION 3.28)
1616
project(qsim LANGUAGES CXX HIP)
1717

18-
if(WIN32)
18+
if(MSVC)
1919
add_compile_options(/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>)
2320
else()
24-
add_compile_options(-O3 -fno-lto)
21+
add_compile_options(-fno-lto)
2522
endif()
2623

2724
include(../GetPybind11.cmake)

pybind_interface/sse/CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@
1515
cmake_minimum_required(VERSION 3.28)
1616
project(qsim)
1717

18-
if(WIN32)
18+
if(MSVC)
1919
add_compile_options(/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>)
2320
else()
24-
add_compile_options(-msse4 -O3 -flto=auto)
21+
add_compile_options(-msse4 -flto=auto)
2522
endif()
2623

2724
if(APPLE)

0 commit comments

Comments
 (0)