1 # Adds Profile Guided Optimization (PGO) flags to the current target
2 function(add_pgo TargetName)
4 set(ProfileFileName "${TargetName}.pgd")
6 set(ProfileFileName "${TargetName}.profdata")
9 set(CLR_CMAKE_OPTDATA_PACKAGEWITHRID "optimization.${CLR_CMAKE_TARGET_OS}-${CLR_CMAKE_TARGET_ARCH}.PGO.CoreCLR")
11 # On case-sensitive file systems, NuGet packages are restored to lowercase paths
12 string(TOLOWER "${CLR_CMAKE_OPTDATA_PACKAGEWITHRID}/${CLR_CMAKE_OPTDATA_VERSION}" OptDataVersionedSubPath)
15 "${CLR_CMAKE_PACKAGES_DIR}/${OptDataVersionedSubPath}/data/${ProfileFileName}"
19 if(CLR_CMAKE_PGO_INSTRUMENT)
21 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE " /LTCG /GENPROFILE")
22 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO " /LTCG /GENPROFILE")
24 if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
25 target_compile_options(${TargetName} PRIVATE -flto -fprofile-instr-generate=/tmp/${TargetName}-%p.profdata)
26 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS " -flto -fuse-ld=gold -fprofile-instr-generate=/tmp/${TargetName}-%p.profdata")
27 endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
29 elseif(CLR_CMAKE_PGO_OPTIMIZE)
30 # If we don't have profile data availble, gracefully fall back to a non-PGO opt build
31 if(NOT EXISTS ${ProfilePath})
32 message("PGO data file NOT found: ${ProfilePath}")
33 else(NOT EXISTS ${ProfilePath})
35 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE " /LTCG /USEPROFILE:PGD=${ProfilePath}")
36 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO " /LTCG /USEPROFILE:PGD=${ProfilePath}")
38 if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
39 if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.6)
41 target_compile_options(${TargetName} PRIVATE -flto -fprofile-instr-use=${ProfilePath} -Wno-profile-instr-out-of-date -Wno-profile-instr-unprofiled)
42 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS " -flto -fuse-ld=gold -fprofile-instr-use=${ProfilePath}")
44 message(WARNING "LTO is not supported, skipping profile guided optimizations")
46 else(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.6)
47 message(WARNING "PGO is not supported; Clang 3.6 or later is required for profile guided optimizations")
48 endif(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.6)
49 endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
51 endif(NOT EXISTS ${ProfilePath})
52 endif(CLR_CMAKE_PGO_INSTRUMENT)