|
| 1 | +# Transition to StableMIR |
| 2 | + |
| 3 | +We have partnered with the Rust compiler team in the initiative to introduce stable |
| 4 | +APIs to the compiler that can be used by third-party tools, which is known as the |
| 5 | +[Stable MIR Project](https://github.com/rust-lang/project-stable-mir), or just StableMIR. |
| 6 | +This means that we are starting to use the new APIs introduced by this project as is, |
| 7 | +despite them not being stable yet. |
| 8 | + |
| 9 | +### StableMIR APIs |
| 10 | + |
| 11 | +For now, the StableMIR APIs are exposed as a crate in the compiler named `stable_mir`. |
| 12 | +This crate includes the definition of structures and methods to be stabilized, |
| 13 | +which are expected to become the stable APIs in the compiler. |
| 14 | +To reduce the migration burden, these APIs are somewhat close to the original compiler interfaces. |
| 15 | +However, some changes have been made to make these APIs cleaner and easier to use. |
| 16 | + |
| 17 | +For example: |
| 18 | +1. The usage of the compiler context (aka `TyCtxt`) is transparent to the user. |
| 19 | + The StableMIR implementation caches this context in a thread local variable, |
| 20 | + and retrieves it whenever necessary. |
| 21 | + - Because of that, code that uses the StableMIR has to be invoked inside a `run` call. |
| 22 | +2. The `DefId` has been specialized into multiple types, |
| 23 | + making its usage less error prone. E.g.: |
| 24 | + `FnDef` represents the definition of a function, |
| 25 | + while `StaticDef` is the definition of a static variable. |
| 26 | + - Note that the same `DefId` may be mapped to different definitions according to its context. |
| 27 | + For example, an `InstanceDef` and a `FnDef` may represent the same function definition. |
| 28 | +3. Methods that used to be exposed as part of `TyCtxt` are now part of a type. |
| 29 | + Example, the function `TyCtxt.instance_mir` is now `Instance::body`. |
| 30 | +4. There is no need for explicit instantiation (monomorphization) of items from an`Instance::body`. |
| 31 | + This method already instantiates all types and resolves all constants before converting |
| 32 | + it to stable APIs. |
| 33 | + |
| 34 | + |
| 35 | +### Performance |
| 36 | + |
| 37 | +Since the new APIs require converting internal data to a stable representation, |
| 38 | +the APIs were also designed to avoid needless conversions, |
| 39 | +and to allow extra information to be retrieved on demand. |
| 40 | + |
| 41 | +For example, `Ty` is just an identifier, while `TyKind` is a structure that can be retrieved via `Ty::kind` method. |
| 42 | +The `TyKind` is a more structured object, thus, |
| 43 | +it is only generated when the `kind` method is invoked. |
| 44 | +Since this translation is not cached, |
| 45 | +many of the functions that the rust compiler used to expose in `Ty`, |
| 46 | +is now only part of `TyKind`. |
| 47 | +The reason being that there is no cache for the `TyKind`, |
| 48 | +and users should do the caching themselves to avoid needless translations. |
| 49 | + |
| 50 | +From our initial experiments with the transition of the reachability algorithm to use StableMIR, |
| 51 | +there is a small penalty of using StableMIR over internal rust compiler APIs. |
| 52 | +However, they are still fairly efficient and it did not impact the overall compilation time. |
| 53 | + |
| 54 | +### Interface with internal APIs |
| 55 | + |
| 56 | +To reduce the burden of migrating to StableMIR, |
| 57 | +and to allow StableMIR to be used together with internal APIs, |
| 58 | +there are two helpful methods to convert StableMIR constructs to internal rustc and back: |
| 59 | + - `rustc_internal::internal()`: Convert a Stable item into an internal one. |
| 60 | + - `rustc_internal::stable()`: Convert an internal item into a Stable one. |
| 61 | + |
| 62 | +Both of these methods are inside `rustc_smir` crate in the `rustc_internal` |
| 63 | +module inside the compiler. |
| 64 | +Note that there is no plan to stabilize any of these methods, |
| 65 | +and there's also no guarantee on its support and coverage. |
| 66 | + |
| 67 | +The conversion is not implemented for all items, and some conversions may be incomplete. |
| 68 | +Please proceed with caution when using these methods. |
| 69 | + |
| 70 | +Besides that, do not invoke any other `rustc_smir` methods, except for `run`. |
| 71 | +This crate's methods are not meant to be invoked externally. |
| 72 | +Note that, the method `run` will also eventually be replaced by a Stable driver. |
| 73 | + |
| 74 | +### Creating and modifying StableMIR items |
| 75 | + |
| 76 | +For now, StableMIR should only be used to get information from the compiler. |
| 77 | +Do not try to create or modify items directly, as it may not work. |
| 78 | +This may result in incorrect behavior or an internal compiler error (ICE). |
| 79 | + |
| 80 | +## Naming conventions in Kani |
| 81 | + |
| 82 | +As we adopt StableMIR, we would like to introduce a few conventions to make it easier to maintain the code. |
| 83 | +Whenever there is a name conflict, for example, `Ty` or `codegen_ty`, |
| 84 | +use a suffix to indicate which API you are using. |
| 85 | +`Stable` for StableMIR and `Internal` for `rustc` internal APIs. |
| 86 | + |
| 87 | +A module should either default its naming to Stable APIs or Internal APIs. |
| 88 | +I.e.: Modules that have been migrated to StableMIR don't need to add the `Stable` suffix to stable items. |
| 89 | +While those that haven't been migrated, should add `Stable`, but no `Internal` is needed. |
| 90 | + |
| 91 | +For example, the `codegen::typ` module will likely include methods: |
| 92 | + |
| 93 | +`codegen_ty(&mut self, Ty)` and `codegen_ty_stable(&mut, TyStable)` to handle |
| 94 | +internal and stable APIs. |
0 commit comments