Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/serjzimmerman/compiler-stuff-with-llvm

Doing some compiler related stuff in Rust and maybe C++ on top of LLVM
https://github.com/serjzimmerman/compiler-stuff-with-llvm

compiler frontend llvm rust

Last synced: 3 days ago
JSON representation

Doing some compiler related stuff in Rust and maybe C++ on top of LLVM

Awesome Lists containing this project

README

        

# 📚 Compiler stuff with LLVM

This repo contains some practice tasks for building LLVM based tooling, passes, e.t.c.

## ❄️ Requirements

To build this repo you need either use [nix](https://nixos.org/) flakes with devshells:
Running the following command will install all of the required dependencies and drop
you in a shell with the right environment:

```bash
nix develop
```

Otherwise you will need the following tools:

- [meson](https://github.com/mesonbuild/meson)
- [ninja](https://github.com/ninja-build/ninja)
- [llvm & clang](https://clang.llvm.org/), default version used by this project is [18](https://releases.llvm.org/18.1.0/docs/ReleaseNotes.html)
- [rust toolchain](https://doc.rust-lang.org/cargo/getting-started/installation.html) built with the same llvm version as the system libraries.
Working version combination is provided in the project's [flake](./flake.nix) and [rust-toolchain](./rust-toolchain.toml).
- [sdl2](https://github.com/libsdl-org/SDL)
- [libffi](https://github.com/libffi/libffi)
- [pkg-config](https://www.freedesktop.org/wiki/Software/pkg-config/)

Steps to install all of those might differ depending on your operating system. If you are not using
the provided flake, then you are on your own.

### Day-Night game and LLVM IR

There are two different implementation of [Day & Night game]().
One is written in Rust and the other in C. Both of them need to link with the support crate: [cswl-sim](./cswl-sim). It provides
the [C API](./cswl-sim/include/cswl-sim/bindings.h), which hides all of the nasty SDL2 details behind a small stable binary interface.

To build and run the C version:

```bash
meson setup build -Dbuildtype=release
meson compile -C build day_night_c_ir day_night_c
build/day_night_c
file build/day_night.ll
```

To run the Rust version:

```bash
cargo run --package cswl-sim --release
```

### 🪵 Logging instrumentation pass

Manual build instructions:

```bash
mkdir -p build
cargo build --release --package cswl-pass --package cswl-pass-logger
opt --load-pass-plugin target/release/libcswl_pass.so --passes=log-inserter \
artifacts/01-sdl-stuff/llvm-ir/day_night.ll -o artifacts/02-pass-stuff/llvm-ir/instrumented.ll -S
clang artifacts/02-pass-stuff/llvm-ir/instrumented.ll \
target/release/libcswl_pass_logger.a \
target/release/libcswl_sim.a \
-lSDL2 -O2 -o build/day_night_c_instrumented
CSWL_PASS_LOGGER_OUTPUT_PATH=output.log timeout 4s build/day_night_c_instrumented
target/release/cswl-pass-statistics < output.log > statistics.txt
csplit statistics.txt '/_snip_here_/' '{*}' \
--prefix artifacts/02-pass-stuff/results/window- \
--digits 1 \
--suppress-matched
rm artifacts/02-pass-stuff/results/window-0 statistics.txt
```

### 💀 Ad-hoc LLVM IR generation 💔

Manual build instructions:

```bash
mkdir artifacts/03-ad-hoc-ir-generation/llvm-ir -p
cargo run --package cswl-irgen --release > \
artifacts/03-ad-hoc-ir-generation/llvm-ir/day_night.ll
```