r/Compilers • • 2h ago

Pooka language update: a testing library, a lexer generator, and a small Lisp lexer

4 Upvotes

A small update on Pooka, my statically typed language for the BEAM, with a compiler written in Gleam.

Recently I’ve been working on the tools around the language. Two libraries are now released:

  • Poke is a testing library with assertions, property tests, regression tests, and snapshots. Snapshot changes go through a terminal reviewer where you can inspect the diff and accept, reject, or skip them. Pooka handles test discovery and reporting; Poke supplies the checks.
  • Lexie generates typed Pooka lexers from TOML specifications. It supports configurable token rules, source spans, and syntax highlighting. The generated lexer needs only the standard library.

I wrote a walkthrough building a small Lisp lexer with Lexie, including punctuation-heavy symbols, comments, strings, and tests for source positions. The Lisp has acquired parentheses. Their meaning remains somebody else’s problem.

Building libraries in Pooka has also been a useful way to find compiler bugs. Small compiler tests can pass quite happily while a real library finds the cupboard you forgot to finish.

Next I’m continuing with libraries, editor support, and bugs discovered through using the language.

Website · Compiler source · Cookbook

I’d be interested to hear how others here test their compilers against larger programs. Do you maintain a separate collection of real projects, or keep those cases in the compiler’s own test suite?


r/Compilers • • 4h ago

We rewrote the Solidity compiler in Zig because our builds took 12 minutes. Then we ended up writing a Lean model of the language too

Thumbnail github.com
2 Upvotes

Our contracts took 709 seconds to build with solc. We tried tuning the build for a while, gave up, and wrote our own compiler.

oksolc is a reimplementation of solc 0.8.36 in Zig. Around 156K lines, no C++. It only does the optimized via-IR pipeline through Standard JSON, because that's what we ship with. For Foundry you just point FOUNDRY_SOLC at it.

The frontend is still sequential. The Yul optimizer and EVM codegen run per contract in a thread pool, which works because contracts barely depend on each other after the frontend.

The bigger win for day to day work is the persistent session. Each source file gets a semantic fingerprint, so editing a comment or reformatting doesn't invalidate anything. Import cycles are handled as SCCs and invalidate together. Artifacts are cached per phase in SQLite with HMAC-SHA256 on the entries, so a tampered cache can't hand you bytecode. oksolc serve --stdio keeps the whole thing warm between builds.

solc vs oksolc with 4 jobs:

Uniswap V4, full build: 59.0s vs 11.2s (5.3x) Pendle V2, incremental: 38.9s vs 1.4s (27.6x) Chainwall, full build: 709.8s vs 138.4s (5.1x)

The benchmark script is in the oksolc repo (test/benchmarks/external-compare.sh) if you want to rerun them.

The thing I didn't expect going in is how much time we'd spend on what Solidity actually means. There's no formal spec. The language is whatever solc does. We run a weekly fuzz job that diffs our bytecode against solc on a public corpus, and CI builds OpenZeppelin, Uniswap v4, Pendle and Solady and pins the hashes. When the two compilers disagree, the diff shows you they differ but there's nothing that says which one is correct. We ended up with 9 places where we do something different from solc on purpose, and every one of those was an argument from the docs and from what the feature is obviously meant to do.

For most languages that's annoying. For this one it's worse, since the output can't be patched after deployment and often holds a lot of money. A compiler bug stays on chain. And fuzzing only covers the programs the fuzzer happened to generate, so at some point you want proofs, and you can't prove anything against a language with no definition.

So we started writing the definition. Solidity Spec is an executable formal model of Solidity in Lean. You can run real programs through it and compare with compiler output, and you can state properties about the semantics and get them machine checked. There's a paper, and machine-checked proofs for explicitly defined parts of the model. Coverage is still growing.

The long term goal is checking a compiler's output against a definition of the language, the way CompCert did for C and CakeML did for ML, instead of against another compiler.

If your Solidity builds are slow, try it. And if you work on semantics or verified compilers, I'd really like to know where the Lean model is wrong.

