Skip to content

Commit e598394

Browse files
committed
Support more projections in custom mir
1 parent 409f4d2 commit e598394

9 files changed

+210
-6
lines changed

compiler/rustc_mir_build/src/build/custom/parse/instruction.rs

+37-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_middle::mir::interpret::{ConstValue, Scalar};
2+
use rustc_middle::mir::tcx::PlaceTy;
23
use rustc_middle::{mir::*, thir::*, ty};
34
use rustc_span::Span;
45
use rustc_target::abi::VariantIdx;
@@ -118,12 +119,42 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
118119
}
119120

120121
fn parse_place(&self, expr_id: ExprId) -> PResult<Place<'tcx>> {
121-
parse_by_kind!(self, expr_id, _, "place",
122-
ExprKind::Deref { arg } => Ok(
123-
self.parse_place(*arg)?.project_deeper(&[PlaceElem::Deref], self.tcx)
124-
),
125-
_ => self.parse_local(expr_id).map(Place::from),
126-
)
122+
self.parse_place_inner(expr_id).map(|(x, _)| x)
123+
}
124+
125+
fn parse_place_inner(&self, expr_id: ExprId) -> PResult<(Place<'tcx>, PlaceTy<'tcx>)> {
126+
let (parent, proj) = parse_by_kind!(self, expr_id, expr, "place",
127+
@call("mir_field", args) => {
128+
let (parent, ty) = self.parse_place_inner(args[0])?;
129+
let field = Field::from_u32(self.parse_integer_literal(args[1])? as u32);
130+
let field_ty = ty.field_ty(self.tcx, field);
131+
let proj = PlaceElem::Field(field, field_ty);
132+
let place = parent.project_deeper(&[proj], self.tcx);
133+
return Ok((place, PlaceTy::from_ty(field_ty)));
134+
},
135+
@call("mir_variant", args) => {
136+
(args[0], PlaceElem::Downcast(
137+
None,
138+
VariantIdx::from_u32(self.parse_integer_literal(args[1])? as u32)
139+
))
140+
},
141+
ExprKind::Deref { arg } => {
142+
parse_by_kind!(self, *arg, _, "does not matter",
143+
@call("mir_make_place", args) => return self.parse_place_inner(args[0]),
144+
_ => (*arg, PlaceElem::Deref),
145+
)
146+
},
147+
ExprKind::Index { lhs, index } => (*lhs, PlaceElem::Index(self.parse_local(*index)?)),
148+
ExprKind::Field { lhs, name: field, .. } => (*lhs, PlaceElem::Field(*field, expr.ty)),
149+
_ => {
150+
let place = self.parse_local(expr_id).map(Place::from)?;
151+
return Ok((place, PlaceTy::from_ty(expr.ty)))
152+
},
153+
);
154+
let (parent, ty) = self.parse_place_inner(parent)?;
155+
let place = parent.project_deeper(&[proj], self.tcx);
156+
let ty = ty.projection_ty(self.tcx, proj);
157+
Ok((place, ty))
127158
}
128159

129160
fn parse_local(&self, expr_id: ExprId) -> PResult<Local> {

library/core/src/intrinsics/mir.rs

+22
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ define!(
8888
fn Discriminant<T>(place: T) -> <T as ::core::marker::DiscriminantKind>::Discriminant
8989
);
9090
define!("mir_set_discriminant", fn SetDiscriminant<T>(place: T, index: u32));
91+
define!("mir_field", fn Field<F>(place: (), field: u32) -> F);
92+
define!("mir_variant", fn Variant<T>(place: T, index: u32) -> ());
93+
define!("mir_make_place", fn __internal_make_place<T>(place: T) -> *mut T);
9194

9295
/// Convenience macro for generating custom MIR.
9396
///
@@ -145,6 +148,25 @@ pub macro mir {
145148
}}
146149
}
147150

