r/Compilers • u/Temporary-Ad8991 • 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.
50
u/hermeticwalrus Sep 21 '26
That seems annoyingly platform dependent
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.
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 ;-)