Mozilla is revising its Rust language's compiler to achieve faster-compiling, faster-running, and more precisely type-checked code.
The changes, as outlined in a blog post that went live yesterday, involve what Rust's creators call midlevel intermediate representation (MIR). It's an additional step in the compilation process, coming after Rust source code is parsed and type-checked, but before it's passed along to the LLVM compiler framework and translated into machine code.
A few more steps now for a few less steps later
On its face, it seems paradoxical. If MIR introduces an extra stage in the compilation process, how's it supposed to speed things up?
As Rust's engineers explain, MIR strips down Rust syntax to a much simpler set of primitives that are easier for the compiler to manipulate. The pared-down core version of Rust generated by MIR "is not something you would want to program in," say Rust's creators, but it also means the compiler deals with constructs that are far easier to optimize.
An example on the Rust blog shows how loops can be optimized in this fashion. The primitives used to replace loops aren't ones you'd want to program in directly (such as the potentially complex
goto statement), but the compiler can make far smarter decisions about how to deploy them than a human programmer might. Similar optimizations are used for program control flow, match expressions, and so on.
This approach has precedent. Apple's Swift language, which also uses LLVM for compiling, has an intermediate representation called SIL. As Rust does with MIR, Swift performs optimizations with SIL that wouldn't be easy to perform with LLVM, such as dealing with the reference-counting system used by Swift's memory management.
We're getting warmed up
Recasting code with MIR has several benefits, claim Rust's engineers. For one, the compilation process can be revamped so that only what's actually changed needs to be recompiled. The compiler can also be streamlined to handle the new MIR syntax.
MIR also allows the compiler to make optimizations specific to Rust that aren't performed by LLVM itself. Some of the planned optimizations, like making memory-zeroing optional, will be uncorked this way.
When can rank-and-file Rustaceans make use of this? The short answer: soon. Plans to put MIR into action have been in the works for about a year, so this represents the culmination of those plans, rather than their first phase.
"Once we're able to run Crater [Rust's build tester] with MIR enabled and see no regressions across the entire crates.io ecosystem," write Rust's developers, "we'll turn it on by default (or, you'll forgive a terrible (wonderful) pun, launch MIR into orbit)."
Sign up for CIO Asia eNewsletters.