Skip to content

StableMIR: Prepare for refactoring #139319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2025
Merged
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
5 changes: 2 additions & 3 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4432,7 +4432,7 @@ dependencies = [
"rustc_span",
"rustc_target",
"scoped-tls",
"stable_mir",
"serde",
"tracing",
]

Expand Down Expand Up @@ -4990,8 +4990,7 @@ checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
name = "stable_mir"
version = "0.1.0-preview"
dependencies = [
"scoped-tls",
"serde",
"rustc_smir",
]

[[package]]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_smir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ rustc_session = { path = "../rustc_session" }
rustc_span = { path = "../rustc_span" }
rustc_target = { path = "../rustc_target" }
scoped-tls = "1.0"
stable_mir = {path = "../stable_mir" }
serde = { version = "1.0.125", features = [ "derive" ] }
tracing = "0.1"
# tidy-alphabetical-end
2 changes: 2 additions & 0 deletions compiler/rustc_smir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ pub mod rustc_internal;

// Make this module private for now since external users should not call these directly.
mod rustc_smir;

pub mod stable_mir;
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_internal/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use stable_mir::{CrateItem, CrateNum, DefId};

use super::RustcInternal;
use crate::rustc_smir::Tables;
use crate::stable_mir;

impl RustcInternal for CrateItem {
type T<'tcx> = rustc_span::def_id::DefId;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use stable_mir::ty::IndexedVal;

use crate::rustc_smir::context::TablesWrapper;
use crate::rustc_smir::{Stable, Tables};
use crate::stable_mir;

mod internal;
pub mod pretty;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_internal/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::io;
use rustc_middle::ty::TyCtxt;

use super::run;
use crate::stable_mir;

pub fn write_smir_pretty<'tcx, W: io::Write>(tcx: TyCtxt<'tcx>, w: &mut W) -> io::Result<()> {
writeln!(
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_smir/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use stable_mir::mir::Mutability;
use stable_mir::ty::{Allocation, ProvenanceMap};

use crate::rustc_smir::{Stable, Tables};
use crate::stable_mir;

/// Creates new empty `Allocation` from given `Align`.
fn new_empty_allocation(align: Align) -> Allocation {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_smir/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use rustc_middle::mir::visit::MutVisitor;
use rustc_middle::ty::{self, TyCtxt};

use crate::rustc_smir::{Stable, Tables};
use crate::stable_mir;

/// Builds a monomorphic body for a given instance.
pub(crate) struct BodyBuilder<'tcx> {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_smir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use stable_mir::{Crate, CrateDef, CrateItem, CrateNum, DefId, Error, Filename, I
use crate::rustc_internal::RustcInternal;
use crate::rustc_smir::builder::BodyBuilder;
use crate::rustc_smir::{Stable, Tables, alloc, filter_def_ids, new_item_kind, smir_crate};
use crate::stable_mir;

impl<'tcx> Context for TablesWrapper<'tcx> {
fn target_info(&self) -> MachineInfo {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_smir/convert/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use stable_mir::target::MachineSize as Size;
use stable_mir::ty::{Align, IndexedVal, VariantIdx};

use crate::rustc_smir::{Stable, Tables};
use crate::stable_mir;

impl<'tcx> Stable<'tcx> for rustc_abi::VariantIdx {
type T = VariantIdx;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_smir/convert/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use rustc_middle::mir::interpret::AllocError;
use rustc_middle::ty::layout::LayoutError;

use crate::rustc_smir::{Stable, Tables};
use crate::stable_mir;

impl<'tcx> Stable<'tcx> for LayoutError<'tcx> {
type T = stable_mir::Error;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_smir/convert/mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use stable_mir::ty::{Allocation, ConstantKind, MirConst};
use stable_mir::{Error, opaque};

use crate::rustc_smir::{Stable, Tables, alloc};
use crate::stable_mir;

impl<'tcx> Stable<'tcx> for mir::Body<'tcx> {
type T = stable_mir::mir::Body;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_smir/convert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use rustc_abi::FieldIdx;

use crate::rustc_smir::{Stable, Tables};
use crate::stable_mir;

mod abi;
mod error;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_smir/convert/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use stable_mir::ty::{
};

use crate::rustc_smir::{Stable, Tables, alloc};
use crate::stable_mir;

impl<'tcx> Stable<'tcx> for ty::AliasTyKind {
type T = stable_mir::ty::AliasKind;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use stable_mir::{CtorKind, ItemKind};
use tracing::debug;

use crate::rustc_internal::IndexMap;
use crate::stable_mir;

mod alloc;
mod builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use std::num::NonZero;
use std::ops::RangeInclusive;

use serde::Serialize;
use stable_mir::compiler_interface::with;
use stable_mir::mir::FieldIdx;
use stable_mir::target::{MachineInfo, MachineSize as Size};
use stable_mir::ty::{Align, IndexedVal, Ty, VariantIdx};
use stable_mir::{Error, Opaque, error};

use crate::compiler_interface::with;
use crate::mir::FieldIdx;
use crate::target::{MachineInfo, MachineSize as Size};
use crate::ty::{Align, IndexedVal, Ty, VariantIdx};
use crate::{Error, Opaque, error};
use crate::stable_mir;

/// A function ABI definition.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
Expand Down Expand Up @@ -149,7 +150,7 @@ pub enum FieldsShape {
Arbitrary {
/// Offsets for the first byte of each field,
/// ordered to match the source definition order.
/// I.e.: It follows the same order as [crate::ty::VariantDef::fields()].
/// I.e.: It follows the same order as [super::ty::VariantDef::fields()].
/// This vector does not go in increasing order.
offsets: Vec<Size>,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@

use std::cell::Cell;

use crate::abi::{FnAbi, Layout, LayoutShape};
use crate::crate_def::Attribute;
use crate::mir::alloc::{AllocId, GlobalAlloc};
use crate::mir::mono::{Instance, InstanceDef, StaticDef};
use crate::mir::{BinOp, Body, Place, UnOp};
use crate::target::MachineInfo;
use crate::ty::{
use stable_mir::abi::{FnAbi, Layout, LayoutShape};
use stable_mir::crate_def::Attribute;
use stable_mir::mir::alloc::{AllocId, GlobalAlloc};
use stable_mir::mir::mono::{Instance, InstanceDef, StaticDef};
use stable_mir::mir::{BinOp, Body, Place, UnOp};
use stable_mir::target::MachineInfo;
use stable_mir::ty::{
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, FieldDef, FnDef, ForeignDef,
ForeignItemKind, ForeignModule, ForeignModuleDef, GenericArgs, GenericPredicates, Generics,
ImplDef, ImplTrait, IntrinsicDef, LineInfo, MirConst, PolyFnSig, RigidTy, Span, TraitDecl,
TraitDef, Ty, TyConst, TyConstId, TyKind, UintTy, VariantDef,
};
use crate::{
use stable_mir::{
AssocItems, Crate, CrateItem, CrateItems, CrateNum, DefId, Error, Filename, ImplTraitDecls,
ItemKind, Symbol, TraitDecls, mir,
};

use crate::stable_mir;

/// This trait defines the interface between stable_mir and the Rust compiler.
/// Do not use this directly.
pub trait Context {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
//! such as, a function, a trait, an enum, and any other definitions.

use serde::Serialize;
use stable_mir::ty::{GenericArgs, Span, Ty};
use stable_mir::{AssocItems, Crate, Symbol, with};

use crate::ty::{GenericArgs, Span, Ty};
use crate::{AssocItems, Crate, Symbol, with};
use crate::stable_mir;

/// A unique identification number for each item accessible for the current compilation unit.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
use std::io::Read;

use serde::Serialize;
use stable_mir::mir::mono::{Instance, StaticDef};
use stable_mir::target::{Endian, MachineInfo};
use stable_mir::ty::{Allocation, Binder, ExistentialTraitRef, IndexedVal, Ty};
use stable_mir::{Error, with};

use crate::mir::mono::{Instance, StaticDef};
use crate::target::{Endian, MachineInfo};
use crate::ty::{Allocation, Binder, ExistentialTraitRef, IndexedVal, Ty};
use crate::{Error, with};
use crate::stable_mir;

/// An allocation in the SMIR global memory can be either a function pointer,
/// a static, or a "real" allocation with some data in it.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use std::io;

use serde::Serialize;

use crate::compiler_interface::with;
use crate::mir::pretty::function_body;
use crate::ty::{
use stable_mir::compiler_interface::with;
use stable_mir::mir::pretty::function_body;
use stable_mir::ty::{
AdtDef, ClosureDef, CoroutineClosureDef, CoroutineDef, GenericArgs, MirConst, Movability,
Region, RigidTy, Ty, TyConst, TyKind, VariantIdx,
};
use crate::{Error, Opaque, Span, Symbol};
use stable_mir::{Error, Opaque, Span, Symbol};

use crate::stable_mir;

/// The SMIR representation of a single function.
#[derive(Clone, Debug, Serialize)]
Expand Down Expand Up @@ -565,7 +566,7 @@ pub enum Rvalue {
///
/// **Needs clarification**: Are there weird additional semantics here related to the runtime
/// nature of this operation?
ThreadLocalRef(crate::CrateItem),
ThreadLocalRef(stable_mir::CrateItem),

/// Computes a value as described by the operation.
NullaryOp(NullOp, Ty),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use std::fmt::{Debug, Formatter};
use std::io;

use serde::Serialize;
use stable_mir::abi::FnAbi;
use stable_mir::crate_def::CrateDef;
use stable_mir::mir::Body;
use stable_mir::ty::{Allocation, ClosureDef, ClosureKind, FnDef, GenericArgs, IndexedVal, Ty};
use stable_mir::{CrateItem, DefId, Error, ItemKind, Opaque, Symbol, with};

use crate::abi::FnAbi;
use crate::crate_def::CrateDef;
use crate::mir::Body;
use crate::ty::{Allocation, ClosureDef, ClosureKind, FnDef, GenericArgs, IndexedVal, Ty};
use crate::{CrateItem, DefId, Error, ItemKind, Opaque, Symbol, with};
use crate::stable_mir;

#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
pub enum MonoItem {
Expand Down Expand Up @@ -117,11 +118,11 @@ impl Instance {
}

/// Resolve an instance starting from a function definition and generic arguments.
pub fn resolve(def: FnDef, args: &GenericArgs) -> Result<Instance, crate::Error> {
pub fn resolve(def: FnDef, args: &GenericArgs) -> Result<Instance, Error> {
with(|context| {
context.resolve_instance(def, args).ok_or_else(|| {
crate::Error::new(format!("Failed to resolve `{def:?}` with `{args:?}`"))
})
context
.resolve_instance(def, args)
.ok_or_else(|| Error::new(format!("Failed to resolve `{def:?}` with `{args:?}`")))
})
}

Expand All @@ -131,11 +132,11 @@ impl Instance {
}

/// Resolve an instance for a given function pointer.
pub fn resolve_for_fn_ptr(def: FnDef, args: &GenericArgs) -> Result<Instance, crate::Error> {
pub fn resolve_for_fn_ptr(def: FnDef, args: &GenericArgs) -> Result<Instance, Error> {
with(|context| {
context.resolve_for_fn_ptr(def, args).ok_or_else(|| {
crate::Error::new(format!("Failed to resolve `{def:?}` with `{args:?}`"))
})
context
.resolve_for_fn_ptr(def, args)
.ok_or_else(|| Error::new(format!("Failed to resolve `{def:?}` with `{args:?}`")))
})
}

Expand All @@ -144,11 +145,11 @@ impl Instance {
def: ClosureDef,
args: &GenericArgs,
kind: ClosureKind,
) -> Result<Instance, crate::Error> {
) -> Result<Instance, Error> {
with(|context| {
context.resolve_closure(def, args, kind).ok_or_else(|| {
crate::Error::new(format!("Failed to resolve `{def:?}` with `{args:?}`"))
})
context
.resolve_closure(def, args, kind)
.ok_or_else(|| Error::new(format!("Failed to resolve `{def:?}` with `{args:?}`")))
})
}

Expand Down Expand Up @@ -195,7 +196,7 @@ impl Debug for Instance {
/// Try to convert a crate item into an instance.
/// The item cannot be generic in order to be converted into an instance.
impl TryFrom<CrateItem> for Instance {
type Error = crate::Error;
type Error = stable_mir::Error;

fn try_from(item: CrateItem) -> Result<Self, Self::Error> {
with(|context| {
Expand All @@ -212,7 +213,7 @@ impl TryFrom<CrateItem> for Instance {
/// Try to convert an instance into a crate item.
/// Only user defined instances can be converted.
impl TryFrom<Instance> for CrateItem {
type Error = crate::Error;
type Error = stable_mir::Error;

fn try_from(value: Instance) -> Result<Self, Self::Error> {
with(|context| {
Expand Down Expand Up @@ -259,7 +260,7 @@ crate_def! {
}

impl TryFrom<CrateItem> for StaticDef {
type Error = crate::Error;
type Error = stable_mir::Error;

fn try_from(value: CrateItem) -> Result<Self, Self::Error> {
if matches!(value.kind(), ItemKind::Static) {
Expand All @@ -271,7 +272,7 @@ impl TryFrom<CrateItem> for StaticDef {
}

impl TryFrom<Instance> for StaticDef {
type Error = crate::Error;
type Error = stable_mir::Error;

fn try_from(value: Instance) -> Result<Self, Self::Error> {
StaticDef::try_from(CrateItem::try_from(value)?)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use std::io::Write;
use std::{fmt, io, iter};

use fmt::{Display, Formatter};

use super::{AggregateKind, AssertMessage, BinOp, BorrowKind, FakeBorrowKind, TerminatorKind};
use crate::mir::{
use stable_mir::mir::{
Operand, Place, RawPtrKind, Rvalue, StatementKind, UnwindAction, VarDebugInfoContents,
};
use crate::ty::{AdtKind, AssocKind, IndexedVal, MirConst, Ty, TyConst};
use crate::{Body, CrateDef, Mutability, with};
use stable_mir::ty::{AdtKind, AssocKind, IndexedVal, MirConst, Ty, TyConst};
use stable_mir::{Body, CrateDef, Mutability, with};

use super::{AggregateKind, AssertMessage, BinOp, BorrowKind, FakeBorrowKind, TerminatorKind};
use crate::stable_mir;

impl Display for Ty {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
//! The only place that `_` is acceptable is to match a field (or
//! variant argument) that does not require visiting.

use crate::mir::*;
use crate::ty::{GenericArgs, MirConst, Region, Ty, TyConst};
use crate::{Error, Opaque, Span};
use stable_mir::mir::*;
use stable_mir::ty::{GenericArgs, MirConst, Region, Ty, TyConst};
use stable_mir::{Error, Opaque, Span};

use crate::stable_mir;

macro_rules! make_mir_visitor {
($visitor_trait_name:ident, $($mutability:ident)?) => {
Expand Down
Loading
Loading