What exactly is LLVM?
Asked Answered
L

7

559

I keep hearing about LLVM all the time. It's in Perl, then it's in Haskell, then someone uses it in some other language? What is it?

  • What exactly distinguishes it from GCC (perspectives = safety etc.)?
Levey answered 1/3, 2010 at 9:6 Comment(1)
There is a good chapter in a book explaining everything nicely here: www.aosabook.org/en/llvm.htmlInquisitionist
F
437

LLVM is a library that is used to construct, optimize and produce intermediate and/or binary machine code.

LLVM can be used as a compiler framework, where you provide the "front-end" (parser and lexer) and the "back-end" (code that converts LLVM's representation to actual machine code).

LLVM can also act as a JIT compiler – it has support for x86/x86_64 and PPC/PPC64 assembly generation with fast code optimizations aimed for compilation speed.

Unfortunately disabled since 2013, there was the ability to play with LLVM's machine code generated from C or C++ code at the demo page.

Fujimoto answered 1/3, 2010 at 9:13 Comment(5)
If you want to play with it, look at this amazing article: gnuu.org/2009/09/18/writing-your-own-toy-compilerFujimoto
The link provided in the answer says "The LLVM demo page is currently disabled."Vigilance
ellcc.org/demo/index.cgi is another way to play with compiling C/C++ through LLVM to various targets, including intermediate codeDowner
can you give an actual example of what it means?Livonia
Now you can play with LLVM generated from C/C++ using the Compiler Explorer. godbolt.orgEulalie
M
197

A good summary of LLVM is this:

enter image description here

At the frontend you have Perl, and many other high level languages. At the backend, you have the natives code that run directly on the machine.

At the centre is your intermediate code representation. If every high level language can be represented in this LLVM IR format, then analysis tools based on this IR can be easily reused - that is the basic rationale.

Medan answered 3/3, 2018 at 7:4 Comment(6)
picture is worth a thousand words 👍Regelation
So it seems that LLVM is useful for compiler designers. Should a programmer care if he compiles with a standard compiler or one based on LLVM, besides analyzing the generated code independently of the source code?Broughton
if you can compile with LLVM to its IR, then a lot of tools may be opened to you for analysing the IR. But if your tool is only capable of analysing the native binaries (x86 for example), then whether it is LLVM generated binaries or gcc-generated binaries, or Intel-compiler generated, has no difference.Medan
could someone share any resource for learning LLVM IR.Pappas
@KrishnaOza 1. wdv4758h.github.io/notes/compiler/llvm-ir.html 2. ubiquitydotnet.github.io/Llvm.NET/articles/Samples/… 3. xiongyingfei.github.io/SA/2015/4%20LLVM%20-%20GaoQing.pdfMedan
source of the picture?Cicily
C
89

LLVM (used to mean "Low Level Virtual Machine" but not anymore) is a compiler infrastructure, written in C++, which is designed for compile-time, link-time, run-time, and "idle-time" optimization of programs written in arbitrary programming languages. Originally implemented for C/C++, the language-independent design (and the success) of LLVM has since spawned a wide variety of front-ends, including Objective C, Fortran, Ada, Haskell, Java bytecode, Python, Ruby, ActionScript, GLSL, and others.

Read this for more explanation
Also check out Unladen Swallow

Concerned answered 1/3, 2010 at 9:26 Comment(3)
.. so what the docs are trying to say; while LLVM is an acronym for Low Level Virtual Machine, the project is simply named LLVM, not the fully spelled out words.Munshi
Earlier it had above abbreviation but later removed en.wikipedia.org/wiki/LLVMVideo
To summarize the comments. "The Low Level Virtual Machine (LLVM)" should be something like "LLVM (which meant "Low Level Virtual Machine" earlier in the projects lifespan)"Emunctory
C
62

According to 'Getting Started With LLVM Core Libraries' book (c):

In fact, the name LLVM might refer to any of the following:

  • The LLVM project/infrastructure: This is an umbrella for several projects that, together, form a complete compiler: frontends, backends, optimizers, assemblers, linkers, libc++, compiler-rt, and a JIT engine. The word "LLVM" has this meaning, for example, in the following sentence: "LLVM is comprised of several projects".

  • An LLVM-based compiler: This is a compiler built partially or completely with the LLVM infrastructure. For example, a compiler might use LLVM for the frontend and backend but use GCC and GNU system libraries to perform the final link. LLVM has this meaning in the following sentence, for example: "I used LLVM to compile C programs to a MIPS platform".

  • LLVM libraries: This is the reusable code portion of the LLVM infrastructure. For example, LLVM has this meaning in the sentence: "My project uses LLVM to generate code through its Just-in-Time compilation framework".

  • LLVM core: The optimizations that happen at the intermediate language level and the backend algorithms form the LLVM core where the project started. LLVM has this meaning in the following sentence: "LLVM and Clang are two different projects".

  • The LLVM IR: This is the LLVM compiler intermediate representation. LLVM has this meaning when used in sentences such as "I built a frontend that translates my own language to LLVM".

Chalet answered 26/1, 2015 at 10:11 Comment(0)
G
39

LLVM is basically a library used to build compilers and/or language oriented software. The basic gist is although you have gcc which is probably the most common suite of compilers, it is not built to be re-usable ie. it is difficult to take components from gcc and use it to build your own application. LLVM addresses this issue well by building a set of "modular and reusable compiler and toolchain technologies" which anyone could use to build compilers and language oriented software.

Garretson answered 4/5, 2014 at 23:37 Comment(2)
So LLVM is the library, and Clang is the compiler?Spitz
Clang is the C/C++ frontend compiler of LLVM. It converts C code to LLVM bitcode, which is translated into some Assembly language by a backend compiler.Cephalad
S
9

Low Level Virtual Machine(LLVM)

[Xcode build process]

Alternative: GCC(GNU Compiler Collection). GDB(GNU Debugger) - debug tool. Supports more languages and architectures.

LLVM - is umbrella project(set of libraries) it is a brand name with different projects(IR - Intermediate Representation, debug tool...) And now it is not acronym of Virtual Machine. LLDB(LLVM Debugger) - debug tool. Supported by Big companies

Compiler:

Language FrontEnd(Many: Clang, Haskel, Swiftc...) -> Optimizer(Single) -> Backend(Many: arm64, x86_64, armv7...)

FrontEnd generates Intermediate Representation (IR, LLVM IR). This common language allows simple scale process. If you are creating new language you are responsible only for FrontEnd, if you are developing new architecture you should take care about BackEnd. It is a kind of .class file in JVM which are used by ClassLoader[About]

There are thee equivalent IR forms:

llvm-dis can be used to convert bitcode into human readable

Simonnesimonpure answered 28/3, 2021 at 18:7 Comment(0)
O
8

The LLVM Compiler Infrastructure is particularly useful for performing optimizations and transformations on code. It also consists of a number of tools serving distinct usages. llvm-prof is a profiling tool that allows you to do profiling of execution in order to identify program hotspots. Opt is an optimization tool that offers various optimization passes (dead code elimination for instance).

Importantly LLVM provides you with the libraries, to write your own Passes. For instance if you require to add a range check on certain arguments that are passed into certain functions of a Program, writing a simple LLVM Pass would suffice.

For more information on writing your own Pass, check this http://llvm.org/docs/WritingAnLLVMPass.html

Oira answered 4/2, 2014 at 23:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.