Failed to compile hello world with clang built from source code

I built llvm+clang+lld from source code in a docker container.

% CC=$HOME/toolchains/bin/gcc CXX=$HOME/toolchains/bin/g++ \
cmake ../llvm \
-G "Unix Makefiles" \
-DLLVM_ENABLE_PROJECTS="clang;lld" \
-DCMAKE_BUILD_TYPE=MinSizeRel \
-DLLVM_TARGETS_TO_BUILD="X86" \
-DCMAKE_CXX_LINK_FLAGS="-Wl,-rpath,$HOME/toolchains/lib64 -L$HOME/toolchains/lib64"
% make -j && sudo make install

Then i try to compile a hello world program with clang.

// test.c
#include <stdio.h>

int main(int argc, char**argv) {
	printf("hello\n");
	return 0;
}

Then I compile the test.c with clang and got following error message.

% clang test.c
**error: unknown target CPU 'x86-64'**

Are there something wrong with my building process?

The llvm version is 11.0.0 release.
The version of gcc under $HOME/toolchains is 5.2.0.
My docker version is:
$sudo docker version
Client:
Version: 17.06.2-ce
API version: 1.30
Go version: go1.8.3
Git commit: e046fc5
Built: Tue Oct 17 02:06:56 2017
OS/Arch: linux/amd64

Server:
Version: 17.06.2-ce
API version: 1.30 (minimum version 1.12)
Go version: go1.8.3
Git commit: e046fc5
Built: Tue Oct 17 02:09:18 2017
OS/Arch: linux/amd64
Experimental: false

This is surprising, try which clang to make sure it executes the one you just built.
Also try clang -v test.c which will print a bit more info (hopefully).

1 Like

Thank you for you reply.
There is only one clang in my system and i think it executes the one.
I tried -v, and i got

% clang -v test.c
clang version 11.0.0
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.2
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.5
Selected GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.5
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
 "/usr/local/bin/clang-11" -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name test.c -mrelocation-model static -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -fno-split-dwarf-inlining -debugger-tuning=gdb -v -resource-dir /usr/local/lib/clang/11.0.0 -internal-isystem /usr/local/include -internal-isystem /usr/local/lib/clang/11.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir /root/test_clang -ferror-limit 19 -fgnuc-version=4.2.1 -fcolor-diagnostics -faddrsig -o /tmp/test-8120b4.o -x c test.c
error: unknown target CPU 'x86-64'

I found it is using a wrong toolchain, and I specified the toolchain I used when building llvm+clang(gcc-5.2.0).
However, the error message remains the same.

% clang -v --gcc-toolchain==/root/toolchains test.c
clang version 11.0.0
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
 "/usr/local/bin/clang-11" -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name test.c -mrelocation-model static -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -fno-split-dwarf-inlining -debugger-tuning=gdb -v -resource-dir /usr/local/lib/clang/11.0.0 -internal-isystem /usr/local/include -internal-isystem /usr/local/lib/clang/11.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir /root/test_clang -ferror-limit 19 -fgnuc-version=4.2.1 -fcolor-diagnostics -faddrsig -o /tmp/test-e9419c.o -x c test.c
error: unknown target CPU 'x86-64'

Is there anything wrong with the “Target: x86_64-unknown-linux-gnu”? The vendor part of the triple is unknown.

No that is fine. When I copy your cc1 invocation on a clang I just built with similar option as you it works for me.

If you’d like to debug, your error comes from here I believe: https://github.com/llvm/llvm-project/blob/main/clang/lib/Basic/Targets.cpp#L649

The “setCPU” method is a virtual and implemented by each target, here for X86: https://github.com/llvm/llvm-project/blob/main/clang/lib/Basic/Targets/X86.h#L326

You can add some print statement around there and rebuild clang. Or build in debug mode and use a debugger here.

./bin/clang  "/usr/local/bin/clang-11" -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name test.c -mrelocation-model static -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -fno-split-dwarf-inlining -debugger-tuning=gdb -v -resource-dir /usr/local/lib/clang/11.0.0 -internal-isystem /usr/local/include -internal-isystem /usr/local/lib/clang/11.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir /root/test_clang -ferror-limit 19 -fgnuc-version=4.2.1 -fcolor-diagnostics -faddrsig -o /tmp/test-e9419c.o -x c test.c
1 Like

I think the function parseArchX86 is not likely to return CK_None, because the input parameter CPU is “x86-64” according to the error message.

I changed this line

to

return CK_x86_64;

and the error message gone.

We’re getting close then :slight_smile:
I’d try to add print in the loop itself for P.Name and CPU to begin with, and for the other param of the check as needed.

I think i found the problem.
The version of gcc used to compile llvm is buggy when processing constexpr objects.
After switching from gcc-5.2.0 to gcc-6.4.0, no error message is printed.

The operator[] overload of FeatureBitset returns a wrong value in gcc-5.2.0

Thanks a lot for your help!! :grin:

Good to know, we claim to support gcc >=5.1.0, seems like we should fix FeatureBitset to workaround this bug, or static_assert against this GCC version.