Sunday, July 12, 2009

s/GCC/Clang

I've been using Clang as a drop-in replacement for GCC for compiling C
code and so far it has been pretty good. While I did not see any
drastic increase in performance of the generated code, the compile
times have come down considerably. For example, compiling a fresh tree
of the latest emacs pretest took 1m55s using GCC on my quad-core
box. The same test with Clang took just over a minute. Other things
that I've built using Clang that are working well without any problems
include: GNU Smalltalk, cmus, tmux and a whole bunch of stuff.

Clang is a C/C++ and Objective-C frontend for the LLVM "compiler
infrastructure". As of now, the C and Objective-C frontends have
reached production quality on x86 and x86-64. The C++ frontend is no
way near completion and is not expected to be complete in the near
future.

Apart from the compilation speed one other feature that is in favour
of Clang is the shockingly expressive diagnostics. Along with the
message, the exact code fragment that is causing the problem is also
shown. If the C++ frontend is able to provide this feature and I'm
sure it will as it is a fundamental goal, this feature alone is going
to propel the adoption of Clang. GCC sure does have its place thanks
to the support of other languages like Ada, Fortran, Java, Pascal
etc. However Clang improving at this rate is surely going to supplant
GCC atleast on the primary hardware platforms. The BSDs in particular
are quite keen on replacing GCC as is evident from the funding of the
development of the Portable C compiler. With Clang already compiling
the kernel source and 99% of the userland, FreeBSD is seriously
considering replacing GCC with Clang in the base system. I think it is
just a matter of time.

An excellent intro to Clang can be found here:

Clang features can be found here: http://clang.llvm.org/features.html

Instructions for building Clang can be found in the Clang Getting Started
page. In addition to the instructions given there, I added
--enable-optimized to the configure script when building llvm to avoid
building the debug versions as they tend to take up a lot of disk
space.

Building programs with Clang is easy. It comes with a gcc compatible
driver called "clang" so all you need to do is to make sure
llvm/Release/bin is in PATH and run configure as

CC=clang ./configure

1 comments:

Anonymous said...

Would love to see how Objective-c front-end looks on linux, if thats what you mean by x84/64.