3

如何在不向项目添加新构建配置的情况下使用 PGI/PGO 在 MSBuild(仅限命令行)中构建解决方案?我尝试添加命令行参数 /property:WholeProgramOptimization=PGInstrument 看起来不错,它会创建 PGD 文件,但在我运行应用程序后不会创建 PGC。显然我在 MSBuild 命令行中遗漏了一些东西。

4

2 回答 2

3

我花了两天时间在这个问题上摸索,终于找到了解决方法:

首先,您需要构建程序的检测版本:

msbuild.exe
    /t:Rebuild "Letter:\path\YourProject.vcxproj"
    /p:Configuration=Release
    /p:Platform=x64
    /p:WholeProgramOptimization=PGInstrument

请注意,在我的机器上,可以在此处找到 MSBuild C:\Program Files (x86)\MSBuild\12.0\Bin\msbuild.exe:. 参数的顺序非常重要。如果您的项目放在 /p 选项之后,它可能会覆盖它们。

应该已经在您的输出目录中创建了一个 pgd 文件。稍后您将需要它的路径。

然后你需要检测程序:

Letter:\path\OutPutDir\YourProject.exe

在这一步中,显然您需要添加参数来为您的程序提供所需的数据。如果您的程序抱怨无法访问 pgort120.dll,您可以在脚本中添加如下一行:set PATH=%PATH%;C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64

最后,您可以构建程序的优化版本:

msbuild.exe
    /t:LibLinkOnly "Letter:\path\YourProject.vcxproj"
    /p:Configuration=Release
    /p:Platform=x64
    /p:WholeProgramOptimization=PGOptimize
    /p:LinkTimeCodeGeneration=PGOptimization
    /p:ProfileGuidedDatabase="Letter:\path\OutPutDir\YourProject.pgd"

这里需要用到第一步的pgd文件的地址。请注意,目标是 LibLinkOnly,因为不需要重新编译所有内容。

希望这对其他人有帮助。

于 2014-12-28T09:39:19.893 回答
1

基于 Arnaud 的回答,如果没有它,我永远不会想到这一点,还有其他有用的设置。在我的解决方案中,我只想使用 PGO 构建 20 多个项目中的一个,这需要额外的标志来防止构建所有引用的项目:

/p:BuildProjectReferences=false

使用它来停止 Rebuild 目标正在构建的目标项目所引用的项目。您需要将其添加到 /t:Rebuild 命令以及从 VS 16.8.0 开始的 /t:LibLinkOnly 命令。

在 VS2019 中,您会发现 pgort140.dll 埋在:Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.21.27702\bin\HostxNN\xNN 文件夹中,其中 xNN 是 x86 或 x64 视情况而定。

在解决方案的情况下,您可能有一个取决于解决方案设置的 $(OutDir) 输出位置,但是当您构建一个单独的项目时最终会出现在错误的位置,因此您可能都需要告诉项目输出所在的文件:

/p:OutDir="path to output"

就我而言,解决方案文件包含许多项目,并且我需要使用 PGO 运行一个,我最终得到了一个批处理文件(见下文)。在这种情况下,在顶层我有一个解决方案文件 MyApp.sln,其中包含子文件夹中的许多子项目。我想要的一个叫做 subproj ,它位于 subprob 文件夹中,该文件夹中有一个名为 subproj.vcxproj 的项目文件。每个子项目生成一个 DLL,并且 subproj 对其他子项目存在依赖关系。批处理文件的要点如下:

REM Get the folder we are running out of, assumed to hold the solution file
SET parent=%~dp0

REM Set PLATFORM=x64, TOOLS=HOSTx64\x64 for 64-bits
SET PLATFORM=Win32
SET TOOLS=Hostx86\x86

ECHO Build %PLATFORM% Release of MyApp with PGO of the subprog project
REM Expanding Program Files (x86) causes problems so use PROGRA~2
SET VSPATH=C:\PROGRA~2\Microsoft Visual Studio\2019\Professional
REM Value for VS 16.?.? = \14.21.27702
REM Value for VS 16.3.2 = \14.22.27905\bin
REM Value for VS 16.3.3 = \14.23.28105\bin
REM Value for VS 16.4.0 = \14.24.28314\bin
REM Value for VS 16.5.0 = \14.25.28610
REM Value for VS 16.6.0 = \14.26.28801
REM Value for VS 16.7.0 = \14.27.29110
REM Value for VS 16.8.0 = \14.28.29333
SET VSTOOLS=%VSPATH%\VC\Tools\MSVC\14.21.278.29333\bin
if not exist "%VSTOOLS%" (
ECHO %VSTOOLS% Was not found. This is probably due to a new release. Please
ECHO find the new location and correct this batch file:
ECHO %parent%%me%.bat
exit /b 1
)

REM Set tools path (needed to locate the pgoNnn.dll used for PGO)
set PATH=%PATH%;%VSTOOLS%\%TOOLS%

REM MSB is the path to the MSBuild.exe command
SET MSB="%VSPATH%\MSBuild\Current\Bin\MSBuild.exe"

REM OPT is the common options shared by everything
REM /v: n=normal, m=minimal, q=quiet
SET OPT=/v:m /p:Configuration=Release /p:Platform=%PLATFORM%

REM Set where our output must go. VS likes it to end with \ for $(OutDir)
SET OUTDIR=%parent%%PLATFORM%\Release\

REM It is easier to build everything and rebuild subproj that build all the
REM sub-projects separately.
echo Build the entire solution in the Release build for the desired platform.
%MSB% MyApp.sln %OPT%

echo Now instrument the subproj
%MSB% /t:Rebuild "subproj\subproj.vcxproj" %OPT% /p:OutDir=%OUTDIR% /p:WholeProgramOptimization=PGInstrument /p:BuildProjectReferences=false

echo Run MyApp to exercise the subproj DLL as needed to generate the PGO database
%parent%%PLATFORM%\Release\MyApp.exe arguments as required...

echo Now build PGO optimized version of subproj
%MSB% /t:LibLinkOnly "subproj\subproj.vcxproj" %OPT% /p:WholeProgramOptimization=PGOptimize /p:LinkTimeCodeGeneration=PGOptimization /p:OutDir=%OUTDIR% /p:ProfileGuidedDatabase="%OUTDIR%compiler.pgd"  /p:BuildProjectReferences=false
于 2019-06-07T17:41:14.050 回答