Skip to content

Commit 28b75a3

Browse files
committed
Move MirPatch from rustc_middle to rustc_mir_transform.
Because it's only used in `rustc_mir_transform`. (Presumably it is currently in `rustc_middle` because lots of other MIR-related stuff is, but that's not a hard requirement.) And because `rustc_middle` is huge and it's always good to make it smaller.
1 parent 46c7236 commit 28b75a3

17 files changed

+43
-42
lines changed

compiler/rustc_middle/src/mir/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ pub mod generic_graphviz;
4949
pub mod graphviz;
5050
pub mod interpret;
5151
pub mod mono;
52-
pub mod patch;
5352
pub mod pretty;
5453
mod query;
5554
mod statement;

compiler/rustc_mir_transform/src/add_moves_for_packed_drops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use rustc_middle::mir::patch::MirPatch;
21
use rustc_middle::mir::*;
32
use rustc_middle::ty::{self, TyCtxt};
43
use tracing::debug;
54

5+
use crate::patch::MirPatch;
66
use crate::util;
77

88
/// This pass moves values being dropped that are within a packed

compiler/rustc_mir_transform/src/add_subtyping_projections.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use rustc_middle::mir::patch::MirPatch;
21
use rustc_middle::mir::visit::MutVisitor;
32
use rustc_middle::mir::*;
43
use rustc_middle::ty::TyCtxt;
54

5+
use crate::patch::MirPatch;
6+
67
pub(super) struct Subtyper;
78

89
struct SubTypeChecker<'a, 'tcx> {

compiler/rustc_mir_transform/src/coroutine.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1061,9 +1061,8 @@ fn insert_switch<'tcx>(
10611061
}
10621062

10631063
fn elaborate_coroutine_drops<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
1064-
use rustc_middle::mir::patch::MirPatch;
1065-
10661064
use crate::elaborate_drop::{Unwind, elaborate_drop};
1065+
use crate::patch::MirPatch;
10671066
use crate::shim::DropShimElaborator;
10681067

10691068
// Note that `elaborate_drops` only drops the upvars of a coroutine, and

compiler/rustc_mir_transform/src/deref_separator.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use rustc_middle::mir::patch::MirPatch;
21
use rustc_middle::mir::visit::NonUseContext::VarDebugInfo;
32
use rustc_middle::mir::visit::{MutVisitor, PlaceContext};
43
use rustc_middle::mir::*;
54
use rustc_middle::ty::TyCtxt;
65

6+
use crate::patch::MirPatch;
7+
78
pub(super) struct Derefer;
89

