Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 6ad9f31

Browse files
dpodderDaniel Podder
authored andcommitted
Add PGO GENPROFILE support to coreclr and clrjit
(Porting commit 114b588 to release/1.1.0) Update the cmake build system to enable support for Profile Guided Optimization (PGO) on Windows, and enable this feature for two target binaries (coreclr and clrjit). With this change, toggle between instrumented and profile-optimized settings for target binaries by passing pgoinstrument argument to the build.cmd Assume profile-optimized mode by default. Fall back to regular non-PGO optimized builds if profile data is not available.
1 parent c00e407 commit 6ad9f31

File tree

7 files changed

+100
-4
lines changed

7 files changed

+100
-4
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,11 @@ if(CLR_CROSS_COMPONENTS_BUILD)
486486
include(crosscomponents.cmake)
487487
endif(CLR_CROSS_COMPONENTS_BUILD)
488488

489+
#-------------------
490+
# Enable PGO support
491+
#-------------------
492+
include(pgosupport.cmake)
493+
489494
#-----------------------------------------
490495
# Add Projects
491496
# - project which require platform header not clr's

build.cmd

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ set __BuildTypeChecked=0
5252
set __BuildTypeRelease=0
5353
set __BuildJit32="-DBUILD_JIT32=0"
5454

55+
set __PgoInstrument=0
56+
5557
REM __PassThroughArgs is a set of things that will be passed through to nested calls to build.cmd
5658
REM when using "all".
5759
set __PassThroughArgs=
@@ -103,6 +105,7 @@ if /i "%1" == "skiptests" (set __BuildTests=0&set processedArgs=!proce
103105
if /i "%1" == "skipbuildpackages" (set __BuildPackages=0&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
104106
if /i "%1" == "usenmakemakefiles" (set __NMakeMakefiles=1&set __ConfigureOnly=1&set __BuildNative=1&set __BuildNativeCoreLib=0&set __BuildCoreLib=0&set __BuildTests=0&set __BuildPackages=0&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
105107
if /i "%1" == "buildjit32" (set __BuildJit32="-DBUILD_JIT32=1"&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
108+
if /i "%1" == "pgoinstrument" (set __PgoInstrument=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
106109
if /i "%1" == "toolset_dir" (set __ToolsetDir=%2&set __PassThroughArgs=%__PassThroughArgs% %2&set processedArgs=!processedArgs! %1 %2&shift&shift&goto Arg_Loop)
107110

108111
if [!processedArgs!]==[] (
@@ -228,7 +231,8 @@ if %__BuildNative% EQU 1 (
228231
echo %__MsgPrefix%Regenerating the Visual Studio solution
229232

230233
pushd "%__IntermediatesDir%"
231-
call "%__SourceDir%\pal\tools\gen-buildsys-win.bat" "%__ProjectDir%" %__VSVersion% %__BuildArch% %__BuildJit32%
234+
set __ExtraCmakeArgs="-DCLR_CMAKE_TARGET_OS=%__BuildOs%" "-DCLR_CMAKE_PACKAGES_DIR=%__PackagesDir%" "-DCLR_CMAKE_PGO_INSTRUMENT=%__PgoInstrument%"
235+
call "%__SourceDir%\pal\tools\gen-buildsys-win.bat" "%__ProjectDir%" %__VSVersion% %__BuildArch% %__BuildJit32% !__ExtraCmakeArgs!
232236
@if defined __echo @echo on
233237
popd
234238
:SkipConfigure
@@ -281,7 +285,7 @@ if /i "%__DoCrossArchBuild%"=="1" (
281285
pushd "%__CrossCompIntermediatesDir%"
282286
set __CMakeBinDir=%__CrossComponentBinDir%
283287
set "__CMakeBinDir=!__CMakeBinDir:\=/!"
284-
set __ExtraCmakeArgs="-DCLR_CROSS_COMPONENTS_BUILD=1" "-DCLR_CMAKE_TARGET_ARCH=%__BuildArch%"
288+
set __ExtraCmakeArgs="-DCLR_CROSS_COMPONENTS_BUILD=1" "-DCLR_CMAKE_TARGET_ARCH=%__BuildArch%" "-DCLR_CMAKE_TARGET_OS=%__BuildOs%" "-DCLR_CMAKE_PACKAGES_DIR=%__PackagesDir%" "-DCLR_CMAKE_PGO_INSTRUMENT=%__PgoInstrument%"
285289
call "%__SourceDir%\pal\tools\gen-buildsys-win.bat" "%__ProjectDir%" %__VSVersion% %__CrossArch% !__ExtraCmakeArgs!
286290
@if defined __echo @echo on
287291
popd
@@ -522,6 +526,7 @@ echo for the specified platform ^(FreeBSD, Linux, NetBSD, OS X or Windows,
522526
echo respectively^).
523527
echo add nativemscorlib to go further and build the native image for designated mscorlib.
524528
echo toolset_dir ^<dir^> : set the toolset directory -- Arm64 use only. Required for Arm64 builds.
529+
echo pgoinstrument: generate instrumented code for profile guided optimization enabled binaries.
525530
echo configureonly: skip all builds; only run CMake ^(default: CMake and builds are run^)
526531
echo skipconfigure: skip CMake ^(default: CMake is run^)
527532
echo skipmscorlib: skip building System.Private.CoreLib ^(default: System.Private.CoreLib is built^).

build.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ usage()
3535
echo "clangx.y - optional argument to build using clang version x.y."
3636
echo "cross - optional argument to signify cross compilation,"
3737
echo " - will use ROOTFS_DIR environment variable if set."
38+
echo "pgoinstrument - generate instrumented code for profile guided optimization enabled binaries."
3839
echo "configureonly - do not perform any builds; just configure the build."
3940
echo "skipconfigure - skip build configuration."
4041
echo "skipnative - do not build native components."
@@ -177,8 +178,9 @@ build_coreclr()
177178

178179
pushd "$__IntermediatesDir"
179180
# Regenerate the CMake solution
180-
echo "Invoking \"$__ProjectRoot/src/pal/tools/gen-buildsys-clang.sh\" \"$__ProjectRoot\" $__ClangMajorVersion $__ClangMinorVersion $__BuildArch $__BuildType $__CodeCoverage $__IncludeTests $generator $__cmakeargs"
181-
"$__ProjectRoot/src/pal/tools/gen-buildsys-clang.sh" "$__ProjectRoot" $__ClangMajorVersion $__ClangMinorVersion $__BuildArch $__BuildType $__CodeCoverage $__IncludeTests $generator "$__cmakeargs"
181+
__ExtraCmakeArgs="-DCLR_CMAKE_TARGET_OS=$__BuildOS -DCLR_CMAKE_PACKAGES_DIR=$__PackagesDir -DCLR_CMAKE_PGO_INSTRUMENT=$__PgoInstrument"
182+
echo "Invoking \"$__ProjectRoot/src/pal/tools/gen-buildsys-clang.sh\" \"$__ProjectRoot\" $__ClangMajorVersion $__ClangMinorVersion $__BuildArch $__BuildType $__CodeCoverage $__IncludeTests $generator $__ExtraCmakeArgs $__cmakeargs"
183+
"$__ProjectRoot/src/pal/tools/gen-buildsys-clang.sh" "$__ProjectRoot" $__ClangMajorVersion $__ClangMinorVersion $__BuildArch $__BuildType $__CodeCoverage $__IncludeTests $generator "$__ExtraCmakeArgs" "$__cmakeargs"
182184
popd
183185
fi
184186

@@ -459,6 +461,7 @@ __RunArgs=
459461
__MSBCleanBuildArgs=
460462
__UseNinja=0
461463
__VerboseBuild=0
464+
__PgoInstrument=0
462465
__ConfigureOnly=0
463466
__SkipConfigure=0
464467
__SkipRestore=""
@@ -558,6 +561,10 @@ while :; do
558561
__UseNinja=1
559562
;;
560563

564+
pgoinstrument)
565+
__PgoInstrument=1
566+
;;
567+
561568
configureonly)
562569
__ConfigureOnly=1
563570
__SkipMSCorLib=1

functions.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ function(install_clr targetName)
179179
else()
180180
install(FILES ${strip_destination_file} DESTINATION .)
181181
endif()
182+
if(CLR_CMAKE_PGO_INSTRUMENT)
183+
if(WIN32)
184+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/${targetName}.pgd DESTINATION PGD OPTIONAL)
185+
endif()
186+
endif()
182187
endif()
183188
endfunction()
184189

pgosupport.cmake

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
function(clr_pgo_unknown_arch)
2+
if (WIN32)
3+
message(FATAL_ERROR "Only AMD64, ARM and I386 are supported for PGO")
4+
else()
5+
message(FATAL_ERROR "PGO not currently supported on the current platform")
6+
endif()
7+
endfunction(clr_pgo_unknown_arch)
8+
9+
# Adds Profile Guided Optimization (PGO) flags to the current target
10+
function(add_pgo TargetName)
11+
if(WIN32)
12+
set(ProfileFileName "${TargetName}.pgd")
13+
endif(WIN32)
14+
15+
file(TO_NATIVE_PATH
16+
"${CLR_CMAKE_PACKAGES_DIR}/Microsoft.DotNet.OptimizationData.Coreclr/${CLR_CMAKE_TARGET_OS}.${CLR_CMAKE_TARGET_ARCH}/${ProfileFileName}"
17+
ProfilePath
18+
)
19+
20+
# Enable PGO only for optimized configs
21+
set(ConfigTypeList RELEASE RELWITHDEBINFO)
22+
23+
foreach(ConfigType IN LISTS ConfigTypeList)
24+
set(LinkFlagsProperty "LINK_FLAGS_${ConfigType}")
25+
if(CLR_CMAKE_PGO_INSTRUMENT)
26+
if(WIN32)
27+
set_property(TARGET ${TargetName} APPEND_STRING PROPERTY ${LinkFlagsProperty} "/LTCG /GENPROFILE")
28+
endif(WIN32)
29+
else(CLR_CMAKE_PGO_INSTRUMENT)
30+
# If we don't have profile data availble, gracefully fall back to a non-PGO opt build
31+
if(EXISTS ${ProfilePath})
32+
if(WIN32)
33+
set_property(TARGET ${TargetName} APPEND_STRING PROPERTY ${LinkFlagsProperty} "/LTCG /USEPROFILE:PGD=${ProfilePath}")
34+
endif(WIN32)
35+
endif(EXISTS ${ProfilePath})
36+
endif(CLR_CMAKE_PGO_INSTRUMENT)
37+
endforeach(ConfigType)
38+
endfunction(add_pgo)
39+
40+
if(WIN32)
41+
if(CLR_CMAKE_PGO_INSTRUMENT)
42+
# Instrumented PGO binaries on Windows introduce an additional runtime dependency, pgort<ver>.dll.
43+
# Make sure we copy it next to the installed product to make it easier to redistribute the package.
44+
45+
string(SUBSTRING ${CMAKE_VS_PLATFORM_TOOLSET} 1 -1 VS_PLATFORM_VERSION_NUMBER)
46+
set(PGORT_FILENAME "pgort${VS_PLATFORM_VERSION_NUMBER}.dll")
47+
48+
get_filename_component(PATH_CXX_ROOTDIR ${CMAKE_CXX_COMPILER} DIRECTORY)
49+
50+
if(CLR_CMAKE_PLATFORM_ARCH_I386)
51+
set(PATH_VS_PGORT_DLL "${PATH_CXX_ROOTDIR}/${PGORT_FILENAME}")
52+
elseif(CLR_CMAKE_PLATFORM_ARCH_AMD64)
53+
set(PATH_VS_PGORT_DLL "${PATH_CXX_ROOTDIR}/../amd64/${PGORT_FILENAME}")
54+
elseif(CLR_CMAKE_PLATFORM_ARCH_ARM)
55+
set(PATH_VS_PGORT_DLL "${PATH_CXX_ROOTDIR}/../arm/${PGORT_FILENAME}")
56+
else()
57+
clr_pgo_unknown_arch()
58+
endif()
59+
60+
if (EXISTS ${PATH_VS_PGORT_DLL})
61+
message(STATUS "Found PGO runtime: ${PATH_VS_PGORT_DLL}")
62+
install(PROGRAMS ${PATH_VS_PGORT_DLL} DESTINATION .)
63+
else()
64+
message(FATAL_ERROR "file not found: ${PATH_VS_PGORT_DLL}")
65+
endif()
66+
67+
endif(CLR_CMAKE_PGO_INSTRUMENT)
68+
endif(WIN32)

src/dlls/mscoree/coreclr/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,6 @@ endif(WIN32)
174174

175175
# add the install targets
176176
install_clr(coreclr)
177+
178+
# Enable profile guided optimization
179+
add_pgo(coreclr)

src/jit/standalone/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,6 @@ target_link_libraries(${JIT_BASE_NAME}
5353

5454
# add the install targets
5555
install_clr(${JIT_BASE_NAME})
56+
57+
# Enable profile guided optimization
58+
add_pgo(${JIT_BASE_NAME})

0 commit comments

Comments
 (0)