-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-130090: Support PGO for clang-cl #129907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
859c89f
a529c39
26fb51f
6e2cb69
7b46aeb
ced66bd
1aae65d
52fd5ab
c2ecb57
ad72df5
74ec74e
79ce418
8c8aa79
2a9da27
ea4de96
3346b9d
9db1a29
4ad2365
1977953
ad2dfce
6edbe07
81b4c1d
263870d
db208ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -504,6 +504,7 @@ Grant Edwards | |
Vlad Efanov | ||
Zvi Effron | ||
John Ehresman | ||
Chris Eibl | ||
Tal Einat | ||
Eric Eisner | ||
Andrew Eland | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Building with ``PlatformToolset=ClangCL`` on Windows now supports PGO | ||
(profile guided optimization). Patch by Chris Eibl with invaluable support from Steve Dover. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup Label="Globals"> | ||
<__PyprojectClangCl_Props_Imported>true</__PyprojectClangCl_Props_Imported> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<!-- CLANG_PROFILE_PATH is configurable for "remote PGO builds" | ||
For convenience, we also accept paths without trailing slashes. | ||
--> | ||
<CLANG_PROFILE_PATH Condition="'$(CLANG_PROFILE_PATH)' == ''">$(OutDir)</CLANG_PROFILE_PATH> | ||
<_CLANG_PROFILE_PATH>$(CLANG_PROFILE_PATH)</_CLANG_PROFILE_PATH> | ||
<_CLANG_PROFILE_PATH Condition="!HasTrailingSlash($(_CLANG_PROFILE_PATH))">$(_CLANG_PROFILE_PATH)\</_CLANG_PROFILE_PATH> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<_profrawFiles Include="$(OutDir)instrumented\$(TargetName)_*.profraw" /> | ||
</ItemGroup> | ||
|
||
<Target Name="EnsureClangProfileData" BeforeTargets="PrepareForBuild" | ||
Condition="'$(SupportPGO)' and $(Configuration) == 'PGUpdate'"> | ||
<Error Text="PGO run did not succeed (no $(TargetName)_*.profraw files) and there is no data to merge" | ||
Condition="$(RequireProfileData) == 'true' and @(_profrawFiles) == ''" /> | ||
</Target> | ||
|
||
<Target Name="MergeClangProfileData" BeforeTargets="PrepareForBuild" | ||
Condition="'$(SupportPGO)' and $(Configuration) == 'PGUpdate'" | ||
Inputs="@(_profrawFiles)" | ||
Outputs="$(OutDir)instrumented\profdata.profdata"> | ||
<Exec | ||
Command='"$(LLVMInstallDir)\bin\llvm-profdata.exe" merge -output="$(OutDir)instrumented\profdata.profdata" "$(OutDir)instrumented\*_*.profraw"' /> | ||
</Target> | ||
|
||
<Target Name="CleanClangProfileData" BeforeTargets="Clean"> | ||
<Delete Files="@(_profrawFiles)" TreatErrorsAsWarnings="true" /> | ||
<Delete Files="$(OutDir)instrumented\profdata.profdata" TreatErrorsAsWarnings="true" /> | ||
</Target> | ||
|
||
<ItemDefinitionGroup> | ||
<ClCompile> | ||
<AdditionalOptions>-Wno-deprecated-non-prototype -Wno-unused-label -Wno-pointer-sign -Wno-incompatible-pointer-types-discards-qualifiers -Wno-unused-function %(AdditionalOptions)</AdditionalOptions> | ||
<AdditionalOptions Condition="'$(Platform)' == 'Win32'">-m32 %(AdditionalOptions)</AdditionalOptions> | ||
<AdditionalOptions Condition="'$(Platform)' == 'x64'">-m64 %(AdditionalOptions)</AdditionalOptions> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there an ARM64 option as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm glad you found this option, though! Much neater way to do it (and a bit embarrassing that the built-in targets don't do it...). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think so, yes. I see some hits in the net, and did a But I do not know what to actually write here in case of I think we should leave that for another PR of someone who does? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. @brandtbucher might be that someone. But at the conditions here will just not set an option on that platform and so it should work the same as today. |
||
<AdditionalOptions Condition="$(Configuration) != 'Debug'">-flto %(AdditionalOptions)</AdditionalOptions> | ||
<AdditionalOptions Condition="$(SupportPGO) and $(Configuration) == 'PGInstrument'">-fprofile-instr-generate=$(_CLANG_PROFILE_PATH)$(TargetName)_%m.profraw %(AdditionalOptions)</AdditionalOptions> | ||
<AdditionalOptions Condition="$(SupportPGO) and $(Configuration) == 'PGUpdate'">-fprofile-instr-use=$(OutDir)instrumented\profdata.profdata -Wno-profile-instr-unprofiled %(AdditionalOptions)</AdditionalOptions> | ||
chris-eibl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</ClCompile> | ||
</ItemDefinitionGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,45 @@ Release | |
settings, though without PGO. | ||
|
||
|
||
Building Python using Clang/LLVM | ||
-------------------------------- | ||
|
||
See https://learn.microsoft.com/cpp/build/clang-support-msbuild | ||
for how to install and use clang-cl bundled with Microsoft Visual Studio. | ||
You can use the IDE to switch to clang-cl for local development, | ||
but because this alters the *.vcxproj files, the recommended way is | ||
to use build.bat: | ||
|
||
build.bat "/p:PlatformToolset=ClangCL" | ||
|
||
All other build.bat options continue to work as with MSVC, so this | ||
will create a 64bit release binary. | ||
|
||
You can also use a specific version of clang-cl downloaded from | ||
https://github.com/llvm/llvm-project/releases, e.g. | ||
clang+llvm-18.1.8-x86_64-pc-windows-msvc.tar.xz. | ||
Given you have extracted that to <my-clang-dir>, you can use it like so | ||
build.bat --pgo "/p:PlatformToolset=ClangCL" "/p:LLVMInstallDir=<my-clang-dir> "/p:LLVMToolsVersion=18" | ||
|
||
Setting LLVMToolsVersion to the major version is enough, although you | ||
can be specific and use 18.1.8 in the above example, too. | ||
|
||
Use the --pgo option to build with PGO (Profile Guided Optimization). | ||
|
||
However, if you want to run the PGO task | ||
on a different host than the build host, you must pass | ||
"/p:CLANG_PROFILE_PATH=<relative-path-to-instrumented-dir-on-remote-host>" | ||
in the PGInstrument step to make sure the profile data is generated | ||
into the instrumented directory when running the PGO task. | ||
Comment on lines
+82
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this needs a worked example. Just from this description, I couldn't confidently specify the parameter and trust that I got it right. |
||
E.g., if you place the instrumented binaries into the folder | ||
"workdir/instrumented" and then run the PGO task using "workdir" | ||
as the current working directory, the usage is | ||
"/p:CLANG_PROFILE_PATH=instrumented" | ||
|
||
Like in the MSVC case, after fetching (or manually copying) the instrumented | ||
folder back into your build tree, you can continue with the PGUpdate | ||
step with no further parameters. | ||
|
||
Building Python using the build.bat script | ||
---------------------------------------------- | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.