Add PGO GENPROFILE support to coreclr and clrjit #7423

Merged
merged 3 commits into from Oct 4, 2016

Projects

None yet

5 participants

@dpodder
Member
dpodder commented Sep 30, 2016 edited

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 setting the pgoinstrument flag when
calling build.cmd or build.sh. Assume profile-optimized mode by default.
Fall back to regular non-PGO optimized builds if profile data is not
available.

@dpodder dpodder Add PGO GENPROFILE support to coreclr and clrjit
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 setting the `PGO_GENERATE` environment
variable to `1` prior to launching the build. Assume profile-optimized
mode by default. Fall back to regular non-PGO optimized builds if
profile data is not available.
57a3d0c
@dpodder dpodder added this to the 1.1.0 milestone Sep 30, 2016
@dpodder dpodder self-assigned this Sep 30, 2016
@brianrob
Member

@gkhanna79, PTAL

@gkhanna79
Member
functions.cmake
@@ -179,6 +179,11 @@ function(install_clr targetName)
else()
install(FILES ${strip_destination_file} DESTINATION .)
endif()
+ if($ENV{PGO_GENERATE})
@janvorli
janvorli Oct 3, 2016 edited Member

I am not happy with using env variables as config switches for cmake. We should add an option to the build.sh and pass it to the cmake using the -D option. The easiest is to add it to the __ExtraCmakeArgs in build.cmd.

@dpodder
dpodder Oct 3, 2016 Member

Sure thing, happy to make the change.

pgosupport.cmake
+ endif(WIN32)
+
+ file(TO_NATIVE_PATH
+ "$ENV{__PackagesDir}/Microsoft.DotNet.OptimizationData.Coreclr/$ENV{__BuildOS}.$ENV{__BuildArch}/${ProfileFileName}"
@janvorli
janvorli Oct 3, 2016 Member

Please use ${CLR_CMAKE_TARGET_ARCH} instead of $ENV{__BuildArch} and

  • add "-DCLR_CMAKE_TARGET_OS=%__BuildOS%" to __ExtraCmakeArgs in build.cmd and then use it here instead of the $ENV{__BuildOS}.
  • add "-DCLR_CMAKE_PACKAGES_DIR=%__PackagesDir%" to __ExtraCmakeArgs in build.cmd and then use it here instead of the $ENV{__PackagesDir}.
@dpodder
dpodder Oct 3, 2016 Member

Easy enough, done

+ # Enable PGO only for optimized configs
+ set(ConfigTypeList RELEASE RELWITHDEBINFO)
+
+ foreach(ConfigType IN LISTS ConfigTypeList)
@janvorli
janvorli Oct 3, 2016 Member

Looping over configs makes no sense on single target cmake generators, like the Unix make. The cmake is run for a single specific configuration.
You can use generator expressions instead of looping over the configs. That would work in both cases. For example, the first set_property would be

set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS $<$<OR:$<CONFIG:Release>,$<CONFIG:Relwithdebinfo>>:/LTCG /GENPROFILE>
@dpodder
dpodder Oct 3, 2016 Member

@janvorli I originally tried this approach, but apparently, cmake does not expand generator expressions for LINK_FLAGS*. The best workaround I could find suggested using target_link_libraries instead, which kind of works, but on Windows it dumps the switches into <AdditionalDependencies> (next to all the *.lib files) instead of <AdditionalOptions>, and this to me seems wrong (although cmake "supports" that, we're relying on undocumented msbuild behavior there).

I could try using target_link_libraries anyway; or I could just target LINK_FLAGS_RELEASE (and simply ignore PGO for checked builds). I'm kind of leaning toward the latter here, but I'm open to it either way... what do you think? (@brianrob, @gkhanna79 - any additional thoughts?)

@janvorli
janvorli Oct 3, 2016 Member

Ouch, that's unfortunate. I've already noticed that the generator expressions don't work at some places. So then please keep it the way you had it.

@dpodder
dpodder Oct 4, 2016 Member

Roger that - I'll keep it as-is. Thanks!

@dpodder dpodder [feedback] don't use environment variables
3c792b4
@@ -233,7 +236,8 @@ if %__BuildNative% EQU 1 (
echo %__MsgPrefix%Regenerating the Visual Studio solution
pushd "%__IntermediatesDir%"
- call "%__SourceDir%\pal\tools\gen-buildsys-win.bat" "%__ProjectDir%" %__VSVersion% %__BuildArch% %__BuildJit32%
+ set __ExtraCmakeArgs="-DCLR_CMAKE_TARGET_OS=%__BuildOs%" "-DCLR_CMAKE_PACKAGES_DIR=%__PackagesDir%" "-DCLR_CMAKE_PGO_INSTRUMENT=%__PgoInstrument%"
@janvorli
janvorli Oct 3, 2016 Member

It seems we need to add the "-DCLR_CMAKE_TARGET_ARCH=%__BuildArch%" here. It is present in the other place.

@dpodder
dpodder Oct 3, 2016 Member

@janvorli I believe it's being set as a default here. It's being set in the other location because host != target in the cross build scenario. Should I still set it here?

@janvorli
janvorli Oct 3, 2016 Member

Ah, right. I am sorry for missing that.

@dpodder dpodder Add new params to build.sh too
56adbca
@janvorli
Member
janvorli commented Oct 3, 2016

LGTM, thank you very much for adding that!

@dpodder
Member
dpodder commented Oct 4, 2016

Thanks @janvorli for the detailed review! I'll just wait for CI to make sure nothing unexpected broke, and if all is well I'll merge the PR. Cheers!

@janvorli janvorli merged commit 114b588 into dotnet:master Oct 4, 2016

12 checks passed

CentOS7.1 x64 Debug Build and Test Build finished.
Details
FreeBSD x64 Checked Build Build finished.
Details
Linux ARM Emulator Cross Debug Build Build finished.
Details
Linux ARM Emulator Cross Release Build Build finished.
Details
OSX x64 Checked Build and Test Build finished.
Details
Ubuntu x64 Checked Build and Test Build finished.
Details
Ubuntu x64 Formatting Build finished.
Details
Windows_NT x64 Debug Build and Test Build finished.
Details
Windows_NT x64 Formatting Build finished.
Details
Windows_NT x64 Release Priority 1 Build and Test Build finished.
Details
Windows_NT x86 legacy_backend Checked Build and Test Build finished.
Details
Windows_NT x86 ryujit Checked Build and Test Build finished.
Details
@dpodder dpodder deleted the dpodder:dev/dapodd/pgo branch Oct 4, 2016
@brianrob brianrob added a commit to brianrob/coreclr that referenced this pull request Oct 21, 2016
@dpodder @brianrob dpodder + brianrob Add PGO GENPROFILE support to coreclr and clrjit (#7423)
* Add PGO GENPROFILE support to coreclr and clrjit

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.
e903494
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment