Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ debug_log = []
[package.metadata.rust-analyzer]
# This package uses rustc crates.
rustc_private=true

[workspace]
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ rustup-clear-toolchain:
rustup override unset --nonexistent
rustup toolchain uninstall "${TOOLCHAIN_NAME}"

TESTDIR=$(CURDIR)/tests/integration/programs
TESTDIR=tests/integration/programs

.PHONY: integration-test
integration-test: TESTS ?= $(shell find $(TESTDIR) -type f -name "*.rs")
Expand Down
16 changes: 9 additions & 7 deletions src/bin/cargo_stable_mir_json.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::env;

use std::io::Write;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};

use anyhow::{bail, Result};
use {
anyhow::{bail, Result},
std::{
env,
io::Write,
os::unix::fs::PermissionsExt,
path::{Path, PathBuf},
},
};

fn main() -> Result<()> {
let args: Vec<_> = env::args().collect();
Expand Down
12 changes: 6 additions & 6 deletions src/driver.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This module provides a compiler driver such that:
//!
//! 1. the rustc compiler context is available
//! 2. the rustc `stable_mir` APIs are available
//! 1. the rustc compiler context is available
//! 2. the rustc `stable_mir` APIs are available
//!
//! It exports a single function:
//!
Expand All @@ -22,10 +22,10 @@ extern crate rustc_interface;
extern crate rustc_middle;
extern crate rustc_session;
extern crate rustc_smir;
use rustc_driver::Compilation;
use rustc_interface::interface::Compiler;
use rustc_middle::ty::TyCtxt;
use rustc_smir::rustc_internal;
use {
rustc_driver::Compilation, rustc_interface::interface::Compiler, rustc_middle::ty::TyCtxt,
rustc_smir::rustc_internal,
};

struct StableMirCallbacks {
callback_fn: fn(TyCtxt) -> (),
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
pub mod driver;
pub mod mk_graph;
pub mod printer;
pub use driver::stable_mir_driver;
pub use printer::*;
pub use {driver::stable_mir_driver, printer::*};
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
use std::env;
pub mod driver;
pub mod printer;
use driver::stable_mir_driver;
use printer::emit_smir;
use stable_mir_json::mk_graph::{emit_d2file, emit_dotfile};
use {
driver::stable_mir_driver,
printer::emit_smir,
stable_mir_json::mk_graph::{emit_d2file, emit_dotfile},
};

fn main() {
let mut args: Vec<String> = env::args().collect();
Expand Down
25 changes: 15 additions & 10 deletions src/mk_graph/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
use std::collections::HashMap;

extern crate stable_mir;
use stable_mir::mir::{
BorrowKind, ConstOperand, Mutability, NonDivergingIntrinsic, Operand, Rvalue, Statement,
StatementKind, Terminator, TerminatorKind,
use {
super::{
index::{AllocIndex, LayoutInfo, TypeEntry, TypeIndex, TypeKind},
util::{function_string, short_fn_name, GraphLabelString},
},
crate::printer::SmirJson,
stable_mir::{
mir::{
BorrowKind, ConstOperand, Mutability, NonDivergingIntrinsic, Operand, Rvalue,
Statement, StatementKind, Terminator, TerminatorKind,
},
ty::{ConstantKind, IndexedVal, MirConst, Ty},
},
};
use stable_mir::ty::{ConstantKind, IndexedVal, MirConst, Ty};

use crate::printer::SmirJson;

use super::index::{AllocIndex, LayoutInfo, TypeEntry, TypeIndex, TypeKind};
use super::util::{function_string, short_fn_name, GraphLabelString};

// =============================================================================
// GraphContext
Expand Down Expand Up @@ -112,7 +116,8 @@ impl GraphContext {
lines
}

/// Resolve a call target to a function name if it's a constant function pointer
/// Resolve a call target to a function name if it's a constant function
/// pointer
pub fn resolve_call_target(&self, func: &Operand) -> Option<String> {
match func {
Operand::Constant(ConstOperand { const_, .. }) => {
Expand Down
18 changes: 11 additions & 7 deletions src/mk_graph/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
use std::collections::HashMap;

extern crate stable_mir;
use stable_mir::abi::{FieldsShape, LayoutShape};
use stable_mir::mir::alloc::GlobalAlloc;
use stable_mir::ty::{IndexedVal, Ty};
use stable_mir::CrateDef;

use crate::printer::{AllocInfo, TypeMetadata};
use {
crate::printer::{AllocInfo, TypeMetadata},
stable_mir::{
abi::{FieldsShape, LayoutShape},
mir::alloc::GlobalAlloc,
ty::{IndexedVal, Ty},
CrateDef,
},
};

// =============================================================================
// Index Structures
Expand Down Expand Up @@ -306,7 +309,8 @@ impl TypeEntry {
.iter()
.map(|&t| FieldInfo {
ty: t,
offset: None, // Enum variant offsets require variant-specific layout
offset: None, /* Enum variant offsets require variant-specific
* layout */
})
.collect(),
})
Expand Down
21 changes: 13 additions & 8 deletions src/mk_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
//! This module provides functionality to generate graph visualizations
//! of Rust's MIR in various formats (DOT, D2).

use std::fs::File;
use std::io::{self, Write};
use std::{
fs::File,
io::{self, Write},
};

extern crate rustc_middle;
use rustc_middle::ty::TyCtxt;

extern crate rustc_session;
use rustc_session::config::{OutFileName, OutputType};

use crate::printer::collect_smir;
use {
crate::printer::collect_smir,
rustc_session::config::{OutFileName, OutputType},
};

// Sub-modules
pub mod context;
Expand All @@ -21,9 +24,11 @@ pub mod output;
pub mod util;

// Re-exports for convenience
pub use context::GraphContext;
pub use index::{AllocEntry, AllocIndex, AllocKind, TypeIndex};
pub use util::GraphLabelString;
pub use {
context::GraphContext,
index::{AllocEntry, AllocIndex, AllocKind, TypeIndex},
util::GraphLabelString,
};

// =============================================================================
// Entry Points
Expand Down
18 changes: 10 additions & 8 deletions src/mk_graph/output/d2.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
//! D2 diagram format output for MIR graphs.

extern crate stable_mir;
use stable_mir::mir::TerminatorKind;

use crate::printer::SmirJson;
use crate::MonoItemKind;

use crate::mk_graph::context::GraphContext;
use crate::mk_graph::util::{
escape_d2, is_unqualified, name_lines, short_name, terminator_targets,
use {
crate::{
mk_graph::{
context::GraphContext,
util::{escape_d2, is_unqualified, name_lines, short_name, terminator_targets},
},
printer::SmirJson,
MonoItemKind,
},
stable_mir::mir::TerminatorKind,
};

impl SmirJson<'_> {
Expand Down
48 changes: 30 additions & 18 deletions src/mk_graph/output/dot.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
//! DOT (Graphviz) format output for MIR graphs.

use std::collections::HashSet;

use dot_writer::{Attributes, Color, DotWriter, Scope, Shape, Style};
use {
dot_writer::{Attributes, Color, DotWriter, Scope, Shape, Style},
std::collections::HashSet,
};

extern crate stable_mir;
use stable_mir::mir::{BasicBlock, ConstOperand, Operand, TerminatorKind, UnwindAction};

use crate::printer::SmirJson;
use crate::MonoItemKind;

use crate::mk_graph::context::GraphContext;
use crate::mk_graph::util::{block_name, is_unqualified, name_lines, short_name, GraphLabelString};
use {
crate::{
mk_graph::{
context::GraphContext,
util::{block_name, is_unqualified, name_lines, short_name, GraphLabelString},
},
printer::SmirJson,
MonoItemKind,
},
stable_mir::mir::{BasicBlock, ConstOperand, Operand, TerminatorKind, UnwindAction},
};

impl SmirJson<'_> {
/// Convert the MIR to DOT (Graphviz) format
Expand Down Expand Up @@ -91,7 +96,8 @@ impl SmirJson<'_> {
local_node.set("color", "palegreen3", false);
drop(local_node);

// Cannot define local functions that capture env. variables. Instead we define _closures_.
// Cannot define local functions that capture env. variables. Instead we
// define _closures_.
let process_block =
|cluster: &mut Scope<'_, '_>, node_id: usize, b: &BasicBlock| {
let name = &item.symbol_name;
Expand Down Expand Up @@ -173,8 +179,11 @@ impl SmirJson<'_> {
.set_label(&dest);
}

// The call edge has to be drawn outside the cluster, outside this function (cluster borrows &mut graph)!
// Code for that is therefore separated into its own second function below.
// The call edge has to be drawn outside
// the cluster, outside this function
// (cluster borrows &mut graph)!
// Code for that is therefore separated
// into its own second function below.
}
Assert {
cond,
Expand Down Expand Up @@ -235,9 +244,10 @@ impl SmirJson<'_> {

drop(c); // so we can borrow graph again

// call edges have to be added _outside_ the cluster of blocks for one function
// because they go between different clusters. Due to a scope/borrow issue, we have
// to make a 2nd pass over the bodies of the item.
// call edges have to be added _outside_ the cluster of blocks for one
// function because they go between different
// clusters. Due to a scope/borrow issue, we have to
// make a 2nd pass over the bodies of the item.
let add_call_edges =
|graph: &mut Scope<'_, '_>, offset: usize, bs: &Vec<BasicBlock>| {
for (i, b) in bs.iter().enumerate() {
Expand All @@ -252,15 +262,17 @@ impl SmirJson<'_> {
if let Some(callee) =
ctx.functions.get(&const_.ty())
{
// callee node/body will be added when its body is added, missing ones added before
// callee node/body will be added when its
// body is added, missing ones added before
graph.edge(
&this_block,
block_name(callee, 0),
)
} else {
let unknown = format!("{}", const_.ty());
// pathological case, could panic! instead.
// all unknown callees will be collapsed into one `unknown` node
// all unknown callees will be collapsed
// into one `unknown` node
graph.edge(&this_block, unknown)
}
}
Expand Down
15 changes: 9 additions & 6 deletions src/mk_graph/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
use std::hash::{DefaultHasher, Hash, Hasher};

extern crate stable_mir;
use stable_mir::mir::{
AggregateKind, BorrowKind, ConstOperand, Mutability, NonDivergingIntrinsic, NullOp, Operand,
Place, ProjectionElem, Rvalue, Terminator, TerminatorKind, UnwindAction,
use {
crate::printer::FnSymType,
stable_mir::{
mir::{
AggregateKind, BorrowKind, ConstOperand, Mutability, NonDivergingIntrinsic, NullOp,
Operand, Place, ProjectionElem, Rvalue, Terminator, TerminatorKind, UnwindAction,
},
ty::{IndexedVal, RigidTy},
},
};
use stable_mir::ty::{IndexedVal, RigidTy};

use crate::printer::FnSymType;

// =============================================================================
// GraphLabelString Trait
Expand Down
Loading