LLVM explained

LLVM
Logo Caption:The LLVM logo, a stylized wyvern[1]
Author:Chris Lattner, Vikram Adve
Developer:LLVM Developer Group
Programming Language:C++
Operating System:Cross-platform
Genre:Compiler
License:UIUC (BSD-style)
Apache License 2.0 with LLVM Exceptions (v9.0.0 or later)[2]

LLVM is a set of compiler and toolchain technologies[3] that can be used to develop a frontend for any programming language and a backend for any instruction set architecture. LLVM is designed around a language-independent intermediate representation (IR) that serves as a portable, high-level assembly language that can be optimized with a variety of transformations over multiple passes.[4] The name LLVM originally stood for Low Level Virtual Machine, though the project has expanded and the name is no longer officially an initialism.

LLVM is written in C++ and is designed for compile-time, link-time, runtime, and "idle-time" optimization. Originally implemented for C and C++, the language-agnostic design of LLVM has since spawned a wide variety of frontends: languages with compilers that use LLVM (or which do not directly use LLVM but can generate compiled programs as LLVM IR) include ActionScript, Ada, C# for .NET,[5] [6] [7] Common Lisp, PicoLisp, Crystal, CUDA, D, Delphi, Dylan, Forth,[8] Fortran, FreeBASIC, Free Pascal, Halide, Haskell, Java bytecode, Julia, Kotlin, LabVIEW's G language,[9] [10] Lua, Objective-C, OpenCL,[11] PostgreSQL's SQL and PLpgSQL,[12] Ruby,[13] Rust,[14] Scala,[15] [16] Swift, Xojo, and Zig.

History

The LLVM project started in 2000 at the University of Illinois at Urbana–Champaign, under the direction of Vikram Adve and Chris Lattner. LLVM was originally developed as a research infrastructure to investigate dynamic compilation techniques for static and dynamic programming languages. LLVM was released under the University of Illinois/NCSA Open Source License,[2] a permissive free software licence. In 2005, Apple Inc. hired Lattner and formed a team to work on the LLVM system for various uses within Apple's development systems. LLVM has been an integral part of Apple's Xcode development tools for macOS and iOS since Xcode 4 in 2011.[17]

In 2006, Lattner started working on a new project named Clang. The combination of Clang frontend and LLVM backend is named Clang/LLVM or simply Clang.

The name LLVM was originally an initialism for Low Level Virtual Machine. However, the LLVM project evolved into an umbrella project that has little relationship to what most current developers think of as a virtual machine. This made the initialism "confusing" and "inappropriate", and since 2011 LLVM is "officially no longer an acronym",[18] but a brand that applies to the LLVM umbrella project.[19] The project encompasses the LLVM intermediate representation (IR), the LLVM debugger, the LLVM implementation of the C++ Standard Library (with full support of C++11 and C++14[20]), etc. LLVM is administered by the LLVM Foundation. Compiler engineer Tanya Lattner became its president in 2014[21] and was in post .[22]

"For designing and implementing LLVM", the Association for Computing Machinery presented Vikram Adve, Chris Lattner, and Evan Cheng with the 2012 ACM Software System Award.[23]

The project was originally available under the UIUC license. After v9.0.0 released in 2019,[24] LLVM relicensed to the Apache License 2.0 with LLVM Exceptions.[2] about 400 contributions had not been relicensed.[25] [26]

Features

LLVM can provide the middle layers of a complete compiler system, taking intermediate representation (IR) code from a compiler and emitting an optimized IR. This new IR can then be converted and linked into machine-dependent assembly language code for a target platform. LLVM can accept the IR from the GNU Compiler Collection (GCC) toolchain, allowing it to be used with a wide array of extant compiler front-ends written for that project. LLVM can also be built with gcc after version 7.5.[27]

LLVM can also generate relocatable machine code at compile-time or link-time or even binary machine code at runtime.

