r/Compilers • • 16h ago

Can I use LLVM with a custom architecture?

12 Upvotes

Hi,

I've never written a compiler (and probably won't) but I'm an EE student and was thinking about designing a custom CPU one day, maybe even as a diploma project, however I do not want to go through a hassle of inventing a new language on top of a custom architecture. So my question is: do I understand correctly that LLVM can convert a piece of code in basically any language like C/C++ into a high-level assembly language that I can then convert into machine code for my hypothethical CPU with relative ease, so I only have to write compiler's backend? If yes, how hard is it generally and where would I begin?


r/Compilers • • 6h ago

FOXVC : Now completely update with v1.0.0 and engine version 0.6.0 alpha done .

Thumbnail
github.com
0 Upvotes

visit and help this language grow this is will be genuinely a good language to contribute I am working day and night on it . But being a solo dev it's getting hard for me to make it better and good . I need You all guys .

Anay Upadhyay - solo dev


r/Compilers • • 23h ago

Question about LLVM's Constraint Elimination Pass

8 Upvotes

Hi redditors, I am trying to understand how clang eliminates the dead code below:

#include <stdio.h>
#include <stdlib.h>

void foo(int M, int N, int P) {
  if (P > N)
    if (N > M)
      if (M > P)
        printf("%x\n", 0xDEADC0DE);
}

When I compile it with clang 20.1.0, I get this assembly:

foo:
        ret

If I compile it with clang 19.1.0 -O2 (-O3 makes no difference), then I get:

foo:
        cmp     edi, edx
        jle     .LBB0_3
        cmp     esi, edi
        jle     .LBB0_3
        cmp     edx, esi
        jle     .LBB0_3
        lea     rdi, [rip + .L.str]
        mov     esi, -559038242
        xor     eax, eax
        jmp     printf@PLT
.LBB0_3:
        ret

.L.str:
        .asciz  "%x\n"

When I do:

clang-20 -O2 -S -emit-llvm -mllvm -print-after-all -o /dev/null foo.c 2>&1

I can see that ConstraintEliminationPass is likely the one that proves that the branch is dead. However, this pass was already present in clang 19. I was wondering if some redditor that followed that implementation could explain how they improved the pass to make it able to solve that case.

Ps.: Notice that the same program would be fully optimized by gcc, already in version 7.1 -O2 (however, not in gcc 6.5, and not in gcc 7.1 -O1), using VRP.


r/Compilers • • 1d ago

Built a CUDA-to-MLIR-to-x86/PTX/GCN compiler with Clang. Handles N-D tensors (1D-6D), templates, shared memory, and warp-size emulation.

2 Upvotes

I built UniversalGPU, an open-source cross-vendor CUDA compiler. It parses CUDA C++ with Clang, generates MLIR, and lowers to x86/PTX/GCN machine code.

Verified working:

- N-dimensional tensors (1D through 6D)

- f16/f32/f64 types

- Templates (function specializations)

- __shared__ memory + __syncthreads

- atomicAdd reductions

- Warp-size emulation (--warp-size=32 for NVIDIA, 64 for AMD)

- x86 machine code (matmul: 8.0 x 16 output verified)

- NVIDIA PTX assembly

- AMD GCN assembly

Known limitations: 20 inline PTX, no multi-file compilation.

Repo: https://github.com/7636-lochan/UniversalGPU

Happy to answer questions about the Clang AST parsing or the runtime shim.


r/Compilers • • 18h ago

I built my own programming language from scratch in C++ — Introducing PIN!

0 Upvotes

Olá a todos!

Estou trabalhando em minha própria linguagem de programação chamada PIN, construída do zero usando C++.

A ideia por trás do PIN é combinar conceitos e inspirações de JSON, Python e Lua em uma linguagem de script leve e legível.

Recursos atuais (PIN Core 0.6):

  • Variáveis ​​e expressões

  • Números, strings e booleanos

  • Arrays e objetos

  • Condições (if / else)

  • Loops (while e for/in)

  • Funções e valores de retorno

  • Funções integradas

  • Console de desenvolvedor interativo

  • Analisador léxico, analisador sintático, AST e ambiente de execução

Estou desenvolvendo o PIN no Termux para Android e Windows, usando C++ e Clang. Tem sido um desafio divertido construir a linguagem e seus componentes internos do zero.

O projeto ainda é experimental e sei que há muito a melhorar, principalmente o backend do compilador, o tratamento de erros e as ferramentas de desenvolvimento.

Gostaria muito de receber seu feedback!

GitHub: https://github.com/andreferrita-del/PIN