910
struct DerefChecker<'a, 'tcx> {

compiler/rustc_mir_transform/src/early_otherwise_branch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::fmt::Debug;
22

3-
use rustc_middle::mir::patch::MirPatch;
43
use rustc_middle::mir::*;
54
use rustc_middle::ty::{Ty, TyCtxt};
65
use tracing::trace;
76

87
use super::simplify::simplify_cfg;
8+
use crate::patch::MirPatch;
99

1010
/// This pass optimizes something like
1111
/// ```ignore (syntax-highlighting-only)

compiler/rustc_mir_transform/src/elaborate_box_derefs.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
55
use rustc_abi::FieldIdx;
66
use rustc_hir::def_id::DefId;
7-
use rustc_middle::mir::patch::MirPatch;
87
use rustc_middle::mir::visit::MutVisitor;
98
use rustc_middle::mir::*;
109
use rustc_middle::span_bug;
1110
use rustc_middle::ty::{Ty, TyCtxt};
1211

12+
use crate::patch::MirPatch;
13+
1314
/// Constructs the types used when accessing a Box's pointer
1415
fn build_ptr_tys<'tcx>(
1516
tcx: TyCtxt<'tcx>,

compiler/rustc_mir_transform/src/elaborate_drop.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::{fmt, iter, mem};
33
use rustc_abi::{FIRST_VARIANT, FieldIdx, VariantIdx};
44
use rustc_hir::lang_items::LangItem;
55
use rustc_index::Idx;
6-
use rustc_middle::mir::patch::MirPatch;
76
use rustc_middle::mir::*;
87
use rustc_middle::span_bug;
98
use rustc_middle::ty::adjustment::PointerCoercion;
@@ -13,6 +12,8 @@ use rustc_span::DUMMY_SP;
1312
use rustc_span::source_map::Spanned;
1413
use tracing::{debug, instrument};
1514

15+
use crate::patch::MirPatch;
16+
1617
/// Describes how/if a value should be dropped.
1718
#[derive(Debug)]
1819
pub(crate) enum DropStyle {

compiler/rustc_mir_transform/src/elaborate_drops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::fmt;
33
use rustc_abi::{FieldIdx, VariantIdx};
44
use rustc_index::IndexVec;
55
use rustc_index::bit_set::DenseBitSet;
6-
use rustc_middle::mir::patch::MirPatch;
76
use rustc_middle::mir::*;
87
use rustc_middle::ty::{self, TyCtxt};
98
use rustc_mir_dataflow::impls::{MaybeInitializedPlaces, MaybeUninitializedPlaces};
@@ -17,6 +16,7 @@ use tracing::{debug, instrument};
1716

1817
use crate::deref_separator::deref_finder;
1918
use crate::elaborate_drop::{DropElaborator, DropFlagMode, DropStyle, Unwind, elaborate_drop};
19+
use crate::patch::MirPatch;
2020

2121
/// During MIR building, Drop terminators are inserted in every place where a drop may occur.
2222
/// However, in this phase, the presence of these terminators does not guarantee that a destructor

compiler/rustc_mir_transform/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ mod errors;
5454
mod ffi_unwind_calls;
5555
mod lint;
5656
mod lint_tail_expr_drop_order;
57+
mod patch;
5758
mod shim;
5859
mod ssa;
5960

compiler/rustc_mir_transform/src/match_branches.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ use std::iter;
22

33
use rustc_abi::Integer;
44
use rustc_index::IndexSlice;
5-
use rustc_middle::mir::patch::MirPatch;
65
use rustc_middle::mir::*;
76
use rustc_middle::ty::layout::{IntegerExt, TyAndLayout};
87
use rustc_middle::ty::{self, ScalarInt, Ty, TyCtxt};
98
use rustc_type_ir::TyKind::*;
109
use tracing::instrument;
1110

1211
use super::simplify::simplify_cfg;
12+
use crate::patch::MirPatch;
1313

1414
pub(super) struct MatchBranchSimplification;
1515

compiler/rustc_middle/src/mir/patch.rs compiler/rustc_mir_transform/src/patch.rs

+20-26
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
use rustc_index::{Idx, IndexVec};
2+
use rustc_middle::mir::*;
3+
use rustc_middle::ty::Ty;
4+
use rustc_span::Span;
15
use tracing::debug;
26

3-
use crate::mir::*;
4-
57
/// This struct represents a patch to MIR, which can add
68
/// new statements and basic blocks and patch over block
79
/// terminators.
8-
pub struct MirPatch<'tcx> {
10+
pub(crate) struct MirPatch<'tcx> {
911
patch_map: IndexVec<BasicBlock, Option<TerminatorKind<'tcx>>>,
1012
new_blocks: Vec<BasicBlockData<'tcx>>,
1113
new_statements: Vec<(Location, StatementKind<'tcx>)>,
@@ -22,7 +24,7 @@ pub struct MirPatch<'tcx> {
2224
}
2325

2426
impl<'tcx> MirPatch<'tcx> {
25-
pub fn new(body: &Body<'tcx>) -> Self {
27+
pub(crate) fn new(body: &Body<'tcx>) -> Self {
2628
let mut result = MirPatch {
2729
patch_map: IndexVec::from_elem(None, &body.basic_blocks),
2830
new_blocks: vec![],
@@ -69,7 +71,7 @@ impl<'tcx> MirPatch<'tcx> {
6971
result
7072
}
7173

72-
pub fn resume_block(&mut self) -> BasicBlock {
74+
pub(crate) fn resume_block(&mut self) -> BasicBlock {
7375
if let Some(bb) = self.resume_block {
7476
return bb;
7577
}
@@ -86,7 +88,7 @@ impl<'tcx> MirPatch<'tcx> {
8688
bb
8789
}
8890

89-
pub fn unreachable_cleanup_block(&mut self) -> BasicBlock {
91+
pub(crate) fn unreachable_cleanup_block(&mut self) -> BasicBlock {
9092
if let Some(bb) = self.unreachable_cleanup_block {
9193
return bb;
9294
}
@@ -103,7 +105,7 @@ impl<'tcx> MirPatch<'tcx> {
103105
bb
104106
}
105107

106-
pub fn unreachable_no_cleanup_block(&mut self) -> BasicBlock {
108+
pub(crate) fn unreachable_no_cleanup_block(&mut self) -> BasicBlock {
107109
if let Some(bb) = self.unreachable_no_cleanup_block {
108110
return bb;
109111
}
@@ -120,7 +122,7 @@ impl<'tcx> MirPatch<'tcx> {
120122
bb
121123
}
122124

123-
pub fn terminate_block(&mut self, reason: UnwindTerminateReason) -> BasicBlock {
125+
pub(crate) fn terminate_block(&mut self, reason: UnwindTerminateReason) -> BasicBlock {
124126
if let Some((cached_bb, cached_reason)) = self.terminate_block
125127
&& reason == cached_reason
126128
{
@@ -139,19 +141,11 @@ impl<'tcx> MirPatch<'tcx> {
139141
bb
140142
}
141143

142-
pub fn is_patched(&self, bb: BasicBlock) -> bool {
144+
pub(crate) fn is_patched(&self, bb: BasicBlock) -> bool {
143145
self.patch_map[bb].is_some()
144146
}
145147

146-
pub fn terminator_loc(&self, body: &Body<'tcx>, bb: BasicBlock) -> Location {
147-
let offset = match bb.index().checked_sub(body.basic_blocks.len()) {
148-
Some(index) => self.new_blocks[index].statements.len(),
149-
None => body[bb].statements.len(),
150-
};
151-
Location { block: bb, statement_index: offset }
152-
}
153-
154-
pub fn new_local_with_info(
148+
pub(crate) fn new_local_with_info(
155149
&mut self,
156150
ty: Ty<'tcx>,
157151
span: Span,
@@ -165,37 +159,37 @@ impl<'tcx> MirPatch<'tcx> {
165159
Local::new(index)
166160
}
167161

168-
pub fn new_temp(&mut self, ty: Ty<'tcx>, span: Span) -> Local {
162+
pub(crate) fn new_temp(&mut self, ty: Ty<'tcx>, span: Span) -> Local {
169163
let index = self.next_local;
170164
self.next_local += 1;
171165
self.new_locals.push(LocalDecl::new(ty, span));
172166
Local::new(index)
173167
}
174168

175-
pub fn new_block(&mut self, data: BasicBlockData<'tcx>) -> BasicBlock {
169+
pub(crate) fn new_block(&mut self, data: BasicBlockData<'tcx>) -> BasicBlock {
176170
let block = BasicBlock::new(self.patch_map.len());
177171
debug!("MirPatch: new_block: {:?}: {:?}", block, data);
178172
self.new_blocks.push(data);
179173
self.patch_map.push(None);
180174
block
181175
}
182176

183-
pub fn patch_terminator(&mut self, block: BasicBlock, new: TerminatorKind<'tcx>) {
177+
pub(crate) fn patch_terminator(&mut self, block: BasicBlock, new: TerminatorKind<'tcx>) {
184178
assert!(self.patch_map[block].is_none());
185179
debug!("MirPatch: patch_terminator({:?}, {:?})", block, new);
186180
self.patch_map[block] = Some(new);
187181
}
188182

189-
pub fn add_statement(&mut self, loc: Location, stmt: StatementKind<'tcx>) {
183+
pub(crate) fn add_statement(&mut self, loc: Location, stmt: StatementKind<'tcx>) {
190184
debug!("MirPatch: add_statement({:?}, {:?})", loc, stmt);
191185
self.new_statements.push((loc, stmt));
192186
}
193187

194-
pub fn add_assign(&mut self, loc: Location, place: Place<'tcx>, rv: Rvalue<'tcx>) {
188+
pub(crate) fn add_assign(&mut self, loc: Location, place: Place<'tcx>, rv: Rvalue<'tcx>) {
195189
self.add_statement(loc, StatementKind::Assign(Box::new((place, rv))));
196190
}
197191

198-
pub fn apply(self, body: &mut Body<'tcx>) {
192+
pub(crate) fn apply(self, body: &mut Body<'tcx>) {
199193
debug!(
200194
"MirPatch: {:?} new temps, starting from index {}: {:?}",
201195
self.new_locals.len(),
@@ -241,14 +235,14 @@ impl<'tcx> MirPatch<'tcx> {
241235
}
242236
}
243237

244-
pub fn source_info_for_index(data: &BasicBlockData<'_>, loc: Location) -> SourceInfo {
238+
fn source_info_for_index(data: &BasicBlockData<'_>, loc: Location) -> SourceInfo {
245239
match data.statements.get(loc.statement_index) {
246240
Some(stmt) => stmt.source_info,
247241
None => data.terminator().source_info,
248242
}
249243
}
250244

251-
pub fn source_info_for_location(&self, body: &Body<'tcx>, loc: Location) -> SourceInfo {
245+
pub(crate) fn source_info_for_location(&self, body: &Body<'tcx>, loc: Location) -> SourceInfo {
252246
let data = match loc.block.index().checked_sub(body.basic_blocks.len()) {
253247
Some(new) => &self.new_blocks[new],
254248
None => &body[loc.block],

compiler/rustc_mir_transform/src/remove_noop_landing_pads.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use rustc_index::bit_set::DenseBitSet;
2-
use rustc_middle::mir::patch::MirPatch;
32
use rustc_middle::mir::*;
43
use rustc_middle::ty::TyCtxt;
54
use rustc_target::spec::PanicStrategy;
65
use tracing::debug;
76

7+
use crate::patch::MirPatch;
8+
89
/// A pass that removes noop landing pads and replaces jumps to them with
910
/// `UnwindAction::Continue`. This is important because otherwise LLVM generates
1011
/// terrible code for these.

compiler/rustc_mir_transform/src/shim.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_hir as hir;
66
use rustc_hir::def_id::DefId;
77
use rustc_hir::lang_items::LangItem;
88
use rustc_index::{Idx, IndexVec};
9-
use rustc_middle::mir::patch::MirPatch;
109
use rustc_middle::mir::*;
1110
use rustc_middle::query::Providers;
1211
use rustc_middle::ty::adjustment::PointerCoercion;
@@ -19,6 +18,7 @@ use rustc_span::{DUMMY_SP, Span};
1918
use tracing::{debug, instrument};
2019

2120
use crate::elaborate_drop::{DropElaborator, DropFlagMode, DropStyle, Unwind, elaborate_drop};
21+
use crate::patch::MirPatch;
2222
use crate::{
2323
abort_unwinding_calls, add_call_guards, add_moves_for_packed_drops, deref_separator, inline,
2424
instsimplify, mentioned_items, pass_manager as pm, remove_noop_landing_pads, simplify,

compiler/rustc_mir_transform/src/sroa.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ use rustc_hir::LangItem;
44
use rustc_index::IndexVec;
55
use rustc_index::bit_set::{DenseBitSet, GrowableBitSet};
66
use rustc_middle::bug;
7-
use rustc_middle::mir::patch::MirPatch;
87
use rustc_middle::mir::visit::*;
98
use rustc_middle::mir::*;
109
use rustc_middle::ty::{self, Ty, TyCtxt};
1110
use rustc_mir_dataflow::value_analysis::{excluded_locals, iter_fields};
1211
use tracing::{debug, instrument};
1312

13+
use crate::patch::MirPatch;
14+
1415
pub(super) struct ScalarReplacementOfAggregates;
1516

1617
impl<'tcx> crate::MirPass<'tcx> for ScalarReplacementOfAggregates {

compiler/rustc_mir_transform/src/unreachable_enum_branching.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use rustc_abi::Variants;
44
use rustc_data_structures::fx::FxHashSet;
55
use rustc_middle::bug;
6-
use rustc_middle::mir::patch::MirPatch;
76
use rustc_middle::mir::{
87
BasicBlock, BasicBlockData, BasicBlocks, Body, Local, Operand, Rvalue, StatementKind,
98
TerminatorKind,
@@ -12,6 +11,8 @@ use rustc_middle::ty::layout::TyAndLayout;
1211
use rustc_middle::ty::{Ty, TyCtxt};
1312
use tracing::trace;
1413

14+
use crate::patch::MirPatch;
15+
1516
pub(super) struct UnreachableEnumBranching;
1617

1718
fn get_discriminant_local(terminator: &TerminatorKind<'_>) -> Option<Local> {

compiler/rustc_mir_transform/src/unreachable_prop.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ use rustc_abi::Size;
66
use rustc_data_structures::fx::FxHashSet;
77
use rustc_middle::bug;
88
use rustc_middle::mir::interpret::Scalar;
9-
use rustc_middle::mir::patch::MirPatch;
109
use rustc_middle::mir::*;
1110
use rustc_middle::ty::{self, TyCtxt};
1211

12+
use crate::patch::MirPatch;
13+
1314
pub(super) struct UnreachablePropagation;
1415

1516
impl crate::MirPass<'_> for UnreachablePropagation {

0 commit comments

Comments
 (0)