r/Compilers • • Sep 21 '26

Our compiler refuses to spill inside a hot loop — it raises a compile error

I wanted to share a rather radical idea I implemented in the compiler for my new programming language (Maxon).

After struggling with the performance of the register allocator in v1 of the self hosted compiler (74% of compile wall time) and of course most of that time is handling spilling, which is NP complete. You either have a fast naive spiller which kills performance, or an exponential algorithm to try and find an optimal solution (which will probably never be actually optimal).

So I decided to punt instead. If the live values exceeds the available pool of registers you get a compile error. The user then must then rewrite the code to reduce the number of live values. This is effectively what the register allocator usually does (spilling live values) but the reasoning is that the user can do a better job deciding what should be spilled than the register allocator can. Plus the compiler doesn't have to do this expensive and slow job on every compile. And you get a simpler register allocator.

It seems to be working well so far. Source is on github if you are interested.

https://github.com/maxon-lang/maxon

16 Upvotes

25 comments sorted by

50

u/hermeticwalrus Sep 21 '26

That seems annoyingly platform dependent

13

u/jnordwick Sep 21 '26 edited Sep 21 '26

if you're writing code where this matters, it's always platform dependent.

I'm happy to see a compiler or language finally making a decision to side with more advanced developers instead of everything being dumbed down.

10

u/BarracudaDefiant4702 Sep 21 '26

I could see where it might be a nice feature as a warning or lint error, but generally unacceptable for base line.

10

u/Axman6 Sep 22 '26

Making this a compiler/language feature means you always have to care, even when it really doesn’t matter because its code that contributes basically nothing to execution performance. I can see this being useful as a pragma in the language to say “tell me when you need to spill here”, but the majority of code it’s not going to matter for. 

1

u/edgmnt_net Sep 21 '26

It's interesting to debate whether straight asm, a thin veneer over asm or a minimal language/compiler is better for such a thing. I suppose a compiler might be able to select instructions better assuming good knowledge of the CPU.

But ordinarily I'd say straight asm is what you want. Maybe write a few versions for highly-optimized primitives covering main CPU subfamilies and a fallback.

3

u/jnordwick Sep 21 '26

As somebody who write a lot of high performance code where I do have to worry about these things, for me, hands down, a language that offers a lot of control but has some escape hatches. I use mostly C++. I could never inline, constant propagation, etc. And dev time does matter.

I would love a language that is a thin layer over llvm IR really (with some other features), even though I only have to deal wit x64 and AVX2/512 mostly.

14

u/flatfinger Sep 21 '26

The normal default behavior should be to simply spill something and figure that performance won't really matter except when programmers say that it does.

-1

u/ratchetfreak Sep 22 '26

I passed -O2 as a compiler flag, that's me saying performance matters

and when I'm writing/debugging the algorithm with -Od I would still want to be sure it can fit in the available registers

5

u/flatfinger Sep 22 '26

Good languages should allow programmer intention to be expressed in source code, rather than relying upon out-of-band mechanisms like compiler options.

-13

u/tripleshielded Sep 21 '26

Are you making his decisions? Build your own system, with your defaults.

17

u/flatfinger Sep 21 '26

Sorry I was sorta trying to counter the person who said "man I'd just write assembly". Having a means of telling a compiler "If you can't do this efficiently, squawk so the programmer can assist" is useful, but in most code bases performance only really matters for a small portion of the code, and a compiler can't be expected to know which parts those are unless it is told.

1

u/torp_fan Sep 22 '26

It's a conversation, troll.

9

u/weichx Sep 21 '26

I love the idea but this should be a tool I invoke to identify hot loop spillage, not a default thing I'd trip over by writing normal code.

22

u/FloweyTheFlower420 Sep 21 '26

"what if we had a compiler except it didn't abstract anything away for you so is basically useless" man i'd just write assembly at this point

6

u/jnordwick Sep 21 '26 edited Sep 21 '26

I its an interesting idea if you can control the region where register spill isn't allowed. that could actually be quite helpful, especially for vector code.

5

u/heliochoerus Sep 21 '26 edited Sep 22 '26

This seems like it would inhibit inlining which would then make most other optimizations ineffective.

5

u/4ed7ff Sep 22 '26 edited Sep 22 '26

Your README says the language is designed for AI to use, but it doesn’t make sense to shift spillage decisions away from algorithms specifically designed for this problem with decades of compiler research behind them, to a general purpose time-and-money-consuming language model running down GPU time on a server somewhere else. Why clog the model’s context window and token credits solving spillage problems when the register allocator does that anyway? It seems like tokens would be better spent designing the software than wrestling with the target’s backend.

Register allocation is highly dependent on instruction selection, and small changes to the code can result in very different instruction selection decisions, meaning the code’s semantic validity is incredibly fragile to change.

Also, what happens when you have to call another function? Spillage has to occur anyway to vacate caller-saved registers based on the calling convention.

5

u/dnpetrov Sep 22 '26

If you are spilling in a hot loop, on practically all modern hardware that goes to L1 data cache. Did you actually measure the performance of generated code?

4

u/reini_urban Sep 22 '26

I also had this idea to warn about register spills in my compiler, and it went pretty far. Only until I checked with real code, not just toy tests. real code is big and messy.

7

u/Used_Inspector_7898 Sep 21 '26

Oh no, las cosas que hace la IA T.T

2

u/RevengerWizard Sep 21 '26

It could be put under a compiler flag, but otherwise it seems rather unstable, think of a small change in the register allocator algorithm that doesn’t spill a certain slot anymore (or the inverse).
Although, I’m quite fond of having a way to explicitly force a variable to always stay in a register, like the `register` keyword in C.

Interestingly, if you exhaust all registers in an asm statement in C, it does give a compile error.

1

u/torp_fan Sep 22 '26

Someone wrote "if you're writing code where this matters, it's always platform dependent", but as described the compiler has no way to know whether it matters. Clearly this should be behind an optimization flag and/or pragma.

1

u/realestLink Sep 25 '26

This is legit AI slop 💀

1

u/Kai_ Oct 02 '26

How is it different from https://rake-lang.org

0

u/GoblinsGym Sep 21 '26

Bonus points for a snarky haiku error message ;-)