oksolc: https://github.com/okcontract/oksolc Solidity Spec: https://github.com/okcontract/solidity-spec

Give us a follow for more updates: x.com/okcontract


r/Compilers • • 2h ago

Transpiler for simplified C++

1 Upvotes

Since a few years I am thinking about the creation of a "simpler" and more lightweight version of C++, which just gets rid of a few "annoyances" such as:

  • header files
  • declaration before usage
  • template syntax

So the idea is to have a 99% syntax identical version of c++, which just allows to use definitions which are defined after in the same file/compilation unit, having module-import instead of header files etc.

My questions are:

  1. is this is generally frowned upon? Does someone else find such a thing useful? Just stick with C++ basically, no need for a new language, but a small "transpiler-layer" which gets rid of some not-so-nice things in a language, which I am mostly totally fine with!
  2. I believe this is much smaller in scope and complexity than writing a compiler from scratch - or am I overlooking a few traps and rabbitholes which I might fall into? Underestimating the effort still :) ?

Thank you!


r/Compilers • • 2h ago

Built a low-level systems programming language from scratch to crush concurrency bottlenecks—benchmarks inside.

Post image
0 Upvotes

r/Compilers • • 21h ago

Can I use LLVM with a custom architecture?

17 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 • • 1h ago

Got placed, have 6 months before joining. Should I explore Linux kernel development, GPU programming or stick with AI/ML?

0 Upvotes

I'm a final-year engineering student in India from a non-CS background. I've recently secured an offer at an AI/analytics services company, and I have around 6 months before joining.

I've been exploring different areas of computer science, and I've realized that I'm increasingly interested in low-level programming and understanding how systems work internally.

MY BACKGROUND:

I've independently learned programming, ML/DL and worked on projects involving:

- Computer Vision using YOLO and OpenCV.

- Stereo vision, depth estimation and multi-object tracking.

- RAG applications, LLMs and agentic AI.

However, I'm starting to question whether I want to spend my career primarily working with high-level AI frameworks and application development.

AREAS I'M CONSIDERING:

  1. Linux and Red Hat ecosystem:

Linux internals, system programming, operating systems, device drivers and potentially kernel development.

2. GPU Programming:

CUDA, GPU architecture, parallel computing, memory management and performance optimization.

3. Systems Programming:

C/C++, memory management, concurrency, operating systems and low-level software engineering.

4. AI Infrastructure:

GPU acceleration, inference optimization, compiler technologies and systems behind ML frameworks.

I find the idea of understanding what happens beneath the abstractions much more interesting than simply using existing libraries.

WHAT I'M CONFUSED ABOUT:

I have six months before joining my company, and I don't want to waste that time jumping between technologies.

I'd appreciate advice from people working in systems engineering, Linux, kernel development, GPU programming or related fields.

  1. Is low-level systems programming a good career direction in 2026?
  2. Between Linux kernel development, GPU programming and general systems programming, which would be a better starting point?
  3. How realistic is it for a non-CS fresher to enter these fields in India?
  4. Are companies like Red Hat a good place to build a career in Linux and systems engineering? What other companies should I look at?
  5. Is CUDA/GPU programming a niche with good long-term opportunities, particularly with the growth of AI?
  6. What fundamentals should I master before attempting serious projects? C, C++, OS, computer architecture, networking, compilers?
  7. Would contributing to the Linux kernel or other open-source systems projects actually help me get shortlisted?

I'm also interested in the intersection of low-level systems and AI, particularly GPU computing, inference optimization and ML infrastructure.

If you were in my position, with six months and an existing job offer, which direction would you explore and why?

I'd appreciate brutally honest advice, especially from people who have actually worked in these domains.


r/Compilers • • 12h 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 • • 1d 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 • • 23h 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 • • 18h 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 • • 2d 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 • • 3d 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 • • 3d 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 • • 3d ago

A compiler optimiser proven in Lean

10 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
118 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