Skip to content

mrjameshamilton/llvm-helloworld

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LLVM Hello World

This repository contains two simple "Hello World" examples to help get started with LLVM:

  • helloworld.ll: a minimal "Hello World" written in textual LLVM IR.
  • gen.cpp: a minimal "Hello World" generator using the LLVM C++ API.

Installing LLVM

There is an automatic installation script available to easily install LLVM on Ubuntu/Debian systems available here, for example to install LLVM 19:

$ wget https://apt.llvm.org/llvm.sh
$ chmod +x llvm.sh
$ ./llvm.sh 19

Executing helloworld.ll

The easiest way to execute an LLVM IR script is using lli, the LLVM interpreter.

$ lli helloworld.ll

A script can also be compiled into an executable using clang:

$ clang helloworld.ll -o helloworld
$ ./helloworld

Building the generator

The small C++ example demonstrates the basic use of the LLVM C++ API, specifically the IRBuilder to generate LLVM IR.

clang++ command line

# Compile the source to an object file.
$ clang++ `llvm-config --cxxflags` -o gen.o -c gen.cpp
# Link the object file with the LLVM libraries
$ clang++ `llvm-config --ldflags` -o gen gen.o `llvm-config --libs core` 
# Execute the generator and pipe the output to the LLVM IR interpreter.
$ ./gen | lli

Note how the llvm-config tool can be used to generate the necessary compiler and linker flags, for compiling & linking against LLVM.

cmake project

$ mkdir -p build
$ cmake -S . -B build -G Ninja
$ ninja -C build
# Execute the generator and pipe the output to the LLVM IR interpreter.
$ build/gen | lli