Celina G. Val
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# Stable MIR Design *Note: The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in [RFC 2119](https://www.ietf.org/rfc/rfc2119.txt).* ## Introduction Many static analysis tools rely on the internals of the rust compiler to be able to efficiently analyze programs that were written in Rust. Most tools analyze the mid-level IR (MIR) together with rustc's type system. The MIR is a much simpler language when compared to Rust, but much richer than LLVM's IR. In order to do that today, these tools use rustc's internal APIs to extend the compiler. These mechanisms only work for nightly toolchain distributions, and the API changes very often, which makes maintenance very costly. Another issue is that the semantics of the MIR is not well documented, which can potentially impact the result of these analysis. The goal of the Stable MIR project is to provide a stable interface to the Rust compiler that allow tool developers to develop sophisticated analysis with a reduced maintenance cost without compromising the compiler development speed. ### Tenets 1. Enable tool developers to implement sophisticated analysis with low maintenance cost. 2. Do not compromise the development and innovation speed of the rust compiler. 3. Once stable (v1.0+), releases must follow semantic versioning https://semver.org/. 5. Backward compatible changes are preferred, not required. 6. The rust compiler should expose interface to Stable MIR versions released 3 months prior to its release. It may support multiple Stable MIR versions at the same time to achieve this. 7. The SMIR should be performant, with minimum memory and processing overhead when compared to using internal MIR. 8. Unstable APIs should be developed under feature gates and they shall be excluded from the semantic versioning. ## Project Milestones This is a big endeavor for both compiler and tool developers. We anticipate that tool developers will need to make changes to their code to transition from internal APIs to stable ones. Whenever possible, the new APIs should mimic the current APIs to reduce the transition burden to tool developers. For the compiler developers, once an API has been incorporated to the Stable MIR, they shall be maintained following the tenets and the [Stable MIR API guidelines](#api-guidelines). We will follow a gradual transition to the new model. ### Minimum Viable Product (Stable MIR v0.1) The first milestone will be landing a version of stable MIR that allows a MIR consumer (e.g. kani, miri, or a mini-miri demo) to switch its use of MIR datastructures completely over to it (even if it still uses other unstable APIs). We envision the following requirements for this release: * Provide API to emit the body of a function solely with stable MIR datastructures. * Provide opaque objects which will store enough information for users to leverage other parts of the compiler. E.g.: Extract the type of an Operand. For this release, there will be no changes to how tools load the Rustc library or find the functions/statics they want to get the stable MIR for. Also, only nightly will be supported, even just to access the stable MIR datastructures. We will only support MIR's ["runtime-optimized"](https://github.com/rust-lang/rust/blob/50d3ba5bcbf5c7e13d4ce068d3339710701dd603/compiler/rustc_middle/src/mir/syntax.rs#L101). ### API Coverage and Stabilization (v0.2+) Subsequent releases of the Stable MIR will focus on increasing the API coverage to include other parts of the compiler that are required to semantically analyze the program as well as APIs used to provide a similar UX to the compiler. * Provide API to visit all items (static variables, constants, and functions) of every crate compiled (current crate + dependencies). * Support emitting stable MIR of analysis-MIR ### Long term goal (v1.0+) The first stable release (v1.0) shall only be done once the SMIR has sufficient API coverage and the APIs are stable. The following changes are orthogonal to the stabilization effort but shall be implemented to improve the overall experience, and they are in the scope of this project: * Completely decouple tools from Rustc's internal APIs. Including invoking Rustc's driver, error reporting, type queries. * Support different MIR dialects. * Interface can be used with all toolchain channels (stable, beta, nightly) * Provide a server / client architecture that provides a more robust and interactive experience. * The Stable MIR can be used as an input to the compiler. * possible future extension: share the MIR datastructures with the stable MIR datastructures via https://github.com/rust-lang/compiler-team/issues/233 ## MVP Design The `stable-mir` will follow a similar approach to `proc-macro2`. It's implementation will be broken down into two main crates: * `stable_mir`: Public crate, to be published on crates.io, which will contain the stable data structure as well as proxy APIs to make calls to the compiler. * `rustc_smir`: The compiler crate that will translate from internal MIR to SMIR. This crate will also implement APIs that will be invoked by `stable-mir` to query the compiler for more information. This will help tools to communicate with the rust compiler via stable APIs. Tools will depend on `stable_mir` crate, which will invoke the compiler using APIs defined in `rustc_smir`. I.e.: ``` ┌───────────────────────────────┐ ┌───────────────────────────────┐ │ External Tool ┌──────────┐ │ │ ┌──────────┐ Rust Compiler │ │ │ │ │ │ │ │ │ │ │stable_mir| │ │ │rustc_smir│ │ │ │ │ ├──────────►| │ │ │ │ │ │ │◄──────────┤ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ └──────────┘ │ │ └──────────┘ │ └───────────────────────────────┘ └───────────────────────────────┘ ``` A compile time check will be added to trigger an error if the `stable_mir` is not compatible with the current compiler. ### Multiple version support In order to support multiple versions of `stable_mir`, the `rustc_smir` will contain modules that can be mapped to different versions of `stable_mir`. Example, the `stable_mir` v0.1, will invoke APIs from the `rustc_smir::version_0_1::*`, while the version v.0.2 will invoke `rustc_smir::version_0_2::*`. What about forward compatibility? See [Open Questions section](#Open-Questions). ### MIR / SMIR interoperability Until we have enough coverage, users will still rely on internal APIs to initialize the Stable MIR constructs and to retrieve further information that hasn't been covered by stable APIs. E.g.: Getting the type of an operand. There are a few ways that this can be implemented, and we still need to assess them before settling into one. A few options are: 1. Some Stable MIR API's will still handle internal compiler data structures, such as `TyCtxt` and `Ty`. These types would be re-exported using an `unstable` or `internal` module. 2. Stable MIR constructs can be converted to internal ones. 3. Expose internal compiler APIs that support Stable MIR constructs. ## API Guidelines ### MIR Datatypes * No interning, everything is using `Box<T>` instead of `&'tcx T' * Types are opaque handles (`struct Ty(String)`) that for now only offer `Display` impls and no other API * we'll quickly iterate to (`struct Ty(usize)`) that the API has a map for figuring out the `Ty<'tcx>` from and then querying that for infomation (e.g. size, layout, ...). ### Type Datatypes * re-use `rustc_type_ir` for our types. This is independent of TyCtxt but still exposes the entirety of `TyKind` and similar. # Appendix ## Open Questions 1. Should MIR dialects coverage be a requirement for v1.0? 2. Should we support forward compatibility? I.e.: Using a new version of `stable-mir` with an older version of the compiler? Do we have a use case in mind? 3. What traits should StableMIR types implement? For now, we've been adding Clone and Debug. We should come up with a guideline to when we can add more traits. E.g.:: * Hash * Copy * PartialEq / Eq * PartialOrd / Ord * Serialize / Deserialize (serde): This one could be guarded by a feature. ## FAQ ## Requirements The table below contains a more detailed list of requirements and in which version of `stable-mir` they should be met. For the target version, we use the following values: * **v0.1:** represents the minimum viable product. * **v0.2+:** represents subsequent releases of `stable-mir` that increase the API coverage. * **v1.0+:** represents the long term goal. | # | Requirements | Description | Target Version* | | --| ------------ | ----------- | --------------- | | 0 | Re-use `rustc_smir` | Remove all unstable depencencies of `rustc_smir` | v0.1 | | 1 | MIR Datatypes | Provide API that correspond to the current [MIR Datatypes](https://rustc-dev-guide.rust-lang.org/mir/index.html#mir-data-types). | v0.1 | | 2 | Support API | Provide API to retrieve code location, attributes, debug information. | v0.2+ | | 3 | Message API | Provide API to generate user friendly and json messages. | v0.2+ | 4 | Type API | Provide API to retrieve type information (including layout) | v0.2+ | | 5 | Semantically versioned API | New versions of `stable-mir` follow semantic versioning (https://semver.org/). | v1.0 | | 6 | MIR Dialects | Support multiple MIR dialects. | v1.0+ | | 8 | Stable channel | `stable-mir` can be used with all toolchain channels including stable. | v1.0+ | | 9 | Server/Client | The `stable-mir` APIs are implemented over IPC. Tools no longer need to link against rust compiler library. | v1.0+ | | 10 | Load SMir | The compiler is able to load SMir definitions and compile it to the target platform. | v1.0+ |

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully