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)
26 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS " -flto -fuse-ld=gold -fprofile-instr-generate")
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(EXISTS ${ProfilePath})
33 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE " /LTCG /USEPROFILE:PGD=${ProfilePath}")
34 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO " /LTCG /USEPROFILE:PGD=${ProfilePath}")
36 if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
37 if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.6)
39 target_compile_options(${TargetName} PRIVATE -flto -fprofile-instr-use=${ProfilePath} -Wno-profile-instr-out-of-date)
40 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS " -flto -fuse-ld=gold -fprofile-instr-use=${ProfilePath}")
42 message(WARNING "LTO is not supported, skipping profile guided optimizations")
44 else(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.6)
45 message(WARNING "PGO is not supported; Clang 3.6 or later is required for profile guided optimizations")
46 endif(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.6)
47 endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
49 endif(EXISTS ${ProfilePath})
50 endif(CLR_CMAKE_PGO_INSTRUMENT)