Skip to content

Commit 8a8ee1a

Browse files
committed
inliner: Use substs_for_mir_body
Changes from 68965 extended the kind of instances that are being inlined. For some of those, the `instance_mir` returns a MIR body that is already expressed in terms of the types found in substitution array, and doesn't need further substitution. Use `substs_for_mir_body` to take that into account.
1 parent 9d78d1d commit 8a8ee1a

File tree

9 files changed

+142
-42
lines changed

9 files changed

+142
-42
lines changed

compiler/rustc_codegen_cranelift/src/common.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -361,13 +361,11 @@ impl<'tcx, M: Module> FunctionCx<'_, 'tcx, M> {
361361
where
362362
T: TypeFoldable<'tcx> + Copy,
363363
{
364-
if let Some(substs) = self.instance.substs_for_mir_body() {
365-
self.tcx
366-
.subst_and_normalize_erasing_regions(substs, ty::ParamEnv::reveal_all(), value)
367-
} else {
368-
self.tcx
369-
.normalize_erasing_regions(ty::ParamEnv::reveal_all(), *value)
370-
}
364+
self.instance.subst_mir_and_normalize_erasing_regions(
365+
self.tcx,
366+
ty::ParamEnv::reveal_all(),
367+
value
368+
)
371369
}
372370

373371
pub(crate) fn clif_type(&self, ty: Ty<'tcx>) -> Option<Type> {

compiler/rustc_codegen_ssa/src/mir/mod.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
9292
T: Copy + TypeFoldable<'tcx>,
9393
{
9494
debug!("monomorphize: self.instance={:?}", self.instance);
95-
if let Some(substs) = self.instance.substs_for_mir_body() {
96-
self.cx.tcx().subst_and_normalize_erasing_regions(
97-
substs,
98-
ty::ParamEnv::reveal_all(),
99-
&value,
100-
)
101-
} else {
102-
self.cx.tcx().normalize_erasing_regions(ty::ParamEnv::reveal_all(), *value)
103-
}
95+
self.instance.subst_mir_and_normalize_erasing_regions(
96+
self.cx.tcx(),
97+
ty::ParamEnv::reveal_all(),
98+
value,
99+
)
104100
}
105101
}
106102

compiler/rustc_middle/src/ty/instance.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
22
use crate::ty::print::{FmtPrinter, Printer};
3-
use crate::ty::subst::InternalSubsts;
3+
use crate::ty::subst::{InternalSubsts, Subst};
44
use crate::ty::{self, SubstsRef, Ty, TyCtxt, TypeFoldable};
55
use rustc_errors::ErrorReported;
66
use rustc_hir::def::Namespace;
@@ -470,10 +470,33 @@ impl<'tcx> Instance<'tcx> {
470470
/// This function returns `Some(substs)` in the former case and `None` otherwise -- i.e., if
471471
/// this function returns `None`, then the MIR body does not require substitution during
472472
/// codegen.
473-
pub fn substs_for_mir_body(&self) -> Option<SubstsRef<'tcx>> {
473+
fn substs_for_mir_body(&self) -> Option<SubstsRef<'tcx>> {
474474
if self.def.has_polymorphic_mir_body() { Some(self.substs) } else { None }
475475
}
476476

477+
pub fn subst_mir<T>(&self, tcx: TyCtxt<'tcx>, v: &T) -> T
478+
where
479+
T: TypeFoldable<'tcx> + Copy,
480+
{
481+
if let Some(substs) = self.substs_for_mir_body() { v.subst(tcx, substs) } else { *v }
482+
}
483+
484+
pub fn subst_mir_and_normalize_erasing_regions<T>(
485+
&self,
486+
tcx: TyCtxt<'tcx>,
487+
param_env: ty::ParamEnv<'tcx>,
488+
v: &T,
489+
) -> T
490+
where
491+
T: TypeFoldable<'tcx> + Clone,
492+
{
493+
if let Some(substs) = self.substs_for_mir_body() {
494+
tcx.subst_and_normalize_erasing_regions(substs, param_env, v)
495+
} else {
496+
tcx.normalize_erasing_regions(param_env, v.clone())
497+
}
498+
}
499+
477500
/// Returns a new `Instance` where generic parameters in `instance.substs` are replaced by
478501
/// identify parameters if they are determined to be unused in `instance.def`.
479502
pub fn polymorphize(self, tcx: TyCtxt<'tcx>) -> Self {

compiler/rustc_mir/src/interpret/eval_context.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -505,11 +505,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
505505
frame: &Frame<'mir, 'tcx, M::PointerTag, M::FrameExtra>,
506506
value: T,
507507
) -> T {
508-
if let Some(substs) = frame.instance.substs_for_mir_body() {
509-
self.tcx.subst_and_normalize_erasing_regions(substs, self.param_env, &value)
510-
} else {
511-
self.tcx.normalize_erasing_regions(self.param_env, value)
512-
}
508+
frame.instance.subst_mir_and_normalize_erasing_regions(*self.tcx, self.param_env, &value)
513509
}
514510

515511
/// The `substs` are assumed to already be in our interpreter "universe" (param_env).

compiler/rustc_mir/src/monomorphize/collector.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -543,11 +543,11 @@ impl<'a, 'tcx> MirNeighborCollector<'a, 'tcx> {
543543
T: TypeFoldable<'tcx>,
544544
{
545545
debug!("monomorphize: self.instance={:?}", self.instance);
546-
if let Some(substs) = self.instance.substs_for_mir_body() {
547-
self.tcx.subst_and_normalize_erasing_regions(substs, ty::ParamEnv::reveal_all(), &value)
548-
} else {
549-
self.tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), value)
550-
}
546+
self.instance.subst_mir_and_normalize_erasing_regions(
547+
self.tcx,
548+
ty::ParamEnv::reveal_all(),
549+
&value,
550+
)
551551
}
552552
}
553553

