-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Add stable Instance::body() and RustcInternal trait #116964
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.