Skip to content

Commit bd2d8d5

Browse files
celinvaladpaco-awszhassan-aws
authored
Change reachability module to use StableMIR (rust-lang#2894)
Rewrite the reachability module to use Stable APIs wherever possible. Note that in StableMIR the instance body is already monomorphized and constants are already evaluated, which simplifies the code for most of it, except to handle stubbing issue rust-lang#2589. For the stubbing issue, we still use a mix of stable and internal APIs to detect an invalid monomorphization. Co-authored-by: Adrian Palacios <73246657+adpaco-aws@users.noreply.github.com> Co-authored-by: Zyad Hassan <88045115+zhassan-aws@users.noreply.github.com>
1 parent 5fc7172 commit bd2d8d5

File tree

7 files changed

+442
-394
lines changed

7 files changed

+442
-394
lines changed

docs/src/dev-documentation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ developers (including external contributors):
1414
3. [Development setup recommendations for working with `cbmc`](./cbmc-hacks.md).
1515
4. [Development setup recommendations for working with `rustc`](./rustc-hacks.md).
1616
5. [Guide for testing in Kani](./testing.md).
17+
6. [Transition to StableMIR](./stable_mir.md).
1718

1819
> **NOTE**: The developer documentation is intended for Kani developers and not
1920
users. At present, the project is under heavy development and some items

docs/src/stable_mir.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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.

kani-compiler/src/kani_compiler.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use rustc_hir::definitions::DefPathHash;
3434
use rustc_interface::Config;
3535
use rustc_middle::ty::TyCtxt;
3636
use rustc_session::config::{ErrorOutputType, OutputType};
37+
use rustc_smir::rustc_internal;
3738
use rustc_span::ErrorGuaranteed;
3839
use std::collections::{BTreeMap, HashMap};
3940
use std::fs::File;
@@ -400,9 +401,12 @@ impl Callbacks for KaniCompiler {
400401
) -> Compilation {
401402
if self.stage.is_init() {
402403
self.stage = rustc_queries.global_ctxt().unwrap().enter(|tcx| {
403-
check_crate_items(tcx, self.queries.lock().unwrap().args().ignore_global_asm);
404-
self.process_harnesses(tcx)
405-
});
404+
rustc_internal::run(tcx, || {
405+
check_crate_items(tcx, self.queries.lock().unwrap().args().ignore_global_asm);
406+
self.process_harnesses(tcx)
407+
})
408+
.unwrap()
409+
})
406410
}
407411

408412
self.prepare_codegen()

kani-compiler/src/kani_middle/provide.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,14 @@ fn collect_and_partition_mono_items(
7676
tcx: TyCtxt,
7777
key: (),
7878
) -> queries::collect_and_partition_mono_items::ProvidedValue {
79-
let entry_fn = tcx.entry_fn(()).map(|(id, _)| id);
80-
let local_reachable = filter_crate_items(tcx, |_, def_id| {
81-
tcx.is_reachable_non_generic(def_id) || entry_fn == Some(def_id)
82-
});
83-
// We do not actually need the value returned here.
84-
collect_reachable_items(tcx, &local_reachable);
79+
rustc_smir::rustc_internal::run(tcx, || {
80+
let entry_fn = tcx.entry_fn(()).map(|(id, _)| id);
81+
let local_reachable = filter_crate_items(tcx, |_, def_id| {
82+
tcx.is_reachable_non_generic(def_id) || entry_fn == Some(def_id)
83+
});
84+
// We do not actually need the value returned here.
85+
collect_reachable_items(tcx, &local_reachable);
86+
})
87+
.unwrap();
8588
(rustc_interface::DEFAULT_QUERY_PROVIDERS.collect_and_partition_mono_items)(tcx, key)
8689
}

0 commit comments

Comments
 (0)