LLVM supports a language-independent instruction set and type system.[4] Each instruction is in static single assignment form (SSA), meaning that each variable (called a typed register) is assigned once and then frozen. This helps simplify the analysis of dependencies among variables. LLVM allows code to be compiled statically, as it is under the traditional GCC system, or left for late-compiling from the IR to machine code via just-in-time compilation (JIT), similar to Java. The type system consists of basic types such as integer or floating-point numbers and five derived types: pointers, arrays, vectors, structures, and functions. A type construct in a concrete language can be represented by combining these basic types in LLVM. For example, a class in C++ can be represented by a mix of structures, functions and arrays of function pointers.

The LLVM JIT compiler can optimize unneeded static branches out of a program at runtime, and thus is useful for partial evaluation in cases where a program has many options, most of which can easily be determined unneeded in a specific environment. This feature is used in the OpenGL pipeline of Mac OS X Leopard (v10.5) to provide support for missing hardware features.[28]

Graphics code within the OpenGL stack can be left in intermediate representation and then compiled when run on the target machine. On systems with high-end graphics processing units (GPUs), the resulting code remains quite thin, passing the instructions on to the GPU with minimal changes. On systems with low-end GPUs, LLVM will compile optional procedures that run on the local central processing unit (CPU) that emulate instructions that the GPU cannot run internally. LLVM improved performance on low-end machines using Intel GMA chipsets. A similar system was developed under the Gallium3D LLVMpipe, and incorporated into the GNOME shell to allow it to run without a proper 3D hardware driver loaded.[29]

In 2011, programs compiled by GCC outperformed those from LLVM by 10%, on average.[30] [31] In 2013, phoronix reported that LLVM had caught up with GCC, compiling binaries of approximately equal performance.[32]

Components

LLVM has become an umbrella project containing multiple components.

Frontends

LLVM was originally written to be a replacement for the extant code generator in the GCC stack,[33] and many of the GCC frontends have been modified to work with it, resulting in the now-defunct LLVM-GCC suite. The modifications generally involve a GIMPLE-to-LLVM IR step so that LLVM optimizers and codegen can be used instead of GCC's GIMPLE system. Apple was a significant user of LLVM-GCC through Xcode 4.x (2013).[34] [35] This use of the GCC frontend was considered mostly a temporary measure, but with the advent of Clang and advantages of LLVM and Clang's modern and modular codebase (as well as compilation speed), is mostly obsolete.

LLVM currently supports compiling of Ada, C, C++, D, Delphi, Fortran, Haskell, Julia, Objective-C, Rust, and Swift using various frontends.

Widespread interest in LLVM has led to several efforts to develop new frontends for many languages. The one that has received the most attention is Clang, a newer compiler supporting C, C++, and Objective-C. Primarily supported by Apple, Clang is aimed at replacing the C/Objective-C compiler in the GCC system with a system that is more easily integrated with integrated development environments (IDEs) and has wider support for multithreading. Support for OpenMP directives has been included in Clang since release 3.8.[36]

The Utrecht Haskell compiler can generate code for LLVM. While the generator was in early stages of development, in many cases it was more efficient than the C code generator.[37] The Glasgow Haskell Compiler (GHC) backend uses LLVM and achieves a 30% speed-up of compiled code relative to native code compiling via GHC or C code generation followed by compiling, missing only one of the many optimizing techniques implemented by the GHC.[38]

Many other components are in various stages of development, including, but not limited to, the Rust compiler, a Java bytecode frontend, a Common Intermediate Language (CIL) frontend, the MacRuby implementation of Ruby 1.9, various frontends for Standard ML, and a new graph coloring register allocator.

Intermediate representation

The core of LLVM is the intermediate representation (IR), a low-level programming language similar to assembly. IR is a strongly typed reduced instruction set computer (RISC) instruction set which abstracts away most details of the target. For example, the calling convention is abstracted through call and ret instructions with explicit arguments. Also, instead of a fixed set of registers, IR uses an infinite set of temporaries of the form %0, %1, etc. LLVM supports three equivalent forms of IR: a human-readable assembly format,[39] an in-memory format suitable for frontends, and a dense bitcode format for serializing. A simple "Hello, world!" program in the IR format:

