Skip to content

Commit

Permalink
Create StableMir replacer for SMirCalls
Browse files Browse the repository at this point in the history
  • Loading branch information
ouz-a committed Aug 29, 2023
1 parent cedbe5c commit c2fe0bf
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 33 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4251,8 +4251,11 @@ dependencies = [
name = "rustc_smir"
version = "0.0.0"
dependencies = [
"rustc_driver",
"rustc_hir",
"rustc_interface",
"rustc_middle",
"rustc_session",
"rustc_span",
"rustc_target",
"scoped-tls",
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_smir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ rustc_hir = { path = "../rustc_hir", optional = true }
rustc_middle = { path = "../rustc_middle", optional = true }
rustc_span = { path = "../rustc_span", optional = true }
rustc_target = { path = "../rustc_target", optional = true }
rustc_driver = { path = "../rustc_driver", optional = true }
rustc_interface = { path = "../rustc_interface", optional = true}
rustc_session = {path = "../rustc_session", optional = true}
tracing = "0.1"
scoped-tls = "1.0"

Expand All @@ -18,4 +21,7 @@ default = [
"rustc_middle",
"rustc_span",
"rustc_target",
"rustc_driver",
"rustc_interface",
"rustc_session",
]
41 changes: 41 additions & 0 deletions compiler/rustc_smir/src/rustc_internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
use std::fmt::Debug;
use std::string::ToString;

use crate::rustc_internal;
use crate::{
rustc_smir::Tables,
stable_mir::{self, with},
};
use rustc_driver::{Callbacks, Compilation, RunCompiler};
use rustc_interface::{interface, Queries};
use rustc_middle::ty::TyCtxt;
use rustc_session::EarlyErrorHandler;
pub use rustc_span::def_id::{CrateNum, DefId};

fn with_tables<R>(mut f: impl FnMut(&mut Tables<'_>) -> R) -> R {
Expand Down Expand Up @@ -163,3 +167,40 @@ pub type Opaque = impl Debug + ToString + Clone;
pub(crate) fn opaque<T: Debug>(value: &T) -> Opaque {
format!("{value:?}")
}

pub struct StableMir {
args: Vec<String>,
callback: fn(TyCtxt<'_>),
}

impl StableMir {
/// Creates a new `StableMir` instance, with given test_function and arguments.
pub fn new(args: Vec<String>, callback: fn(TyCtxt<'_>)) -> Self {
StableMir { args, callback }
}

/// Runs the compiler against given target and tests it with `test_function`
pub fn run(&mut self) {
rustc_driver::catch_fatal_errors(|| {
RunCompiler::new(&self.args.clone(), self).run().unwrap();
})
.unwrap();
}
}

impl Callbacks for StableMir {
/// Called after analysis. Return value instructs the compiler whether to
/// continue the compilation afterwards (defaults to `Compilation::Continue`)
fn after_analysis<'tcx>(
&mut self,
_handler: &EarlyErrorHandler,
_compiler: &interface::Compiler,
queries: &'tcx Queries<'tcx>,
) -> Compilation {
queries.global_ctxt().unwrap().enter(|tcx| {
rustc_internal::run(tcx, || (self.callback)(tcx));
});
// No need to keep going.
Compilation::Stop
}
}
3 changes: 1 addition & 2 deletions compiler/rustc_smir/src/stable_mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
use std::cell::Cell;

use crate::rustc_smir::Tables;

use self::ty::{
GenericDef, Generics, ImplDef, ImplTrait, PredicateKind, Span, TraitDecl, TraitDef, Ty, TyKind,
};
use crate::rustc_smir::Tables;

pub mod mir;
pub mod ty;
Expand Down
34 changes: 3 additions & 31 deletions tests/ui-fulldeps/stable-mir/crate-info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,12 @@
#![feature(rustc_private)]
#![feature(assert_matches)]

extern crate rustc_driver;
extern crate rustc_hir;
extern crate rustc_interface;
extern crate rustc_middle;
extern crate rustc_session;
extern crate rustc_smir;

use rustc_driver::{Callbacks, Compilation, RunCompiler};
use rustc_hir::def::DefKind;
use rustc_interface::{interface, Queries};
use rustc_middle::ty::TyCtxt;
use rustc_session::EarlyErrorHandler;
use rustc_smir::{rustc_internal, stable_mir};
use std::assert_matches::assert_matches;
use std::io::Write;
Expand Down Expand Up @@ -130,8 +124,8 @@ fn get_item<'a>(

/// This test will generate and analyze a dummy crate using the stable mir.
/// For that, it will first write the dummy crate into a file.
/// It will invoke the compiler using a custom Callback implementation, which will
/// invoke Stable MIR APIs after the compiler has finished its analysis.
/// Then it will create a `StableMir` using custom arguments and then
/// it will run the compiler.
fn main() {
let path = "input.rs";
generate_input(&path).unwrap();
Expand All @@ -142,29 +136,7 @@ fn main() {
CRATE_NAME.to_string(),
path.to_string(),
];
rustc_driver::catch_fatal_errors(|| {
RunCompiler::new(&args, &mut SMirCalls {}).run().unwrap();
})
.unwrap();
}

struct SMirCalls {}

impl Callbacks for SMirCalls {
/// Called after analysis. Return value instructs the compiler whether to
/// continue the compilation afterwards (defaults to `Compilation::Continue`)
fn after_analysis<'tcx>(
&mut self,
_handler: &EarlyErrorHandler,
_compiler: &interface::Compiler,
queries: &'tcx Queries<'tcx>,
) -> Compilation {
queries.global_ctxt().unwrap().enter(|tcx| {
rustc_smir::rustc_internal::run(tcx, || test_stable_mir(tcx));
});
// No need to keep going.
Compilation::Stop
}
rustc_internal::StableMir::new(args, test_stable_mir).run();
}

fn generate_input(path: &str) -> std::io::Result<()> {
Expand Down

0 comments on commit c2fe0bf

Please sign in to comment.