Skip to content

Commit f521d93

Browse files
authored
Rollup merge of #112717 - celinval:stable-mir-rvalue-1, r=oli-obk
Implement a few more rvalue translation to smir Add the implementation for a few more RValue variants. For now, I simplified the stable version of `RValue::Ref` by removing the notion of Region. r? `@oli-obk`
2 parents 4353b1e + 633c022 commit f521d93

File tree

7 files changed

+369
-41
lines changed

7 files changed

+369
-41
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3948,6 +3948,7 @@ dependencies = [
39483948
"rustc_hir",
39493949
"rustc_middle",
39503950
"rustc_span",
3951+
"rustc_target",
39513952
"scoped-tls",
39523953
"tracing",
39533954
]

compiler/rustc_smir/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ version = "0.0.0"
44
edition = "2021"
55

66
[dependencies]
7-
rustc_hir = { path = "../rustc_hir" }
7+
# Use optional dependencies for rustc_* in order to support building this crate separately.
8+
rustc_hir = { path = "../rustc_hir", optional = true }
89
rustc_middle = { path = "../rustc_middle", optional = true }
910
rustc_span = { path = "../rustc_span", optional = true }
11+
rustc_target = { path = "../rustc_target", optional = true }
1012
tracing = "0.1"
1113
scoped-tls = "1.0"
1214

1315
[features]
1416
default = [
17+
"rustc_hir",
1518
"rustc_middle",
1619
"rustc_span",
20+
"rustc_target",
1721
]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[toolchain]
2-
channel = "nightly-2023-02-28"
2+
channel = "nightly-2023-06-14"
33
components = [ "rustfmt", "rustc-dev" ]

compiler/rustc_smir/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
1313
#![cfg_attr(not(feature = "default"), feature(rustc_private))]
1414
#![feature(local_key_cell_methods)]
1515
#![feature(ptr_metadata)]
16+
#![feature(type_alias_impl_trait)] // Used to define opaque types.
17+
18+
// Declare extern rustc_* crates to enable building this crate separately from the compiler.
19+
#[cfg(not(feature = "default"))]
20+
extern crate rustc_hir;
21+
#[cfg(not(feature = "default"))]
22+
extern crate rustc_middle;
23+
#[cfg(not(feature = "default"))]
24+
extern crate rustc_span;
25+
#[cfg(not(feature = "default"))]
26+
extern crate rustc_target;
1627

1728
pub mod rustc_internal;
1829
pub mod stable_mir;

compiler/rustc_smir/src/rustc_internal/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
//! For that, we define APIs that will temporarily be public to 3P that exposes rustc internal APIs
44
//! until stable MIR is complete.
55
6+
use std::fmt::Debug;
7+
use std::string::ToString;
8+
69
use crate::{
710
rustc_smir::Tables,
811
stable_mir::{self, with},
@@ -49,3 +52,10 @@ pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
4952
pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
5053
crate::stable_mir::run(Tables { tcx, def_ids: vec![], types: vec![] }, f);
5154
}
55+
56+
/// A type that provides internal information but that can still be used for debug purpose.
57+
pub type Opaque = impl Debug + ToString + Clone;
58+
59+
pub(crate) fn opaque<T: Debug>(value: &T) -> Opaque {
60+
format!("{value:?}")
61+
}

compiler/rustc_smir/src/rustc_smir/mod.rs

Lines changed: 162 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
//!
88
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
99
10+
use crate::rustc_internal::{self, opaque};
1011
use crate::stable_mir::{self, ty::TyKind, Context};
1112
use rustc_middle::mir;
1213
use rustc_middle::ty::{self, Ty, TyCtxt};
1314
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
15+
use rustc_target::abi::FieldIdx;
1416
use tracing::debug;
1517