compiler/rustc_mir/src/transform/inline.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_index::vec::Idx;
66
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
77
use rustc_middle::mir::visit::*;
88
use rustc_middle::mir::*;
9-
use rustc_middle::ty::subst::Subst;
109
use rustc_middle::ty::{self, ConstKind, Instance, InstanceDef, ParamEnv, Ty, TyCtxt};
1110
use rustc_span::{hygiene::ExpnKind, ExpnData, Span};
1211
use rustc_target::spec::abi::Abi;
@@ -128,17 +127,15 @@ impl Inliner<'tcx> {
128127
self.tcx.instance_mir(callsite.callee.def)
129128
};
130129

131-
let callee_body: &Body<'tcx> = &*callee_body;
132-
133-
let callee_body = if self.consider_optimizing(callsite, callee_body) {
134-
self.tcx.subst_and_normalize_erasing_regions(
135-
&callsite.callee.substs,
136-
self.param_env,
137-
callee_body,
138-
)
139-
} else {
130+
if !self.consider_optimizing(callsite, &callee_body) {
140131
continue;
141-
};
132+
}
133+
134+
let callee_body = callsite.callee.subst_mir_and_normalize_erasing_regions(
135+
self.tcx,
136+
self.param_env,
137+
callee_body,
138+
);
142139