@.str = internal constant [14 x i8] c"Hello, world\0A\00"

declare i32 @printf(ptr, ...)

define i32 @main(i32 %argc, ptr %argv) nounwind

The many different conventions used and features provided by different targets mean that LLVM cannot truly produce a target-independent IR and retarget it without breaking some established rules. Examples of target dependence beyond what is explicitly mentioned in the documentation can be found in a 2011 proposal for "wordcode", a fully target-independent variant of LLVM IR intended for online distribution.[40] A more practical example is PNaCl.[41]

The LLVM project also introduces another type of intermediate representation named MLIR[42] which helps build reusable and extensible compiler infrastructure by employing a plugin architecture named Dialect.[43] It enables the use of higher-level information on the program structure in the process of optimization including polyhedral compilation.

Backends

At version 16, LLVM supports many instruction sets, including IA-32, x86-64, ARM, Qualcomm Hexagon, LoongArch, M68K, MIPS, NVIDIA Parallel Thread Execution (PTX, also named NVPTX in LLVM documentation), PowerPC, AMD TeraScale,[44] most AMD GPU recent ones (also named AMDGPU in LLVM documentation),[45] SPARC, z/Architecture (also named SystemZ in LLVM documentation), and XCore.

Some features are not available on some platforms. Most features are present for IA-32, x86-64, z/Architecture, ARM, and PowerPC.[46] RISC-V is supported as of version 7.

In the past, LLVM also supported other backends, fully or partially, including C backend, Cell SPU, mblaze (MicroBlaze),[47] AMD R600, DEC/Compaq Alpha (Alpha AXP)[48] and Nios2,[49] but that hardware is mostly obsolete, and LLVM developers decided the support and maintenance costs were no longer justified.

LLVM also supports WebAssembly as a target, enabling compiled programs to execute in WebAssembly-enabled environments such as Google Chrome / Chromium, Firefox, Microsoft Edge, Apple Safari or WAVM. LLVM-compliant WebAssembly compilers typically support mostly unmodified source code written in C, C++, D, Rust, Nim, Kotlin and several other languages.

The LLVM machine code (MC) subproject is LLVM's framework for translating machine instructions between textual forms and machine code. Formerly, LLVM relied on the system assembler, or one provided by a toolchain, to translate assembly into machine code. LLVM MC's integrated assembler supports most LLVM targets, including IA-32, x86-64, ARM, and ARM64. For some targets, including the various MIPS instruction sets, integrated assembly support is usable but still in the beta stage.

Linker

The lld subproject is an attempt to develop a built-in, platform-independent linker for LLVM.[50] lld aims to remove dependence on a third-party linker., lld supports ELF, PE/COFF, Mach-O, and WebAssembly[51] in descending order of completeness. lld is faster than both flavors of GNU ld.

Unlike the GNU linkers, lld has built-in support for link-time optimization (LTO). This allows for faster code generation as it bypasses the use of a linker plugin, but on the other hand prohibits interoperability with other flavors of LTO.[52]

C++ Standard Library

The LLVM project includes an implementation of the C++ Standard Library named libc++, dual-licensed under the MIT License and the UIUC license.[53]

Since v9.0.0, it was relicensed to the Apache License 2.0 with LLVM Exceptions.[2]

Polly

This implements a suite of cache-locality optimizations as well as auto-parallelism and vectorization using a polyhedral model.[54]

Debugger

See main article: article and LLDB (debugger).

C Standard Library

llvm-libc is an incomplete, upcoming, ABI independent C standard library designed by and for the LLVM project.[55]

Derivatives

Due to its permissive license, many vendors release their own tuned forks of LLVM. This is officially recognized by LLVM's documentation, which suggests against using version numbers in feature checks for this reason.[56] Some of the vendors include:

