LLVM

The LLVM compiler infrastructure project is a set of compiler and toolchain technologies,[3] which can be used to develop a front end for any programming language and a back end 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]

LLVM
Original author(s)Vikram Adve, Chris Lattner
Developer(s)LLVM Developer Group
Initial release2003 (2003)
Stable release
10.0.1 / July 22, 2020 (2020-07-22)[1]
Repository
Written inC++
Operating systemCross-platform
TypeCompiler
LicenseUIUC (BSD-style)
Apache License 2.0 with LLVM Exceptions (v9.0.0 or later)[2]
Websitewww.llvm.org

LLVM is written in C++ and is designed for compile-time, link-time, run-time, and "idle-time" optimization. Originally implemented for C and C++, the language-agnostic design of LLVM has since spawned a wide variety of front ends: languages with compilers that use LLVM include ActionScript, Ada, C#,[5][6][7] Common Lisp, Crystal, CUDA, D, Delphi, Dylan, Fortran, Graphical G Programming Language,[8] Halide, Haskell, Java bytecode, Julia, Kotlin, Lua, Objective-C, OpenCL,[9] PostgreSQL's SQL and PLpgSQL,[10] Ruby,[11] Rust, Scala,[12] Swift, and Xojo.

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.[13] LLVM is an integral part of Apple's latest development tools for macOS and iOS.[14] Since 2013, Sony has been using LLVM's primary front end Clang compiler in the software development kit (SDK) of its PlayStation 4 console.[15]

The name LLVM was originally an initialism for Low Level Virtual Machine. This abbreviation has officially been removed to avoid confusion, as the LLVM has evolved into an umbrella project that has little relationship to what most current developers think of as virtual machines.[16] Now, LLVM is a brand that applies to the LLVM umbrella project, 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[17]), etc. LLVM is administered by the LLVM Foundation. Its president is compiler engineer Tanya Lattner.[18]

"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.[19]

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

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 compilers written for that project.

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

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.[20]

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.[21]

For run-time performance of the compiled programs, GCC formerly outperformed LLVM by 10% on average in 2011.[22][23] Newer results in 2013 indicate that LLVM has now caught up with GCC in this area, and is now compiling binaries of approximately equal performance.[24]

Components

LLVM has become an umbrella project containing multiple components.

Front ends