1618
impl<'tcx> Context for Tables<'tcx> {
@@ -119,11 +121,21 @@ fn smir_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> stable_mir::Crate {
119121
stable_mir::Crate { id: crate_num.into(), name: crate_name, is_local }
120122
}
121123

122-
pub trait Stable {
124+
/// Trait used to convert between an internal MIR type to a Stable MIR type.
125+
pub(crate) trait Stable {
126+
/// The stable representation of the type implementing Stable.
123127
type T;
128+
/// Converts an object to the equivalent Stable MIR representation.
124129
fn stable(&self) -> Self::T;
125130
}
126131

132+
impl Stable for DefId {
133+
type T = stable_mir::CrateItem;
134+
fn stable(&self) -> Self::T {
135+
rustc_internal::crate_item(*self)
136+
}
137+
}
138+
127139
impl<'tcx> Stable for mir::Statement<'tcx> {
128140
type T = stable_mir::mir::Statement;
129141
fn stable(&self) -> Self::T {
@@ -155,27 +167,136 @@ impl<'tcx> Stable for mir::Rvalue<'tcx> {
155167
match self {
156168
Use(op) => stable_mir::mir::Rvalue::Use(op.stable()),
157169
Repeat(_, _) => todo!(),
158-
Ref(_, _, _) => todo!(),
159-
ThreadLocalRef(_) => todo!(),
160-
AddressOf(_, _) => todo!(),
161-
Len(_) => todo!(),
170+
Ref(region, kind, place) => {
171+
stable_mir::mir::Rvalue::Ref(opaque(region), kind.stable(), place.stable())
172+
}
173+
ThreadLocalRef(def_id) => stable_mir::mir::Rvalue::ThreadLocalRef(def_id.stable()),
174+
AddressOf(mutability, place) => {
175+
stable_mir::mir::Rvalue::AddressOf(mutability.stable(), place.stable())
176+
}
177+
Len(place) => stable_mir::mir::Rvalue::Len(place.stable()),
162178
Cast(_, _, _) => todo!(),
163-
BinaryOp(_, _) => todo!(),
179+
BinaryOp(bin_op, ops) => {
180+
stable_mir::mir::Rvalue::BinaryOp(bin_op.stable(), ops.0.stable(), ops.1.stable())
181+
}
164182
CheckedBinaryOp(bin_op, ops) => stable_mir::mir::Rvalue::CheckedBinaryOp(
165183
bin_op.stable(),
166184
ops.0.stable(),
167185
ops.1.stable(),
168186
),
169187
NullaryOp(_, _) => todo!(),
170188
UnaryOp(un_op, op) => stable_mir::mir::Rvalue::UnaryOp(un_op.stable(), op.stable()),
171-
Discriminant(_) => todo!(),
189+
Discriminant(place) => stable_mir::mir::Rvalue::Discriminant(place.stable()),
172190
Aggregate(_, _) => todo!(),
173191
ShallowInitBox(_, _) => todo!(),
174-
CopyForDeref(_) => todo!(),
192+
CopyForDeref(place) => stable_mir::mir::Rvalue::CopyForDeref(place.stable()),
193+
}
194+
}
195+
}
196+
197+
impl Stable for mir::Mutability {
198+
type T = stable_mir::mir::Mutability;
199+
fn stable(&self) -> Self::T {
200+
use mir::Mutability::*;
201+
match *self {
202+
Not => stable_mir::mir::Mutability::Not,
203+
Mut => stable_mir::mir::Mutability::Mut,
204+
}
205+
}
206+
}
207+
208+
impl Stable for mir::BorrowKind {
209+
type T = stable_mir::mir::BorrowKind;
210+
fn stable(&self) -> Self::T {
211+
use mir::BorrowKind::*;
212+
match *self {
213+
Shared => stable_mir::mir::BorrowKind::Shared,
214+
Shallow => stable_mir::mir::BorrowKind::Shallow,
215+
Mut { kind } => stable_mir::mir::BorrowKind::Mut { kind: kind.stable() },
216+
}
217+
}
218+
}
219+
220+
impl Stable for mir::MutBorrowKind {
221+
type T = stable_mir::mir::MutBorrowKind;
222+
fn stable(&self) -> Self::T {
223+
use mir::MutBorrowKind::*;
224+
match *self {
225+
Default => stable_mir::mir::MutBorrowKind::Default,
226+
TwoPhaseBorrow => stable_mir::mir::MutBorrowKind::TwoPhaseBorrow,
227+
ClosureCapture => stable_mir::mir::MutBorrowKind::ClosureCapture,
228+
}
229+
}
230+
}
231+
232+
impl<'tcx> Stable for mir::NullOp<'tcx> {
233+
type T = stable_mir::mir::NullOp;
234+
fn stable(&self) -> Self::T {
235+
use mir::NullOp::*;
236+
match self {
237+
SizeOf => stable_mir::mir::NullOp::SizeOf,
238+
AlignOf => stable_mir::mir::NullOp::AlignOf,
239+
OffsetOf(indices) => {
240+
stable_mir::mir::NullOp::OffsetOf(indices.iter().map(|idx| idx.stable()).collect())
241+
}
242+
}
243+
}
244+
}
245+
246+
impl Stable for mir::CastKind {
247+
type T = stable_mir::mir::CastKind;
248+
fn stable(&self) -> Self::T {
249+
use mir::CastKind::*;
250+
match self {
251+
PointerExposeAddress => stable_mir::mir::CastKind::PointerExposeAddress,
252+
PointerFromExposedAddress => stable_mir::mir::CastKind::PointerFromExposedAddress,
253+
Pointer(cast) => stable_mir::mir::CastKind::Pointer(cast.stable()),
254+
DynStar => stable_mir::mir::CastKind::DynStar,
255+
IntToInt => stable_mir::mir::CastKind::IntToInt,
256+
FloatToInt => stable_mir::mir::CastKind::FloatToInt,
257+
FloatToFloat => stable_mir::mir::CastKind::FloatToFloat,
258+
IntToFloat => stable_mir::mir::CastKind::IntToFloat,
259+
PtrToPtr => stable_mir::mir::CastKind::PtrToPtr,
260+
FnPtrToPtr => stable_mir::mir::CastKind::FnPtrToPtr,
261+
Transmute => stable_mir::mir::CastKind::Transmute,
175262
}
176263
}
177264
}
178265

266+
impl Stable for ty::adjustment::PointerCast {
267+
type T = stable_mir::mir::PointerCast;
268+
fn stable(&self) -> Self::T {
269+
use ty::adjustment::PointerCast;
270+
match self {
271+
PointerCast::ReifyFnPointer => stable_mir::mir::PointerCast::ReifyFnPointer,
272+
PointerCast::UnsafeFnPointer => stable_mir::mir::PointerCast::UnsafeFnPointer,
273+
PointerCast::ClosureFnPointer(unsafety) => {
274+
stable_mir::mir::PointerCast::ClosureFnPointer(unsafety.stable())
275+
}
276+
PointerCast::MutToConstPointer => stable_mir::mir::PointerCast::MutToConstPointer,
277+
PointerCast::ArrayToPointer => stable_mir::mir::PointerCast::ArrayToPointer,
278+
PointerCast::Unsize => stable_mir::mir::PointerCast::Unsize,
279+
}
280+
}
281+
}
282+
283+
impl Stable for rustc_hir::Unsafety {
284+
type T = stable_mir::mir::Safety;
285+
fn stable(&self) -> Self::T {
286+
match self {
287+
rustc_hir::Unsafety::Unsafe => stable_mir::mir::Safety::Unsafe,
288+
rustc_hir::Unsafety::Normal => stable_mir::mir::Safety::Normal,
289+
}
290+
}
291+
}
292+
293+
impl Stable for FieldIdx {
294+
type T = usize;
295+
fn stable(&self) -> Self::T {
296+
self.as_usize()
297+
}
298+
}
299+
179300
impl<'tcx> Stable for mir::Operand<'tcx> {
180301
type T = stable_mir::mir::Operand;
181302
fn stable(&self) -> Self::T {
@@ -211,34 +332,38 @@ impl Stable for mir::UnwindAction {
211332
}
212333
}
213334

214-
fn rustc_assert_msg_to_msg<'tcx>(
215-
assert_message: &rustc_middle::mir::AssertMessage<'tcx>,
216-
) -> stable_mir::mir::AssertMessage {
217-
use rustc_middle::mir::AssertKind;
218-
match assert_message {
219-
AssertKind::BoundsCheck { len, index } => {
220-
stable_mir::mir::AssertMessage::BoundsCheck { len: len.stable(), index: index.stable() }
221-
}
222-
AssertKind::Overflow(bin_op, op1, op2) => {
223-
stable_mir::mir::AssertMessage::Overflow(bin_op.stable(), op1.stable(), op2.stable())
224-
}
225-
AssertKind::OverflowNeg(op) => stable_mir::mir::AssertMessage::OverflowNeg(op.stable()),
226-
AssertKind::DivisionByZero(op) => {
227-
stable_mir::mir::AssertMessage::DivisionByZero(op.stable())
228-
}
229-
AssertKind::RemainderByZero(op) => {
230-
stable_mir::mir::AssertMessage::RemainderByZero(op.stable())
231-
}
232-
AssertKind::ResumedAfterReturn(generator) => {
233-
stable_mir::mir::AssertMessage::ResumedAfterReturn(generator.stable())
234-
}
235-
AssertKind::ResumedAfterPanic(generator) => {
236-
stable_mir::mir::AssertMessage::ResumedAfterPanic(generator.stable())
237-
}
238-
AssertKind::MisalignedPointerDereference { required, found } => {
239-
stable_mir::mir::AssertMessage::MisalignedPointerDereference {
240-
required: required.stable(),
241-
found: found.stable(),
335+
impl<'tcx> Stable for mir::AssertMessage<'tcx> {
336+
type T = stable_mir::mir::AssertMessage;
337+
fn stable(&self) -> Self::T {
338+
use rustc_middle::mir::AssertKind;
339+
match self {
340+
AssertKind::BoundsCheck { len, index } => stable_mir::mir::AssertMessage::BoundsCheck {
341+
len: len.stable(),
342+
index: index.stable(),
343+
},
344+
AssertKind::Overflow(bin_op, op1, op2) => stable_mir::mir::AssertMessage::Overflow(
345+
bin_op.stable(),
346+
op1.stable(),
347+
op2.stable(),
348+
),
349+
AssertKind::OverflowNeg(op) => stable_mir::mir::AssertMessage::OverflowNeg(op.stable()),
350+
AssertKind::DivisionByZero(op) => {
351+
stable_mir::mir::AssertMessage::DivisionByZero(op.stable())
352+
}
353+
AssertKind::RemainderByZero(op) => {
354+
stable_mir::mir::AssertMessage::RemainderByZero(op.stable())
355+
}
356+
AssertKind::ResumedAfterReturn(generator) => {
357+
stable_mir::mir::AssertMessage::ResumedAfterReturn(generator.stable())
358+
}
359+
AssertKind::ResumedAfterPanic(generator) => {
360+
stable_mir::mir::AssertMessage::ResumedAfterPanic(generator.stable())
361+
}
362+
AssertKind::MisalignedPointerDereference { required, found } => {
363+
stable_mir::mir::AssertMessage::MisalignedPointerDereference {
364+
required: required.stable(),
365+
found: found.stable(),
366+
}
242367
}
243368
}
244369
}
@@ -363,7 +488,7 @@ impl<'tcx> Stable for mir::Terminator<'tcx> {
363488
Assert { cond, expected, msg, target, unwind } => Terminator::Assert {
364489
cond: cond.stable(),
365490
expected: *expected,
366-
msg: rustc_assert_msg_to_msg(msg),
491+
msg: msg.stable(),
367492
target: target.as_usize(),
368493
unwind: unwind.stable(),
369494
},

0 commit comments

Comments
 (0)