Clang

Clang /ˈklæŋ/[5] is a compiler front end for the C, C++, Objective-C and Objective-C++ programming languages, as well as the OpenMP,[6] OpenCL, RenderScript, CUDA and HIP[7] frameworks. It uses the LLVM compiler infrastructure as its back end and has been part of the LLVM release cycle since LLVM 2.6.

Clang
Clang being used to compile Chicken
Original author(s)Chris Lattner
Developer(s)LLVM Developer Group
Initial releaseSeptember 26, 2007 (2007-09-26)[1]
Stable release
11.0.1[2] / January 6, 2021 (2021-01-06)
Repository
Written inC++
Operating systemUnix-like
PlatformLLVM (ARMv7, AArch64, IA-32, x64, ppc64le)[3]
TypeCompiler
License
Websiteclang.llvm.org

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.[8][9] Its contributors include Apple, Microsoft, Google, ARM, Sony, Intel and Advanced Micro Devices (AMD). It is open-source software,[10] with source code released under the University of Illinois/NCSA License, a permissive free software licence. Since v9.0.0, it was relicensed to the Apache License 2.0 with LLVM Exceptions.[4]

Clang 11, the latest major version of Clang as of January 2021, has full support for all published C++ standards and partial support for the upcoming C++20 standard.[11] Since v6.0.0, Clang compiles C++ using the GNU++14 dialect by default, which includes features from the C++14 standard and conforming GNU extensions.[12]

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

Background

Starting in 2005, Apple Inc. made extensive use of LLVM in a number of commercial products,[14] including the iOS SDK and Xcode 3.1.

One of the first uses of LLVM was an OpenGL code compiler for OS X that converts OpenGL calls into more fundamental calls for graphics processing units (GPU) that do not support certain features. This allowed Apple to support OpenGL on computers using Intel Graphics Media Accelerator (GMA) chipsets, increasing performance on those machines.[15] For GPUs that support it, the code is compiled to fully exploit the underlying hardware, but on GMA machines, LLVM compiles the same OpenGL code into subroutines to ensure continued proper function.

LLVM was intended originally to use GCC's front end, but GCC turned out to cause some problems for developers of LLVM and at Apple. The GCC source code is a large and somewhat cumbersome system for developers to work with; as one long-time GCC developer put it referring to LLVM, "Trying to make the hippo dance is not really a lot of fun".[16]

Apple software makes heavy use of Objective-C, but the Objective-C front-end in GCC is a low priority for GCC developers. Also, GCC does not integrate smoothly into Apple's integrated development environment (IDE).[17] Finally, GCC is licensed under the terms of GNU General Public License (GPL) version 3, which requires developers who distribute extensions for, or modified versions of, GCC to make their source code available, whereas LLVM has a BSD-like license[18] which does not have such a requirement.

Apple chose to develop a new compiler front end from scratch, supporting C, Objective-C and C++.[17] This "clang" project was open-sourced in July 2007.[19]

Design

Clang is intended to work on top of LLVM.[18] 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. This is one reason why most OpenCL implementations are built with Clang and LLVM.

One of Clang's main goals is to provide a library-based architecture,[20] to allow the compiler to be more tightly tied to tools that interact with source code, such as an integrated development environment (IDE) 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. For instance, GCC uses a step called fold that is key to the overall compile process, which has the side effect of translating the code tree into a form that looks unlike the original source code. If an error is found during or after the fold step, it can be difficult to translate that back into one location in the original source. Also, vendors using the GCC stack within IDEs use separate tools to index the code, to provide features like syntax highlighting and autocomplete.

Clang is designed to retain more information during the compiling process than GCC, and to preserve the overall form of the original code. The goal of this is to make it easier to map errors back into the original source. The error reports offered by Clang are also aimed to be more detailed and specific, as well as machine-readable, so IDEs can index the output of the compiler during compiling. Modular design of the compiler can offer source code indexing, syntax checking, and other features normally associated with rapid application development systems. The parse tree is also more suitable for supporting automated code refactoring, as it directly represents the original source code.

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. In many cases, Clang can be used or swapped out for GCC as needed, with no other effects on the toolchain as a whole. It supports most of the commonly used GCC options. A sub-project Flang by Nvidia and The Portland Group added Fortran support.[21]

