Skip to content
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

Fix a linker bug with Clang on Windows #3084

Closed
wants to merge 1 commit into from

Conversation

mstembera
Copy link
Contributor

When compiling
make build ARCH=x86-64-bmi2 COMP=clang -j
with Clang version 10.0.1 (latest from MSYS2) on Windows the link step fails with:

benchmark.o: file not recognized: file format not recognized
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [Makefile:844: stockfish] Error 1
make[1]: Leaving directory '/c/Projects/forks/Stockfish/src'
make: *** [Makefile:716: build] Error 2

bench: 3736029

@vondele
Copy link
Member

vondele commented Aug 31, 2020

that's a workaround for a compiler bug, right?
@gvreuls can you test this as well?

@mstembera
Copy link
Contributor Author

Yes.

@vondele
Copy link
Member

vondele commented Aug 31, 2020

BTW, does this influence compilation on macOS?

@ArminHHJ
Copy link

I do a compile on macOS (10.16.6) yesterday and some minutes bevor.
make build ARCH=x86-64-bmi2 COMP=clang

No problems, binary running :)

@mstembera mstembera force-pushed the ClangWin branch 2 times, most recently from 4cc11fd to 4a19eb3 Compare August 31, 2020 06:42
@mstembera
Copy link
Contributor Author

@vondele Thanks. It probably would have affected maOS. New version pushed.
I also wonder about lines 606 and 608(in master). Seems like they should be nested the same way I nested them in the patch.

@gvreuls
Copy link
Contributor

gvreuls commented Aug 31, 2020

@mstembera

I also wonder about lines 606 and 608(in master). Seems like they should be nested the same way I nested them in the patch.

https://github.com/official-stockfish/Stockfish/blob/e0bafa1911ede61b9268e0b461a5d8856d6cd6be/src/Makefile#L606
Means "if the string MINGW is found in $(KERNEL) then ..." (literally if the result of searching the string MINGW in $(KERNEL) is not empty then ...)

So basically lines 606 and 608 are an or condition
if "MINGW" or "MSYS" is found in $(KERNEL) then add -save-temps to CXXFLAGS

What you do is an and condition:
if "MINGW" and "MSYS" are found in $(KERNEL) then add -flto=thin to CXXFLAGS

Because $(KERNEL) either contains "MINGW" (when run from the MSYS shell) or "MSYS" (when run from the Windows CMD shell) but never both, your condition can never be true so this basically never adds -flto=thin to CXXFLAGS.

If you want to add -flto=thin to CXXFLAGS only if $(KERNEL) neither contains "MINGW" nor "MSYS" you do:

        ifeq ($(findstring MINGW,$(KERNEL)),)
        ifeq ($(findstring MSYS,$(KERNEL)),)
            CXXFLAGS += -flto=thin
        endif
        endif

(notice the change from ifneq to ifeq)

Edit: about the indentation: The convention is that if several ifeq/ifneq are part of the same condition but can't be written on a single line (because make doesn't have real && and || operators) you don't indent.

@snicolet
Copy link
Member

Maybe we should not use link time optimization at all: we keep patching SF source and Makefile to workaround around buggy lto compiler implementations :-(

@vondele
Copy link
Member

vondele commented Aug 31, 2020

AFAIK, most issues have been windows related. So fixing #2958 would help.

@mstembera
Copy link
Contributor Author

@gvreuls Thank you for taking the time to explain. Fixed.

@erbsenzaehler
Copy link

Using the llvm linker with adding the line -fuse-ld=lld to the linker parameters does work for me on msys2.

@mstembera
Copy link
Contributor Author

@erbsenzaehler Using CXXFLAGS += -flto=thin -fuse-ld=lld I get this error:
clang++: error: invalid linker name in argument '-fuse-ld=lld'
Do I need something else to invoke the llvm linker?

@erbsenzaehler
Copy link

The linker is not installed by default. You can get it with pacman -Syu mingw-w64-x86_64-lld

@mstembera
Copy link
Contributor Author

@erbsenzaehler Thanks! I can confirm that works. I leave it to @vondele whether we want to depend on the lld linker being installed or disable lto.

@vondele
Copy link
Member

vondele commented Aug 31, 2020

I would think that since clang on mingw is anyway not the default, we can expect users of clang to install the needed linker for a functional toolchain. Not so different from the situation on ubuntu where just installing clang++ is not enough needs llvm installed as well. So, I would tend to add the correct lld flag instead of disabling lto. @mstembera can you do that?

@mstembera
Copy link
Contributor Author

Done. However should it be added for Windows only?

@mstembera
Copy link
Contributor Author

Ok Appveyor didn't like the flag so windows only.

@vondele vondele added the to be merged Will be merged shortly label Sep 1, 2020
@vondele vondele closed this in a057f17 Sep 1, 2020
lucabrivio pushed a commit to lucabrivio/Stockfish that referenced this pull request Sep 1, 2020
other linkers might fail to link during the LTO phase.

The linker might have to be installed using
`pacman -Syu mingw-w64-x86_64-lld`

closes official-stockfish/Stockfish#3084

No functional change.
Dantist pushed a commit to Dantist/Stockfish that referenced this pull request Dec 22, 2020
other linkers might fail to link during the LTO phase.

The linker might have to be installed using
`pacman -Syu mingw-w64-x86_64-lld`

closes official-stockfish#3084

No functional change.
BM123499 pushed a commit to BM123499/Stockfish that referenced this pull request Feb 22, 2021
other linkers might fail to link during the LTO phase.

The linker might have to be installed using
`pacman -Syu mingw-w64-x86_64-lld`

closes official-stockfish#3084

No functional change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to be merged Will be merged shortly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants