PGO compile using VS2022 clang compiler

Discussion of chess software programming and technical issues.

Moderator: Ras

Cardoso
Posts: 363
Joined: Thu Mar 16, 2006 7:39 pm
Location: Portugal
Full name: Alvaro Cardoso

PGO compile using VS2022 clang compiler

Post by Cardoso »

Hi,
I'm using Visual Studio 2022 Pro and only recently I started to use Clang from the Visual Studio 2022 environment. It compiled my code and runs fine. But now I want to make a PGO compile.
In the past and using MSVC I copied the instrumented compile (engineturk.exe) to the application folder along with
pgort140.dll, and after some tests and exiting the application it generated engineturk!1.pgc file
which I copied to the compile output folder and then execute the
Build->Profile Guided Optimization-> Optimize
from the VS2022 menu.

But now with the Clang compiler when I do
Build->Profile Guided Optimization-> Instrument

I get the error:

Code: Select all

1>lld-link : error : could not open '/PGD:C:\Users\Alvaro\source\repos\engineturk\engine\x64\Release\engineturk.pgd': invalid argument
So what are the steps to make a PGO compile from the VS2022 menu using the LLVM clang-cl
compiler that I installed from the VS2022 install program?

Thx,
Alvaro
Joost Buijs
Posts: 1632
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: PGO compile using VS2022 clang compiler

Post by Joost Buijs »

The PGO options in the Visual Studio 2022 build menu are meant to be used with the MSVC compiler only.

It is possible to get PGO with CLang to work by using the command line options -fprofile-inst-generate and -fprofile-use
The instrumented executable will generate the file 'default.profraw', you have to convert this with llvm-profdata.exe to 'default.profdata' before you can use the profile data to optimize the executable by building it with -fprofile-use.

There is a description over here: https://clang.llvm.org/docs/UsersManual ... timization

In the past I got it to work with instrumentation, with sampling I never tried, sampling is less accurate anyway.
Joost Buijs
Posts: 1632
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: PGO compile using VS2022 clang compiler

Post by Joost Buijs »

Of course '-fprofile-inst-generate' has to be '-fprofile-instr-generate' and '-fprofile-use' has to be '-fprofile-instr-use'.
I recalled it from memory, but that was not accurate.
Cardoso
Posts: 363
Joined: Thu Mar 16, 2006 7:39 pm
Location: Portugal
Full name: Alvaro Cardoso

Re: PGO compile using VS2022 clang compiler

Post by Cardoso »

Thanks Joost.
Since I need the instrumented executable to run on the application folder (C:\ProfoundTurkish\) because of several files dependencies and GUI initialization commands do I need some dll to do a job similar to pgort140.dll from MSVC? Or is the instrumented .exe independent of any PGO dll for gathering performance data?
Joost Buijs
Posts: 1632
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: PGO compile using VS2022 clang compiler

Post by Joost Buijs »

The instrumented executable does not seem to depend upon a dll, I just tried, the instrumented executable is also quite large, so I assume it contains all the logic it needs. This is with static linking, with dynamic linking it could be different.

MSVC uses CLang 14.0.5, I use the latest 15.0.1, I don't think this will make a difference.

You can use CLang 15.0.1 from within Visual Studio by installing LLVM 15.0.1 and putting a manifest file 'Directory.build.props' in your project folder.

It should contain:

<Project>
<PropertyGroup>
<LLVMInstallDir>C:\Program Files\LLVM</LLVMInstallDir>
<LLVMToolsVersion>15.0.1</LLVMToolsVersion>
</PropertyGroup>
</Project>
Joost Buijs
Posts: 1632
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: PGO compile using VS2022 clang compiler

Post by Joost Buijs »

When I run the instrumented executable from within Visual Studio everything looks fine, when I run the instrumented executable from a different folder there seems to be a strange issue though. It looks fine at first sight, no errors, but somehow the engine does not terminate as it should.

When I let the engine search for 10 seconds and run the instrumented executable under Visual Studio it terminates after 10 seconds and the 'default.profraw' file is generated, when I run the same instrumented executable from a different folder the search does not terminate and the engine keeps searching. When I terminate the engine manually the 'default.profraw' file has a zero size.

My engine uses an exeption to terminate the search, it looks as if exeptions are disabled when I run the instrumented executable from a different folder. This is very weird, something I can't explain.
Cardoso
Posts: 363
Joined: Thu Mar 16, 2006 7:39 pm
Location: Portugal
Full name: Alvaro Cardoso

Re: PGO compile using VS2022 clang compiler

Post by Cardoso »

Hi,
I compiled my project using "-fprofile-instr-generate" on the command line options of the compiler (in VS 2022)
I ran my instrumented executable and it ran real slow as expected.
It produced the "default.profraw" file, but when I tried using llvm-profdata.exe on this file and it reported:
warning: default.profraw: unsupported instrumentation profile format version
error: no profile can be merged.
My project is a x64 executable but I tried llvm-profdata.exe both in the normal clang bin folder and the x64\bin folder, both give the same error.
Does anyone has a clue on how to fix this?
thx,
Álvaro
Joost Buijs
Posts: 1632
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: PGO compile using VS2022 clang compiler

Post by Joost Buijs »

Hi,

It is a long time ago that I actually used this.

On the internet I see several people complaining that the transition from CLang 12 to CLang 13 broke something with respect to PGO.
Visual studio currently uses CLang 14, I use CLang 15 in combination with Visual Studio. When I have some time tomorrow I will try if I can get it to work.
Joost Buijs
Posts: 1632
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: PGO compile using VS2022 clang compiler

Post by Joost Buijs »

I tried with both CLang 14.0.5 and and 15.0.2 and get the same problem, the instumented executable generates the "default.profraw" file, when I try to convert it with llvm-profdata.exe it warns about a version conflict and doesn't produce the output file.

In the past with (older CLang versions) it worked flawlessly, I have the feeling that it is a bug. Visual Studio uses CLang-cl which is a MSVC compatible version of CLang, maybe it is not so compatible with CLang anymore as it should.

What I want to try is to circumvent CLang-cl and Visual studio all together by using a make-file. Another option could be to install a CLang version <13, but this is not something I would like.
Cardoso
Posts: 363
Joined: Thu Mar 16, 2006 7:39 pm
Location: Portugal
Full name: Alvaro Cardoso

Re: PGO compile using VS2022 clang compiler

Post by Cardoso »

Thanks Joost for all the work.
Let's wait and see if they fix it.