Performance and GCC compatibility

Clang is designed to be highly compatible with GCC.[9] 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 implements many GCC compiler intrinsics purely for compatibility. For example, even though Clang implements atomic intrinsics which correspond exactly with C11 atomics, it also implements GCC's __sync_* intrinsics for compatibility with GCC and libstdc++. Clang also maintains ABI compatibility with GCC-generated object code. In practice Clang can often be used as a drop-in replacement for GCC.[22]

Clang's developers aim to reduce memory footprint and increase compilation speed compared to competing compilers, such as GCC. In October 2007, they report that Clang compiled the Carbon libraries more than twice as fast as GCC, while using about one-sixth GCC's memory and disk space.[23] However, as of 2011 this was not a typical result.[24][25] As of mid-2014, Clang won more than a third of the benchmarks, with GCC winning most.[26] As of 2014, performance of Clang-compiled programs lagged behind performance of the GCC-compiled program, sometimes by large factors (up to 5.5x),[26] replicating earlier reports of slower performance.[24]

More recent comparisons in November 2016 indicate that both compilers have evolved to increase their performance. As of GCC 4.8.2 versus clang 3.4, on a large harness of test files, GCC outperforms clang by approximately 17% on well-optimized source code. Test results are code-specific, and unoptimized C source code can reverse such differences. The two compilers now seem broadly comparable.[27]

As of 2019, comparison on Intel Ice Lake has shown that Clang 10 has achieved 96% of the performance of GCC 10 over 41 different benchmarks (while winning 22 and losing 19 out of them).[28]

Status history

This table presents only significant steps and releases in Clang history.

Date Highlights
11 July 2007Clang frontend released under open-source licence
25 February 2009Clang/LLVM can compile a working FreeBSD kernel.[29][30]
16 March 2009Clang/LLVM can compile a working DragonFly BSD kernel.[31][32]
23 October 2009Clang 1.0 released, with LLVM 2.6 for the first time.
December 2009Code generation for C and Objective-C reach production quality. Support for C++ and Objective-C++ still incomplete. Clang C++ can parse GCC 4.2 libstdc++ and generate working code for non-trivial programs,[18] and can compile itself.[33]
2 February 2010Clang self-hosting.[34]
20 May 2010Clang latest version built the Boost C++ libraries successfully, and passed nearly all tests.[35]
10 June 2010Clang/LLVM becomes integral part of FreeBSD, but default compiler is still GCC.[36]
25 October 2010Clang/LLVM can compile a working modified Linux kernel.[37]
January 2011Preliminary work completed to support the draft C++0x standard, with a few of the draft's new features supported in Clang development version.[38][11]
10 February 2011Clang can compile a working HotSpot Java virtual machine.[24]
19 January 2012Clang becomes an optional component in NetBSD cross-platform build system, but GCC is still default.[39]
29 February 2012Clang 3.0 can rebuild 91.2% of the Debian archive.[40]
29 February 2012Clang becomes default compiler in MINIX 3[41]
12 May 2012Clang/LLVM announced to replace GCC in FreeBSD.[42]
5 November 2012Clang becomes default compiler in FreeBSD 10.x on amd64/i386.[43]
18 February 2013Clang/LLVM can compile a working modified Android Linux Kernel for Nexus 7.[44][45]
19 April 2013Clang is C++11 feature complete.[46]
6 November 2013Clang is C++14 feature complete.[47]
11 September 2014Clang 3.5 can rebuild 94.3% of the Debian archive. The percentage of failures has dropped by 1.2% per release since January 2013, mainly due to increased compatibility with GCC flags.[48]
October 2016 Clang becomes default compiler for Android[49] (and later only compiler supported by Android NDK[50]).
13 March 2017 Clang 4.0.0 released
26 July 2017Clang becomes default compiler in OpenBSD 6.2 on amd64/i386.[51]
7 September 2017 Clang 5.0.0 released
19 January 2018Clang becomes default compiler in OpenBSD 6.3 on arm.[52]
5 March 2018 Clang is now used to build Google Chrome for Windows.[53] Mozilla does the same change for Firefox in September same year.[54]
8 March 2018 Clang 6.0.0 released
19 September 2018 Clang 7.0.0 released
20 March 2019 Clang 8.0.0 released
1 July 2019Clang becomes default compiler in OpenBSD 6.6 on mips64.[55]
19 September 2019 Clang 9.0.0 released with official RISC-V target support.[56]
29 February 2020 Clang becomes the only C compiler in the FreeBSD base system, with the removal of GCC.[57]
24 March 2020 Clang 10.0.0 released
2 April 2020Clang becomes default compiler in OpenBSD 6.7 on powerpc.[58]
12 October 2020 Clang 11.0.0 released
21 December 2020Clang becomes default compiler in OpenBSD 6.9 on mips64el.[59]

