Enhancing Stable MIR with Serde Serialisation
Runtime Verification Inc. is proud to announce that we have upstreamed Serde serialisation to the Stable MIR project 🎉 PR#126963 to the Rust repository presents itself as the first of many intended contributions to the Stable MIR project from the KMIR team. So let’s dive in and explain what Stable MIR is, and why the serialisation is helpful!
The Value of Stable MIR
Many third party projects wish to do analysis of Rust and are immediately drawn to the value of the Middle Intermediate Representation (MIR) for a rich Control Flow Graph (CFG) Intermediate Representation (IR) that benefits from type checking and borrow checking already being completed. However the inherent instability of MIR has so far been a barrier for convenient access to the compiler internals for such projects, requiring them to pin a version so that breaking changes do not affect their project for new compiler releases. Yet MIR must be unstable so that it can change as required with the continuous improvements of the Rust compiler. Is there a way to have both?
The Stable MIR project intends to provide an intermediary between the application layer and MIR, delivering a stable API for projects to have some guarantees of consistency between compiler releases while accessing MIR. Stable MIR defines an Abstract Syntax Tree (AST) that captures the essence of MIR, encompassing the high level design of MIR as it changes.
Previous Stable MIR Interaction and Extraction
Before our change, the standard way to interact with Stable MIR was through a Rust program that interrupts compilation through Callbacks and implements the logic in the after_analysis function, or through implementing a custom CodegenBackend. However no portability outside of the Rust environment was provided, except a pretty printed textual dump which is only intended for human consumption. Efforts to create portable representations have mostly been locked to particular projects, despite the need being common to many. Really what Stable MIR needs is standardised serialisation through serde!
Implementing Serde Serialisation for Stable MIR
Serde is the canonical serialisation framework for Rust, converting data structures to and from popular formats such as JSON, YAML, and more. Naturally serde is the ideal candidate to achieve portability for Stable MIR, and so the KMIR team at RV worked on implementing that for the Stable MIR AST and it is officially upstreamed into the Rust compiler through PR#126963.
The current state of serialisation implemented is a simplified solution that we intended to extend in the future. However for now, this offers an agreeable and useful first step.
Future Work
The KMIR team at RV are supporters of the Stable MIR project, and plan to help improve and extend the features and functionality to have a versatile API to MIR for all projects. Alongside improvements to the serialisation, we are also considering adding functionality for deserialisation, a Callbacks function to interrupt compilation after monomorphization to inspect the MIR, and potentially more depending on what would be useful for us and others.