Skip to content

Commit 206bfc4

Browse files
committed
Cover statements for stable_mir
1 parent 866710c commit 206bfc4

File tree

2 files changed

+267
-14
lines changed

2 files changed

+267
-14
lines changed

compiler/rustc_smir/src/rustc_smir/mod.rs

+177-13
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
99
1010
use crate::rustc_internal::{self, opaque};
11+
use crate::stable_mir::mir::{CopyNonOverlapping, UserTypeProjection};
1112
use crate::stable_mir::ty::{FloatTy, IntTy, Movability, RigidTy, TyKind, UintTy};
1213
use crate::stable_mir::{self, Context};
1314
use rustc_hir as hir;
14-
use rustc_middle::mir;
15-
use rustc_middle::ty::{self, Ty, TyCtxt};
15+
use rustc_middle::mir::coverage::CodeRegion;
16+
use rustc_middle::mir::{self};
17+
use rustc_middle::ty::{self, Ty, TyCtxt, Variance};
1618
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
1719
use rustc_target::abi::FieldIdx;
1820
use tracing::debug;
@@ -110,17 +112,43 @@ impl<'tcx> Stable<'tcx> for mir::Statement<'tcx> {
110112
Assign(assign) => {
111113
stable_mir::mir::Statement::Assign(assign.0.stable(tables), assign.1.stable(tables))
112114
}
113-
FakeRead(_) => todo!(),
114-
SetDiscriminant { .. } => todo!(),
115-
Deinit(_) => todo!(),
116-
StorageLive(_) => todo!(),
117-
StorageDead(_) => todo!(),
118-
Retag(_, _) => todo!(),
119-
PlaceMention(_) => todo!(),
120-
AscribeUserType(_, _) => todo!(),
121-
Coverage(_) => todo!(),
122-
Intrinsic(_) => todo!(),
123-
ConstEvalCounter => todo!(),
115+
FakeRead(fake_read_place) => stable_mir::mir::Statement::FakeRead(
116+
fake_read_place.0.stable(tables),
117+
fake_read_place.1.stable(tables),
118+
),
119+
SetDiscriminant { place: plc, variant_index: idx } => {
120+
stable_mir::mir::Statement::SetDiscriminant {
121+
place: plc.as_ref().stable(tables),
122+
variant_index: idx.stable(tables),
123+
}
124+
}
125+
Deinit(place) => stable_mir::mir::Statement::Deinit(place.stable(tables)),
126+
StorageLive(place) => stable_mir::mir::Statement::StorageLive(place.stable(tables)),
127+
StorageDead(place) => stable_mir::mir::Statement::StorageDead(place.stable(tables)),
128+
Retag(retag, place) => {
129+
stable_mir::mir::Statement::Retag(retag.stable(tables), place.stable(tables))
130+
}
131+
PlaceMention(place) => stable_mir::mir::Statement::PlaceMention(place.stable(tables)),
132+
AscribeUserType(place_projection, variance) => {
133+
stable_mir::mir::Statement::AscribeUserType(
134+
(
135+
place_projection.as_ref().0.stable(tables),
136+
UserTypeProjection {
137+
base: place_projection.as_ref().1.base.stable(tables),
138+
projection: format!("{:?}", place_projection.as_ref().1.projs),
139+
},
140+
),
141+
variance.stable(tables),
142+
)
143+
}
144+
Coverage(coverage) => stable_mir::mir::Statement::Coverage(stable_mir::mir::Coverage {
145+
kind: coverage.kind.stable(tables),
146+
code_region: coverage.code_region.as_ref().map(|reg| reg.stable(tables)),
147+
}),
148+
Intrinsic(intrinstic) => {
149+
stable_mir::mir::Statement::Intrinsic(intrinstic.stable(tables))
150+
}
151+
ConstEvalCounter => stable_mir::mir::Statement::ConstEvalCounter,
124152
Nop => stable_mir::mir::Statement::Nop,
125153
}
126154
}
@@ -364,6 +392,24 @@ impl<'tcx> Stable<'tcx> for rustc_hir::Unsafety {
364392
}
365393
}
366394

395+
impl<'tcx> Stable<'tcx> for mir::FakeReadCause {
396+
type T = stable_mir::mir::FakeReadCause;
397+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
398+
use mir::FakeReadCause::*;
399+
match self {
400+
ForMatchGuard => stable_mir::mir::FakeReadCause::ForMatchGuard,
401+
ForMatchedPlace(local_def_id) => stable_mir::mir::FakeReadCause::ForMatchedPlace(
402+
local_def_id.map(|id| id.to_def_id().index.index()),
403+
),
404+
ForGuardBinding => stable_mir::mir::FakeReadCause::ForGuardBinding,
405+
ForLet(local_def_id) => stable_mir::mir::FakeReadCause::ForLet(
406+
local_def_id.map(|id| id.to_def_id().index.index()),
407+
),
408+
ForIndex => stable_mir::mir::FakeReadCause::ForIndex,
409+
}
410+
}
411+
}
412+
367413
impl<'tcx> Stable<'tcx> for FieldIdx {
368414
type T = usize;
369415
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
@@ -393,6 +439,104 @@ impl<'tcx> Stable<'tcx> for mir::Place<'tcx> {
393439
}
394440
}
395441

442+
impl<'tcx> Stable<'tcx> for mir::coverage::CoverageKind {
443+
type T = stable_mir::mir::CoverageKind;
444+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
445+
use rustc_middle::mir::coverage::CoverageKind;
446+
match self {
447+
CoverageKind::Counter { function_source_hash, id } => {
448+
stable_mir::mir::CoverageKind::Counter {
449+
function_source_hash: *function_source_hash as usize,
450+
id: id.as_usize(),
451+
}
452+
}
453+
CoverageKind::Expression { id, lhs, op, rhs } => {
454+
stable_mir::mir::CoverageKind::Expression {
455+
id: id.as_usize(),
456+
lhs: lhs.as_usize(),
457+
op: op.stable(tables),
458+
rhs: rhs.as_usize(),
459+
}
460+
}
461+
CoverageKind::Unreachable => stable_mir::mir::CoverageKind::Unreachable,
462+
}
463+
}
464+
}
465+
466+
impl<'tcx> Stable<'tcx> for mir::coverage::Op {
467+
type T = stable_mir::mir::Op;
468+
469+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
470+
use rustc_middle::mir::coverage::Op::*;
471+
match self {
472+
Subtract => stable_mir::mir::Op::Subtract,
473+
Add => stable_mir::mir::Op::Add,
474+
}
475+
}
476+
}
477+
478+
impl<'tcx> Stable<'tcx> for mir::Local {
479+
type T = usize;
480+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
481+
self.as_usize()
482+
}
483+
}
484+
485+
impl<'tcx> Stable<'tcx> for rustc_target::abi::VariantIdx {
486+
type T = usize;
487+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
488+
self.as_usize()
489+
}
490+
}
491+
492+
impl<'tcx> Stable<'tcx> for Variance {
493+
type T = stable_mir::mir::Variance;
494+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
495+
match self {
496+
Variance::Bivariant => stable_mir::mir::Variance::Bivariant,
497+
Variance::Contravariant => stable_mir::mir::Variance::Contravariant,
498+
Variance::Covariant => stable_mir::mir::Variance::Covariant,
499+
Variance::Invariant => stable_mir::mir::Variance::Invariant,
500+
}
501+
}
502+
}
503+
504+
impl<'tcx> Stable<'tcx> for mir::RetagKind {
505+
type T = stable_mir::mir::RetagKind;
506+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
507+
use rustc_middle::mir::RetagKind;
508+
match self {
509+
RetagKind::FnEntry => stable_mir::mir::RetagKind::FnEntry,
510+
RetagKind::TwoPhase => stable_mir::mir::RetagKind::TwoPhase,
511+
RetagKind::Raw => stable_mir::mir::RetagKind::Raw,
512+
RetagKind::Default => stable_mir::mir::RetagKind::Default,
513+
}
514+
}
515+
}
516+
517+
impl<'tcx> Stable<'tcx> for rustc_middle::ty::UserTypeAnnotationIndex {
518+
type T = usize;
519+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
520+
self.as_usize()
521+
}
522+
}
523+
524+
impl<'tcx> Stable<'tcx> for CodeRegion {
525+
type T = stable_mir::mir::CodeRegion;
526+
527+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
528+
match self {
529+
_ => stable_mir::mir::CodeRegion {
530+
file_name: self.file_name.as_u32() as usize,
531+
start_line: self.start_line as usize,
532+
start_col: self.start_col as usize,
533+
end_line: self.end_line as usize,
534+
end_col: self.end_col as usize,
535+
},
536+
}
537+
}
538+
}
539+
396540
impl<'tcx> Stable<'tcx> for mir::UnwindAction {
397541
type T = stable_mir::mir::UnwindAction;
398542
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
@@ -406,6 +550,26 @@ impl<'tcx> Stable<'tcx> for mir::UnwindAction {
406550
}
407551
}
408552

553+
impl<'tcx> Stable<'tcx> for mir::NonDivergingIntrinsic<'tcx> {
554+
type T = stable_mir::mir::NonDivergingIntrinsic;
555+
556+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
557+
use rustc_middle::mir::NonDivergingIntrinsic;
558+
match self {
559+
NonDivergingIntrinsic::Assume(op) => {
560+
stable_mir::mir::NonDivergingIntrinsic::Assume(op.stable(tables))
561+
}
562+
NonDivergingIntrinsic::CopyNonOverlapping(copy_non_overlapping) => {
563+
stable_mir::mir::NonDivergingIntrinsic::CopyNonOverlapping(CopyNonOverlapping {
564+
src: copy_non_overlapping.src.stable(tables),
565+
dst: copy_non_overlapping.dst.stable(tables),
566+
count: copy_non_overlapping.count.stable(tables),
567+
})
568+
}
569+
}
570+
}
571+
}
572+
409573
impl<'tcx> Stable<'tcx> for mir::AssertMessage<'tcx> {
410574
type T = stable_mir::mir::AssertMessage;
411575
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {

compiler/rustc_smir/src/stable_mir/mir/body.rs

+90-1
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,90 @@ pub enum AsyncGeneratorKind {
133133
Fn,
134134
}
135135

136+
// FIXME: We are using `usize` for now but we should be using DefIds and find what
137+
// what those are refering to and then use appropirate ty_defs for them (i.e AdtDef)
138+
/// The FakeReadCause describes the type of pattern why a FakeRead statement exists.
139+
#[derive(Clone, Debug)]
140+
pub enum FakeReadCause {
141+
ForMatchGuard,
142+
ForMatchedPlace(Option<usize>),
143+
ForGuardBinding,
144+
ForLet(Option<usize>),
145+
ForIndex,
146+
}
147+
148+
/// Describes what kind of retag is to be performed
149+
#[derive(Clone, Debug)]
150+
pub enum RetagKind {
151+
FnEntry,
152+
TwoPhase,
153+
Raw,
154+
Default,
155+
}
156+
157+
#[derive(Clone, Debug)]
158+
pub enum Variance {
159+
Covariant,
160+
Invariant,
161+
Contravariant,
162+
Bivariant,
163+
}
164+
165+
#[derive(Clone, Debug)]
166+
pub enum Op {
167+
Subtract,
168+
Add,
169+
}
170+
171+
#[derive(Clone, Debug)]
172+
pub enum CoverageKind {
173+
Counter { function_source_hash: usize, id: usize },
174+
Expression { id: usize, lhs: usize, op: Op, rhs: usize },
175+
Unreachable,
176+
}
177+
178+
#[derive(Clone, Copy, Debug)]
179+
pub struct CodeRegion {
180+
pub file_name: usize,
181+
pub start_line: usize,
182+
pub start_col: usize,
183+
pub end_line: usize,
184+
pub end_col: usize,
185+
}
186+
187+
#[derive(Clone, Debug)]
188+
pub struct Coverage {
189+
pub kind: CoverageKind,
190+
pub code_region: Option<CodeRegion>,
191+
}
192+
193+
#[derive(Clone, Debug)]
194+
pub struct CopyNonOverlapping {
195+
pub src: Operand,
196+
pub dst: Operand,
197+
pub count: Operand,
198+
}
199+
200+
#[derive(Clone, Debug)]
201+
pub enum NonDivergingIntrinsic {
202+
Assume(Operand),
203+
CopyNonOverlapping(CopyNonOverlapping),
204+
}
205+
136206
#[derive(Clone, Debug)]
137207
pub enum Statement {
138208
Assign(Place, Rvalue),
209+
FakeRead(FakeReadCause, Place),
210+
SetDiscriminant { place: Place, variant_index: VariantIdx },
211+
Deinit(Place),
212+
StorageLive(Local),
213+
StorageDead(Local),
214+
Retag(RetagKind, Place),
215+
PlaceMention(Place),
216+
AscribeUserType((Place, UserTypeProjection), Variance),
217+
Coverage(Coverage),
218+
Intrinsic(NonDivergingIntrinsic),
219+
ConstEvalCounter,
139220
Nop,
140221
}
141222

@@ -271,10 +352,18 @@ pub enum Operand {
271352

272353
#[derive(Clone, Debug)]
273354
pub struct Place {
274-
pub local: usize,
355+
pub local: Local,
356+
pub projection: String,
357+
}
358+
359+
#[derive(Clone, Debug)]
360+
pub struct UserTypeProjection {
361+
pub base: UserTypeAnnotationIndex,
275362
pub projection: String,
276363
}
277364

365+
type Local = usize;
366+
278367
type FieldIdx = usize;
279368

280369
/// The source-order index of a variant in a type.

0 commit comments

Comments
 (0)