diff --git a/src/coreclr/pgosupport.cmake b/src/coreclr/pgosupport.cmake index ace1fda4e9c4b7..c123229c28ff7e 100644 --- a/src/coreclr/pgosupport.cmake +++ b/src/coreclr/pgosupport.cmake @@ -67,3 +67,41 @@ function(add_pgo TargetName) endif(NOT EXISTS ${ProfilePath}) 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($<$:-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( + $<$:-fprofile-instr-use=${_PgoGlobalProfilePath}> + $<$:-Wno-profile-instr-out-of-date> + $<$:-Wno-profile-instr-unprofiled> + ) + endif() + endif() + endif() + endif() + endif() +endif()