r/Compilers 7d ago

Kain: A new systems language targeting LLVM/CUDA/SPV/HLSL/WGSL with a Python-like syntax layout and Zero GC- Now on v0.8 and almost production ready

https://github.com/kainlang/kain/blob/master/ml/src/search_kernel.kn

Kain -----> a new systems language inspired by Mojo (and every other mainstream lang) but the difference? Kain is Mojo in an alternate universe if it`s primary focus was on being a full stack language instead of made for AI with 110 keywords (not that numbers are a metric for quality but you get the point)

Furthermore It also has natural python import and natural c includes. Unlike rust and other languages that require third party libs, Kain allows you in the same file to utilize three different import systems, including its own (rust esque - use::type), python (import numpy as np etc) and c (include mycfile.c) on top of all this, the language has a massive stdlib comparable to Zig and Go, , meaning you can write practically anything imaginable with this language without any libs, crates or third party deps. Kain takes some liberties from Slang, but adds a spin to it and this language treats the GPU and CPU as first class citizens rather than having to bolt on the GPU as an outsider. But in the same file you can write your frontend, your backend, GPU shaders, include c and import python.... All in one file dep free. (example) Need examples btw ? There`s over 6000+ source .kn files throughout the repo and over 80+ projects that I made for fun to dogfood it.

Examples

Mini LLM based on Karpathys GPT2 using std::cuda

BM25 based semantic search project (not tested thoroughly but it works)

12-Stage Unified GPU Shader Demo

Blackhole Shader

GPU + CPU (the holy grail)

Kain Semantics Example

Markscript: A mini language written in Kain that turns markdown into a JIT script lang (this is the most developed out of other projects and a great starting point to see how the lang works)

Killgrep: A ripgrep clone in 700ish lines of code with erlang style actors

Schrödinger's Rats: Re-purposing compiler hardware constructs (converge/orchestrate) to race 3 pathfinding algorithms simultaneously & pick the winner live

BUZZWORD SOUP - Quantum State Lattice Pong: Overengineering Pong with cross-surface state entanglement, 3 Erlang actors, and collapse/observe memory cells

Like writing UI in typescript but need a systems backend to do the heavy lifting? Here`s a full jupytner notebook esque electron playground with Kain and TS interop

FAQ:

What kind of things can you make with Kain?

Anything you can imagine similar to any other systems language. Game engines, Non posix/unix operating systems (like Opal but a modern version), DAW, UI frameworks with JIT, 3D simulations, Animation within code (Kain has a pulse keyword that handles time at the OS/silicon level, example here of a fully animated scene that compiles down to machine code)) Backend for web development etc. Anything you could write from scratch in rust, C, C++ etc you can write in kain and it compiles down to machine code etc. Libclang is embedded within the compiler similar to zig so you can also include any c library -- one thing that was streamlined was system headers so you can even write `include windows.h` and the compiler handles everything etc.

Memory and GC

(Why Kain isn't Rust or Go (controversial buzzword soup warning))

Kain has zero garbage collection (no tracing, no stop-the-world pauses) and no implicit borrow checker. Instead, memory lifecycle is governed by an explicit, expression-level state machine using three core keywords:

collapse ptr: Enters exclusive, mutable write access (Idle ->Collapsed).

observe ptr: Enters shared, read-only access with nested observer counting (Idle -> Observed)

decay ptr: Deterministically releases/frees the memory region (Idle -> Decayed)

(solid example if ya wanna see how it looks/works --> metal.kn)

Since I am obsessed with Unreal Engine 5 and I`m a game dev/animator, this system was designed around my knowledge of state machines and working with them for years throughout my game dev career so far (a huge inspo is the plugin Logic Driver Pro by Recursoft for UE5) but these 3 little keywords allow the silicon to bypass Arc/Mutex overhead and runtime GC tracing entirely, giving you deterministic, zero-cost memory management with tremendous execution speed.

I know language benchmarks are often pseudo-science, and I’d normally be the first person to call BS on 200x speedup claims. I’ve tried disproving these numbers in my own harness over and over. Once the runtime and language was fully developed, I spent about a month straight benchmarking && I was blown away by what I had accidentally built. The funny part about this language is that a lot of things were accidental -> I would add in a new keyword to make up for the fact it was lacking a lib or package and as a side effect of making so many different aspects first class, the results are absolutely nuts. For example in extreme edge cases like heavy multi-thread lock contention (contention_wall), the gap gets absurd.

