clang compiler

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

micron
Posts: 155
Joined: Mon Feb 15, 2010 9:33 am
Location: New Zealand

clang compiler

Post by micron »

http://clang.llvm.org/
Clang has link-time optimization (with option -flto), but not profile-guided optimization (pgo).
Below are some comparisons with gcc for building and benchmarking Stockfish, in which clang comes out well.

gcc, no pgo

Code: Select all

$ time make ARCH=x86-64-modern
real	0m16.551s
user	0m14.981s
sys	 0m1.218s

$ stockfish bench 32 1 15 default depth > /dev/null
Total time (ms) : 27595
Nodes searched  : 34823328
Nodes/second    : 1261943
gcc with pgo

Code: Select all

$ time make popcnt-profile-build ARCH=x86-64-modern
real	0m59.756s
user	0m55.749s
sys 	0m3.657s

$ stockfish bench 32 1 15 default depth > /dev/null
Total time (ms) : 26887
Nodes searched  : 34823328
Nodes/second    : 1295173
clang with link-time optimization

Code: Select all

$ time make ARCH=x86-64-modern COMP=clang
real	0m13.593s
user	0m12.215s
sys	 0m0.780s

$ stockfish bench 32 1 15 default depth > /dev/null
Total time (ms) : 25910
Nodes searched  : 34823328
Nodes/second    : 1344011
tvrzsky
Posts: 128
Joined: Sat Sep 23, 2006 7:10 pm
Location: Prague

Re: clang compiler

Post by tvrzsky »

Interesting, which version of gcc it is? And the build with no pgo was -O3? Any other options?
micron
Posts: 155
Joined: Mon Feb 15, 2010 9:33 am
Location: New Zealand

Re: clang compiler

Post by micron »

gcc version 4.2.1 (Apple Inc. build 5666) (dot 3)

The gcc build settings derive from SF's makefile, so that the options are

Code: Select all

g++ -g -Wall -Wcast-qual -fno-exceptions -fno-rtti  -ansi -pedantic -Wno-long-long -Wextra -DNDEBUG -O3 -DIS_64BIT -msse -DUSE_BSFQ -DUSE_POPCNT   -c -o bitboard.o bitboard.cpp
I hacked the makefile to add clang as another compiler, with -O3 and -flto.

Robert P.
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: clang compiler

Post by mcostalba »

micron wrote:gcc version 4.2.1 (Apple Inc. build 5666) (dot 3)

The gcc build settings derive from SF's makefile, so that the options are

Code: Select all

g++ -g -Wall -Wcast-qual -fno-exceptions -fno-rtti  -ansi -pedantic -Wno-long-long -Wextra -DNDEBUG -O3 -DIS_64BIT -msse -DUSE_BSFQ -DUSE_POPCNT   -c -o bitboard.o bitboard.cpp
I hacked the makefile to add clang as another compiler, with -O3 and -flto.

Robert P.
Could you please post the modified version (only the modified part) ?

I am not able to test myself, so I would like to ask if you see some warnings in the compiler output.

Thanks
Marco
micron
Posts: 155
Joined: Mon Feb 15, 2010 9:33 am
Location: New Zealand

Re: clang compiler

Post by micron »

clang is intended as a drop-in replacement for gcc, and so adding it to SF's makefile is little more than copy, paste, change 'gcc' to 'clang'...

ifeq ($(comp),gcc)
...
endif

ifeq ($(comp),clang)
... [as for gcc]
endif

One difference is:
CXX=clang++

The other difference is link time optimization.

Code: Select all

	ifeq ($(comp),gcc)
		CXXFLAGS += -O3
      ...
	endif

	ifeq ($(comp),clang)
		CXXFLAGS += -O3 -flto
      ...
	endif
There were no warnings from clang.
Tord Romstad
Posts: 1808
Joined: Wed Mar 08, 2006 9:19 pm
Location: Oslo, Norway

Re: clang compiler

