183 points · by ksec · 6y ago · 63 comments
It seems like Ruby needs a profile guided optimizer, which means building an IR that is suitable for profile-guided optimization. That’s way different from classic IRs like this since it means having provisions for OSR exit.
I recommend looking at these slides to learn how to do it.
http://www.filpizlo.com/slides/pizlo-speculation-in-jsc-slid... http://www.filpizlo.com/slides/pizlo-splash2018-jsc-compiler...
However, the reason that this is not so simple is that Ruby is a vastly more complicated language than JavaScript (I've worked on implementing both.) Ruby has an enormous standard library and most Ruby programs are just endless calls to the that library, so your compiler must be able to understand the library semantically, either by rewriting it in Ruby (not likely at scale in MRI) or by adding tens of thousands of individually optimised intrinsics (again not likely).
Ruby is digging itself further into a local optima with these approaches optimisations, rather than looking further around for a better global optima.
Maybe the reason why Ruby optimization has problems is nobody has done it the JSC way.
Yes it's the same problem and it's unique to neither Ruby nor JS - but the problem is just scale.
Ruby has a larger library, so needs more rewritten from the current C into Ruby+builtins. And then that rewrite doesn't maintain Ruby semantics (the C API currently used does not exactly match Ruby semantics) so a rewrite matching semantics is often very hard.
> Maybe the reason why Ruby optimization has problems is nobody has done it the JSC way.
People have been trying the approach you use in Core (for over a decade, starting with Rubinius, now TruffleRuby and others), but I think (based on practical experience working on optimising both languages) that it's just a larger problem in Ruby which is why it hasn't been conquered yet.
(for the uninitiated, I kid. Sample of previous naming controversy at https://news.ycombinator.com/item?id=14309903)
"Plans to try MIR light-weight JIT first for CRuby or/and MRuby implementation"
"MIR is strongly typed"
Is there an explanation of how the project bridges the gap between dynamically-typed Ruby and statically-typed MIR?More generally, I'd love to see something like MRuby+MIR be successful. It would be great to see an alternative to the aging LuaJIT.
Dynamic types and garbage collection would then be implemented on/for the abstract MIR machine.
My understanding is that MIR doesn’t tackle this problem, so the code does stay pretty dynamic when compiled, just like its sister project YARV MJIT.
They both need an intermediate profiling mode to specialise and monomorphise but nobody is building that as far as I know.
<quote> No SSA (single static assignment form) for:
Faster optimizations for short optimizations pipeline and small functions (a target usage scenario) Currently SSA could be used only for two optimizations (CCP and GCSE). SSA usage would mean 4 additional passes over IR. If we implement more optimizations, SSA transition is possible when additional time for expensive in/out SSA passes will be less than additional time for non-SSA optimization implementation Simpler and more compact generator code because we can avoid to implement a lot of nontrivial code (for dominator and dominator frontier calculation, a good out of SSA code) </quote>
Of course it's debatable what "modern" and "non trivial" mean, and if they apply to this project. It's very small, but on the (one!) small benchmark the author cites it seems to do quite well, so it's certainly not completely naive.
For whatever it's worth, CompCert doesn't use SSA either, and that's certainly a non-trivial compiler, though arguably the non-triviality does not stem from any advanced optimizations it does.
CRuby with MJIT
MIR ( This )
JRuby ( Ruby on JVM )
TruffleRuby ( Ruby on Graal )
Artichoke ( Ruby on Rust )
And I remember someone mentioned making Ruby with Tracing JIT. ( Not Topaz ) Unfortunately My Google fu is not good enough I can no longer find it.
https://llvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf
EngineYard took Rubinius in a few directions, but I think the main lasting impact was all the RSpec work they did along the way.
Ideally it handles register allocations and generated function caching as well. The current JIT assembler (ASMJIT, Xbyak) requires you to handle register allocations. LLVM is as mentioned quite a heavy dependency to have.
The article mentions Cranelift, but that's a 'middleweight JIT' with a proper SSA IR. I was surprised to see LibJIT has more LOC than Cranelift - I thought it was lighter. (Imperfect proxy for runtime 'weight', of course.)
If you want a lightweight portable JIT engine, there's already GNU Lightning [0], and the atrociously-named Lightening fork [1] (used in the new JIT in the GNU Guile Scheme interpreter, which turned up on the HN front page recently).
Here's a 1996 paper (preprint) on a research JIT named VCODE which executed around 8 instructions to generate each instruction in its output. [2] (Sadly it was never released, as far as I can tell, and is presumably long dead.)
Anyway, with all that said, I wish this project well. No-one's managed to get good performance out of Ruby yet, so it's certainly ambitious. Google gave up on Unladen Swallow, and that was a JIT for Python, which, as I understand it, is more amenable to JIT than Ruby. Even failing that, having a quality rival to GNU Lightning would be worthwhile.
[0] https://www.gnu.org/software/lightning/
[1] https://www.wingolog.org/archives/2019/05/24/lightening-run-...
[2] http://www-leland.stanford.edu/class/cs343/resources/vcode-a...
Anything you write in MIR can be compiled by MIR. Parrot only JITs Parrot bytecode, so the applications are much more limited.
That's the point where "C is nice and simple, it's easy to whip up a compiler" invariably turns into "why the #^(&)# didn't I use an existing frontend?". Real-world C code is messy. The entire sub-project of implementing a C compiler is a needless distraction that will turn into a huge time-suck with zero benefit to the author.
See also: "Why Says C is Simple?" https://people.eecs.berkeley.edu/~necula/cil/cil016.html
Ruby was my go to language for a long dry spell when I had little Lisp development jobs (except for Clojure). Ruby is a great language, as Matz says, Ruby is designed for developer happiness. I stopped using Ruby when more Common Lisp work came my way and then I used Python for five years of deep learning work. I have favorite languages but I used what customers wanted.
That said, I still keep up with Ruby news.
I feel like this list is missing some more, but yeah, there's been a few 'Mir' projects.