O que você acha? Quais recursos você gostaria de ver em uma linguagem como esta?


r/Compilers • • 13h ago

AiLang, Artificial Intelligence Programming Language with a Self hosting x86_64 compiler, performance comparable to gcc -03 llvm clang in many instances.

Thumbnail
github.com
0 Upvotes

Hi my name is Bob I am a contributor to the AiLang project.

Why this is in compilers, rather simple, this repo has a fully self contained self hosting x86_64 optimizing compiler. From the Read me below.

AiLang

A self-hosting systems language designed for consistency, readable code, and AI-assisted development.

AILang is built around a deliberate trade-off: a constrained syntax with a rich set of primitives. Rather than accumulating many special cases, language constructs follow consistent shapes—from primitive arguments to functions and subroutines. The result is code intended to be easy to scan, search with tools like grep, generate with AI, and understand in human–AI pair programming.

The language is not only a proposal: its compiler is written in AILang and compiles AILang programs to native code. The project is working toward version 1.0.0, targeted for December 31, 2026.

Why AILang

  • Consistency over syntax variety. Primitives, arguments, functions, and subroutines follow coherent conventions, reducing the number of forms a reader or code-generation tool needs to learn.

  • A constrained language with substantial primitives. Keep the surface syntax deliberate while providing reusable capabilities for practical systems code.

  • Designed to be read and searched. Clear, regular code is easier to skim, inspect, and navigate with ordinary tools—including grep.

  • Built for human and AI collaboration. Predictable structure supports code generation and review without giving up human readability.

  • Systems programming with a self-hosting compiler. AILang is statically typed and compiles to native code; the compiler itself is implemented in AILang.

Performance

AILang's compiler and generated code are being optimized continuously. Project benchmarks show performance competitive with GCC -O3 on a number of measured workloads, with results varying by program and workload. See the benchmark results for the measurements and test details.

Performance claims should be read in the context of those specific benchmarks—not as a guarantee that every AILang program matches GCC. The goal is to keep improving generated code while retaining the language's emphasis on explicit, readable structure.

AILang grep beats GNU grep 3.11 in 13 out of 14 tests. Here's the scorecard:

TestAILangGNU grep 3.11Winner
'kernel'208 ms311 msAILang — 1.49×
-F 'kernel'212 ms310 msAILang — 1.46×
-i 'kernel'251 ms358 msAILang — 1.42×
-c 'kernel'168 ms190 msAILang — 1.13×
-n 'kernel'315 ms428 msAILang — 1.35×
-v 'kernel'352 ms472 msAILang — 1.34×
-w 'kernel'222 ms615 msAILang — 2.77×
-ic 'kernel'195 ms234 msAILang — 1.20×
-wc 'kernel'179 ms415 msAILang — 2.31×
-Fc 'kernel'169 ms193 msAILang — 1.14×
'UNIQUE_MARKER_XYZ'49 ms79 msAILang — 1.61×
-c 'UNIQUE_MARKER'47 ms78 msAILang — 1.65×
-F 'error_handler'272 ms288 msAILang — 1.05×
-Fc 'error_handler'223 ms163 msGNU — 1.36×

EDIT addition

Regex PatternAILangGNU grepWinner
mem.*buf188 ms291 msAILang 1.55x
ker[nm]el (character class)229 ms405 msAILang 1.77x
^kernel (anchor)170 ms176 msAILang 1.03x (tie)
`socketdaemon` (alternation)387 ms509 ms

https://github.com/AiLang-Author/Ailang-Self-Hosting-/blob/master/AiLang_CoreUtils/grep_bench_repro.sh

Highlights:

  • Word match (-w) is 2.77× faster than GNU grep.

  • Rare-pattern search is 1.65× faster — MemMem's scan-and-skip is very efficient.

  • Case-insensitive search is 1.42× faster — MemMemFold pays off.

  • The only loss is long fixed-pattern count (-Fc error_handler), where GNU's Boyer–Moore–Horspool skip table gives it an edge on 13-character patterns.

  • The AILang binary is 118 KB vs. GNU's 1.7 MB — 14× smaller.

  • 100% correctness — every test matches GNU grep output exactly.

What it can express

AILang is intended to span a broad range of programs: from a small “Hello, world!” to substantial systems software. The repository includes a self-hosted compiler, reusable libraries, command-line utilities, and larger projects such as a custom CAD kernel and an OS runtime layer running on the Linux kernel. These projects are at different stages of development; see their own documentation for current status.

Try AILang

Compile a source file with the compiler:

./ailang.x path/to/program.ailang