See also

Literature

Notes and References

  1. Web site: LLVM Logo . The LLVM Compiler Infrastructure Project.
  2. Web site: LICENSE.TXT. llvm.org. 2019-09-24.
  3. Web site: The LLVM Compiler Infrastructure Project. March 11, 2016.
  4. Web site: LLVM Language Reference Manual. June 9, 2019.
  5. Web site: Announcing LLILC - A new LLVM-based Compiler for .NET . 2020-09-12 . dotnetfoundation.org. December 12, 2021 . https://web.archive.org/web/20211212184833/https://dotnetfoundation.org/blog/2015/04/14/announcing-llilc-llvm-for-dotnet . dead.
  6. Web site: Mono LLVM . March 10, 2013.
  7. Book: Lattner, Chris . Chris Lattner . Brown . Amy . Wilson . Greg . 2011 . The Architecture of Open Source Applications . LLVM . http://www.aosabook.org/en/llvm.html.
  8. Web site: MovForth . . November 28, 2021.
  9. Web site: What's the Difference Between LabVIEW 2017 and LabVIEW NXG?. William Wong. May 23, 2017. Electronic Design.
  10. Web site: NI LabVIEW Compiler: Under the Hood .
  11. News: Larabel . Michael . 11 April 2018 . Khronos Officially Announces Its LLVM/SPIR-V Translator . Phoronix.com.
  12. Web site: 2020-11-12. 32.1. What is JIT compilation?. 2021-01-25 . PostgreSQL Documentation. en.
  13. Web site: Features. RubyMotion. Scratchwork Development LLC. June 17, 2017. RubyMotion transforms the Ruby source code of your project into ... machine code using a[n] ... ahead-of-time (AOT) compiler, based on LLVM..
  14. Web site: Code Generation - Guide to Rustc Development. rust-lang.org. 2023-01-04.
  15. Web site: Reedy . Geoff . September 24, 2012 . Compiling Scala to LLVM . St. Louis, Missouri, United States . February 19, 2013 .
  16. Web site: Scala Native . 26 November 2023.
  17. Web site: Developer Tools Overview . Apple Developer . Apple . https://web.archive.org/web/20110423095129/https://developer.apple.com/technologies/tools/ . April 23, 2011.
  18. The name of LLVM . Lattner . Chris . Chris Lattner . llvm-dev . December 21, 2011 . 'LLVM' is officially no longer an acronym. The acronym it once expanded too was confusing, and inappropriate almost from day 1. :) As LLVM has grown to encompass other subprojects, it became even less useful and meaningless. . March 2, 2016.
  19. Book: Lattner . Chris . Brown . Amy . Wilson . Greg . The architecture of open source applications . 1 June 2011 . 978-1257638017 . https://www.aosabook.org/en/llvm.html . LLVM. Lulu.com . The name 'LLVM' was once an acronym, but is now just a brand for the umbrella project..
  20. Web site: "libc++" C++ Standard Library.
  21. Web site: The LLVM Foundation . Lattner . Chris . Chris Lattner . April 3, 2014 . LLVM Project Blog.
  22. Web site: Board of Directors . 19 March 2024 . LLVM Foundation.
  23. Web site: ACM Software System Award . ACM.
  24. Web site: Wennborg . Hans . [llvm-announce] LLVM 9.0.0 Release ]. 19 September 2019.
  25. Web site: Relicensing Long Tail . foundation.llvm.org. 11 November 2022.
  26. Web site: LLVM relicensing - long tail . LLVM Project . Google Docs . 27 November 2022 .
  27. Web site: ⚙ D156286 [docs] Bump minimum GCC version to 7.5 ]. 2023-07-28 . reviews.llvm.org.
  28. Lattner . Chris . Chris Lattner . August 15, 2006 . A cool use of LLVM at Apple: the OpenGL stack . llvm-dev . March 1, 2016.
  29. Michael Larabel, "GNOME Shell Works Without GPU Driver Support", phoronix, November 6, 2011
  30. Web site: Makarov . V. . SPEC2000: Comparison of LLVM-2.9 and GCC4.6.1 on x86 . October 3, 2011.
  31. Web site: Makarov . V. . SPEC2000: Comparison of LLVM-2.9 and GCC4.6.1 on x86_64 . October 3, 2011.
  32. Web site: Larabel . Michael . December 27, 2012 . LLVM/Clang 3.2 Compiler Competing With GCC . March 31, 2013.
  33. Lattner . Chris . Chris Lattner . Adve . Vikram . Vikram Adve . Architecture For a Next-Generation GCC . First Annual GCC Developers' Summit . May 2003 . September 6, 2009.
  34. Web site: LLVM Compiler Overview . developer.apple.com.
  35. Web site: Xcode 5 Release Notes. Apple Inc..
  36. Web site: Clang 3.8 Release Notes . August 24, 2016.
  37. Web site: Compiling Haskell To LLVM . February 22, 2009.
  38. Web site: LLVM Project Blog: The Glasgow Haskell Compiler and LLVM . May 17, 2010 . August 13, 2010.
  39. Web site: LLVM Language Reference Manual . . 10 January 2023 . LLVM.org.
  40. Web site: Kang . Jin-Gu . Wordcode: more target independent LLVM bitcode . 1 December 2019.
  41. Web site: PNaCl: Portable Native Client Executables . 25 April 2012 . dead . https://web.archive.org/web/20120502135033/http://nativeclient.googlecode.com/svn/data/site/pnacl.pdf . 2 May 2012 . dmy-all.
  42. Web site: MLIR . 2022-06-07 . mlir.llvm.org.
  43. Web site: Dialects - MLIR . 2022-06-07 . mlir.llvm.org.
  44. [LLVMdev] RFC: R600, a new backend for AMD GPUs ]. llvm-dev . Tom . Stellard . March 26, 2012.
  45. Web site: User Guide for AMDGPU Backend — LLVM 15.0.0git documentation.
  46. http://llvm.org/docs/CodeGenerator.html#target-feature-matrix Target-specific Implementation Notes: Target Feature Matrix
  47. Web site: Remove the mblaze backend from llvm . GitHub . July 25, 2013 . January 26, 2020.
  48. Web site: Remove the Alpha backend. . GitHub . October 27, 2011 . January 26, 2020.
  49. Web site: [Nios2] Remove Nios2 backend . GitHub . January 15, 2019 . January 26, 2020.
  50. Web site: lld - The LLVM Linker. The LLVM Project. May 10, 2017.
  51. Web site: WebAssembly lld port.
  52. Web site: 42446 – lld can't handle gcc LTO files . bugs.llvm.org.
  53. Web site: "libc++" C++ Standard Library.
  54. Web site: Polly - Polyhedral optimizations for LLVM.
  55. Web site: llvm-libc: An ISO C-conformant Standard Library — libc 15.0.0git documentation . 2022-07-18 . libc.llvm.org.
  56. Web site: Clang Language Extensions . Clang 12 documentation . Note that marketing version numbers should not be used to check for language features, as different vendors use different numbering schemes. Instead, use the Feature Checking Macros..
  57. Web site: apple/llvm-project . Apple . 5 September 2020.
  58. Web site: IBM C/C++ and Fortran compilers to adopt LLVM open source infrastructure.
  59. Web site: Intel C/C++ compilers complete adoption of LLVM . 2021-08-17 . Intel . en.
  60. Web site: lanl/kitsune . Los Alamos National Laboratory . 27 February 2020.
  61. Web site: NVVM IR Specification 1.5. The current NVVM IR is based on LLVM 5.0.
  62. Book: Lattner, Chris . Chris Lattner . March 15, 2012 . The Architecture of Open Source Applications . Amy Brown, Greg Wilson . Chapter 11 . 978-1257638017 . http://www.aosabook.org/en/llvm.html.