News for hackers. Stuff that matters.
Re:Mir: Strongly typed IR to implement fast and lightweight ... by zer0zzz on Tuesday December 26, 2023 @09:18PM (#38778366)
How many times are folks gonna reuse this name?
Re:Mir: Strongly typed IR to implement fast and lightweight ... by dang on Wednesday December 27, 2023 @12:26AM (#38779357)
"Please don't complain about tangential annoyances—e.g. article or website formats, name collisions, or back-button breakage. They're too common to be interesting."

https://news.ycombinator.com/newsguidelines.html

Re:Mir: Strongly typed IR to implement fast and lightweight ... by zer0zzz on Wednesday December 27, 2023 @07:59AM (#38781516)
Noted. Sorry about that.
Re:Mir: Strongly typed IR to implement fast and lightweight ... by schneems on Tuesday December 26, 2023 @11:45PM (#38779167)
It means peace/world in Russian. He said so in his rubykaigi keynote. Given that, the more peace in this world, the better, I think.

I’m curious if he still sees a path or benefit for Ruby switching to such a tool.

Re:Mir: Strongly typed IR to implement fast and lightweight ... by pixelpoet on Tuesday December 26, 2023 @10:18PM (#38778727)
On that note, I've seen quite a few zer0s in my time!
Re:Mir: Strongly typed IR to implement fast and lightweight ... by slimsag on Tuesday December 26, 2023 @11:04PM (#38778995)
a proportionate number of times to people leaving comments like this
Re:Mir: Strongly typed IR to implement fast and lightweight ... by 082349872349872 on Tuesday December 26, 2023 @09:19PM (#38778372)
MIR is a start; someone ought to continue with the following downstream projects:

TRYD (Trace Rewriter Yielding Dynamics): reconstitutes a CFG from traces in order to not only JIT single hot paths but also multiple interlacing warm ones

MAI (Machine-Agnostic Interpreter): support for mobile processes and agents

Re:Mir: Strongly typed IR to implement fast and lightweight ... by jashmatthews on Wednesday December 27, 2023 @12:07AM (#38779276)
> TRYD (Trace Rewriter Yielding Dynamics): reconstitutes a CFG from traces in order to not only JIT single hot paths but also multiple interlacing warm ones

Do you have more information on that? Googling didn't turn much up. It sounds similar to the Trace Tree or region based compilation approaches?

Re:Mir: Strongly typed IR to implement fast and lightweight ... by Tiberium on Wednesday December 27, 2023 @02:08AM (#38779773)
That reply is a pun based on the fact that Mir (Мир) means peace in a lot of Slavic languages, and "Мир! Труд! Май!" ("Peace! Labour! May!") was a popular slogan in the USSR for celebrating 1st of May (https://en.wikipedia.org/wiki/International_Workers%27_Day)
Re:Mir: Strongly typed IR to implement fast and lightweight ... by 082349872349872 on Wednesday December 27, 2023 @03:35AM (#38780161)
Tiberium is absolutely correct, but I do admit I'd love a TRYD-like project. Thanks for giving me some useful search terms!

(to be clear, what I had in mind: not just merging trace heads to form a tree, but merging their tails as well [à la NFA minimisation] to form a cyclic graph; after which one could use region-based techniques on the identified cycles.

Rpython's metatracing does a reasonable job for multiple interlacing warm paths, but there's still room for improvement)

Re:Mir: Strongly typed IR to implement fast and lightweight ... by andyferris on Tuesday December 26, 2023 @11:13PM (#38779033)
Is the project still active?
Re:Mir: Strongly typed IR to implement fast and lightweight ... by tomcam on Wednesday December 27, 2023 @04:00AM (#38780277)
Re:Mir: Strongly typed IR to implement fast and lightweight ... by dibyendu on Wednesday December 27, 2023 @05:37AM (#38780744)
Yes it is, but developer is no longer being funded by RedHat to work on it so its slowed down.
Re:Mir: Strongly typed IR to implement fast and lightweight ... by mkleczek on Wednesday December 27, 2023 @12:37AM (#38779413)
I think it would be worth mentioning GraalVM and https://github.com/oracle/truffleruby in competitors section.
Re:Mir: Strongly typed IR to implement fast and lightweight ... by charrondev on Wednesday December 27, 2023 @03:26AM (#38780111)
LuaJit and PHP both use [DynASM](https://luajit.org/dynasm.html) as well.
Re:Mir: Strongly typed IR to implement fast and lightweight ... by binary132 on Wednesday December 27, 2023 @04:41PM (#38787099)
TIL PHP uses dynasm, here I thought only LuaJIT did. Cool!
Re:Mir: Strongly typed IR to implement fast and lightweight ... by charrondev on Wednesday December 27, 2023 @06:19PM (#38788075)
Here’s the RFC for reference https://wiki.php.net/rfc/jit

It passed and landed in PHP 8.0.

Re:Mir: Strongly typed IR to implement fast and lightweight ... by binary132 on Thursday December 28, 2023 @05:26PM (#38799504)
I’m surprised their benchmark only saw a 200% performance improvement. LuaJIT is way faster than Lua in many cases.
Re:Mir: Strongly typed IR to implement fast and lightweight ... by charrondev on Friday December 29, 2023 @11:04AM (#38806357)
So the PHP implementation was built into the opcache extension which already gives a massive speed up over purely interpreted PHP.

If you benchmark JIT PHP vs PHP with no opcache you will see 500% speed up or more depending on the workload.

Of course most PHP web servers tend to be pretty IO bound. The project I work on sees ~a 20% in best case response time which is quite nice.

Re:Mir: Strongly typed IR to implement fast and lightweight ... by sparkie on Wednesday December 27, 2023 @10:19AM (#38782812)
Add GNU jitter to the list: https://www.gnu.org/software/jitter/
Re:Mir: Strongly typed IR to implement fast and lightweight ... by yawpitch on Wednesday December 27, 2023 @01:48AM (#38779681)
Assumed this was about the other MIR: https://llvm.org/docs/MIRLangRef.html
Re:Mir: Strongly typed IR to implement fast and lightweight ... by densh on Wednesday December 27, 2023 @02:31AM (#38779853)
Re:Mir: Strongly typed IR to implement fast and lightweight ... by noelwelsh on Wednesday December 27, 2023 @02:14AM (#38779799)
Looks like a fun project but I think the window of opportunity might have passed. The Shopify JIT seems to be the way forward for Ruby JITs, and WASM is the obvious choice for new programming languages.
Re:Mir: Strongly typed IR to implement fast and lightweight ... by vidarh on Wednesday December 27, 2023 @03:50AM (#38780225)
A new jit (rjit) just landed as an experimental option in 3.3. Ruby is very receptive to trying new things if they look promising.
Re:Mir: Strongly typed IR to implement fast and lightweight ... by viraptor on Wednesday December 27, 2023 @06:08AM (#38780888)
Also rjit is yet-another-jit in Ruby. There's mjit, yjit, rjit, and many external ones. We're getting at least two a year recently, so why not mir as well.
Re:Mir: Strongly typed IR to implement fast and lightweight ... by cozzyd on Wednesday December 27, 2023 @03:17AM (#38780078)
Too bad it doesn't support SIMD. Would be nice for an easily embeddable SIMD-aware JIT to exist.
Re:Mir: Strongly typed IR to implement fast and lightweight ... by mike_hearn on Wednesday December 27, 2023 @01:21PM (#38784833)
What do you mean by easily embeddable? There are JVM JITs that understand SIMD to some extent, and let you write manual SIMD code using the vector API.
Re:Mir: Strongly typed IR to implement fast and lightweight ... by IshKebab on Wednesday December 27, 2023 @02:21PM (#38785495)
It means you can easily use it from another program. Pretty much it has to be written in a language without a runtime that can expose a C interface, i.e. C, C++, Zig or Rust.
Re:Mir: Strongly typed IR to implement fast and lightweight ... by cozzyd on Wednesday December 27, 2023 @04:50PM (#38787208)
I guess you can do something like emit code on a memfd, exec ispc to another memfd and dlopen the output but... clunky.
Re:Mir: Strongly typed IR to implement fast and lightweight ... by mike_hearn on Wednesday December 27, 2023 @05:15PM (#38787460)
I don't follow. You can load every JITC I know of with C or C++ interfaces. And what is a JITC if not a runtime?
Re:Mir: Strongly typed IR to implement fast and lightweight ... by IshKebab on Wednesday December 27, 2023 @05:51PM (#38787839)
The runtime I'm talking about is for the language that the JIT compiler is written in.

If I want to use a JIT compiler that is written in Java in my C++ game I now have to distribute the Java runtime with my game. That's an example of "not easily embeddable".

Re:Mir: Strongly typed IR to implement fast and lightweight ... by mike_hearn on Thursday December 28, 2023 @06:07AM (#38792231)
Two things here:

1. If by runtime you mean "set of files" you can (these days) compile the Graal JIT compiler to a fully standalone DSO/DLL or even statically link the results into a larger executable. And because the Graal JIT understands Truffle that means making standalone statically linkable JITCs for any language is now very easy.

But that said, games always consist of more than one file anyway so the importance of this doesn't seem too high.

2. If by runtime you mean "support code" then C, C++ and Rust all have runtimes so you'd really need to define what specifically you need from a runtime.

Re:Mir: Strongly typed IR to implement fast and lightweight ... by IshKebab on Thursday December 28, 2023 @02:44PM (#38797687)
I didn't know that about Graal but I guess it depends on how easy that process is. Remember we're talking about "easily embeddable". You can embed anything with enough effort!
Re:Mir: Strongly typed IR to implement fast and lightweight ... by mike_hearn on Friday December 29, 2023 @04:50AM (#38803284)
See here for a guide on how to compile some Java code to a native library:

https://www.graalvm.org/latest/reference-manual/native-image...

One of the kinds of code you can compile this way is a language interpreter using the Truffle framework, which is a way to write interpreters that turns them into advanced JIT compilers automatically. If you do that then your language interpreter, runtime and the Graal/Truffle JIT compiler will all be compiled into your shared library. You can then export a C API from it using the technique shown in that article to load and execute code.

Re:Mir: Strongly typed IR to implement fast and lightweight ... by killcoder on Thursday December 28, 2023 @08:28AM (#38793251)
WebAssembly in something like wasmtime is probably the closest we've got to this. Though I wish WebAssembly supported more (wider) SIMD instructions.
Re:Mir: Strongly typed IR to implement fast and lightweight ... by gradschoolfail on Wednesday December 27, 2023 @04:22AM (#38780392)
Main takeaway, it avoids SSA

Earlier HN discussion 183pts 63 comments

https://news.ycombinator.com/item?id=22098399

Whats the chance nateb2022? this is way too close (in name) to Lattner’s MLIR to ignore, i thought this morning as I was brushing my teeth. {not to mention LLVM’s own MIR}

https://llvm.org/docs/MIRLangRef.html

Re:Mir: Strongly typed IR to implement fast and lightweight ... by dibyendu on Wednesday December 27, 2023 @05:36AM (#38780740)
SSA is used internally, but the instruction set does not require SSA, if that's what you meant
Re:Mir: Strongly typed IR to implement fast and lightweight ... by gradschoolfail on Wednesday December 27, 2023 @11:44AM (#38783729)
“ Using SSA simplifies the optimization implementations, but building and destroying this form is expensive. Using SSA for the MIR generator's short-pass pipeline makes little sense to me, so I don't use it.”

In his blogpost.

Re:Mir: Strongly typed IR to implement fast and lightweight ... by pizlonator on Wednesday December 27, 2023 @09:10AM (#38782027)
Pro jit hacker here. Super cool seeing folks build stuff like this.

Thoughts:

- great to see an SSA pipeline, but that implies that this jit is not ideal for baseline jiting. That’s fine. I guess I would explicitly call out that although it’s light, it is an optimizer.

- which reminds me - a JIT based VM is most defined by how powerful its top JIT is, not how light it is. Maybe since MIR is an optimizer, it’s meant to eventually not be light? It’s generally a bad idea to set out to build a jit, but to then aim in between baseline and full opt. This middle light jits are best built after you’ve evolved your top and bottom tiers (sorta like what happened in V8).

- jit IRs need to be given space to evolve to point directly at the user heap, which precludes any kind of reliable serialization. You can dump IR for debugging but you can’t parse IR. This MIR thing is serializable, which would make it an awesome AOT IR. But the serialization code will get in the way once folks try to implement jit based optimizations (like anything speculative). Best to kill the serialization now before it becomes a problem.

- if the first front end is C11 then you risk biasing the jit to be very C oriented. Languages that need JIts usually also need weird stuff that C doesn’t need. Super important to test a weirder language. Maybe Lua, since it’s weird but simple.

Anyway, this is cool shit. Nice to see people building jits!

Re:Mir: Strongly typed IR to implement fast and lightweight ... by gradschoolfail on Wednesday December 27, 2023 @11:46AM (#38783761)
“Using SSA simplifies the optimization implementations, but building and destroying this form is expensive. Using SSA for the MIR generator's short-pass pipeline makes little sense to me, so I don't use it.”

Thats from his blog motivating the project. https://developers.redhat.com/blog/2020/01/20/mir-a-lightwei...

Re:Mir: Strongly typed IR to implement fast and lightweight ... by seertaak on Wednesday December 27, 2023 @09:23AM (#38782166)
> great to see an SSA pipeline, but that implies that this jit is not ideal for baseline jiting.

I'd be interested in knowing the reason -- it's not clear to me tbh.

Re:Mir: Strongly typed IR to implement fast and lightweight ... by pizlonator on Wednesday December 27, 2023 @11:04AM (#38783272)
OK, so here's the deal with SSA and the running time of your compiler.

- You can convert code to SSA in something like O(N log N) or even close to O(N) if you get fancy enough. The conversion step is not very expensive to run, though it is annoying to implement. Converting to SSA is fine, that's not the problem.

- Optimizations implemented on top of SSA are cheap to run. That's sort of the point of SSA. So, that's not the problem.

- SSA optimizations achieve goodness when you have multiple of those optimizations running in something like a fixpoint. It's pointless to say "I have SSA" and then just implement one of those optimizations. I don't think MIR does that; like most SSA optimizers, it has multiple optimizations. The problem sort of starts here: SSA sort of implies that you're building an optimizer with multiple optimizations. Even if each opt is cheap, having SSA usually implies that you're going to pile on a bunch of them. So, you'll have cost either from death-by-a-thousand-cuts, or you'll eventually add an optimization that's superlinear.

- After your run SSA optimizations, you'll have nontrivial global data flow - as in, there will be data flows between basic blocks that weren't there when you started, and if you do a lot of different SSA optimizations, then these data flows will have a very complex shape. That's caused by the fact that SSA is really good at enabling compilers to "move" operations to the place in control flow where they make the most sense. Sometimes operations get moved very far, leading to values that are live for a long time (they are live across many instructions). This is where the problem gets significantly worse.

- Even without SSA optimizations, SSA form adds complexity to the data flow graph because of Phi nodes. You almost always want the Phi and its inputs to use the same register in the end, but that's not a given. This adds to the problem even more.

- Probably the reason why you went for SSA was perf. But to get perf from SSA-optimized code, you need to have some way of detangling that data flow graph. You'll want to coalesce those Phis, for example - otherwise SSA will actually pessimise your code by introducing lots of redundant move instructions. Then you'll also want to split ranges (make it so a variable can have one register in one part of a function and a different register in another part), since SSA optimizations are notorious for creating variables that live for a long time and interfere with everything. And then you'll definitely need a decent register allocator. No matter how you implement them, the combo of coalescing, splitting, and register allocation will be superlinear. It's going to be a major bottleneck in your compiler, to the point that you'll wish you had a baseline JIT.

Those things I mentioned - coalsce, split, regalloc - are expensive enough that you won't want them in a JIT that is the bottom tier. But they're great to have in any compiler that isn't the bottom tier. So, it's a good idea to have some non-SSA baseline (or template, or copy-and-patch, or whatever you want to call it) JIT, or a decent interpreter, as your bottom tier.

Re:Mir: Strongly typed IR to implement fast and lightweight ... by noelwelsh on Wednesday December 27, 2023 @09:38AM (#38782324)
I assume SSA is too slow.
Re:Mir: Strongly typed IR to implement fast and lightweight ... by binary132 on Wednesday December 27, 2023 @04:52PM (#38787219)
When I first read the blog post announcing this project, I got really excited, but unfortunately supporting Windows is not important to the author (correct me if I’m wrong?) and I am mainly interested in projects that are portable between at least windows and Linux, so it isn’t really an option for me.