See also

References

  1. "LLVM 2.1 Release Notes".
  2. "Download LLVM releases".
  3. LLVM Download Page
  4. LICENSE.TXT, llvm.org, retrieved September 24, 2019
  5. Christopher, Eric (July 3, 2008). "simply wonder pronunciation of Clang". LLVMdev (Mailing list). Retrieved September 22, 2015.
  6. "OpenMP Support". LLVM Project Blog. Retrieved March 28, 2016.
  7. "C++ Heterogeneous-Compute Interface for Portability". Retrieved February 18, 2020.
  8. "Clang Language Extensions". Retrieved November 8, 2017. In addition to the language extensions listed here, Clang aims to support a broad range of GCC extensions.
  9. Clang - Features and Goals: GCC Compatibility, April 15, 2013
  10. Clang "Getting started" instructions, Clang.llvm.org, retrieved September 18, 2012
  11. "C++ and C++'0x Support in Clang". LLVM.
  12. {cite web|publisher=LLVM|url=https://releases.llvm.org/6.0.0/tools/clang/docs/ReleaseNotes.html%7Ctitle=Clang 6.0.0 Release Notes}}
  13. "Clang Static Analyzer". LLVM. Retrieved September 3, 2009.
  14. Treat, Adam (February 19, 2005). "mkspecs and patches for LLVM compile of Qt4". Qt4-preview-feedback (Mailing list). Archived from the original on October 4, 2011.
  15. Lattner, Chris (May 25, 2007). LLVM for OpenGL and other stuff (Slides). LLVM Developers' Meeting.
  16. Zadeck, Kenneth (November 19, 2005). "Re: LLVM/GCC Integration Proposal". GCC development (Mailing list).
  17. Naroff, Steve (May 25, 2007). New LLVM C Front-end (Slides). LLVM Developers' Meeting.
  18. Clang team, clang: a C language family frontend for LLVM
  19. Lattner, Chris (July 11, 2007). "New LLVM C front-end: "clang"". cfe-dev (Mailing list).
  20. Clang: Utility and Applications: Library Based Architecture
  21. FLANG: NVIDIA Brings Fortran To LLVM
  22. Clang can often be used as a drop-in replacement for GCC, May 2020
  23. Clang - Features and Goals: Fast compiles and Low Memory Use, October 2007
  24. Simonis, Volker (February 10, 2011). "Compiling the HotSpot VM with Clang". Archived from the original on February 18, 2011. Retrieved February 13, 2011. While the overall GCC compatibility is excellent and the compile times are impressive, the performance of the generated code is still lacking behind a recent GCC version.
  25. "Benchmarking LLVM & Clang Against GCC 4.5". Phoronix. April 21, 2010. Retrieved February 13, 2011. Binaries from LLVM-GCC and Clang both struggled to compete with GCC 4.5.0 in the timed HMMer benchmark of a Pfam database search. LLVM-GCC and Clang were about 23% slower(...)Though LLVM / Clang isn't the performance champion at this point, both components continue to be under very active development and there will hopefully be more news to report in the coming months
  26. "GCC 4.9 VS. LLVM Clang 3.5 Linux Compiler Benchmarks". OpenBenchmarking.org. April 14, 2014. Retrieved June 25, 2014.
  27. "Clang vs GCC - which produces better binaries?". stackoverflow.com. Retrieved October 1, 2018.
  28. Michael Larabel (December 23, 2019). "LLVM Clang Achieves ~96% The Performance Of GCC On Intel Ice Lake".
  29. Divacky, Roman. "[Announce] clang/llvm can compile booting FreeBSD kernel on i386/amd64".
  30. Building FreeBSD with Clang, Wiki.freebsd.org, August 24, 2012, retrieved September 18, 2012
  31. Hornung, Alex. "llvm/clang once more".
  32. Clang, DragonFly BSD, retrieved September 18, 2012
  33. "Clang can compile LLVM and Clang". LLVM Project Blog.
  34. "Clang Successfully Self-Hosts". LLVM Project Blog.
  35. Gregor, Doug. "Clang++ Builds Boost!". LLVM Project Blog.
  36. Davis, Brad. "FreeBSD Status Reports April–June , 2010".
  37. Clang builds a working Linux Kernel (Boots to RL5 with SMP, networking and X, self hosts), Lists.cs.uiuc.edu, archived from the original on September 7, 2015, retrieved September 18, 2012
  38. Gregor, Douglas (January 26, 2011). "New C++0x feature support in Clang" (Mailing list). Archived from the original on January 30, 2011. Retrieved January 29, 2011.
  39. Sonnenberger, Jörg (January 19, 2012). "Status of NetBSD and LLVM". Archived from the original on September 21, 2016. Retrieved February 26, 2014.
  40. Ledru, Sylvestre. "Rebuild of the Debian archive with clang".
  41. "Official Minix 3 website: News".
  42. Gerzo, Daniel (May 12, 2012). "FreeBSD Quarterly Status Report January–March , 2012" (Mailing list). Retrieved May 14, 2012.
  43. Davis, Brooks (November 5, 2012). "Heads Up: Clang now the default on x86" (Mailing list). Retrieved November 7, 2012.
  44. Webster, Behan (February 18, 2013). "LLVMLinux: Compiling Android with LLVM" (PDF). Retrieved May 11, 2013.
  45. Tinti, Vinicius (March 17, 2013). "LLVMLinux: Nexus 7". Retrieved May 11, 2013.
  46. Du Toit, Stefanus. "Clang is C++11 feature complete as of *just now*!".
  47. "[llvm-project] Revision 194194".
  48. Ledru, Sylvestre. "Rebuild of Debian using Clang 3.5.0".
  49. "NDK Revision History | Android Developers". developer.android.com. Retrieved March 6, 2018.
  50. "NDK Revision History | Android NDK | Android Developers". Android Developers. Retrieved April 30, 2018.
  51. Nagy, Robert (July 26, 2017). "switch the default compiler on amd64 and i386 to clang" (Mailing list). Retrieved September 30, 2017.
  52. Kettenis, Mark (January 19, 2018). "Switch the default compiler on armv7 to clang" (Mailing list). Retrieved August 27, 2020.
  53. "Clang is now used to build Chrome for Windows". blog.llvm.org. Retrieved April 7, 2018.
  54. "1443590 - Use clang-cl for Windows builds we ship to users". bugzilla.mozilla.org. Retrieved April 7, 2018.
  55. Hankala, Visa (July 1, 2019). "Switch the default compiler on octeon to clang" (Mailing list). Retrieved August 27, 2020.
  56. "LLVM 9 Releases With Official RISC-V Target Support, Asm Goto, Clang 9, And More". riscv.org. September 26, 2019. Retrieved September 26, 2019.
  57. "FreeBSD svn commit r358454".
  58. Kettenis, Mark (April 2, 2020). "Switch the default compiler on powerpc to clang" (Mailing list). Retrieved August 27, 2020.
  59. Hankala, Visa (December 21, 2020). "Make clang the default compiler on loongson" (Mailing list). Retrieved January 1, 2021.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.