forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgosupport.cmake
More file actions
110 lines (104 loc) · 7.07 KB
/
Copy pathpgosupport.cmake
File metadata and controls
110 lines (104 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
include(CheckCXXSourceCompiles)
include(CheckCXXCompilerFlag)
# VC++ guarantees support for LTCG (LTO's equivalent)
if(NOT WIN32)
# Function required to give CMAKE_REQUIRED_* local scope
function(check_have_lto_and_pgodata_supported profile_path)
set(CMAKE_REQUIRED_FLAGS "-flto -fprofile-instr-use=${profile_path} -Wno-profile-instr-out-of-date -Wno-profile-instr-unprofiled -Wno-profile-instr-missing")
set(CMAKE_REQUIRED_LIBRARIES -flto)
check_cxx_source_compiles("int main() { return 0; }" HAVE_LTO_AND_PGO_DATA_SUPPORTED)
endfunction(check_have_lto_and_pgodata_supported)
check_cxx_compiler_flag(-faligned-new COMPILER_SUPPORTS_F_ALIGNED_NEW)
endif(NOT WIN32)
# Adds Profile Guided Optimization (PGO) flags to the current target
function(add_pgo TargetName)
if(CLR_CMAKE_PGO_INSTRUMENT)
if(CLR_CMAKE_HOST_WIN32)
set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE " /LTCG /GENPROFILE")
set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO " /LTCG /GENPROFILE")
else(CLR_CMAKE_HOST_WIN32)
if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
target_compile_options(${TargetName} PRIVATE -flto -fprofile-instr-generate)
set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS " -flto -fprofile-instr-generate")
endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
endif(CLR_CMAKE_HOST_WIN32)
elseif(CLR_CMAKE_PGO_OPTIMIZE AND NOT CLR_CMAKE_ENABLE_SANITIZERS)
if(CLR_CMAKE_HOST_WIN32)
set(ProfileFileName "${TargetName}.pgd")
else(CLR_CMAKE_HOST_WIN32)
set(ProfileFileName "coreclr.profdata")
endif(CLR_CMAKE_HOST_WIN32)
set(ProfileSourcePath "${CLR_CMAKE_OPTDATA_PATH}/data/${ProfileFileName}")
# If we don't have profile data available, gracefully fall back to a non-PGO opt build
if(NOT EXISTS "${ProfileSourcePath}")
message("PGO data file NOT found: ${ProfileSourcePath}")
elseif(CMAKE_GENERATOR MATCHES "Visual Studio")
# MSVC is sensitive to exactly the options passed during PGO optimization and Ninja and
# MSBuild differ slightly (but not meaningfully for runtime behavior)
message("Cannot use PGO optimization built with Ninja from MSBuild. Re-run build with Ninja to apply PGO information")
else()
if(CLR_CMAKE_HOST_WIN32)
# link.exe opens PGO databases without file sharing, so use a build-local copy.
set(ProfilePath "${CMAKE_CURRENT_BINARY_DIR}/${ProfileFileName}")
configure_file("${ProfileSourcePath}" "${ProfilePath}" COPYONLY)
file(TO_NATIVE_PATH "${ProfilePath}" ProfilePath)
set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE " /LTCG /USEPROFILE:PGD=\"${ProfilePath}\"")
set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO " /LTCG /USEPROFILE:PGD=\"${ProfilePath}\"")
add_compile_definitions(WITH_NATIVE_PGO)
else(CLR_CMAKE_HOST_WIN32)
file(TO_NATIVE_PATH "${ProfileSourcePath}" ProfilePath)
if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
check_have_lto_and_pgodata_supported(${ProfilePath})
if(HAVE_LTO_AND_PGO_DATA_SUPPORTED)
message(STATUS "Enabling profile guided optimizations for ${TargetName}")
target_compile_options(${TargetName} PRIVATE -flto -fprofile-instr-use=${ProfilePath} -Wno-profile-instr-out-of-date -Wno-profile-instr-unprofiled -Wno-profile-instr-missing)
set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS " -flto -fprofile-instr-use=${ProfilePath}")
add_compile_definitions(WITH_NATIVE_PGO)
else(HAVE_LTO_AND_PGO_DATA_SUPPORTED)
message(WARNING "LTO is not supported or PGO optimization data not compatible, skipping profile guided optimizations for ${TargetName}")
endif(HAVE_LTO_AND_PGO_DATA_SUPPORTED)
endif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
endif(CLR_CMAKE_HOST_WIN32)
endif()
endif(CLR_CMAKE_PGO_INSTRUMENT)
endfunction(add_pgo)
# On non-Windows platforms, PGO compile flags (-fprofile-instr-generate for instrumentation,
# -fprofile-instr-use for optimization) must be applied at directory scope so they also affect
# the static and object libraries (cee_wks, utilcode, coreclrpal, etc.) that are linked into
# the final shared libraries. The per-target add_pgo function above only applies compile flags
# to the shared library target itself, which only covers its directly compiled sources
# (e.g. mscoree.cpp and exports.cpp for coreclr). add_pgo still sets the per-target LTO
# link flags needed by the final shared library.
# On Windows, PGO is handled entirely via link-time flags (/LTCG) so this isn't needed.
if(NOT WIN32)
if(CLR_CMAKE_PGO_INSTRUMENT)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-fprofile-instr-generate>)
# All shared libraries need to link the profiling runtime to resolve
# instrumentation symbols from their static library dependencies.
add_link_options(-fprofile-instr-generate)
endif()
endif()
elseif(CLR_CMAKE_PGO_OPTIMIZE AND NOT CLR_CMAKE_ENABLE_SANITIZERS)
file(TO_NATIVE_PATH "${CLR_CMAKE_OPTDATA_PATH}/data/coreclr.profdata" _PgoGlobalProfilePath)
if(EXISTS "${_PgoGlobalProfilePath}")
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
check_have_lto_and_pgodata_supported("${_PgoGlobalProfilePath}")
if(HAVE_LTO_AND_PGO_DATA_SUPPORTED)
message(STATUS "Enabling profile guided optimizations globally for coreclr static libraries")
add_compile_options(
$<$<COMPILE_LANGUAGE:C,CXX>:-fprofile-instr-use=${_PgoGlobalProfilePath}>
$<$<COMPILE_LANGUAGE:C,CXX>:-Wno-profile-instr-out-of-date>
$<$<COMPILE_LANGUAGE:C,CXX>:-Wno-profile-instr-unprofiled>
$<$<COMPILE_LANGUAGE:C,CXX>:-Wno-profile-instr-missing>
)
endif()
endif()
endif()
endif()
endif()
endif()