-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #116964 - celinval:smir-mono-body, r=oli-obk
Add stable Instance::body() and RustcInternal trait The `Instance::body()` returns a monomorphized body. For that, we had to implement visitor that monomorphize types and constants. We are also introducing the RustcInternal trait that will allow us to convert back from Stable to Internal. Note that this trait is not yet visible for our users as it depends on Tables. We should probably add a new trait that can be exposed. The tests here are very simple, and I'm planning on creating more exhaustive tests in the project-mir repo. But I was hoping to get some feedback here first. r? ```@oli-obk```
- Loading branch information
Showing
10 changed files
with
217 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//! Module containing the translation from stable mir constructs to the rustc counterpart. | ||
//! | ||
//! This module will only include a few constructs to allow users to invoke internal rustc APIs | ||
//! due to incomplete stable coverage. | ||
// Prefer importing stable_mir over internal rustc constructs to make this file more readable. | ||
use crate::rustc_smir::{MaybeStable, Tables}; | ||
use rustc_middle::ty::{self as rustc_ty, Ty as InternalTy}; | ||
use stable_mir::ty::{Const, GenericArgKind, GenericArgs, Region, Ty}; | ||
use stable_mir::DefId; | ||
|
||
use super::RustcInternal; | ||
|
||
impl<'tcx> RustcInternal<'tcx> for DefId { | ||
type T = rustc_span::def_id::DefId; | ||
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T { | ||
tables.def_ids[*self] | ||
} | ||
} | ||
|
||
impl<'tcx> RustcInternal<'tcx> for GenericArgs { | ||
type T = rustc_ty::GenericArgsRef<'tcx>; | ||
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T { | ||
tables.tcx.mk_args_from_iter(self.0.iter().map(|arg| arg.internal(tables))) | ||
} | ||
} | ||
|
||
impl<'tcx> RustcInternal<'tcx> for GenericArgKind { | ||
type T = rustc_ty::GenericArg<'tcx>; | ||
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T { | ||
match self { | ||
GenericArgKind::Lifetime(reg) => reg.internal(tables).into(), | ||
GenericArgKind::Type(ty) => ty.internal(tables).into(), | ||
GenericArgKind::Const(cnst) => cnst.internal(tables).into(), | ||
} | ||
} | ||
} | ||
|
||
impl<'tcx> RustcInternal<'tcx> for Region { | ||
type T = rustc_ty::Region<'tcx>; | ||
fn internal(&self, _tables: &mut Tables<'tcx>) -> Self::T { | ||
todo!() | ||
} | ||
} | ||
|
||
impl<'tcx> RustcInternal<'tcx> for Ty { | ||
type T = InternalTy<'tcx>; | ||
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T { | ||
match tables.types[self.0] { | ||
MaybeStable::Stable(_) => todo!(), | ||
MaybeStable::Rustc(ty) => ty, | ||
} | ||
} | ||
} | ||
|
||
impl<'tcx> RustcInternal<'tcx> for Const { | ||
type T = rustc_ty::Const<'tcx>; | ||
fn internal(&self, _tables: &mut Tables<'tcx>) -> Self::T { | ||
todo!() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//! Logic required to produce a monomorphic stable body. | ||
//! | ||
//! We first retrieve and monomorphize the rustc body representation, i.e., we generate a | ||
//! monomorphic body using internal representation. | ||
//! After that, we convert the internal representation into a stable one. | ||
use crate::rustc_smir::{Stable, Tables}; | ||
use rustc_middle::mir; | ||
use rustc_middle::mir::visit::MutVisitor; | ||
use rustc_middle::ty::{self, Ty, TyCtxt}; | ||
|
||
/// Builds a monomorphic body for a given instance. | ||
pub struct BodyBuilder<'tcx> { | ||
tcx: TyCtxt<'tcx>, | ||
instance: ty::Instance<'tcx>, | ||
} | ||
|
||
impl<'tcx> BodyBuilder<'tcx> { | ||
pub fn new(tcx: TyCtxt<'tcx>, instance: ty::Instance<'tcx>) -> Self { | ||
BodyBuilder { tcx, instance } | ||
} | ||
|
||
pub fn build(mut self, tables: &mut Tables<'tcx>) -> stable_mir::mir::Body { | ||
let mut body = self.tcx.instance_mir(self.instance.def).clone(); | ||
let generics = self.tcx.generics_of(self.instance.def_id()); | ||
if generics.requires_monomorphization(self.tcx) { | ||
self.visit_body(&mut body); | ||
} | ||
body.stable(tables) | ||
} | ||
|
||
fn monomorphize<T>(&self, value: T) -> T | ||
where | ||
T: ty::TypeFoldable<TyCtxt<'tcx>>, | ||
{ | ||
self.instance.instantiate_mir_and_normalize_erasing_regions( | ||
self.tcx, | ||
ty::ParamEnv::reveal_all(), | ||
ty::EarlyBinder::bind(value), | ||
) | ||
} | ||
} | ||
|
||
impl<'tcx> MutVisitor<'tcx> for BodyBuilder<'tcx> { | ||
fn visit_ty_const(&mut self, ct: &mut ty::Const<'tcx>, _location: mir::Location) { | ||
*ct = self.monomorphize(*ct); | ||
} | ||
|
||
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: mir::visit::TyContext) { | ||
*ty = self.monomorphize(*ty); | ||
} | ||
|
||
fn tcx(&self) -> TyCtxt<'tcx> { | ||
self.tcx | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters