The Crystal Programming Language Forum

Can Crystal use MIR for faster compiler/interpreter?

I wonder if MIR could improve performance of upcoming crystal interpreter mode and/or crystal compiler.

@asterite Would using MIR for interpreter basically mean no reuse of current work on interpreter (hand written byte code generation and execution)?

I’ve noticed MIR project recently had the first release

What is MIR

MIR speed compared to GCC

1 Like
  • Each local variable has type which can be only 64-bit integer, float, double, or long double

I guess that’s a no for me.

Can you elaborate on this please? Do you mean MIR has not enough types for Crystal?

Since the plan for MIR is to try it with CRuby… shouldn’t it mean it has enough types to implement Ruby and why not enough types for Crystal then?

All values in Ruby are 40 bytes. So they can easly represent those using 64 bytes, I guess.

I crystal types can have pretty much any size. If you have a StaticArray(UInt8, 1024), that’s a variable that takes 1024 bytes. It seems there’s no way to represent that in MIR.

I think MIR is more suited for dynamic languages. Or maybe languages that don’t have structs or stack-allocated things. Not sure.

I went ahead and asked MIR author for clarification too, and it seems like MIR can actually be used? Can MIR be used for Crystal? · Discussion #215 · vnmakarov/mir · GitHub



Feel free (you, or anyone) to then replace the current interpreter with a MIR backend. It took me like 3~4 months to write the current interpreter (most of the time was spend thinking how to generate bytecode, the actual VM wasn’t that hard to implement), so I won’t do the MIR translation because I don’t have that time, plus I’m not sure what would be the benefits, or how you would use MIR to debug Crystal code.

1 Like

Ary, I’ve asked about MIR because I’m interested in both Crystal and MIR. I was exited about MIR because of very fast compilation speed it provides and because of possible use of MIR for Ruby JIT.

I lack the knowledge in this area to understand what exactly it would take to use MIR in Crystal for interpreter or compiler and if that’s a good idea or even possible at all. Hence my dumb questions in this thread and in MIR project.

No offense intended. I wasn’t trying to suggest to replace current interpreter which I also track from the beginning and very exited about :heart: :heart: :heart:

1 Like

Maybe I can try using MIR for a small portion of the interpreter and see how it behaves :thinking:


Oh man, I hope I haven’t derailed you :laughing: Please get some 1-2 month rest from Crystal you wanted :wink:

Haha, nah. I couldn’t find any public documentation of how to use MIR or what’s the language about (it seems pretty preliminar, plus there’s a warning that says that it’s not used for production) so I won’t do anything for now. Also it seems it has some other competitor projects. Given that I just built everything from scratch, I’m not super motivated to use any of those other tools. The nice thing about the current interpreter is that it depends on very few things, mainly libffi (and libreadline for a nicer debugger experience).

My guess is that Ruby will never use this, they prefer not depending on other projects. When you add a dependency then you depend on that project’s bugs and timeline. This is happening all of the time for us with LLVM. It would have been better to just have a C backend and use a textual representation for it. No dependencies other than a C compiler.


Yeah, please don’t abandon current hand written interpreter.

I couldn’t find any public documentation of how to use MIR or what’s the language about (it seems pretty preliminar, plus there’s a warning that says that it’s not used for production) so I won’t do anything for now.

Very true. MIR is not complete yet, but has been in development for quite a while.

Here is some guy using it since 2019 for some language and seem to be impressed by performance First use of MIR JIT in Ravi

And c2mir in MIR project itself can also be considered as kinda docs on how to use it :)

MIR author mentioned LLVM API instability. I think it means he’s going to make MIR API very stable. Also MIR is very small compared to LLVM.

What I see people who use LLVM for their language implementation are tired of LLVM API instability, difficulties to support different versions of LLVM, LLVM size and compilation speed, difficulties to get review of their patches for LLVM, etc.

1 Like

C to MIR generated code speed and compilation speed is already very impressive.

More details: The MIR C interpreter and Just-in-Time (JIT) compiler

My short-term plan is to offer a first release of the MIR project, including the C-to-MIR compiler, before the end of 2021.

That already happened.

Also MIR author is going to do more optimizations in the near future:

Although C-to-MIR compilation speed was never a major goal, its speed is quite competitive with other C compilers. I have found that some people prefer to use the C language instead of MIR for their JIT implementations. This makes improving C-to-MIR compilation speed an important task. Therefore, I put it as a first priority.

1 Like

It may be non trivial to replace the llvm back end … :slight_smile:

Does MIR support nice debugging?

If they do that will mean that my 6 months worth of time will be flushed into the toilet :slight_smile:

1 Like

Well, my interpreter already supports debugging and it’s better than anything lldb or gdb can offer.


I cannot guarantee that I can spend much time (with my 4 parallel projects going and 12-15 hours a day allocated to that) but I would love to find a little time and see if I can help you to play with it and possibly to improve it). If it can be debugged in interpreter mode with all features from VSCode, it will be amazing as it will save a lot of headaches with constant compilation.


Yeah, it would be great to improve the current interpreter so that it exposes an interface to the outside world so that you can hook it into VSCode. Maybe we could even work together on that! You write the VSCode plugin and tell me which things you need from the interpreter, and I work on exposing those things from the interpreter.

That said, fork in interpreted mode crashes randomly, so I’d prefer to wait until someone fixes that. I can’t conceive distributing an interpreter where that fails. I know, fork is probably not used by many, but it’s used under the hood for, and that is used a lot. So for now I don’t want to spend more time on the interpreter, and I’d prefer nobody spends time on it except on fixing that fork bug.


I’ve asked MIR author and he posted a detailed answer: Can MIR be used for Crystal? · Discussion #215 · vnmakarov/mir · GitHub


It doesn’t seem that MIR is there yet, but…

Work on CRuby JIT based on MIR seem to be started already and it would require some MIR improvements (probably those including better debugging too).

Author says that:

I have plans to implement debugging using gdb JIT interface JIT Interface (Debugging with GDB) in the future. I think it will be useful for me and other people who will try to use MIR project for their JITs.

As for data types required for Crystal… author of Ravi language (Lua-like, uses MIR for JIT compilation) shared his experience of using MIR:

MIR IR does not have support for aggregate types in the IR - unlike LLVM. This does make it harder to use MIR as you have to code the logic for accessing aggregate data structures yourself. That’s why I use the C as the intermediate language in Ravi - since MIR comes with a C compiler that does all this for you.