For installation, options, and compiler commands, see install_compiler.sh and the compiler documentation.

Start learning with the programming manuals and browse the demo programs. The workspace guide describes the repository layout.

Project status

AILang is under active development. The compiler, language, libraries, and example systems are evolving; expect incomplete features and occasional regressions. The project is progressing toward its December 31, 2026 version 1.0.0 target. Check the relevant project documentation and tests for the current state of individual components.


r/Compilers • • 1d ago

Quark, a compiled language for data work (typed tables, columnar vectors, BLAS)

5 Upvotes

I've been building Quark, a small language for working with data. Types are inferred, and you add annotations where you want them checked. The compiler (Go) tries to catch argument types, list and dict element types, return types, names across files, and every column a query reads before the program runs.

incl 'std/csv'

struct Sale:
    region: str
    product: str
    units: int
    price: float

sales: tbl[Sale] = unwrap(csv::read_file('sales.csv'))

by_region = sales | s ->
    mutate(revenue = s.units * s.price)
    | group_by(s.region)
    | summarize(revenue = sum(s.revenue), orders = count())
    | sort(desc(s.revenue))
print(by_region)

An 8-operation column expression on 10M rows is about twice as fast as Polars, and a 300×300 matrix product is within 25% of NumPy. String operations and CSV parsing is slower.

Vectors follow the Apache Arrow layout, and a table col is a vec shared with the table. Arithmetic runs on the whole column, and a vec[bool] filters another vector. std/blas passes those f64 buffers to cpp's blas without copying them.

incl 'std/blas'

x = vec [1.0, 1.0, 1.0, 2.0, 1.0, 3.0, 1.0, 4.0]
y = vec [3.0, 5.0, 7.0, 9.0]
g = blas::syrk(x, 4, 2)                   # XᵀX
r = blas::gemv(x, 4, 2, y, true)          # Xᵀy
det = g[0] * g[3] - g[1] * g[2]
print((g[3] * r[0] - g[1] * r[1]) / det)  # intercept: 1.0
print((g[0] * r[1] - g[2] * r[0]) / det)  # slope: 2.0

Very early and unstable, really interested to know people's thoughts on viability of this, design choices or anything else: https://github.com/amolbrkr/quark-lang


r/Compilers • • 1d ago

Why is my C++ dynamic runtime backend slower than CPython?

7 Upvotes

Hi everyone,

I'm 15 and building my own dynamically-typed programming language. The repository below is the C++ runtime/backend I wrote to handle variables, memory, and cross-type operations:

Repo: https://github.com/mxrfp/Dynamic-Type-System-in-C-

To avoid dynamic_cast and heap fragmentation, I built it around:

Double Dispatch via virtual functions to resolve operations like Int + Float or String Int.

std::pmr::unsynchronized_pool_resource inside an RAII wrapper (`Type`) to manage object lifetimes.

Since it's written in C++ with a custom memory pool, I expected it to be at least slightly faster than Python on basic benchmarks instead, it turns out to be slower than CPython.

The code is still very premature, so I'd really appreciate it if someone with more experience could take a look and help me understand where the main performance bottlenecks are.

Thanks in advance!


r/Compilers • • 1d ago

Tensor Seeks Layout: Formalizing Layout Selection for ML Compilers

Thumbnail
arxiv.org
4 Upvotes

r/Compilers • • 1d ago

Serialization unit tests for a Crafting Interpreters clox bytecode vm?

1 Upvotes

I'm working my way through the C portion of Crafting Interpreters with my own modern C++ implementation. I’ve just finished chapter 21 and am about to implement local variables. So I can now begin to write basic scripts or programs in my version of the language.

I've never done anything with C++ serialization. I expect it will work similar to the Rust Serde crate or the C++ fmt library. Standard library containers will already have serialization support and I would need to implement a type trait or function of some kind for my custom types. I would do this for my Opcode's enum class and then I could have an expected serialized bytecode output, hopefully generated at compile time, that is easily written and updated. I could compare this to what my compiler generates. As I progress I could use tested good code output to generate more serialized tests and then I would only have to check new sections of the opcode output against hand written serialized bytecode.

Is this a viable approach for testing compiler output. Do I even need a serialization library? Could I just add constexpr to my code and reorganize the translation units so that I can generate compile time bytecode. What advice could more experienced developers offer? Any tips or suggestions would be appreciated.

Thanks.


r/Compilers • • 1d ago

We replaced NVCC with our own PTX compiler. Results after a 57-entry hackathon.

Thumbnail
reddit.com
0 Upvotes

r/Compilers • • 2d ago

