Skip to content

Commit 7a35dac

Browse files
ouz-acelinval
andauthored
Use stable_mir api instead of internal api for analysis (rust-lang#2871)
This update introduces several `stable_mir` APIs to `Kani`. Previously, we relied on internal APIs to accomplish our goals. However, with the recent enhancements to `stable_mir`, we no longer need these internal APIs. This pull request hopefully marks the beginning of a migration towards `stable_mir` for `Kani`. Co-authored-by: Celina G. Val <[email protected]>
1 parent 994be04 commit 7a35dac

File tree

2 files changed

+51
-48
lines changed

2 files changed

+51
-48
lines changed

kani-compiler/src/kani_middle/analysis.rs

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
//! This module will perform all the analyses requested. Callers are responsible for selecting
88
//! when the cost of these analyses are worth it.
99
10-
use rustc_middle::mir::mono::MonoItem;
11-
use rustc_middle::mir::visit::Visitor as MirVisitor;
12-
use rustc_middle::mir::{Location, Rvalue, Statement, StatementKind, Terminator, TerminatorKind};
10+
use rustc_middle::mir::mono::MonoItem as InternalMonoItem;
1311
use rustc_middle::ty::TyCtxt;
12+
use rustc_smir::rustc_internal;
13+
use stable_mir::mir::mono::MonoItem;
14+
use stable_mir::mir::{
15+
visit::Location, MirVisitor, Rvalue, Statement, StatementKind, Terminator, TerminatorKind,
16+
};
1417
use std::collections::HashMap;
1518
use std::fmt::Display;
1619

@@ -20,29 +23,30 @@ use std::fmt::Display;
2023
/// - Number of items per type (Function / Constant / Shims)
2124
/// - Number of instructions per type.
2225
/// - Total number of MIR instructions.
23-
pub fn print_stats<'tcx>(tcx: TyCtxt<'tcx>, items: &[MonoItem<'tcx>]) {
24-
let item_types = items.iter().collect::<Counter>();
25-
let visitor = items
26-
.iter()
27-
.filter_map(|&mono| {
28-
if let MonoItem::Fn(instance) = mono {
29-
Some(tcx.instance_mir(instance.def))
30-
} else {
31-
None
32-
}
33-
})
34-
.fold(StatsVisitor::default(), |mut visitor, body| {
35-
visitor.visit_body(body);
36-
visitor
37-
});
38-
eprintln!("====== Reachability Analysis Result =======");
39-
eprintln!("Total # items: {}", item_types.total());
40-
eprintln!("Total # statements: {}", visitor.stmts.total());
41-
eprintln!("Total # expressions: {}", visitor.exprs.total());
42-
eprintln!("\nReachable Items:\n{item_types}");
43-
eprintln!("Statements:\n{}", visitor.stmts);
44-
eprintln!("Expressions:\n{}", visitor.exprs);
45-
eprintln!("-------------------------------------------")
26+
pub fn print_stats<'tcx>(tcx: TyCtxt<'tcx>, items: &[InternalMonoItem<'tcx>]) {
27+
rustc_internal::run(tcx, || {
28+
let items: Vec<MonoItem> = items.iter().map(rustc_internal::stable).collect();
29+
let item_types = items.iter().collect::<Counter>();
30+
let visitor = items
31+
.iter()
32+
.filter_map(
33+
|mono| {
34+
if let MonoItem::Fn(instance) = mono { Some(instance) } else { None }
35+
},
36+
)
37+
.fold(StatsVisitor::default(), |mut visitor, body| {
38+
visitor.visit_body(&body.body());
39+
visitor
40+
});
41+
eprintln!("====== Reachability Analysis Result =======");
42+
eprintln!("Total # items: {}", item_types.total());
43+
eprintln!("Total # statements: {}", visitor.stmts.total());
44+
eprintln!("Total # expressions: {}", visitor.exprs.total());
45+
eprintln!("\nReachable Items:\n{item_types}");
46+
eprintln!("Statements:\n{}", visitor.stmts);
47+
eprintln!("Expressions:\n{}", visitor.exprs);
48+
eprintln!("-------------------------------------------")
49+
});
4650
}
4751

4852
#[derive(Default)]
@@ -54,20 +58,20 @@ struct StatsVisitor {
5458
exprs: Counter,
5559
}
5660

57-
impl<'tcx> MirVisitor<'tcx> for StatsVisitor {
58-
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
61+
impl MirVisitor for StatsVisitor {
62+
fn visit_statement(&mut self, statement: &Statement, location: Location) {
5963
self.stmts.add(statement);
6064
// Also visit the type of expression.
6165
self.super_statement(statement, location);
6266
}
6367

64-
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, _location: Location) {
68+
fn visit_terminator(&mut self, terminator: &Terminator, _location: Location) {
6569
self.stmts.add(terminator);
6670
// Stop here since we don't care today about the information inside the terminator.
6771
// self.super_terminator(terminator, location);
6872
}
6973

70-
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, _location: Location) {
74+
fn visit_rvalue(&mut self, rvalue: &Rvalue, _location: Location) {
7175
self.exprs.add(rvalue);
7276
// Stop here since we don't care today about the information inside the rvalue.
7377
// self.super_rvalue(rvalue, location);
@@ -115,8 +119,8 @@ impl<T: Into<Key>> FromIterator<T> for Counter {
115119
#[derive(Debug, Eq, Hash, PartialEq)]
116120
struct Key(pub &'static str);
117121

118-
impl<'tcx> From<&MonoItem<'tcx>> for Key {
119-
fn from(value: &MonoItem) -> Self {
122+
impl From<&MonoItem> for Key {
123+
fn from(value: &stable_mir::mir::mono::MonoItem) -> Self {
120124
match value {
121125
MonoItem::Fn(_) => Key("function"),
122126
MonoItem::GlobalAsm(_) => Key("global assembly"),
@@ -125,18 +129,18 @@ impl<'tcx> From<&MonoItem<'tcx>> for Key {
125129
}
126130
}
127131

128-
impl<'tcx> From<&Statement<'tcx>> for Key {
129-
fn from(value: &Statement<'tcx>) -> Self {
132+
impl From<&Statement> for Key {
133+
fn from(value: &Statement) -> Self {
130134
match value.kind {
131-
StatementKind::Assign(_) => Key("Assign"),
135+
StatementKind::Assign(..) => Key("Assign"),
132136
StatementKind::Deinit(_) => Key("Deinit"),
133137
StatementKind::Intrinsic(_) => Key("Intrinsic"),
134138
StatementKind::SetDiscriminant { .. } => Key("SetDiscriminant"),
135139
// For now, we don't care about the ones below.
136-
StatementKind::AscribeUserType(_, _)
140+
StatementKind::AscribeUserType { .. }
137141
| StatementKind::Coverage(_)
138142
| StatementKind::ConstEvalCounter
139-
| StatementKind::FakeRead(_)
143+
| StatementKind::FakeRead(..)
140144
| StatementKind::Nop
141145
| StatementKind::PlaceMention(_)
142146
| StatementKind::Retag(_, _)
@@ -146,29 +150,26 @@ impl<'tcx> From<&Statement<'tcx>> for Key {
146150
}
147151
}
148152

149-
impl<'tcx> From<&Terminator<'tcx>> for Key {
150-
fn from(value: &Terminator<'tcx>) -> Self {
153+
impl From<&Terminator> for Key {
154+
fn from(value: &Terminator) -> Self {
151155
match value.kind {
156+
TerminatorKind::Abort => Key("Abort"),
152157
TerminatorKind::Assert { .. } => Key("Assert"),
153158
TerminatorKind::Call { .. } => Key("Call"),
154159
TerminatorKind::Drop { .. } => Key("Drop"),
155160
TerminatorKind::CoroutineDrop => Key("CoroutineDrop"),
156161
TerminatorKind::Goto { .. } => Key("Goto"),
157-
TerminatorKind::FalseEdge { .. } => Key("FalseEdge"),
158-
TerminatorKind::FalseUnwind { .. } => Key("FalseUnwind"),
159162
TerminatorKind::InlineAsm { .. } => Key("InlineAsm"),
160-
TerminatorKind::UnwindResume => Key("UnwindResume"),
163+
TerminatorKind::Resume { .. } => Key("Resume"),
161164
TerminatorKind::Return => Key("Return"),
162165
TerminatorKind::SwitchInt { .. } => Key("SwitchInt"),
163-
TerminatorKind::UnwindTerminate(_) => Key("UnwindTerminate"),
164166
TerminatorKind::Unreachable => Key("Unreachable"),
165-
TerminatorKind::Yield { .. } => Key("Yield"),
166167
}
167168
}
168169
}
169170

170-
impl<'tcx> From<&Rvalue<'tcx>> for Key {
171-
fn from(value: &Rvalue<'tcx>) -> Self {
171+
impl From<&Rvalue> for Key {
172+
fn from(value: &Rvalue) -> Self {
172173
match value {
173174
Rvalue::Use(_) => Key("Use"),
174175
Rvalue::Repeat(_, _) => Key("Repeat"),
@@ -177,8 +178,8 @@ impl<'tcx> From<&Rvalue<'tcx>> for Key {
177178
Rvalue::AddressOf(_, _) => Key("AddressOf"),
178179
Rvalue::Len(_) => Key("Len"),
179180
Rvalue::Cast(_, _, _) => Key("Cast"),
180-
Rvalue::BinaryOp(_, _) => Key("BinaryOp"),
181-
Rvalue::CheckedBinaryOp(_, _) => Key("CheckedBinaryOp"),
181+
Rvalue::BinaryOp(..) => Key("BinaryOp"),
182+
Rvalue::CheckedBinaryOp(..) => Key("CheckedBinaryOp"),
182183
Rvalue::NullaryOp(_, _) => Key("NullaryOp"),
183184
Rvalue::UnaryOp(_, _) => Key("UnaryOp"),
184185
Rvalue::Discriminant(_) => Key("Discriminant"),

kani-compiler/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ extern crate rustc_interface;
2626
extern crate rustc_metadata;
2727
extern crate rustc_middle;
2828
extern crate rustc_session;
29+
extern crate rustc_smir;
2930
extern crate rustc_span;
3031
extern crate rustc_target;
32+
extern crate stable_mir;
3133
// We can't add this directly as a dependency because we need the version to match rustc
3234
extern crate tempfile;
3335

0 commit comments

Comments
 (0)