Page 1 of 1

Using Link-Time-Optimization in mingw/gcc: Any experience?

Posted: Thu Aug 01, 2019 10:16 am
by RubiChess
Hi.

I started to reorganize some of my code seperating one big module into several smaller modules (cpp files) which resulted in a slowdown (msys2/mingw64/gcc/g++ build), probably because the optimization used so far only works inside one module.

Then I discovered the -flto switch and gave it a try. Sadly the resulting exe just crashed at start with a segmentation violation error.
But... if I remove the -static switch and compile again, the resulting exe (needing some of the mingw64 dlls now) seems to work fine and gave a great speed improvement.
I don't like builds that rely on some dlls so I tried everything to make it work with -static but failed so far. I read several things about -flto in Internet from "Broken in windows, don't use it!" to some advises how to make it work using different linker/bin utils/parameters but nothing worked for me so far.

Has anybody of you experience in this field? Any hint is welcome.

.Andreas

Re: Using Link-Time-Optimization in mingw/gcc: Any experience?

Posted: Thu Aug 01, 2019 11:15 am
by xr_a_y
Here is Minic mingw compile line :

Code: Select all

x86_64-w64-mingw32-g++ -Wall -Wno-char-subscripts -Wno-reorder -DNDEBUG -O3 -flto -march=native --std=c++14 minic.cc -static -static-libgcc -static-libstdc++ -o minic -Wl,-Bstatic -lpthread
Don't know if that helps ...

Re: Using Link-Time-Optimization in mingw/gcc: Any experience?

Posted: Thu Aug 01, 2019 11:42 am
by RubiChess
xr_a_y wrote: Thu Aug 01, 2019 11:15 am Here is Minic mingw compile line :

Code: Select all

x86_64-w64-mingw32-g++ -Wall -Wno-char-subscripts -Wno-reorder -DNDEBUG -O3 -flto -march=native --std=c++14 minic.cc -static -static-libgcc -static-libstdc++ -o minic -Wl,-Bstatic -lpthread
Don't know if that helps ...
I will try. Thanks.

Re: Using Link-Time-Optimization in mingw/gcc: Any experience?

Posted: Thu Aug 01, 2019 5:10 pm
by jdart
I don't use MingW. -flto works fine with gcc. You need to use the gold linker on Linux (-fuse-ld=gold). How effective it is may vary. They have put quite a few improvements into LTO in gcc versions 8 and 9.

--Jon

Re: Using Link-Time-Optimization in mingw/gcc: Any experience?

Posted: Fri Aug 02, 2019 6:15 am
by RubiChess
No luck so far.
It turns out that the call to getline(cin, inputsting) in the UCI input loop causes the segmentation fault when I compile with -static and -flto. Strange. Any bell ringing here?
I could rewrite it using cin >> inputstring or even fgets() which seems to work but I would like to find the real reason.

.Andreas

Re: Using Link-Time-Optimization in mingw/gcc: Any experience?

Posted: Sat Aug 03, 2019 11:29 am
by Ras
RubiChess wrote: Fri Aug 02, 2019 6:15 amIt turns out that the call to getline(cin, inputsting) in the UCI input loop causes the segmentation fault
How does that even compile? According to http://man7.org/linux/man-pages/man3/getline.3.html , the prototype looks like this:

Code: Select all

ssize_t getline(char **lineptr, size_t *n, FILE *stream);

Re: Using Link-Time-Optimization in mingw/gcc: Any experience?

Posted: Sat Aug 03, 2019 12:19 pm
by mar
Ras wrote: Sat Aug 03, 2019 11:29 am How does that even compile? According to http://man7.org/linux/man-pages/man3/getline.3.html , the prototype looks like this:

Code: Select all

ssize_t getline(char **lineptr, size_t *n, FILE *stream);
Because he's using C++ https://en.cppreference.com/w/cpp/strin ... ng/getline

Re: Using Link-Time-Optimization in mingw/gcc: Any experience?

Posted: Sat Aug 10, 2019 9:01 pm
by RubiChess
End of story: I switch to clang which builds static windows binaries combining lto and profile instrumented optimization. This binary is smaller and even slightly faster than the gcc built binary.
Build script is a little tricky using MSVC nmake and some llvm profiling tool that is not included in their windows distribution. But it works somehow.

- Andreas