Skip to content

Commit c4eee40

Browse files
committedMay 28, 2019
[CMake] Default options for faster executables on MSVC
Differential Revision: https://reviews.llvm.org/D55056 llvm-svn: 361826
1 parent a815cbb commit c4eee40

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed
 

‎llvm/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,10 @@ option(LLVM_ENABLE_LLD "Use lld as C and C++ linker." OFF)
435435
option(LLVM_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
436436
option(LLVM_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
437437

438+
if (MSVC)
439+
option(LLVM_ENABLE_INCREMENTAL_LINK "Link incrementally. Enabling it might produce slower executables." OFF)
440+
endif()
441+
438442
option(LLVM_ENABLE_DUMP "Enable dump functions even when assertions are disabled" OFF)
439443

440444
if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )

‎llvm/cmake/modules/ChooseMSVCCRT.cmake

+25-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ macro(set_flag_in_var flagsvar regex flag)
5050
set(${flagsvar} "${${flagsvar}}" CACHE STRING "${flagsvar_docs}" FORCE)
5151
endmacro(set_flag_in_var)
5252

53+
macro(disable_MT_if_LLDB build message)
54+
if (LLVM_TOOL_LLDB_BUILD)
55+
if ((NOT ${build} STREQUAL "DEBUG") AND (LLVM_USE_CRT_${build} STREQUAL "MT"))
56+
if (LLVM_TOOL_CLANG_BUILD OR LLVM_TOOL_LLD_BUILD)
57+
set(performance " This might impact runtime performance for Clang or LLD. Preferably build them separately.")
58+
endif()
59+
message(WARNING "${message}.${performance}")
60+
set(LLVM_USE_CRT_${build} "MD")
61+
endif()
62+
endif()
63+
endmacro(disable_MT_if_LLDB)
5364

5465
macro(choose_msvc_crt MSVC_CRT)
5566
if(LLVM_USE_CRT)
@@ -66,13 +77,26 @@ variables (LLVM_USE_CRT_DEBUG, etc) instead.")
6677
get_current_crt(LLVM_USE_CRT_${build}
6778
MSVC_CRT_REGEX
6879
CMAKE_CXX_FLAGS_${build})
80+
81+
# Make /MT the default in Release builds to make them faster
82+
# and avoid the DLL function thunking.
83+
if ((${build} STREQUAL "MINSIZEREL") OR
84+
(${build} STREQUAL "RELEASE") OR
85+
(${build} STREQUAL "RELWITHDEBINFO"))
86+
set(LLVM_USE_CRT_${build} "MT")
87+
endif()
88+
89+
disable_MT_if_LLDB(${build} "Using /MD as required by LLDB")
90+
6991
set(LLVM_USE_CRT_${build}
7092
"${LLVM_USE_CRT_${build}}"
7193
CACHE STRING "Specify VC++ CRT to use for ${build_type} configurations."
7294
FORCE)
7395
set_property(CACHE LLVM_USE_CRT_${build}
7496
PROPERTY STRINGS ;${${MSVC_CRT}})
75-
endif(NOT LLVM_USE_CRT_${build})
97+
else()
98+
disable_MT_if_LLDB(${build} "Disabling /MT as required by LLDB")
99+
endif()
76100
endforeach(build_type)
77101

78102
foreach(build_type ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE})

‎llvm/cmake/modules/HandleLLVMOptions.cmake

+8
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,14 @@ if( MSVC )
418418
# "Enforce type conversion rules".
419419
append("/Zc:rvalueCast" CMAKE_CXX_FLAGS)
420420

421+
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC" AND NOT LLVM_ENABLE_INCREMENTAL_LINK)
422+
foreach(CONFIG RELEASE RELWITHDEBINFO MINSIZEREL)
423+
foreach(FLAG EXE MODULE SHARED STATIC)
424+
string(REGEX REPLACE "[-/](INCREMENTAL:YES|INCREMENTAL:NO|INCREMENTAL)" "/INCREMENTAL:NO" CMAKE_${FLAG}_LINKER_FLAGS_${CONFIG} "${CMAKE_${FLAG}_LINKER_FLAGS_${CONFIG}}")
425+
endforeach()
426+
endforeach()
427+
endif()
428+
421429
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT LLVM_ENABLE_LTO)
422430
# clang-cl and cl by default produce non-deterministic binaries because
423431
# link.exe /incremental requires a timestamp in the .obj file. clang-cl

0 commit comments

Comments
 (0)