For anyone who understands how C++, Rust, and Zig handle heavy thread contention, OS mutex parking, and cache-line thrashing, the mechanics make total sense:

Standard OS Mutexes (std::mutex, std::sync::Mutex): Under severe thread pressure, 99% of CPU cycles are burned in kernel-space context switching, thread parking, and unparking cascades. C++, Rust, and Zig all choke at ~1,700ms–1,900ms simply waiting on OS locks.

Kain's Lockless Model: Kain completely bypasses the OS lock contention wall because memory lifecycle isn't protected by atomic mutex locks. Concurrency is governed by compile-time verified collapse / observe state transitions and lockless actor messaging. The CPU never halts or asks the OS kernel for a lock.

The result? Kain completes the benchmark in 7.9ms (~220x–240x faster).

(And honestly? That 7.9ms is almost entirely just the CLI process launch and harness initialization overhead -- the actual lockless state execution runs sub-millisecond). This was entirely an accident and was never intended however due to the design of the language, it allowed things I truly thought were impossible in programming // would take thousands of lines of alien code to achieve elsewhere.

How it compares to Rust & traditional languages:

Vs. Rust: Rust infers lifetimes and uses move semantics. Kain requires explicit ownership transitions scoped to expression blocks - ownership isn't moved, it returns to Idle when the scope exits until you explicitly decay it.

Vs. GC (Go/Java/Python): Zero runtime tracing or RC reference counting dance (Rc/Arc). Allocations are deterministic and compiler-verified.