143140
// Copy only unevaluated constants from the callee_body into the caller_body.
144141
// Although we are only pushing `ConstKind::Unevaluated` consts to
@@ -317,7 +314,7 @@ impl Inliner<'tcx> {
317314
work_list.push(target);
318315
// If the place doesn't actually need dropping, treat it like
319316
// a regular goto.
320-
let ty = place.ty(callee_body, tcx).subst(tcx, callsite.callee.substs).ty;
317+
let ty = callsite.callee.subst_mir(self.tcx, &place.ty(callee_body, tcx).ty);
321318
if ty.needs_drop(tcx, self.param_env) {
322319
cost += CALL_PENALTY;
323320
if let Some(unwind) = unwind {
@@ -379,8 +376,7 @@ impl Inliner<'tcx> {
379376
let ptr_size = tcx.data_layout.pointer_size.bytes();
380377

381378
for v in callee_body.vars_and_temps_iter() {
382-
let v = &callee_body.local_decls[v];
383-
let ty = v.ty.subst(tcx, callsite.callee.substs);
379+
let ty = callsite.callee.subst_mir(self.tcx, &callee_body.local_decls[v].ty);
384380
// Cost of the var is the size in machine-words, if we know
385381
// it.
386382
if let Some(size) = type_size_of(tcx, self.param_env, ty) {
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// ignore-wasm32-bare compiled with panic=abort by default
2+
#![crate_type = "lib"]
3+
4+
// EMIT_MIR inline_shims.clone.Inline.diff
5+
pub fn clone<A, B>(f: fn(A, B)) -> fn(A, B) {
6+
f.clone()
7+
}
8+
9+
// EMIT_MIR inline_shims.drop.Inline.diff
10+
pub fn drop<A, B>(a: *mut Vec<A>, b: *mut Option<B>) {
11+
unsafe { std::ptr::drop_in_place(a) }
12+
unsafe { std::ptr::drop_in_place(b) }
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
- // MIR for `clone` before Inline
2+
+ // MIR for `clone` after Inline
3+
4+
fn clone(_1: fn(A, B)) -> fn(A, B) {
5+
debug f => _1; // in scope 0 at $DIR/inline-shims.rs:5:20: 5:21
6+
let mut _0: fn(A, B); // return place in scope 0 at $DIR/inline-shims.rs:5:36: 5:44
7+
let mut _2: &fn(A, B); // in scope 0 at $DIR/inline-shims.rs:6:5: 6:6
8+
+ scope 1 (inlined <fn(A, B) as Clone>::clone - shim(fn(A, B))) { // at $DIR/inline-shims.rs:6:5: 6:14
9+
+ }
10+
11+
bb0: {
12+
StorageLive(_2); // scope 0 at $DIR/inline-shims.rs:6:5: 6:6
13+
_2 = &_1; // scope 0 at $DIR/inline-shims.rs:6:5: 6:6
14+
- _0 = <fn(A, B) as Clone>::clone(move _2) -> bb1; // scope 0 at $DIR/inline-shims.rs:6:5: 6:14
15+
- // mir::Constant
16+
- // + span: $DIR/inline-shims.rs:6:7: 6:12
17+
- // + literal: Const { ty: for<'r> fn(&'r fn(A, B)) -> fn(A, B) {<fn(A, B) as std::clone::Clone>::clone}, val: Value(Scalar(<ZST>)) }
18+
- }
19+
-
20+
- bb1: {
21+
+ _0 = (*_2); // scope 1 at $DIR/inline-shims.rs:6:5: 6:14
22+
StorageDead(_2); // scope 0 at $DIR/inline-shims.rs:6:13: 6:14
23+
return; // scope 0 at $DIR/inline-shims.rs:7:2: 7:2
24+
}
25+
}
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
- // MIR for `drop` before Inline
2+
+ // MIR for `drop` after Inline
3+
4+
fn drop(_1: *mut Vec<A>, _2: *mut Option<B>) -> () {
5+
debug a => _1; // in scope 0 at $DIR/inline-shims.rs:10:19: 10:20
6+
debug b => _2; // in scope 0 at $DIR/inline-shims.rs:10:35: 10:36
7+
let mut _0: (); // return place in scope 0 at $DIR/inline-shims.rs:10:54: 10:54
8+
let _3: (); // in scope 0 at $DIR/inline-shims.rs:11:14: 11:40
9+
let mut _4: *mut std::vec::Vec<A>; // in scope 0 at $DIR/inline-shims.rs:11:38: 11:39
10+
let mut _5: *mut std::option::Option<B>; // in scope 0 at $DIR/inline-shims.rs:12:38: 12:39
11+
scope 1 {
12+
}
13+
scope 2 {
14+
+ scope 3 (inlined drop_in_place::<Option<B>> - shim(Some(Option<B>))) { // at $DIR/inline-shims.rs:12:14: 12:40
15+
+ let mut _6: isize; // in scope 3 at $DIR/inline-shims.rs:12:14: 12:40
16+
+ let mut _7: isize; // in scope 3 at $DIR/inline-shims.rs:12:14: 12:40
17+
+ }
18+
}
19+
20+
bb0: {
21+
StorageLive(_3); // scope 0 at $DIR/inline-shims.rs:11:5: 11:42
22+
StorageLive(_4); // scope 1 at $DIR/inline-shims.rs:11:38: 11:39
23+
_4 = _1; // scope 1 at $DIR/inline-shims.rs:11:38: 11:39
24+
_3 = drop_in_place::<Vec<A>>(move _4) -> bb1; // scope 1 at $DIR/inline-shims.rs:11:14: 11:40
25+
// mir::Constant
26+
// + span: $DIR/inline-shims.rs:11:14: 11:37
27+
// + literal: Const { ty: unsafe fn(*mut std::vec::Vec<A>) {std::intrinsics::drop_in_place::<std::vec::Vec<A>>}, val: Value(Scalar(<ZST>)) }
28+
}
29+
30+
bb1: {
31+
StorageDead(_4); // scope 1 at $DIR/inline-shims.rs:11:39: 11:40
32+
StorageDead(_3); // scope 0 at $DIR/inline-shims.rs:11:41: 11:42
33+
StorageLive(_5); // scope 2 at $DIR/inline-shims.rs:12:38: 12:39
34+
_5 = _2; // scope 2 at $DIR/inline-shims.rs:12:38: 12:39
35+
- _0 = drop_in_place::<Option<B>>(move _5) -> bb2; // scope 2 at $DIR/inline-shims.rs:12:14: 12:40
36+
- // mir::Constant
37+
- // + span: $DIR/inline-shims.rs:12:14: 12:37
38+
- // + literal: Const { ty: unsafe fn(*mut std::option::Option<B>) {std::intrinsics::drop_in_place::<std::option::Option<B>>}, val: Value(Scalar(<ZST>)) }
39+
+ _6 = discriminant((*_5)); // scope 3 at $DIR/inline-shims.rs:12:14: 12:40
40+
+ switchInt(move _6) -> [0_isize: bb2, otherwise: bb3]; // scope 3 at $DIR/inline-shims.rs:12:14: 12:40
41+
}
42+
43+
bb2: {
44+
StorageDead(_5); // scope 2 at $DIR/inline-shims.rs:12:39: 12:40
45+
return; // scope 0 at $DIR/inline-shims.rs:13:2: 13:2
46+
+ }
47+
+
48+
+ bb3: {
49+
+ drop((((*_5) as Some).0: B)) -> bb2; // scope 3 at $DIR/inline-shims.rs:12:14: 12:40
50+
}
51+
}
52+

0 commit comments

Comments
 (0)