|
| 1 | +# Initial Integration |
| 2 | + |
| 3 | +In order to use `stable_mir` in your crate, you will need to do the following: |
| 4 | + |
| 5 | +1. Use a nightly toolchain that includes the `stable_mir` crate. |
| 6 | +2. Install at least the following rustup components: "rustc-dev" and "llvm-tools" |
| 7 | +3. Declare `stable_mir` as an external crate at the top of your crate: |
| 8 | + |
| 9 | +```rust |
| 10 | +extern crate stable_mir; |
| 11 | +``` |
| 12 | + |
| 13 | +For 1 and 2, we highly recommend adding a "rust-toolchain.toml" file to your project. |
| 14 | +We also recommend to pin down a specific nightly version, to ensure all users and tests are using the same compiler |
| 15 | +version. |
| 16 | +Therefore, the same `stable_mir` crate version. E.g.: |
| 17 | + |
| 18 | +```toml |
| 19 | +# Example of a rust-toolchain.toml |
| 20 | +[toolchain] |
| 21 | +# Update the date to change the toolchain version. |
| 22 | +channel = "nightly-2024-06-17" |
| 23 | +components = ["llvm-tools", "rustc-dev", "rust-src"] |
| 24 | +``` |
| 25 | + |
| 26 | +## Initializing StableMIR |
| 27 | + |
| 28 | +There's currently no stable way to initialize the Rust compiler and StableMIR. |
| 29 | +See [#0069](https://github.com/rust-lang/project-stable-mir/issues/69) for more details. |
| 30 | + |
| 31 | +Instead, StableMIR includes two unstable workarounds to give you a quick start. |
| 32 | +The `run` and `run_with_tcx` macros, both from present in the `rustc_smir` crate. |
| 33 | + |
| 34 | +In order to use the `run` macro, you first need to declare the following external crates: |
| 35 | + |
| 36 | +```rust |
| 37 | +extern crate rustc_driver; |
| 38 | +extern crate rustc_interface; |
| 39 | +#[macro_use] |
| 40 | +extern crate rustc_smir; |
| 41 | +// This one you should know already. =) |
| 42 | +extern crate stable_mir; |
| 43 | +``` |
| 44 | + |
| 45 | +Then start the driver using the `run!()` macro: |
| 46 | + |
| 47 | +```rust |
| 48 | + let result = run!(rustc_args, callback_fn); |
| 49 | +``` |
| 50 | + |
| 51 | +This macro takes two arguments: |
| 52 | + |
| 53 | +1. A vector with the command arguments to the compiler. |
| 54 | +2. A callback function, which can either be a closure expression or a function ident. |
| 55 | + - This callback function shouldn't take any argument, and it should return a `ControlFlow<B,C>`. |
| 56 | + |
| 57 | +It will trigger the compilation in the current process, with the provided arguments, and invoke the callback after the |
| 58 | +compiler ran all its analyses, but before code generation. |
| 59 | + |
| 60 | +The macro will return a `Result<C, CompilerError<B>>` representing the compilation result and the callback return value. |
| 61 | + |
| 62 | +The second option is the `run_with_tcx!()` macro, which is very similar to the `run` macro. |
| 63 | +The main difference is that this macro passes a copy of the Rust compiler context (`TyCtxt`) to the callback, |
| 64 | +which allows the user to also make calls to internal compiler APIs. |
| 65 | + |
| 66 | +Note that this option also requires the declaration of the `rustc_middle` external crate, i.e., you should now have the |
| 67 | +following declarations: |
| 68 | + |
| 69 | +```rust |
| 70 | +extern crate rustc_driver; |
| 71 | +extern crate rustc_interface; |
| 72 | +extern crate rustc_middle; // This one is new! |
| 73 | +#[macro_use] |
| 74 | +extern crate rustc_smir; |
| 75 | +extern crate stable_mir; |
| 76 | +``` |
| 77 | + |
| 78 | +## Scope of StableMIR objects |
| 79 | + |
| 80 | +StableMIR objects should not be used outside the scope of the callback function. |
| 81 | +Any usage outside this scope can panic or return an incorrect value. |
| 82 | + |
| 83 | +For example, the following code is valid, since the logic we are storing is only used while the callback function |
| 84 | +is running: |
| 85 | + |
| 86 | +```rust |
| 87 | +fn print_items(rustc_args: Vec<String>) { |
| 88 | + let _result = run!(rustc_args, || { |
| 89 | + for item in stable_mir::all_local_items() { |
| 90 | + // Using items inside the callback! |
| 91 | + println!(" - {}", item.name()) |
| 92 | + } |
| 93 | + }); |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +However, the following usage isn't valid, and `stable_mir` will panic when we invoke the `name()` function. |
| 98 | + |
| 99 | +```rust |
| 100 | +fn broken_print_items(rustc_args: Vec<String>) { |
| 101 | + let result = run!(rustc_args, || { ControlFlow::Continue(stable_mir::all_local_items())}); |
| 102 | + if let ControlFlow::Continue(items) = result { |
| 103 | + for item in items { |
| 104 | + // Using item outside the callback function is wrong! |
| 105 | + println!(" - {}", item.name()) |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +StableMIR objects should also not be shared across different threads. |
| 112 | + |
| 113 | +## Analyzing crate definitions |
| 114 | + |
| 115 | +TODO |
| 116 | + |
| 117 | +## Analyzing monomorphic instances |
| 118 | + |
| 119 | +TODO |
0 commit comments