A minimal example to create and execute a LLVM IR module in Zig.
First of all, you need to install LLVM and Zig according to your system.
with Scoop
scoop install llvm zig
Warning
untested
with Chocolatey
choco install llvm zig
Warning
untested
apt-get install llvm zig
Warning
untested
with Homebrew
brew install llvm zig
Tip
Tested successfully with LLVM 17.0.6 and Zig 0.11+.
git clone https://github.com/seyhajin/llvm-ir-zig
Alternatively, download zip from Github repository and extract wherever you want.
Note
For simplicity, the program uses LLVM's dynamic libraries installed in your environment.
Build and run program :
zig build run
Output:
Hello, world!
sum(2, 3)=5
LLVM IR module equivalent of:
int sum(int a, int b) {
return a + b;
}
void main() {
printf("Hello, world!\nsum(2, 3)=%d\n", sum(2, 3));
}