Getting MingW profiling working

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Tom Likens
Posts: 303
Joined: Sat Apr 28, 2012 6:18 pm
Location: Austin, TX

Getting MingW profiling working

Post by Tom Likens »

I thought I'd pass this on as it's been a frustrating problem for me. Essentially, while I was able to get MingW to generate a Window's executable under Linux I wasn't able to get the profile-guided optimizations to work. For that I had to dual-boot into Windows 7 and compile my chess engine there. That's always galled me, because I use Windows for nothing else in my personal life and it was a pain.

I'd tried a number of different solutions, but none seemed to work. Don had suggested compiling it under Linux with PGO enabled and then using the .gcda files produced when recompiling it with MingW. But that never worked for me, (even though I meticulously ensured that both versions of g++ were identical). It kept complaining about the profiling data being incorrect. Finally, I've hit on a solution that works and produces an executable every bit as fast as what I'm getting out of Windows.

1. I installed the Windows version of MingW 4.8.1 under Wine in Linux, (this worked without a hitch).
2. I then modified my make file command to the following:

Code: Select all

MINGW_CC64_WIN = "/home/tlikens/.wine/drive_c/Program Files/mingw-builds/x64-4.8.1-posix-seh-rev2/mingw64/bin/x86_64-w64-mingw32-g++.exe"

##--------------------------------------------------------------------------##
## GCC 64-bit profile-gathering version targeted for 64-bit Window,
## using the MingW compiler.
##--------------------------------------------------------------------------##
win64-profile: CC      = $(MINGW_CC64_WIN)
win64-profile: OPT     = -DX86_64 -DFAST -m64 -Ofast -fno-rtti -fno-exceptions
win64-profile: OPT    := $(OPT) -fomit-frame-pointer -march=k8 -mtune=generic
win64-profile: OPT    := $(OPT) -static -DUSE_POPCNT
win64-profile: OPT    := $(OPT) -fprofile-generate=profile
win64-profile: DBG     = -Wall -W -Wno-sign-compare
win64-profile: LFLAGS  = -fprofile-generate=profile -static
win64-profile: $(OBJS)
	$(LINKER) $(LFLAGS) -o $(NAME) $(OBJS)

##--------------------------------------------------------------------------##
## GCC 64-bit profile-gathering version targeted for 64-bit Windows machines
##--------------------------------------------------------------------------##
win64-use: CC     = $(MINGW_CC64_WIN)
win64-use: OPT	  = -DX86_64 -DFAST -m64 -Ofast -fno-rtti -fno-exceptions
win64-use: OPT   := $(OPT) -fomit-frame-pointer -march=k8 -mtune=generic
win64-use: OPT   := $(OPT) -static -fprofile-use -DUSE_POPCNT
win64-use: OPT   := $(OPT) -fprofile-dir=profile
win64-use: DBG    = -Wall -W -Wno-sign-compare
win64-use: LFLAGS = -fprofile-use=profile -static
win64-use: $(OBJS)
	$(LINKER) $(LFLAGS) -o $(NAME).exe $(OBJS)
One of the important things to note here, is that the profile directory is a relative path. Previously, I always used an absolute path and that simply didn't work. When the profile-gathering version of the engine would finish the profile directory would always be empty.

Also, it's important to realize that this runs under wine, so you'll need a working 64-bit installation of wine. Once I tried this though it all seems to work. Gcc finds the .gcda files and correctly applies them.

The beauty of all this now is that I *never* have to boot into Windows!

regards,
--tom