LLVM was originally written to be a replacement for the existing code generator in the GCC stack,[25] and many of the GCC front ends 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 has historically been an important user of llvm-gcc.[26]. This (use of 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 front ends.

Widespread interest in LLVM has led to several efforts to develop new front ends for a variety of languages. The one that has received the most attention is Clang, a new 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.[27]

The Utrecht Haskell compiler can generate code for LLVM. Though the generator is in the early stages of development, in many cases it has been more efficient than the C code generator.[28] There is a Glasgow Haskell Compiler (GHC) backend using LLVM that achieves a 30% speed-up of the 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.[29]

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

Intermediate representation

LLVM IR is used e.g., by radeonsi and by llvmpipe. Both are part of Mesa 3D.

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 computing (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, an in-memory format suitable for frontends, and a dense bitcode format for serializing. A simple "Hello, world!" program in the IR format:[30]

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

declare i32 @printf(i8*, ...)

define i32 @main(i32 %argc, i8** %argv) nounwind {
entry:
    %tmp1 = getelementptr [14 x i8], [14 x i8]* @.str, i32 0, i32 0
    %tmp2 = call i32 (i8*, ...) @printf( i8* %tmp1 ) nounwind
    ret i32 0
}

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.[31]

Back ends

At version 3.4, LLVM supports many instruction sets, including ARM, Qualcomm Hexagon, MIPS, Nvidia Parallel Thread Execution (PTX; called NVPTX in LLVM documentation), PowerPC, AMD TeraScale,[32] AMD Graphics Core Next (GCN), SPARC, z/Architecture (called SystemZ in LLVM documentation), x86, x86-64, and XCore. Some features are not available on some platforms. Most features are present for x86, x86-64, z/Architecture, ARM, and PowerPC.[33] RISC-V is supported as of version 7. In the past LLVM also supported fully or partially other backends, including C backend, Cell SPU, mblaze (MicroBlaze),[34] AMD R600, DEC/Compaq Alpha (Alpha AXP)[35] and Nios2,[36] but most of this hardware is mostly obsolete, and the LLVM support and maintenance for it couldn't be justified.

LLVM also supports WebAssembly as a target, which allows to compile programs and execute them in WebAssembly environment like Google Chrome / Chromium, Firefox, Microsoft Edge, Apple Safari or WAVM. WebAssembly support allows to use mostly unmodified C, C++, D, Rust, Nim, Kotlin and possibly other third-party LLVM-based languages source codes, programs and libraries and target them into WebAssembly.

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 x86, 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.[37] lld aims to remove dependence on a third-party linker. As of May 2017, lld supports ELF, PE/COFF, Mach-O, and WebAssembly[38] in descending order of completeness.[37] In cases where lld is insufficient, another linker such as GNU ld can be used.

Using lld allows link-time optimization. When link-time optimization is enabled, the compiler generates LLVM bitcode instead of native code, and native code generation is done by the linker.

C++ Standard Library

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

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.[40]

Debugger

gollark: !!!
gollark: ↑ 4160 upvotes!
gollark: ?urban apioform
gollark: apio form.
gollark: <@854350605702660106> <@854350605702660106> <@854350605702660106> <@854350605702660106>

See also

Literature

  • Chris Lattner - The Architecture of Open Source Applications - Chapter 11 LLVM, ISBN 978-1257638017, released 2012 under CC BY 3.0 (Open Access).[41]
  • LLVM: A Compilation Framework for Lifelong Program Analysis & Transformation, a published paper by Chris Lattner, Vikram Adve

References

  1. "LLVM 10.0.1 Release". Github. July 22, 2020. Retrieved July 22, 2020.
  2. "LICENSE.TXT". llvm.org. Retrieved September 24, 2019.
  3. "The LLVM Compiler Infrastructure Project". Retrieved March 11, 2016.
  4. "LLVM Language Reference Manual". Retrieved June 9, 2019.
  5. Announcing LLILC - A new LLVM-based Compiler for .NET, retrieved April 17, 2015
  6. Mono LLVM, retrieved March 10, 2013
  7. Chris Lattner (2011). "LLVM". In Amy Brown; Greg Wilson (eds.). The Architecture of Open Source Applications.
  8. William Wong (May 23, 2017). "What's the Difference Between LabVIEW 2017 and LabVIEW NXG?". Electronic Design.
  9. Michael Larabel (April 11, 2018). "Khronos Officially Announces Its LLVM/SPIR-V Translator". phoronix.com.
  10. https://www.postgresql.org/docs/12/jit-reason.html
  11. "Features". RubyMotion. Scratchwork Development LLC. Retrieved 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.
  12. Reedy, Geoff (September 24, 2012). "Compiling Scala to LLVM". St. Louis, Missouri, United States. Retrieved February 19, 2013. Cite journal requires |journal= (help)
  13. Adam Treat (February 19, 2005), mkspecs and patches for LLVM compile of Qt4, archived from the original on October 4, 2011, retrieved January 27, 2012
  14. "Apple LLVM Compiler", Developer Tools, Apple, retrieved January 27, 2012
  15. Developer Toolchain for ps4 (PDF), retrieved February 24, 2015
  16. Lattner, Chris (December 21, 2011). "The name of LLVM". llvm-dev (Mailing list). Retrieved March 2, 2016.
  17. ""libc++" C++ Standard Library".
  18. Chris Lattner (April 3, 2014). "The LLVM Foundation". LLVM Project Blog.
  19. "ACM Software System Award". ACM.
  20. Chris Lattner (August 15, 2006). "A cool use of LLVM at Apple: the OpenGL stack". llvm-dev (Mailing list). Retrieved March 1, 2016.
  21. Michael Larabel, "GNOME Shell Works Without GPU Driver Support", phoronix, November 6, 2011
  22. V. Makarov. "SPEC2000: Comparison of LLVM-2.9 and GCC4.6.1 on x86". Retrieved October 3, 2011.
  23. V. Makarov. "SPEC2000: Comparison of LLVM-2.9 and GCC4.6.1 on x86_64". Retrieved October 3, 2011.
  24. Michael Larabel (December 27, 2012). "LLVM/Clang 3.2 Compiler Competing With GCC". Retrieved March 31, 2013.
  25. Lattner, Chris; Vikram Adve (May 2003). Architecture For a Next-Generation GCC. First Annual GCC Developers' Summit. Retrieved September 6, 2009.
  26. "LLVM Compiler Overview". developer.apple.com.
  27. "Clang 3.8 Release Notes". Retrieved August 24, 2016.
  28. "Compiling Haskell To LLVM". Retrieved February 22, 2009.
  29. "LLVM Project Blog: The Glasgow Haskell Compiler and LLVM". Retrieved August 13, 2010.
  30. For the full documentation, refer to llvm.org/docs/LangRef.html.
  31. Kang, Jin-Gu. "Wordcode: more target independent LLVM bitcode" (PDF). Retrieved December 1, 2019.
  32. Stellard, Tom (March 26, 2012). "[LLVMdev] RFC: R600, a new backend for AMD GPUs". llvm-dev (Mailing list).
  33. Target-specific Implementation Notes: Target Feature Matrix // The LLVM Target-Independent Code Generator, LLVM site.
  34. "Remove the mblaze backend from llvm". GitHub. July 25, 2013. Retrieved January 26, 2020.
  35. "Remove the Alpha backend". GitHub. October 27, 2011. Retrieved January 26, 2020.
  36. "[Nios2] Remove Nios2 backend". GitHub. January 15, 2019. Retrieved January 26, 2020.
  37. "lld - The LLVM Linker". The LLVM Project. Retrieved May 10, 2017.
  38. "WebAssembly lld port".
  39. ""libc++" C++ Standard Library".
  40. "Polly - Polyhedral optimizations for LLVM".
  41. Chris Lattner (March 15, 2012). "Chapter 11". The Architecture of Open Source Applications. Amy Brown, Greg Wilson. ISBN 978-1257638017.

This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.