Post by Tord Romstad »

micron wrote:gcc version 4.2.1 (Apple Inc. build 5666) (dot 3)
That's an extremely old GCC version. GCC 4.2.1 was released in July 2007. The current version is 4.5.2, and a release candidate for 4.6 was released just a couple of weeks ago.

Comparing a recent version of clang to an almost four year old version of GCC is quite meaningless, especially when you enable link-time optimization in clang. GCC 4.2.1 does not support link-time optimization, but 4.5 does. It is possible that clang would still come out ahead in a direct comparison to the latest GCC version, but I doubt it.
Dann Corbit
Posts: 12540
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: clang compiler

Post by Dann Corbit »

Tord Romstad wrote:
micron wrote:gcc version 4.2.1 (Apple Inc. build 5666) (dot 3)
That's an extremely old GCC version. GCC 4.2.1 was released in July 2007. The current version is 4.5.2, and a release candidate for 4.6 was released just a couple of weeks ago.

Comparing a recent version of clang to an almost four year old version of GCC is quite meaningless, especially when you enable link-time optimization in clang. GCC 4.2.1 does not support link-time optimization, but 4.5 does. It is possible that clang would still come out ahead in a direct comparison to the latest GCC version, but I doubt it.
I recently saw a comparison (admittedly not chess programming) but standard GCC positively smoked clang in every test:
http://www.phoronix.com/scan.php?page=a ... _gcc&num=1

Now, this is not a recent test either, but from logical inference (clang uses a virtual machine) it does not seem feasible to me for it to be equally fast.

On the other hand, I would love to be pleasanly surprised by a virtual machine that can smoke truly compiled code. But, after all, we do not expect Java, C# or other virtual machine languages to suddenly start outperforming C.

I do suspect that the LLVM might be a great engine for interpreted things like Java and VB.NET but I would be shocked if it ever beats native optimized assembly output.
Teemu Pudas
Posts: 88
Joined: Wed Mar 25, 2009 12:49 pm

Re: clang compiler

Post by Teemu Pudas »

Dann Corbit wrote:I recently saw a comparison (admittedly not chess programming) but standard GCC positively smoked clang in every test:
http://www.phoronix.com/scan.php?page=a ... _gcc&num=1
Not a test of clang, GCC lost twice and there were two chess benchmarks (Crafty and TSCP).
wgarvin
Posts: 838
Joined: Thu Jul 05, 2007 5:03 pm
Location: British Columbia, Canada

Re: clang compiler

Post by wgarvin »

Dann Corbit wrote:Now, this is not a recent test either, but from logical inference (clang uses a virtual machine) it does not seem feasible to me for it to be equally fast.
clang is just the compiler frontend project. Its a fully native compiler thats just using LLVM as the backend code generator and optimizer.

Before clang was written, there used to be a fork or something of gcc that used LLVM for code gen [edit: called LLVM-GCC, I guess]. They made clang because gcc's front end is old and crufty. clang is faster, uses less memory, and can supposedly report more useful diagnostics. The source is also (apparently) much easier to change, or to re-use in other projects such as IDEs or code analysis tools etc.

The LLVM project is a bunch of infrastructure that can also be used to make runtime compilers (like JITs for a virtual machine environment) but the clang compiler being benchmarked above generates native code just like gcc, that doesnt need or use a virtual machine environment at runtime.
I recently saw a comparison (admittedly not chess programming) but standard GCC positively smoked clang in every test:
http://www.phoronix.com/scan.php?page=a ... _gcc&num=1
That comparison also uses the same four-year-old gcc version 4.2.1.
rbarreira
Posts: 900
Joined: Tue Apr 27, 2010 3:48 pm

Re: clang compiler

Post by rbarreira »

I was quite impressed with gcc 4.5.0, in my benchmarks its generated code was as fast as with the newest Intel Compiler on Intel CPUs (and of course much faster on AMD CPUs, due to the purposefully bad performance icc has there).