151+
/// Helper macro that allows you to treat a value expression like a place expression.
152+
///
153+
/// This is necessary in combination with the [`Field`] and [`Variant`] methods. Specifically,
154+
/// something like this won't compile on its own, reporting an error about not being able to assign
155+
/// to such an expression:
156+
///
157+
/// ```rust,ignore(syntax-highlighting-only)
158+
/// Field(something, 0) = 5;
159+
/// ```
160+
///
161+
/// Instead, you'll need to write
162+
///
163+
/// ```rust,ignore(syntax-highlighting-only)
164+
/// place!(Field(something, 0)) = 5;
165+
/// ```
166+
pub macro place($e:expr) {
167+
(*::core::intrinsics::mir::__internal_make_place($e))
168+
}
169+
148170
/// Helper macro that extracts the `let` declarations out of a bunch of statements.
149171
///
150172
/// This macro is written using the "statement muncher" strategy. Each invocation parses the first
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#![feature(custom_mir, core_intrinsics)]
2+
3+
extern crate core;
4+
use core::intrinsics::mir::*;
5+
6+
union U {
7+
a: i32,
8+
b: u32,
9+
}
10+
11+
// EMIT_MIR projections.unions.built.after.mir
12+
#[custom_mir(dialect = "built")]
13+
fn unions(u: U) -> i32 {
14+
mir!({
15+
RET = u.a;
16+
Return()
17+
})
18+
}
19+
20+
// EMIT_MIR projections.tuples.built.after.mir
21+
#[custom_mir(dialect = "analysis", phase = "post-cleanup")]
22+
fn tuples(i: (u32, i32)) -> (u32, i32) {
23+
mir!(
24+
// FIXME(JakobDegen): This is necessary because we can't give type hints for `RET`
25+
let temp: (u32, i32);
26+
{
27+
temp.0 = i.0;
28+
temp.1 = i.1;
29+
30+
RET = temp;
31+
Return()
32+
}
33+
)
34+
}
35+
36+
// EMIT_MIR projections.unwrap.built.after.mir
37+
#[custom_mir(dialect = "built")]
38+
fn unwrap(opt: Option<i32>) -> i32 {
39+
mir!({
40+
RET = Field(Variant(opt, 1), 0);
41+
Return()
42+
})
43+
}
44+
45+
// EMIT_MIR projections.unwrap_deref.built.after.mir
46+
#[custom_mir(dialect = "built")]
47+
fn unwrap_deref(opt: Option<&i32>) -> i32 {
48+
mir!({
49+
RET = *Field::<&i32>(Variant(opt, 1), 0);
50+
Return()
51+
})
52+
}
53+
54+
// EMIT_MIR projections.set.built.after.mir
55+
#[custom_mir(dialect = "built")]
56+
fn set(opt: &mut Option<i32>) {
57+
mir!({
58+
place!(Field(Variant(*opt, 1), 0)) = 10;
59+
Return()
60+
})
61+
}
62+
63+
// EMIT_MIR projections.simple_index.built.after.mir
64+
#[custom_mir(dialect = "built")]
65+
fn simple_index(a: [i32; 10], b: &[i32]) -> i32 {
66+
mir!({
67+
let temp = 3;
68+
RET = a[temp];
69+
RET = (*b)[temp];
70+
Return()
71+
})
72+
}
73+
74+
fn main() {
75+
assert_eq!(unions(U { a: 5 }), 5);
76+
assert_eq!(tuples((5, 6)), (5, 6));
77+
78+
assert_eq!(unwrap(Some(5)), 5);
79+
assert_eq!(unwrap_deref(Some(&5)), 5);
80+
let mut o = Some(5);
81+
set(&mut o);
82+
assert_eq!(o, Some(10));
83+
84+
assert_eq!(simple_index([0; 10], &[0; 10]), 0);
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// MIR for `set` after built
2+
3+
fn set(_1: &mut Option<i32>) -> () {
4+
let mut _0: (); // return place in scope 0 at $DIR/projections.rs:+0:31: +0:31
5+
6+
bb0: {
7+
(((*_1) as variant#1).0: i32) = const 10_i32; // scope 0 at $DIR/projections.rs:+2:9: +2:48
8+
return; // scope 0 at $DIR/projections.rs:+3:9: +3:17
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// MIR for `simple_index` after built
2+
3+
fn simple_index(_1: [i32; 10], _2: &[i32]) -> i32 {
4+
let mut _0: i32; // return place in scope 0 at $DIR/projections.rs:+0:45: +0:48
5+
let mut _3: usize; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
6+
7+
bb0: {
8+
_3 = const 3_usize; // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
9+
_0 = _1[_3]; // scope 0 at $DIR/projections.rs:+3:9: +3:22
10+
_0 = (*_2)[_3]; // scope 0 at $DIR/projections.rs:+4:9: +4:25
11+
return; // scope 0 at $DIR/projections.rs:+5:9: +5:17
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// MIR for `tuples` after built
2+
3+
fn tuples(_1: (u32, i32)) -> (u32, i32) {
4+
let mut _0: (u32, i32); // return place in scope 0 at $DIR/projections.rs:+0:29: +0:39
5+
let mut _2: (u32, i32); // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
6+
7+
bb0: {
8+
(_2.0: u32) = (_1.0: u32); // scope 0 at $DIR/projections.rs:+5:13: +5:25
9+
(_2.1: i32) = (_1.1: i32); // scope 0 at $DIR/projections.rs:+6:13: +6:25
10+
_0 = _2; // scope 0 at $DIR/projections.rs:+8:13: +8:23
11+
return; // scope 0 at $DIR/projections.rs:+9:13: +9:21
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// MIR for `unions` after built
2+
3+
fn unions(_1: U) -> i32 {
4+
let mut _0: i32; // return place in scope 0 at $DIR/projections.rs:+0:20: +0:23
5+
6+
bb0: {
7+
_0 = (_1.0: i32); // scope 0 at $DIR/projections.rs:+2:9: +2:18
8+
return; // scope 0 at $DIR/projections.rs:+3:9: +3:17
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// MIR for `unwrap` after built
2+
3+
fn unwrap(_1: Option<i32>) -> i32 {
4+
let mut _0: i32; // return place in scope 0 at $DIR/projections.rs:+0:32: +0:35
5+
6+
bb0: {
7+
_0 = ((_1 as variant#1).0: i32); // scope 0 at $DIR/projections.rs:+2:9: +2:40
8+
return; // scope 0 at $DIR/projections.rs:+3:9: +3:17
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// MIR for `unwrap_deref` after built
2+
3+
fn unwrap_deref(_1: Option<&i32>) -> i32 {
4+
let mut _0: i32; // return place in scope 0 at $DIR/projections.rs:+0:39: +0:42
5+
6+
bb0: {
7+
_0 = (*((_1 as variant#1).0: &i32)); // scope 0 at $DIR/projections.rs:+2:9: +2:49
8+
return; // scope 0 at $DIR/projections.rs:+3:9: +3:17
9+
}
10+
}

0 commit comments

Comments
 (0)