Looking for a YouTube playlist on how C++ files, headers, .o, .a, and .so artifacts work under the hood on Linux (compilation, LLVM IR, symbol tracking, linking, and loading)

5 Upvotes

Hi everyone, I’m trying to build a solid mental model of how C++ source files and headers are transformed and managed under the hood on Linux (ELF) as they become a running process. Specifically, I want to understand: • How translation units, headers, and LLVM IR work during compilation • How ELF object files (.o), static archives (.a), and shared libraries (.so) are structured • How symbol tables, name mangling, and symbol resolution/relocation work during linking • How the Linux dynamic loader (ld.so) maps these binaries and resolves symbols at runtime I've already watched a few well-known standalone videos (such as The Cherno's compiler/linker episodes and Ofek Shilon's CppCon talk on linkers and loaders), which were great. However, I’m looking for a structured YouTube playlist or multi-part series that walks through these low-level transformations step-by-step, ideally with terminal examples (clang++, llvm-nm, llvm-objdump, readelf, ldd, ar, etc.). Does anyone have recommendations for a good playlist or video series that covers this? Thanks in advance!


r/Compilers • • 2d ago

Feedback on a transpiler project

13 Upvotes

I'm thinking about making a small scripting language with a domain focus in transpiling to rust code. The overall goal of the language is to have easier to use types and structures that will transpile to more complex rust code. Probably a dumb and difficult project but it was just an idea to give me something to do to write code again. I'm curious from you all with experience in the compiler field, if you think this project is a fun idea and what issues I could face and also if it's actually a big waste of time. Thank you all for commenting! :)


r/Compilers • • 2d ago

Kairo v0.2.3: Self-Hosted, General Purpose Systems Language, with Bi-directional C++ Interop, Cross compiles to ~48 Targets, and ships with an LSP

7 Upvotes

Hi r/Compilers!

Kairo (previously called Helix) is a statically typed, self-hosted systems language and compiler toolchain, built around full two-way C and C++ interop, that we have been working on for the past ~3 years.

Some stats about the codebase:

  • Compiler Compilers ~420,000 lines of Kairo code found in Compiler/ Analyzer/ Linker/ Extras/

  • ~2,500 tests checked with LLVM lit, including parity suites that use clang as the oracle

  • Documentation

  • Compiler specification (every pass has a written contract)

  • Native LSP server

  • kld, a cross-targeting wrapper around LLVM’s lld that checks ABI-relevant compiler metadata at link time

  • kcc, a drop-in clang replacement that cross-compiles C/C++ out of the box to 48 targets

  • Formatter using the Wadler/Lindig pretty-printing algorithm

  • Native debugger support (GDB, LLDB, WinDbg)

  • LLVM is cross-compiled for 14 targets using kcc

I’ll start with Kairo. It’s a pragmatic language that supports many features, Two-way C++ interop, and 5 different paradigms: procedural imperative systems, Object-oriented, generic trait-based, Functional, and Compile-time metaprogramming. The main goal of Kairo was a readable language with varying levels of expressiveness, allowing developers to write code that is both clear and interacts directly with the lowest level of hardware. Kairo will have a formal memory safety proof model. However, as of now, that is still only spec’d and will not start until we have real users of the language, real bugs, and stabilization work complete - we want the foundation to be solid before formalizing the safety guarantees.

On the middle end of the language, The compiler has about 87 scheduled semantic passes as of writing this (why so many, you may ask? Kairo is a really large language with a lot of feature-interaction complexity). The compiler performs almost all the resolution logic itself, even for code being imported from C++. Kairo's Sema passes handle name lookup, ADL, overload resolution, conversions, specialization choice, constraint checking, SFINAE, and access checking. Clang is only used to get layout info (size, alignment, scope, members) and for completing an instance of an imported class template, and deducing the template arguments of an imported function template that Kairo’s overload resolution is considering. Though, Kairo isn’t fully semantically independent from clang yet. Some clang-generated errors ([CL001]) still come through, but we’re burning down that list, and the v1.0 goal is complete semantic independence.

Kairo can also cross-compile out of the box to 48 targets and freestanding environments, but the builtins don't support freestanding yet.

Here are 3 examples:

class Buf {
    var n: i32 = 7
    fn Buf(self) {}
    @move fn Buf(self, other: Self) { self.n = other.n }
}
fn take(b: Buf) -> i32 { return b.n }
fn main() -> i32 {
    var b = Buf()
    return take(b) + b.n  // error [SC052E]: 'b' is used after being moved
}

Using C++ in Kairo:

ffi "c++" import "vector"
ffi "c++" import "algorithm"

