1 function(clr_pgo_unknown_arch)
3 message(FATAL_ERROR "Only AMD64, ARM and I386 are supported for PGO")
5 message(FATAL_ERROR "PGO not currently supported on the current platform")
7 endfunction(clr_pgo_unknown_arch)
9 # Adds Profile Guided Optimization (PGO) flags to the current target
10 function(add_pgo TargetName)
12 set(ProfileFileName "${TargetName}.pgd")
14 set(ProfileFileName "${TargetName}.profdata")
17 set(CLR_CMAKE_OPTDATA_PACKAGEWITHRID "optimization.${CLR_CMAKE_TARGET_OS}-${CLR_CMAKE_TARGET_ARCH}.PGO.CoreCLR")
19 # On case-sensitive file systems, NuGet packages are restored to lowercase paths
20 string(TOLOWER "${CLR_CMAKE_OPTDATA_PACKAGEWITHRID}/${CLR_CMAKE_OPTDATA_VERSION}" OptDataVersionedSubPath)
23 "${CLR_CMAKE_PACKAGES_DIR}/${OptDataVersionedSubPath}/data/${ProfileFileName}"
27 if(CLR_CMAKE_PGO_INSTRUMENT)
29 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE " /LTCG /GENPROFILE")
30 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO " /LTCG /GENPROFILE")
32 if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
33 target_compile_options(${TargetName} PRIVATE -flto -fprofile-instr-generate)
34 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS " -flto -fuse-ld=gold -fprofile-instr-generate")
35 endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
37 else(CLR_CMAKE_PGO_INSTRUMENT)
38 # If we don't have profile data availble, gracefully fall back to a non-PGO opt build
39 if(EXISTS ${ProfilePath})
41 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE " /LTCG /USEPROFILE:PGD=${ProfilePath}")
42 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO " /LTCG /USEPROFILE:PGD=${ProfilePath}")
44 if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
45 if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.6)
47 target_compile_options(${TargetName} PRIVATE -flto -fprofile-instr-use=${ProfilePath} -Wno-profile-instr-out-of-date)
48 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS " -flto -fuse-ld=gold -fprofile-instr-use=${ProfilePath}")
50 message(WARNING "LTO is not supported, skipping profile guided optimizations")
52 else(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.6)
53 message(WARNING "PGO is not supported; Clang 3.6 or later is required for profile guided optimizations")
54 endif(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.6)
55 endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
57 endif(EXISTS ${ProfilePath})
58 endif(CLR_CMAKE_PGO_INSTRUMENT)
62 if(CLR_CMAKE_PGO_INSTRUMENT)
63 # Instrumented PGO binaries on Windows introduce an additional runtime dependency, pgort<ver>.dll.
64 # Make sure we copy it next to the installed product to make it easier to redistribute the package.
66 string(SUBSTRING ${CMAKE_VS_PLATFORM_TOOLSET} 1 -1 VS_PLATFORM_VERSION_NUMBER)
67 set(PGORT_FILENAME "pgort${VS_PLATFORM_VERSION_NUMBER}.dll")
69 get_filename_component(PATH_CXX_ROOTDIR ${CMAKE_CXX_COMPILER} DIRECTORY)
71 if(CLR_CMAKE_PLATFORM_ARCH_I386)
72 set(PATH_VS_PGORT_DLL "${PATH_CXX_ROOTDIR}/${PGORT_FILENAME}")
73 elseif(CLR_CMAKE_PLATFORM_ARCH_AMD64)
74 set(PATH_VS_PGORT_DLL "${PATH_CXX_ROOTDIR}/../amd64/${PGORT_FILENAME}")
75 elseif(CLR_CMAKE_PLATFORM_ARCH_ARM)
76 set(PATH_VS_PGORT_DLL "${PATH_CXX_ROOTDIR}/../arm/${PGORT_FILENAME}")
78 clr_pgo_unknown_arch()
81 if (EXISTS ${PATH_VS_PGORT_DLL})
82 message(STATUS "Found PGO runtime: ${PATH_VS_PGORT_DLL}")
83 install(PROGRAMS ${PATH_VS_PGORT_DLL} DESTINATION .)
85 message(FATAL_ERROR "file not found: ${PATH_VS_PGORT_DLL}")
88 endif(CLR_CMAKE_PGO_INSTRUMENT)