Skip to content

Commit 52ce1f7

Browse files
committed
Support statics in custom mir
1 parent 7578100 commit 52ce1f7

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

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

+23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_middle::mir::interpret::{ConstValue, Scalar};
12
use rustc_middle::{mir::*, thir::*, ty};
23

34
use super::{parse_by_kind, PResult, ParseCtxt};
@@ -45,6 +46,8 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
4546
fn parse_operand(&self, expr_id: ExprId) -> PResult<Operand<'tcx>> {
4647
parse_by_kind!(self, expr_id, expr, "operand",
4748
@call("mir_move", args) => self.parse_place(args[0]).map(Operand::Move),
49+
@call("mir_static", args) => self.parse_static(args[0]),
50+
@call("mir_static_mut", args) => self.parse_static(args[0]),
4851
ExprKind::Literal { .. }
4952
| ExprKind::NamedConst { .. }
5053
| ExprKind::NonHirLiteral { .. }
@@ -79,4 +82,24 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
7982
ExprKind::VarRef { id } => Ok(self.block_map[id]),
8083
)
8184
}
85+
86+
fn parse_static(&self, expr_id: ExprId) -> PResult<Operand<'tcx>> {
87+
let expr_id = parse_by_kind!(self, expr_id, _, "static",
88+
ExprKind::Deref { arg } => *arg,
89+
);
90+
91+
parse_by_kind!(self, expr_id, expr, "static",
92+
ExprKind::StaticRef { alloc_id, ty, .. } => {
93+
let const_val =
94+
ConstValue::Scalar(Scalar::from_pointer((*alloc_id).into(), &self.tcx));
95+
let literal = ConstantKind::Val(const_val, *ty);
96+
97+
Ok(Operand::Constant(Box::new(Constant {
98+
span: expr.span,
99+
user_ty: None,
100+
literal
101+
})))
102+
},
103+
)
104+
}
82105
}

library/core/src/intrinsics/mir.rs

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ define!("mir_goto", fn Goto(destination: BasicBlock) -> BasicBlock);
8080
define!("mir_retag", fn Retag<T>(place: T));
8181
define!("mir_retag_raw", fn RetagRaw<T>(place: T));
8282
define!("mir_move", fn Move<T>(place: T) -> T);
83+
define!("mir_static", fn Static<T>(s: T) -> &'static T);
84+
define!("mir_static_mut", fn StaticMut<T>(s: T) -> *mut T);
8385

8486
/// Convenience macro for generating custom MIR.
8587
///

src/test/mir-opt/building/custom/consts.rs

+13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ fn consts<const C: u32>() {
1818
})
1919
}
2020

21+
static S: i32 = 5;
22+
static mut T: i32 = 10;
23+
// EMIT_MIR consts.statics.built.after.mir
24+
#[custom_mir(dialect = "built")]
25+
fn statics() {
26+
mir!({
27+
let _a: &i32 = Static(S);
28+
let _b: *mut i32 = StaticMut(T);
29+
Return()
30+
})
31+
}
32+
2133
fn main() {
2234
consts::<5>();
35+
statics();
2336
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// MIR for `statics` after built
2+
3+
fn statics() -> () {
4+
let mut _0: (); // return place in scope 0 at $DIR/consts.rs:25:14: 25:14
5+
let mut _1: &i32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
6+
let mut _2: *mut i32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
7+
8+
bb0: {
9+
_1 = const {alloc1: &i32}; // scope 0 at $DIR/consts.rs:+0:1: +0:13
10+
// mir::Constant
11+
// + span: $DIR/consts.rs:27:31: 27:32
12+
// + literal: Const { ty: &i32, val: Value(Scalar(alloc1)) }
13+
_2 = const {alloc2: *mut i32}; // scope 0 at $DIR/consts.rs:+0:1: +0:13
14+
// mir::Constant
15+
// + span: $DIR/consts.rs:28:38: 28:39
16+
// + literal: Const { ty: *mut i32, val: Value(Scalar(alloc2)) }
17+
return; // scope 0 at $DIR/consts.rs:+0:1: +0:13
18+
}
19+
}
20+
21+
alloc2 (static: T, size: 4, align: 4) {
22+
0a 00 00 00 │ ....
23+
}
24+
25+
alloc1 (static: S, size: 4, align: 4) {
26+
05 00 00 00 │ ....
27+
}

0 commit comments

Comments
 (0)