fn main() -> i32 {
    var v = cxx::std::vector<i32>()  v.push_back(3)  v.push_back(1)
    cxx::std::sort(v.begin(), v.end())  // the overload and the template chosen by Kairo
    return v[0] + (v.size() as i32)  // exit with code 3
}

Using Kairo in C++:

/* geo.k
pub class <T> Vec2 {
    pub var x: T  pub var y: T
    pub fn Vec2(self, x: T, y: T) { self.x = x  self.y = y }
    pub fn flip(const self) -> Vec2<T> { return Vec2<T>(self.y, self.x) }
}
*/

#include "geo.k"

int main() {
    geo::Vec2<int>    a(1, 2);    // a Kairo generic, instantiated from C++
    geo::Vec2<double> b(0.5, 4.0); // and again, at another type
    return a.flip().x + static_cast<int>(b.flip().x); // 2 + 4 = 6
}

Kairo also tries to remove a lot of the UB surface found in C++. In Kairo TUs, many things implicitly allowed in C++ are not. Nothing changes if you compile C++ with kcc or use it through interop; the rules apply at the .k boundary.

Now about Kcc, it’s a clang replacement that can cross-compile out of the box. The first time you pass --target, kcc (and kairo) build that target’s C library and C++ runtimes (libc++, libc++abi, libunwind, compiler-rt) from sources that ship in the archive and cache them. That takes about 10 to 30 seconds, once. glibc is built from source too, any version from 2.17 up (--target x86_64-linux-gnu.2.17), so your binary runs on that glibc or newer. MSVC works without Visual Studio: a script pulls the MSVC runtime and Windows SDK straight from Microsoft through xwin, after you accept their license. It ships 17 LLVM backends, so with --freestanding it also covers microcontrollers, eBPF and GPU object code.

kcc also allows a really nice feature to work cleanly:

    #include "my_kairo.k"

The direct inclusion of a Kairo file in C++, without generated headers being stored anywhere on disk (kcc never writes a header when invoked), allows a C++ codebase to be migrated incrementally, one file at a time. We had to make one small (~55-line) patch to clang’s preprocessor for this to work. When clang enters a .k file, it’s handed a module’s interface as pre-lexed tokens located in the .k file itself. Nothing else about the preprocessor changes, so the include can be under an #if, after a #pragma, or even inside a function, and it is respected the same as any other include. Errors that involve the Kairo code point at the .k source. kcc takes normal clang arguments and also compiles Kairo code; kcc foo.k and kcc foo.cpp both work.

We also used kcc to cross-compile LLVM/clang/lld itself (changed the C++ compiler to kcc), which is how the toolchain ships for 14 hosts:

  • Linux (musl, static): x86_64, i686, aarch64, armv7, riscv64

  • Windows (MinGW): x86_64, i686, aarch64, armv7

  • macOS: x86_64 (10.15+), arm64 (11.0+)

  • FreeBSD: x86_64, aarch64

  • NetBSD: x86_64

Each host can cross-compile to all 48 targets.

On the LSP, Kals provides end-to-end latency: p50 of 100 ms and p95 of 350 ms. To avoid patching clangd, it writes temporary headers to disk for all Kairo files that aren't used in any compilation process, and passes stock clangd an -ivfsoverlay that maps them. This means clangd keeps full LSP and autocomplete support in a C++ file that includes a Kairo file.

What’s not done yet: lifetime/escape analysis, virtual interfaces and async aren’t implemented, and the compiler’s own source is still migrating from raw pointers to Kairo’s safe pointers. The test suite runs on x86_64 Linux; the other hosts build and are tested working, but haven't had full suite runs yet. There's still quite a long way ahead to go still.

We’d love feedback on the compiler and toolchain docs, or anything else. Contributions are welcome. Feel free to try out the Kairo compiler and toolchain.

P.S. Hello from u/Lord_Mystic12 - I am the other dev on the project, and logo designer for Kairo. We are really proud to have come this far into this project, I will also be responding in the comments.


r/Compilers • • 2d ago

A compiler optimiser proven in Lean

9 Upvotes

Since AI models came up with some ridiculous Lean proofs in Math, I tried writing a compiler for array kernels with an optimiser proven correct in Lean, instead of relying on tests. AI is very suitable for these kind of proofs.

The IR and its semantics are formalised in Lean. Each optimization pass has a Lean proven theorem proving that the pass doesn't change what the program computes. AI does the heavy grinding of actually proving it.

Proven so far: vectorisation and multiple accumulators (reassociating reductions).