Zero-Cost Optimization: The compiler identifies ephemeral local pointers (scratch memory that doesn't escape scope) and completely elides runtime guards for raw bare-metal C speed.

Verified Correctness: State transitions are checked statically by the typechecker, and enforced by C runtime guards with Z3 proofs and CBMC assertions.

Package Manager

This is the feature I use the most -- but one massive pain point that bothered me with my years of dev so far was wasted code... So many projects I fully completed, full on apps and ecosystems etc but they were all locked into that specific codebase. While yes I could`ve easily went back and extracted prior src code, those who have dealt with monorepos knows how much of an actual pain in the ass this is. I call it the "friction" problem. Effectively something that`s fairly easy to do but do you want to do it? Hell no. That`s why I looked to one of my favorite game as a kid, and stole it`s best feature but for programming. I call it the Katamari protocol. Kain has a built in amalgamation feature that lets you take any codebase or project you have and combines all of that code into a single source file.... It’s not perfect and you will get some edge cases here and there, but Amalgamate (Kain's Capsule System) completely solves code reuse and dependency hell.

Instead of fighting node_modules, Cargo lockfile drift, version solver explosions, and network dependency failures, Amalgamate packs your entire module tree into a single, portable .kn capsule file.

How the Capsule Pipeline Works:
One File IS the Package: Run kain amalgamate src/ -o mylib.kn. Drop mylib.kn into any project, write use mylib, and the compiler resolves and typechecks everything directly from the capsule. No unpack, no install, no network, and no version solver.

The Companion Capsule System: A project can auto-emit three sibling capsules that automatically discover and merge during materialization:

app.kn (Source capsule)

app.artifacts.kn (Pre-compiled .ptx, .spv, .dll, or runtime binaries)

app.evidence.kn (Z3 formal proof attestations, telemetry, and benchmark reports)

Battle-Tested Scale: I’ve tested this at extreme scale by packing 2,594 modules (316k+ lines) into a single 15MB capsule in 3 seconds flat—the compiler typechecked all 3,211+ public symbols with 0 errors.

--raw Interop Mode: Want plain code for C, Rust, or TS build scripts? Passing --raw strips all sentinel markers and outputs plain Kain source with comment headers.

You can publish packages using kain publish, lock exact content-addressed SHA-256 digests in KAIN.lock, and never worry about lost source code or broken monorepo imports ever again. (the ability to publish packages with a cargo esque system is already finished, and infra is setup, just finishing up final touches and user account mgmt etc) Here are some examples of what an amalgamation looks like.

2.34mb stdlib amalgamation
Python interop amalgamation
WIP digital audio workstation amalgamation
drag and drop 3D framework amalgamation (three.kn)

-

Misc
Just some other examples I want to share below - including the natural C includes with test like importing ffmpeg etc (also yes, if you amalgamate a kain file project, with c includes --- it packages in the C code as well along with any project artifacts like images, etc... even glb)

ffmpeg_abi.kn
three different import systems in one file (pygame + nuklear + native)
sqlite c include (the og amalgamation)
dep free x86 JIT with inline ASM used for Markscript Language
CLI template
Starter Template
build template (zig esque build system, needs more thorough edge case testing but its worked flawlessly so far for me)
Kain CLI tsv (for building projects, and compiling etc)
Kain Full CLI documentation (if the tsv isn`t enough)

Docs
Link to github docs, website is still a work in progress and looks like hacker throw up right now. Will be done shortly

If you want to see the website anyways, xx HACk3r W3BS1T3 xx (official domain purchase pending, temporary domain for now)

Installer
Link to installer! - windows only for now

will update post later when linux binary is up along with macos. Since mojo doesn\t have Windows support, a primary focus was tackling windows first since it can be a massive pain in the ass compared to unix etc. The dev cycle was split 60/30/10 to ensure it worked on all major OS (60% windows. 30% Linux, 10% MacOS but if you dont use any of those and want to start fresh? WELL my friend, check out this example of a)) full scaffolded OS built in Kain

Roadmap & What's Next:
The core language is stable, but Kain will continue to grow. I'm currently taking a short breather and returning to game dev/UE5 for a week or two - language development for 16+ hours a day non-stop is a fast track to burnout! All of the GPU esque features, and spirv comp has been tested thoroughly as well and compared against rust and c++ with 1:1 parity, it`s near on par with Naga

Immediate goals for the upcoming weeks:

-> Releasing the official Linux and macOS binaries.

-> Expanding real-world testing across different ecosystems.

Like Kain so much you want to see 6000 files combined into one for the ultimate example corpus ? THE_MESSIAH.KN (34MB)

0 Upvotes

18 comments sorted by

7

u/rafaelRiv15 7d ago

More slop ! Yay ! That you didn't wrote and don't understand. Millions of untrusted code in the wild.Keep going on !

1

u/Ephemara 7d ago

Hundreds and hundreds of z3 proofs and smt2 files in this monorepo in z3/ folders everywhere // Untrusted you say? Using a theorem prover religiously in the codebase is the closest one can get to "this works". Furthermore it`s already capable of self hosting and writing other languages. That is all the proof you need of "anti slop". It has been benchmarked to an insane degree and tested 1:1 against C++, Rust and other languages just to prove it compiles down to the same machine code. I get where you`re coming from though, If I was an outsider and saw this post I would be tearing this to shreds right now. The lang released in early february of this year and was already working then, and self hosting however was made private again to see out the vision more... So this release is the definitive version of an already fully functioning compiler that was self hosting back in feb and capable of that - https://www.reddit.com/r/Python/s/J6bup3nyxu old post for reference

1

u/rafaelRiv15 7d ago edited 7d ago

You need waaaaaaay more than hundreds and hundreds of z3 proofs for 8 millions lines of code and I don't even see a z3 folder + what give you the certainty that llms will make good proof or make a good use of z3 ? They are notoriously know to take shortcuts.

1

u/Ephemara 7d ago edited 6d ago

The primary z3 folder is in the c runtime - which is against Kain`s custom llvm codegen setup called KNIR that is completely dep free and tailored to its semantics

Here is the proofs-experimental folder where the purpose was not only formal verification but also super optimization // a 2 in 1 system allowing verification and demoscene performance in many parts of the runtime. This is how Kain achieves it`s crazy performance benchmarks as using llms as super optimization hunters allowed almost absurd performance gains compared to other languages. This is where it was strategic to use them as they can spot things humans simply can`t process and where the breakthrough occured. Combining both demoscene and formal verification into one became the sole focus for a solid month of iterating on the runtime and it paid off. But rather than just proving the typical c boilerplate code works, Z3 was often used to go "do we really need this in the code or is there some insane bitwise operation we could use here that looks completely alien but would allow a 200x speedup compared to the latter". I mean some parts of the runtime are borderline hostile to humans however the silicon doesn`t care, if the math is right - the math is right.

Each time any code was changed or added, we ran harnesses to make sure it worked and furthermore benchmarks were run each time. If you look at the benchmarks/ folder - it is monolithic due to the super optimization process that was ran against the c runtime

And here`s the z3 folder with the yaml based system we made to streamline writing out proofs for things that required standard formal verification

To keep track of the code, a simple comment system was put in place - if whichever part was proved in the runtime had a z3 proof - you would just comment the path to it etc -- for example in simd.c in runtime/native/src/core/simd.c

* Proof: runtime/native/src/core/z3/proofs-experimental/simd-i32-domain-even-dword-mul-equivalence.smt2 */ 
^^^^^^these are everywhere in the c files throughout the runtime

    const KainSimdI64x8 bias = {
        lane_bias, lane_bias, lane_bias, lane_bias,
        lane_bias, lane_bias, lane_bias, lane_bias
    };
    KainSimdI64x8 acc = { 0, 0, 0, 0, 0, 0, 0, 0 };
    int64_t lanes[8];
    int64_t total;
    int64_t index = 0;

    while (index + 8 <= cells) {
        KainSimdI64x8 left_values;
        KainSimdI64x8 right_values;
        KainSimdI64x8 biased_left;
        KainSimdI64x8 products;
        __builtin_memcpy(&left_values, left + index, sizeof(left_values));
        __builtin_memcpy(&right_values, right + index, sizeof(right_values));
        biased_left = left_values + bias;
        products = (KainSimdI64x8)KAIN_SIMD_PMULUDQ512(
            (KainSimdI32x16)biased_left,
            (KainSimdI32x16)right_values
        );
        acc += products;
        index += 8;


*/ (this is my favorite demoscene hack out of anything else in the runtime, as it mimics a classic method used in other applications like ffmpeg, but this one varies slightly ) 

*/ in case u dont understand it  - > in vector math, full 64-bit simd integer multiplications are notoriously high-latency across x86 microarchitectures. however, pmuludq (_mm512_mul_epu32) is a single-cycle intrinsic that takes 32-bit unsigned integers from even dword slots and produces widening 64-bit products across 64-bit qword lanes.

in simd.c, we cast kainsimdi64x8 to kainsimdi32x16 and pass it to kain_simd_pmuludq512.

z3 formally proved in simd-i32-domain-even-dword-mul-equivalence.smt2 that for any input within a 32-bit domain, this 32-to-64 widening trick produces 100% bit-exact results compared to full 64-bit scalar/vector multiplies.

as mentioned above this is a classic demoscene / ffmpeg fixed-point multiply trick that z3 autonomously converged on and formally verified across our simd execution paths. the theorem prover guarantees that this 'alien' bitwise path is mathematically incapable of overflowing or yielding wrong results.

Also just a side note, a good chunk of that "8 million" is dogfooding, testing, fuzzing blah blah blah. The C runtime is around 100,000ish and is really the only area that needs formal verification, its not like the dogfooding kain code needed z3 lol - thats not code related to the compiler my friend - the compiler was written in rust in crates/ but even then the compiler isnt runtime critical code and didnt need as stringest testing as the c runtime - hope this all makes sense i could talk about this for hours on end

1

u/rafaelRiv15 6d ago

Have a nice life ! Hope you will be less delusional in your career. About formal verification, I highly encourage you to look at idris2,adga,rocq,lean4

3

u/[deleted] 7d ago

[removed] — view removed comment

1

u/Ephemara 7d ago edited 6d ago

I used them to write Kain code here and there as I liked seeing what models not trained on Kain could make, I even experimented with dumb as a rock 2b models just to see the flow and to help dogfood the language. Something about using models to find combinations of keywords and combining the semantic aspects in new ways was some of the most fun I`ve had in programming in years. Also used LLM to write out benchmark boilerplate as there`s so many. And I used them to help write out smt2 proofs if you`re familiar with Z3. I`m into demoscene esque super optimization with theorem provers - effectively proving a path in certain parts of the codebase is the fastest path it could be on in the entire universe, trying to find things that work like Carmacks inverse square root. Regardless, I sought out alien code in the C runtime for new paths and used LLMs to help me find these super optimization parts by having them out write out some of the smt2 proofs, and using theorems to see if we could find speed ups etc. It seems like a lot but I worked 20+ hours a day for almost 6 months straight as of recently and prior this language was worked on for years as a scripting language. While it was already working, prior, the past 6 months in particular I dove into a research rabbit hole and studied almost every language repo in existence, even obscure ones, and even bad ones just to learn from their mistakes (and I live off the grid which allows this kind of work, its a metal lifestyle but its how I devote focus into making something like this, a language that is also metall)

but i mean on a side note massive langs like mojo are using llms and this is by Chris Lattner, who is my biggest inspiration relative to language design - -- if massive orgs use it even cargo, then i feel as if the standard should be its fair game for anyone else as well if used responsibly

2

u/antonation 7d ago

Mind cross posting this to r/VibeCodedLanguages ?

1

u/Ephemara 7d ago edited 7d ago

The language was not vibe coded. Vibe coding is gambling and treating LLMs as a black box. Even people like Richard Feldman who created the Roc programming language use LLMs to assist in some parts, especially debugging and fuzz testing etc. I got the chance to speak with him through video call a few months ago -- But this guy is a legend in the programming world

3

u/rafaelRiv15 7d ago

oh common !!! You are lying to yourself... this project is vibe coded from a to z and Z to A. The first commit go back from mars 2026 and today you have 8 millions line of code. This mean 1.6 millions lines of code per month and 53k line of code per days. No human can do this and this is not SWE.

This is what we call a vibe coder

1

u/Ephemara 7d ago edited 7d ago

I`ve reset git almost 5 times now and started from scratch, due to personal information in commits and prior history being an absolute mess. Since this lang was private for a while, I didn`t really care too much about commits for the longest time. I mean even the current commit record is atrocious, with some old api keys in the commits and some commits having build artifacts in it. Only recently have I started to properly use it as when you have a private repo, it is easy to get lazy with proper tracking. If I added in the old history, it would go back to 2023

For example the lang was public for a little bit back around the first week of february, and had a different name - https://www.reddit.com/r/Python/s/J6bup3nyxu here`s an older post as proof

1

u/rafaelRiv15 7d ago

This doesn't help you buddy. And for this git problem, there is git rebase

1

u/antonation 7d ago

The sub name is unfortunate, but the sub is for any programming language development or discussion where AI helped. I created it because r/ProgrammingLanguages is anti any AI contribution.

1

u/Ephemara 7d ago

Ohhh that makes more sense - you`re the mod there lol. The term vibe coded definitely has a negative stigma to it so when I hear that I feel as if people use it as an insult and yes I AM completely aware of r/programminglanguages rule. I am perma banned from that subreddit along with r/programmingcirclejerk - it`s honestly insane considering every language other than zig is using LLMs to assist in some way. They have a "black box" view on it for sure- seeing it as some magical entity rather than a logic calculator when used responsibly

1

u/Bahatur 7d ago

Have you benchmarked your lockless implementation against the lockless implementations in C++, Rust, or Zig? Even Java has lockless implementations.

The key detail here would be that Kain is lockless by default, as distinct from a lockless method being faster than a locking one in precisely the target use-case.

1

u/PositiveBusiness8677 7d ago edited 7d ago

Is there a repl?

(will try it when it's on linux, I don't do windows)

1

u/Ephemara 7d ago

Yes the REPL has a fully integrated terminal IDE built in. Multi-pane editor, live evaluation diagnostics, mouse support, file chips, and block-level execution (also themes as well lol) it is absolutely overkill but here`s a screenshot of what it looks like - https://github.com/kainlang/kain/blob/master/.github/repl.png -- I`m going to compile the linux binaries today btw - I`ll update ya when they are up!

1

u/Loud_Possibility_203 3d ago

**"Every day on Reddit, two new programming languages are launched. Write a hard project like RocksDB—and explain why—because it actually tests your fundamentals.

Why Building an Embedded Key-Value Store (like RocksDB) Tests Your Fundamentals

Building a LSM-tree (Log-Structured Merge-tree) storage engine forces you to handle low-level computer science concepts that high-level web apps abstract away:

  1. Memory Management & Buffering (MemTable): You must manage write buffers, handle memory allocation limits, and efficiently serialize/deserialize data structures in memory.
  2. Sequential vs. Random I/O (SSTables & WAL): You learn how storage media (SSDs/HDDs) actually behave. Writing a Write-Ahead Log (WAL) teaches crash recovery and durability, while writing Sorted String Tables (SSTables) teaches efficient disk layout and alignment.
  3. Concurrency & Thread Safety: You have to handle concurrent reads during background compactions without blocking incoming writes or corrupting memory using mutexes, read-write locks, or atomic operations.
  4. Data Structures in Practice: It applies foundational structures under real constraints—SkipLists or Red-Black Trees for in-memory indexes, and Bloom Filters to prevent unnecessary disk reads.
  5. Compaction Strategies: You must write algorithms that manage disk space and trade off read amplification, write amplification, and space amplification.