Clang Compiler


 


 

 

Introduction

Clang is a compiler front end for the C, C++, Objective-C and Objective-C++ programming languages, as well as the OpenMP, OpenCL, RenderScript, CUDA and HIP frameworks. It uses the LLVM compiler infrastructure as its back-end[2].

It is designed to act as a drop-in replacement for the GNU Compiler Collection (GCC), supporting most of its compilation flags and unofficial language extensions. Its contributors include Apple, Microsoft, Google, ARM, Sony, Intel and Advanced Micro Devices (AMD). It is open-source software, with source code[1].

Clang 12, the latest major version of Clang as of November 2020, has full support for all published C++ standards.

The Clang project includes the Clang front end, a static analyzer, and several code analysis tools[2].

 

 

What is LLVM Infrastructure?

The LLVM compiler infrastructure project is a set of compiler and toolchain technologies, which can be used to develop a front end for any programming language and a back end for any instruction set architecture. LLVM has little to do with traditional virtual machines. Thus stands for “Low Level Virtual Machine”[2].

Clang is an "LLVM native" C/C++/Objective-C compiler, which aims to deliver amazingly fast compiles, extremely useful error and warning messages and to provide a platform for building great source level tools. The Clang Static Analyzer and Clang-tidy are tools that automatically find bugs in your code, and are great examples of the sort of tools that can be built using the Clang frontend as a library to parse C/C++ code[1].

 

 

Design

Clang is intended to work on top of LLVM. The combination of Clang and LLVM provides most of the toolchain, to allow replacing the full GCC stack. Because it is built with a library-based design, like the rest of LLVM, Clang is easy to embed into other applications[1].

One of Clang's main goals is to provide a library-based architecture, to allow the compiler to be more tightly tied to tools that interact with source code, such as an Integrated Development Environment (IDE) and Graphical User Interface (GUI). In contrast, GCC is designed to work in a compile-link-debug workflow, and integrating it with other tools is not always easy[1].

Clang compiles only C-like languages, such as C, C++, Objective-C, Objective-C++, OpenCL, CUDA, and HIP. For other languages, like Ada, LLVM remains dependent on GCC or another compiler frontend[2].



Performance & GCC Compatibility

Clang is designed to be highly compatible with GCC. Clang's command-line interface is similar to and shares many flags and options with GCC. Clang implements many GNU language extensions and enables them by default. Clang's developers aim to reduce memory footprint and increase compilation speed compared to competing compilers, such as GCC[i].

 

Test Server

Architecture: x86_64
Processor: Intel (R) Xeon (R) Platinum 8163 CPU @ 2.50 GHz
L1 data cache: 32 KB
L2 cache: 1,024 KB
L3 cache: 33,792 KB
Memory: 800 GB
Operating system: Alibaba Group Enterprise Linux Server release 7.2 (Paladin)
Kernel: 4.9.151–015.ali3000.alios7.x86_64
Compiler: Clang/LLVM 8.0 GCC8.3.1

 

Compilation Performance Comparison

The GCC compilation process is as follows: read the source file, pre-process the source file, convert it into an IR, optimize and generate an assembly file. Then the assembler generates an object file. Clang and LLVM do not rely on independent compilers, but integrate self-implemented compilers at the backend. The process of generating assembly files is omitted in the process of generating object files. The object file is generated directly from the IR. Besides, compared with the GCC IR, the data structure of LLVM IR is more concise. It occupies less memory during compilation and supports faster traversal. Therefore, Clang and LLVM are advantageous in terms of the compilation time, which is proven by the data obtained from SPEC compilation, as shown in the figure below. Clang reduces the single-thread compilation time by 5% to 10% compared with GCC. Therefore, Clang offers more advantages for the construction of large projects[i].


Performance Comparison Graph[i]


Features & Goals

 

End-User Features:

  • Fast compiles and low memory use
  • Expressive diagnostics
  • GCC compatibility [2]

 

Internal Design and Implementation:

  • A real-world, production quality compiler
  • A simple and hackable code base
  • A single unified parser for C, Objective C, C++, and Objective C++
  • Conformance with C/C++/Objective C and their variants[2]

 

 

Advantages of Clang and LLVM

  • Emerging languages are using the LLVM frameworks, such as Swift, Rust, Julia, and Ruby[i].
  • Clang and LLVM comply with the C and C ++ standards more strictly than GCC. GNU Inline and other problems during GCC upgrade do not occur[i].
  • Clang also supports some extensions, such as attributes for thread security check[i].
  • Clang provides additional useful tools, such as scan-build and clang static analyzer for static analysis, clang-format and clang-tidy for syntax analysis, as well as the editor plug-in Clangd[i].
  • Clang provides more accurate and friendly diagnostic information, and highlights error messages, error lines, error line prompts, and repair suggestions. Clang regards the diagnostic information as a feature. The diagnostic information began to be improved only from GCC 5.0, and became mature in GCC 8[i].


References

1]    https://en.wikipedia.org/wiki/Clang

2]    http://clang.llvm.org/features.html

 

Credits To

[i]    https://alibaba-cloud.medium.com/gcc-vs-clang-llvm-an-in-depth-comparison-of-c-c-compilers-a516597d5994 

for Test Server info, Performance Comparison info and Graph and also advantages.



Comments