On an Apple M4 Pro, a kernel that takes the max of 1M f32 values runs in 37 µs, against 64 µs for the same loop in C with clang -O3 (1.7x) and 45 µs with -ffast-math (1.2x), and close to the memory bandwidth of one core.

The compiler is not finished or published yet. What are your thoughts on the idea?


r/Compilers • • 2d ago

A programming language + neural network, both built from scratch on Android.

Post image
0 Upvotes

I implemented a neural network from scratch in my **own programming language**, San

I've been developing my own programming language called San in python, and I wanted to test it with something more interesting than the usual interpreter demos.

Code for neural network: https://github.com/anubhav-1207/san/blob/main/examples/neural_network.san

Docs for the neural network: https://github.com/anubhav-1207/san/blob/main/docs/neural-network.md

I decided to implement a neural network entirely in San. That learns XOR from 17 random numbers over 2000 epochs.

Architecture:

  • Input layer: 2 neurons

  • Hidden layer: 4 neurons

  • Output layer: 1 neuron

  • Activation: Sigmoid

  • Loss: Mean Squared Error

  • Training: Stochastic Gradient Descent with Backpropagation

Everything is implemented using San's native values, arrays, functions, loops, stdlib and arithmetic Like, NumPy, PyTorch, TensorFlow, or any machine-learning libraries.

What it implements:

  • Forward propagation

  • Sigmoid activation

  • Backpropagation

  • Gradient descent

  • Loss calculation

  • Training over multiple epochs

  • Predictions on the XOR dataset

A little background

San is a programming language I've been building from scratch. Interestingly, I developed it entirely on an Android tablet, without relying on a personal PC.

Y'all might say that python isn't the best language for lang dev but it was my school project so they won't accept any other language except python.

Repository: https://github.com/anubhav-1207/san

I'd particularly like to hear what other computational experiments would be interesting to implement in a small interpreted language like this.


r/Compilers • • 2d ago

MLIRBench - A Benchmark and Dataset for Compiler and Hardware-Aware Reasoning

Thumbnail
arxiv.org
2 Upvotes

r/Compilers • • 2d ago

v2.2.1 is ready!

Thumbnail
reddit.com
1 Upvotes

Tons of improvements have recently hit the deck, ready to be checked out and tinkered with! We are now fully self hosted and no longer require LLVM or Python post bootstrap.

Before we get to those upgrades though, a quick note, the language was renamed to Ideka due to discoverability issues with the name Flux.

First up, we now have full support for both x86_64 and ARM64.

Secondly, we have a brand new thread-safe version of the standard heap allocator!

We’ve fixed a codegen bug that was breaking SHA384, as well as expanded the cryptography library significantly. This upgrade was followed by the addition of a full native TLS v1.3 implementation as well.

Third, Discord bots, anyone? Yes, Discord.IDK has arrived. While still in beta, the library allows you to do most basic things save for interactions and a handful of other features.

And last but not least, SQLite is nearing completion and will be released within the next 6-8 hours.

We hope you enjoy all the new changes!

Get the compiler at GitHub


r/Compilers • • 3d ago

Introduce LLVM Z80 Backend

Post image
116 Upvotes

Hello, I'm posting here because I think many people might be interested in our project.

We are developing a complete Z80 backend for LLVM. It currently supports the Z80 and SM83 (Game Boy CPU) and offers performance and stability comparable to existing compilers for Z80.

You can check the benchmark here (though I made it, so it may not be completely fair) :
https://zlfn.github.io/z80-bench/

And you can also try it out on godbolt: https://godbolt.org/z/4v19Gjd9E

Our goal is to create a modern and efficient Z80 compiler that can be used as a toolchain for various retro computing projects.

We are looking for users and contributors for our project! If you like the project, feel free to try it out, and if you have any suggestions for improvement, please let us know through project issues or discussions. Thanks!

https://github.com/llvm-z80/llvm-z80


r/Compilers • • 2d ago

I built LuaC — a standalone Lua-to-machine-code compiler (JIT & AOT) in C99 with a custom Vulkan Studio & Android port

0 Upvotes

Hey everyone! I've been working on LuaC, a standalone compiler that translates Lua-like \`.lua\` files into machine code via generated C — without using the original Lua VM or LuaJIT.

Highlights:

