Skip to content

Commit

Permalink
Unrolled build for rust-lang#117417
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#117417 - celinval:smir-visitor, r=oli-obk

Add a stable MIR visitor

This change also adds a few utility functions as well and extend most `mir` and `ty` ADTs to implement `PartialEq` and `Eq`.

Fixes rust-lang/project-stable-mir#32

r? `@oli-obk`
  • Loading branch information
rust-timer authored Nov 1, 2023
2 parents 9d83ac2 + af7472e commit 634fed0
Show file tree
Hide file tree
Showing 8 changed files with 662 additions and 85 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
tables.create_def_id(def_id)
}

fn instance_mangled_name(&self, def: InstanceDef) -> String {
let tables = self.0.borrow_mut();
let instance = tables.instances[def];
tables.tcx.symbol_name(instance).name.to_string()
}

fn mono_instance(&self, item: stable_mir::CrateItem) -> stable_mir::mir::mono::Instance {
let mut tables = self.0.borrow_mut();
let def_id = tables[item.0];
Expand Down
7 changes: 4 additions & 3 deletions compiler/stable_mir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ pub type DefKind = Opaque;
pub type Filename = Opaque;

/// Holds information about an item in the crate.
/// For now, it only stores the item DefId. Use functions inside `rustc_internal` module to
/// use this item.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct CrateItem(pub DefId);

Expand Down Expand Up @@ -224,6 +222,9 @@ pub trait Context {
/// Get the instance.
fn instance_def_id(&self, instance: InstanceDef) -> DefId;

/// Get the instance mangled name.
fn instance_mangled_name(&self, instance: InstanceDef) -> String;

/// Convert a non-generic crate item into an instance.
/// This function will panic if the item is generic.
fn mono_instance(&self, item: CrateItem) -> Instance;
Expand Down Expand Up @@ -259,7 +260,7 @@ pub fn with<R>(f: impl FnOnce(&dyn Context) -> R) -> R {
}

/// A type that provides internal information but that can still be used for debug purpose.
#[derive(Clone)]
#[derive(Clone, Eq, PartialEq)]
pub struct Opaque(String);

impl std::fmt::Display for Opaque {
Expand Down
2 changes: 2 additions & 0 deletions compiler/stable_mir/src/mir.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod body;
pub mod mono;
pub mod visit;

pub use body::*;
pub use visit::MirVisitor;
74 changes: 38 additions & 36 deletions compiler/stable_mir/src/mir/body.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ty::{AdtDef, ClosureDef, Const, CoroutineDef, GenericArgs, Movability, Region};
use crate::ty::{AdtDef, ClosureDef, Const, CoroutineDef, GenericArgs, Movability, Region, Ty};
use crate::Opaque;
use crate::{ty::Ty, Span};
use crate::Span;

/// The SMIR representation of a single function.
#[derive(Clone, Debug)]
Expand All @@ -12,10 +12,10 @@ pub struct Body {
// The first local is the return value pointer, followed by `arg_count`
// locals for the function arguments, followed by any user-declared
// variables and temporaries.
locals: LocalDecls,
pub(super) locals: LocalDecls,

// The number of arguments this function takes.
arg_count: usize,
pub(super) arg_count: usize,
}

impl Body {
Expand All @@ -35,7 +35,7 @@ impl Body {

/// Return local that holds this function's return value.
pub fn ret_local(&self) -> &LocalDecl {
&self.locals[0]
&self.locals[RETURN_LOCAL]
}

/// Locals in `self` that correspond to this function's arguments.
Expand All @@ -60,7 +60,7 @@ impl Body {

type LocalDecls = Vec<LocalDecl>;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LocalDecl {
pub ty: Ty,
pub span: Span,
Expand All @@ -72,13 +72,13 @@ pub struct BasicBlock {
pub terminator: Terminator,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Terminator {
pub kind: TerminatorKind,
pub span: Span,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum TerminatorKind {
Goto {
target: usize,
Expand Down Expand Up @@ -122,7 +122,7 @@ pub enum TerminatorKind {
},
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct InlineAsmOperand {
pub in_value: Option<Operand>,
pub out_place: Option<Place>,
Expand All @@ -131,15 +131,15 @@ pub struct InlineAsmOperand {
pub raw_rpr: String,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum UnwindAction {
Continue,
Unreachable,
Terminate,
Cleanup(usize),
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum AssertMessage {
BoundsCheck { len: Operand, index: Operand },
Overflow(BinOp, Operand, Operand),
Expand All @@ -151,7 +151,7 @@ pub enum AssertMessage {
MisalignedPointerDereference { required: Operand, found: Operand },
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum BinOp {
Add,
AddUnchecked,
Expand All @@ -177,20 +177,20 @@ pub enum BinOp {
Offset,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum UnOp {
Not,
Neg,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CoroutineKind {
Async(CoroutineSource),
Coroutine,
Gen(CoroutineSource),
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CoroutineSource {
Block,
Closure,
Expand All @@ -204,7 +204,7 @@ pub(crate) type LocalDefId = Opaque;
pub(crate) type Coverage = Opaque;

/// The FakeReadCause describes the type of pattern why a FakeRead statement exists.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum FakeReadCause {
ForMatchGuard,
ForMatchedPlace(LocalDefId),
Expand All @@ -214,42 +214,42 @@ pub enum FakeReadCause {
}

/// Describes what kind of retag is to be performed
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum RetagKind {
FnEntry,
TwoPhase,
Raw,
Default,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum Variance {
Covariant,
Invariant,
Contravariant,
Bivariant,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CopyNonOverlapping {
pub src: Operand,
pub dst: Operand,
pub count: Operand,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum NonDivergingIntrinsic {
Assume(Operand),
CopyNonOverlapping(CopyNonOverlapping),
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Statement {
pub kind: StatementKind,
pub span: Span,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum StatementKind {
Assign(Place, Rvalue),
FakeRead(FakeReadCause, Place),
Expand All @@ -266,7 +266,7 @@ pub enum StatementKind {
Nop,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Rvalue {
/// Creates a pointer with the indicated mutability to the place.
///
Expand Down Expand Up @@ -378,7 +378,7 @@ pub enum Rvalue {
Use(Operand),
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum AggregateKind {
Array(Ty),
Tuple,
Expand All @@ -387,49 +387,51 @@ pub enum AggregateKind {
Coroutine(CoroutineDef, GenericArgs, Movability),
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Operand {
Copy(Place),
Move(Place),
Constant(Constant),
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Place {
pub local: Local,
/// projection out of a place (access a field, deref a pointer, etc)
pub projection: String,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UserTypeProjection {
pub base: UserTypeAnnotationIndex,
pub projection: String,
}

pub type Local = usize;

pub const RETURN_LOCAL: Local = 0;

type FieldIdx = usize;

/// The source-order index of a variant in a type.
pub type VariantIdx = usize;

type UserTypeAnnotationIndex = usize;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Constant {
pub span: Span,
pub user_ty: Option<UserTypeAnnotationIndex>,
pub literal: Const,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SwitchTarget {
pub value: u128,
pub target: usize,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum BorrowKind {
/// Data must be immutable and is aliasable.
Shared,
Expand All @@ -446,26 +448,26 @@ pub enum BorrowKind {
},
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum MutBorrowKind {
Default,
TwoPhaseBorrow,
ClosureCapture,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Mutability {
Not,
Mut,
}

#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Safety {
Unsafe,
Normal,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PointerCoercion {
/// Go from a fn-item type to a fn-pointer type.
ReifyFnPointer,
Expand All @@ -492,7 +494,7 @@ pub enum PointerCoercion {
Unsize,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CastKind {
PointerExposeAddress,
PointerFromExposedAddress,
Expand All @@ -507,7 +509,7 @@ pub enum CastKind {
Transmute,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum NullOp {
/// Returns the size of a value of that type.
SizeOf,
Expand Down
4 changes: 4 additions & 0 deletions compiler/stable_mir/src/mir/mono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ impl Instance {
with(|context| context.instance_ty(self.def))
}

pub fn mangled_name(&self) -> String {
with(|context| context.instance_mangled_name(self.def))
}

/// Resolve an instance starting from a function definition and generic arguments.
pub fn resolve(def: FnDef, args: &GenericArgs) -> Result<Instance, crate::Error> {
with(|context| {
Expand Down
Loading

0 comments on commit 634fed0

Please sign in to comment.