# Stable MIR Empowering everyone to use ~~secret~~ compiler powers ---- ## What? * Analyze Rust code programmatically * Via a stableā„¢ rustc API * Designed and pushed off by Celina Val Note: Skipping a `Why?` section for the first bullet point, as y'all know why we are here ---- ## Why a (stable) rustc API? * Community crates lag behind * Community crates diverge * Unstable rustc APIs bitrot ---- ## rustc_middle::mir ```rust let main = tcx.entry_fn().unwrap(); let body = tcx.optimized_mir(main); ``` ```rust assert_eq!(body.blocks().len(), 1); ``` ```rust assert!(matches!( body.blocks()[0].terminator().kind, TerminatorKind::Return, ); ``` ---- ## Sneak peek ```rust let main = stable_mir::entry_fn().unwrap(); let body = main.body(); ``` ```rust assert_eq!(body.blocks.len(), 1); ``` ```rust assert!(matches!( body.blocks[0].terminator, Terminator::Return, ); ``` Note: Before we start, here's a real example snippet ---- ## Short Term Goals * Unstable rustc API that rarely changes in practice * Get two tools off rustc_middle::mir * still use unstable tcx and driver APIs * Evaluate performance of `smir` Note: We're working on this right now. Something to show in the next few months. Coordinated help is always appreciated. ---- ## Long Term Goals * Semver trick * Very long term: expose API from stable Rust * Get two tools to depend solely on `smir` * Have a stable crate on crates.io Note: Add new API, implement old API in terms of new one. --- ## Design ---- ### Short term plan ```mermaid flowchart LR classDef red fill:#FF0000 classDef reddish fill:#FF8888 main --> driver main --> SMIR main --> smir subgraph tool main end subgraph rustc MIR[(MIR)]:::red driver:::red --> smir MIR -.-> smir:::red -.-> SMIR subgraph smir_crate smir SMIR[(SMIR)]:::reddish end end ``` ---- ### Long term plan ```mermaid flowchart LR classDef red fill:#FF0000 main --> stable_driver main --> SMIR subgraph tool main end subgraph stable_mir SMIR[(SMIR)] end subgraph rustc MIR[(MIR)]:::red stable_driver --> driver:::red --> smir MIR -.-> smir:::red -.-> SMIR end ``` ---- ### crates.io ```mermaid flowchart LR SMIR --> panic_free SMIR --> unnecessary_clone SMIR --> foo subgraph analyzer panic_free unnecessary_clone foo[...] end subgraph stable_mir SMIR[(SMIR)] end subgraph optimizer SMIR --> inline --> SMIR end ``` ---- ### Nitty gritty details * thread local storage (avoids `TyCtxt` style) * no arenas, Box/Rc/String everywhere * lazy datastructures (`struct Ty(SomeId);`) * no roundtrippable serialization :slightly_frowning_face: Note: TLS: allows convenience methods. You don't see any of this ---- ### Why not ... * ... plugin/wasm API? * ... just serialization? * ... proc-macroish API? * ... something else... Note: this design is in flux. Main point is not breaking y'all on a weekly basis ---- When can you use it? Note: Soon, wanna help? I can prototype all the individual hard parts. ---- Tell me about your use cases! Note: Tell me all about it in the questions or chat with me after the talk ---- ## Summary * rustc is a library * we want to make it more of a library * incrementally create your own Rust code muncher * we're building a rustc API that changes rarely * and when it does gives you a grace period * contributions welcome :wink:
{"metaMigratedAt":"2023-06-18T01:33:09.331Z","metaMigratedFrom":"YAML","title":"Stable MIR","breaks":true,"contributors":"[{\"id\":\"ce357653-6779-4c50-b873-5c2ef0815935\",\"add\":4921,\"del\":1262}]","description":"Empowering everyone touse secret compiler powers"}
    448 views