\- JIT & AOT: Compiles to native shared libraries (\`dlopen\`) at runtime or standalone executables.

\- Ownership + Cycle Collector: Deterministic cleanup via reference ownership cells + lightweight cycle collection for cyclic tables/upvalues (no stop-the-world GC threads).

\- LuaC Studio (Vulkan GUI): Built with Nuklear + a custom Vulkan renderer, featuring a code editor and a 2D Engine mode (60 Hz fixed-step physics).

\- Android Port: Includes an APK build with multi-file project support and Vulkan viewport.

\- Batteries included: 197 native APIs (JSON, TCP/UDP, mbedTLS HTTPS/WSS, miniaudio DSP, stackful coroutines).

Currently in Alpha 0.5.0 with prebuilt binaries via GitHub Actions. Would love any code review, C99/Vulkan feedback, or suggestions!

GitHub: https://github.com/ehekufa/LuaC/releases/tag/Alpha-0-5-0


r/Compilers • • 2d ago

Spectre - A design-by-contract programming language with an explicit allocator-centric API that outperforms Zig by up to 6x on General Purpose Benchmarks

Thumbnail spectrelang.org
0 Upvotes

r/Compilers • • 3d ago

Adapting CPython for yk producing a VM with a meta-tracing JIT compiler

Thumbnail tratt.net
4 Upvotes

r/Compilers • • 2d ago

My Programming Language

0 Upvotes

Hey everyone! In my last post, I briefly mentioned that I've been tinkering with my own programming language. Well, it's finally time to properly introduce it: Meet - Kir.

​Kir - is a statically typed language built completely from scratch - including its own custom compiler and virtual machine. My goal was to create something with a clean, readable syntax (heavily inspired by Kotlin, which I've used a lot) while giving the language its own distinct identity.

​Here’s a quick taste of what it looks like:

fun calculateDamage(base: int, multiplier: float): float {

let damage = base * multiplier

return damage

}

fun main() {

let baseDamage = 25

let multiplier = 1.5

let result = calculateDamage(baseDamage, multiplier)

}

​The project is still under active development as I flesh out features, optimize the architecture, and nail down the design. I'm aiming to drop the first public version in 2027.

​Super excited to share more updates as things come together! What do you guys think?


r/Compilers • • 3d ago

Tessera: a tensor-first language with an LLVM backend and a self-hosting bootstrap. I did the initial design and implementation by hand, then built the rest with Codex and Claude

3 Upvotes

I've been working on Tessera since late 2024. It's an experimental, tensor-first language for numerical computing and data analysis, and it compiles to native code through LLVM. The name comes from tesseract, a 4D tensor.

GitHub: https://github.com/dyanos/csec-

(The repo is named csec- and the compiler binary is csec++. Both are historical names; the language itself is Tessera.)

Docs, if you want to look before commenting:

How it was built

I want to be upfront about this:

  • I did the initial design and early implementation by hand. That covers the language concept, the lexer and parser, the AST, LLVM IR generation, constructors, C++-style name mangling, function argument types and external functions, and the first LaTeX-style math syntax.

  • After that, most of the new code was written with AI coding agents. First I used OpenAI Codex, which did the native runtime, tensor and simulation features, native interop, templates, and the first self-hosting bootstrap. Later I used Anthropic's Claude (Claude Code), which did the ownership checker, async/await, microthreads, the standard library, LLVM 18–23 support, and Parquet data frames. About 110 of the ~200 commits are co-authored with Claude.

  • My role shifted to designer, reviewer and tester. I decide semantics, review changes, and build the verification that keeps the agents honest. Strict tests turned out to be essential: without them, agents confidently break things.

Compiler overview

  • Frontend: a hand-written C++17 lexer and parser, then the AST, then semantic and ownership checks.

  • Backend: direct LLVM IR generation. Output can be a native exe (the default, via llc plus a linker), an object file, .ll, or a JIT run. --static links the runtime into the executable.

  • LLVM 18 through 23 build from one codebase, through a small compatibility header (src/llvm_compat.h). One LLVM 23 change was nasty: getTerminator() changed behavior silently instead of failing to compile.

  • Native runtime: a separate library for I/O, math, sockets, threads and dynamic library loading. [@DllImport] imports native symbols, and C++ name mangling covers both Itanium and MSVC.

What the code and codegen look like

A small affine layer:

def affine(x: Tensor<Double, 1, 3>,
           w: Tensor<Double, 3, 2>,
           b: Tensor<Double, 1, 2>): Tensor<Double, 1, 2> {
    return (x @ w) + b;
}

Tensors are runtime objects of the form %csec.tensor = { i64 rank, ptr dims, i64 count, ptr data }. @ becomes an i/j/k loop nest. This is the inner k loop from --emit-ir, which by default runs only mem2reg:

tensor.matmul.k.body:
  %3 = mul i64 %tensor.matmul.i.i.0, %tensor.matmul.shared
  %tensor.matmul.left.index = add i64 %3, %tensor.matmul.k.idx
  %4 = mul i64 %tensor.matmul.k.idx, %tensor.matmul.cols
  %tensor.matmul.right.index = add i64 %4, %tensor.matmul.j.idx
  %5 = getelementptr double, ptr %tensor.data8, i64 %tensor.matmul.right.index
  %tensor.matmul.right = load double, ptr %5, align 8
  %6 = getelementptr double, ptr %tensor.data, i64 %tensor.matmul.left.index
  %tensor.matmul.left = load double, ptr %6, align 8
  %tensor.matmul.product = fmul double %tensor.matmul.left, %tensor.matmul.right
  %tensor.matmul.current = load double, ptr %tensor.matmul.sum.ptr, align 8
  %tensor.matmul.next = fadd double %tensor.matmul.current, %tensor.matmul.product
  store double %tensor.matmul.next, ptr %tensor.matmul.sum.ptr, align 8
  ...

The + b becomes a flat loop over the data buffers:

tensor.elem.data.body:
  %tensor.elem.l = load double, ptr %tensor.elem.left, align 8
  %tensor.elem.r = load double, ptr %tensor.elem.right, align 8
  %tensor.add = fadd double %tensor.elem.l, %tensor.elem.r
  store double %tensor.add, ptr %tensor.elem.dst, align 8

Ownership is strict by default, with an explicit move <- and borrow &:

def peek(acct: &Account): Int = acct.balance()   // borrow
def close(acct: Account): Int = acct.balance()   // takes ownership

val acct: Account = new Account(100);
peek(&acct);          // ok, acct still owned
close(<- acct);       // moved
// using acct here -> compile error: "use of moved value 'acct'"

Erlang-style microthreads:

import std.microthread

def main(): Int {
    val parent = self();
    val worker = spawn([=]() -> {
        val n = receive();
        send(parent, n * 2);
        return 0;
    });
    send(worker, 21);
    return receive();   // 42
}

Parts that might interest this sub

  • Self-hosting bootstrap. A second compiler written in Tessera itself (lexer, parser and an LLVM-text emitter) compiles its own source to a fixed point. Emitter changes are risky because the compiler compiles itself with them, so a codegen bug can surface one stage later. The fixed-point check caught a lot of agent mistakes.

  • Ownership checking. Types are Copy or owned (affine), with explicit move and borrow. Move checking tracks each path separately through if branches and while loops, and owned locals are freed on every return path. Full drop glue for heap classes is still unfinished.

  • Concurrency runtime. Microthreads get their own stacks and a hand-written assembly context switch on POSIX. Blocking pipe, shared-memory and mailbox operations yield to other microthreads instead of blocking the OS thread. async/await uses the same runtime, with a rule against holding a borrow across an await.

  • Byte-level I/O. String byte indexing compiles to a single load, and there's memory-mapped and chunked file input. A parallel wc -l written in Tessera runs at about 92% of a hand-written C++ mmap+SIMD version.

Current state and what's naive

Right now I'm working on performance, starting with std.dataframe (Parquet/CSV data frames). The tensor codegen above is still the first, straightforward version:

  • Shapes aren't used by codegen yet. The shape is in the type, but dimensions are read from the runtime header. Trip counts aren't constants, and shape mismatches aren't caught at compile time.

  • Temporaries leak. The tensor created by x @ w is never freed, because tensor temporaries aren't part of drop elaboration yet; only named locals are.

  • Loop counters can't become registers. They are allocas emitted inside the loop body, so mem2reg can't promote them. -O2 cleans up much of this, but the frontend should emit it correctly.

  • There's no fusion. (x @ w) + b makes two passes and allocates two tensors.

The plan is to fix these while working on dataframe and tensor performance, probably by adding a tensor-level middle IR before LLVM.

Testing

There are positive and negative fixture suites, semantic and ownership suites, and concurrency and stdlib tests. The same suites run through both the C++ compiler and the self-hosted path. Every tests/positive fixture compiles.

Feedback I'd like

  1. How you'd structure drop elaboration, including temporaries, in an AST-to-LLVM compiler with no MIR-style IR.

  2. Whether a tensor-level middle IR (static shapes, fusion, tiling) is worth adding before LLVM, or whether it's better to lean on LLVM's loop passes and polyhedral tools.

  3. Columnar dataframe kernels: what tends to matter most in practice. Is it vectorized column loops, avoiding materialization, or the I/O and decoding path?

  4. Experiences, good or bad, with AI coding agents on compiler codebases.

Thanks for reading!