Skip to content

Commit 649c73f

Browse files
committed
Simplify Cache wrapper to single type, impl Deref on it, fix all compilation errors in librustc_codegen_ssa
1 parent c0592fa commit 649c73f

File tree

15 files changed

+132
-162
lines changed

15 files changed

+132
-162
lines changed

src/librustc/mir/cache.rs

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::mir::{BasicBlock, BasicBlockData, Body, LocalDecls, Location, Success
66
use rustc_data_structures::graph::{self, GraphPredecessors, GraphSuccessors};
77
use rustc_data_structures::graph::dominators::{dominators, Dominators};
88
use std::iter;
9-
use std::ops::{Index, IndexMut};
9+
use std::ops::{Deref, DerefMut, Index, IndexMut};
1010
use std::vec::IntoIter;
1111

1212
#[derive(Clone, Debug)]
@@ -111,33 +111,21 @@ impl Cache {
111111
}
112112
}
113113

114-
pub struct OwningCache<'tcx> {
114+
pub struct BodyCache<T> {
115115
cache: Cache,
116-
body: Body<'tcx>,
116+
body: T,
117117
}
118118

119-
impl<'tcx> OwningCache<'tcx> {
120-
pub fn borrow(&mut self) -> BorrowedCache<'_, 'tcx> {
121-
BorrowedCache {
122-
cache: &mut self.cache,
123-
body: &self.body,
124-
}
125-
}
126-
127-
pub fn borrow_mut(&mut self) -> MutCache<'_, 'tcx> {
128-
MutCache {
129-
cache: &mut self.cache,
130-
body: &mut self.body,
119+
impl<T> BodyCache<T> {
120+
pub fn new(body: T) -> Self {
121+
Self {
122+
cache: Cache::new(),
123+
body
131124
}
132125
}
133126
}
134127

135-
pub struct BorrowedCache<'a, 'tcx> {
136-
cache: &'a mut Cache,
137-
body: &'a Body<'tcx>
138-
}
139-
140-
impl<'a, 'tcx> BorrowedCache<'a, 'tcx> {
128+
impl<'a, 'tcx> BodyCache<&'a Body<'tcx>> {
141129
#[inline]
142130
pub fn predecessors_for(&mut self, bb: BasicBlock) -> &[BasicBlock] {
143131
self.cache.predecessors_for(bb, self.body)
@@ -159,7 +147,14 @@ impl<'a, 'tcx> BorrowedCache<'a, 'tcx> {
159147
}
160148
}
161149

162-
impl<'a, 'tcx> Index<BasicBlock> for BorrowedCache<'a, 'tcx> {
150+
impl<'a, 'tcx> Deref for BodyCache<&'a Body<'tcx>> {
151+
type Target = Body<'tcx>;
152+
fn deref(&self) -> &Body<'tcx> {
153+
self.body
154+
}
155+
}
156+
157+
impl<'a, 'tcx> Index<BasicBlock> for BodyCache<&'a Body<'tcx>> {
163158
type Output = BasicBlockData<'tcx>;
164159

165160
#[inline]
@@ -168,16 +163,16 @@ impl<'a, 'tcx> Index<BasicBlock> for BorrowedCache<'a, 'tcx> {
168163
}
169164
}
170165

171-
impl<'a, 'tcx> graph::DirectedGraph for BorrowedCache<'a, 'tcx> {
166+
impl<'a, 'tcx> graph::DirectedGraph for BodyCache<&'a Body<'tcx>> {
172167
type Node = BasicBlock;
173168
}
174169

175-
impl<'a, 'graph, 'tcx> graph::GraphPredecessors<'graph> for BorrowedCache<'a, 'tcx> {
170+
impl<'a, 'graph, 'tcx> graph::GraphPredecessors<'graph> for BodyCache<&'a Body<'tcx>> {
176171
type Item = BasicBlock;
177172
type Iter = IntoIter<BasicBlock>;
178173
}
179174

180-
impl<'a, 'tcx> graph::WithPredecessors for BorrowedCache<'a, 'tcx> {
175+
impl<'a, 'tcx> graph::WithPredecessors for BodyCache<&'a Body<'tcx>> {
181176
fn predecessors(
182177
&mut self,
183178
node: Self::Node,
@@ -186,19 +181,19 @@ impl<'a, 'tcx> graph::WithPredecessors for BorrowedCache<'a, 'tcx> {
186181
}
187182
}
188183

189-
impl<'a, 'tcx> graph::WithNumNodes for BorrowedCache<'a, 'tcx> {
184+
impl<'a, 'tcx> graph::WithNumNodes for BodyCache<&'a Body<'tcx>> {
190185
fn num_nodes(&self) -> usize {
191186
self.body.num_nodes()
192187
}
193188
}
194189

195-
impl<'a, 'tcx> graph::WithStartNode for BorrowedCache<'a, 'tcx> {
190+
impl<'a, 'tcx> graph::WithStartNode for BodyCache<&'a Body<'tcx>> {
196191
fn start_node(&self) -> Self::Node {
197192
self.body.start_node()
198193
}
199194
}
200195

201-
impl<'a, 'tcx> graph::WithSuccessors for BorrowedCache<'a, 'tcx> {
196+
impl<'a, 'tcx> graph::WithSuccessors for BodyCache<&'a Body<'tcx>> {
202197
fn successors(
203198
&self,
204199
node: Self::Node,
@@ -207,17 +202,12 @@ impl<'a, 'tcx> graph::WithSuccessors for BorrowedCache<'a, 'tcx> {
207202
}
208203
}
209204

210-
impl<'a, 'b, 'tcx> graph::GraphSuccessors<'b> for BorrowedCache<'a, 'tcx> {
205+
impl<'a, 'b, 'tcx> graph::GraphSuccessors<'b> for BodyCache<&'a Body<'tcx>> {
211206
type Item = BasicBlock;
212207
type Iter = iter::Cloned<Successors<'b>>;
213208
}
214209

215-
pub struct MutCache<'a, 'tcx> {
216-
cache: &'a mut Cache,
217-
body: &'a mut Body<'tcx>,
218-
}
219-
220-
impl<'a, 'tcx> MutCache<'a, 'tcx> {
210+
impl<'a, 'tcx> BodyCache<&'a mut Body<'tcx>> {
221211
#[inline]
222212
pub fn body(&mut self) -> &mut Body<'tcx> {
223213
self.body
@@ -234,7 +224,21 @@ impl<'a, 'tcx> MutCache<'a, 'tcx> {
234224
}
235225
}
236226

237-
impl<'a, 'tcx> Index<BasicBlock> for MutCache<'a, 'tcx> {
227+
impl<'a, 'tcx> Deref for BodyCache<&'a mut Body<'tcx>> {
228+
type Target = Body<'tcx>;
229+
230+
fn deref(&self) -> &Body<'tcx> {
231+
self.body
232+
}
233+
}
234+
235+
impl<'a, 'tcx> DerefMut for BodyCache<&'a mut Body<'tcx>> {
236+
fn deref_mut(&mut self) -> &mut Body<'tcx> {
237+
self.body
238+
}
239+
}
240+
241+
impl<'a, 'tcx> Index<BasicBlock> for BodyCache<&'a mut Body<'tcx>> {
238242
type Output = BasicBlockData<'tcx>;
239243

240244
#[inline]
@@ -243,13 +247,9 @@ impl<'a, 'tcx> Index<BasicBlock> for MutCache<'a, 'tcx> {
243247
}
244248
}
245249

246-
impl<'a, 'tcx> IndexMut<BasicBlock> for MutCache<'a, 'tcx> {
250+
impl<'a, 'tcx> IndexMut<BasicBlock> for BodyCache<&'a mut Body<'tcx>> {
247251
fn index_mut(&mut self, index: BasicBlock) -> &mut Self::Output {
248252
self.cache.invalidate_predecessors();
249253
&mut self.body.basic_blocks[index]
250254
}
251255
}
252-
253-
//CloneTypeFoldableAndLiftImpls! {
254-
// Cache,
255-
//}

src/librustc/mir/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use syntax::symbol::Symbol;
3838
use syntax_pos::{Span, DUMMY_SP};
3939

4040
pub use crate::mir::interpret::AssertMessage;
41+
pub use crate::mir::cache::BodyCache;
4142

4243
pub mod cache;
4344
pub mod interpret;
@@ -2596,7 +2597,11 @@ impl Location {
25962597
}
25972598

25982599
/// Returns `true` if `other` is earlier in the control flow graph than `self`.
2599-
pub fn is_predecessor_of<'tcx>(&self, other: Location, mut body_cache: cache::BorrowedCache<'_, 'tcx>) -> bool {
2600+
pub fn is_predecessor_of<'tcx>(
2601+
&self,
2602+
other: Location,
2603+
mut body_cache: BodyCache<&'_ Body<'tcx>>
2604+
) -> bool {
26002605
// If we are in the same block as the other location and are an earlier statement
26012606
// then we are a predecessor of `other`.
26022607
if self.block == other.block && self.statement_index < other.statement_index {

src/librustc/mir/visit.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::ty::subst::SubstsRef;
22
use crate::ty::{CanonicalUserTypeAnnotation, Ty};
33
use crate::mir::*;
4-
use crate::mir::cache::*;
54
use syntax_pos::Span;
65

76
// # The MIR Visitor
@@ -72,7 +71,10 @@ macro_rules! make_mir_visitor {
7271
// Override these, and call `self.super_xxx` to revert back to the
7372
// default behavior.
7473

75-
fn visit_body(&mut self, body_cache: & $($mutability)? cache_type!('tcx $($mutability)?)) {
74+
fn visit_body(
75+
&mut self,
76+
body_cache: & $($mutability)? BodyCache<&'_ $($mutability)? Body<'tcx>>
77+
) {
7678
self.super_body(body_cache);
7779
}
7880

@@ -241,8 +243,10 @@ macro_rules! make_mir_visitor {
241243
// The `super_xxx` methods comprise the default behavior and are
242244
// not meant to be overridden.
243245

244-
fn super_body(&mut self,
245-
body_cache: & $($mutability)? cache_type!('tcx $($mutability)?)) {
246+
fn super_body(
247+
&mut self,
248+
body_cache: & $($mutability)? BodyCache<&'_ $($mutability)? Body<'tcx>>
249+
) {
246250
let span = body_cache.body().span;
247251
if let Some(yield_ty) = &$($mutability)? body_cache.body().yield_ty {
248252
self.visit_ty(yield_ty, TyContext::YieldTy(SourceInfo {
@@ -793,7 +797,11 @@ macro_rules! make_mir_visitor {
793797

794798
// Convenience methods
795799

796-
fn visit_location(&mut self, body_cache: & $($mutability)? cache_type!('tcx $($mutability)?), location: Location) {
800+
fn visit_location(
801+
&mut self,
802+
body_cache: & $($mutability)? BodyCache<&'_ $($mutability)? Body<'tcx>>,
803+
location: Location
804+
) {
797805
let basic_block = & $($mutability)? body_cache[location.block];
798806
if basic_block.statements.len() == location.statement_index {
799807
if let Some(ref $($mutability)? terminator) = basic_block.terminator {
@@ -809,11 +817,6 @@ macro_rules! make_mir_visitor {
809817
}
810818
}
811819

812-
macro_rules! cache_type {
813-
($tcx:lifetime mut) => {MutCache<'_, $tcx>};
814-
($tcx:lifetime) => {BorrowedCache<'_, $tcx>};
815-
}
816-
817820
macro_rules! visit_place_fns {
818821
(mut) => (
819822
fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;

src/librustc_codegen_ssa/base.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use rustc::hir::def_id::{DefId, LOCAL_CRATE};
3131
use rustc::middle::cstore::EncodedMetadata;
3232
use rustc::middle::lang_items::StartFnLangItem;
3333
use rustc::middle::weak_lang_items;
34+
use rustc::mir::BodyCache;
3435
use rustc::mir::mono::{CodegenUnitNameBuilder, CodegenUnit, MonoItem};
3536
use rustc::ty::{self, Ty, TyCtxt, Instance};
3637
use rustc::ty::layout::{self, Align, TyLayout, LayoutOf, VariantIdx, HasTyCtxt};
@@ -374,7 +375,9 @@ pub fn codegen_instance<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
374375
let lldecl = cx.get_fn(instance);
375376

376377
let mir = cx.tcx().instance_mir(instance.def);
377-
mir::codegen_mir::<Bx>(cx, lldecl, &mir, instance, sig);
378+
// TODO(nashenas88) move this into instance_mir before merging PR
379+
let mut mir = BodyCache::new(mir);
380+
mir::codegen_mir::<Bx>(cx, lldecl, &mut mir, instance, sig);
378381
}
379382

380383
/// Creates the `main` function which will initialize the rust runtime and call

src/librustc_codegen_ssa/mir/analyze.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use rustc_index::bit_set::BitSet;
55
use rustc_data_structures::graph::dominators::Dominators;
66
use rustc_index::vec::{Idx, IndexVec};
7-
use rustc::mir::{self, Location, TerminatorKind};
7+
use rustc::mir::{self, Body, BodyCache, Location, TerminatorKind};
88
use rustc::mir::visit::{
99
Visitor, PlaceContext, MutatingUseContext, NonMutatingUseContext, NonUseContext,
1010
};
@@ -17,10 +17,10 @@ use super::FunctionCx;
1717
use crate::traits::*;
1818

1919
pub fn non_ssa_locals<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
20-
fx: &FunctionCx<'a, 'tcx, Bx>,
20+
fx: &mut FunctionCx<'a, 'tcx, Bx>,
2121
) -> BitSet<mir::Local> {
22-
let mir = fx.mir;
23-
let mut analyzer = LocalAnalyzer::new(fx);
22+
let mir = fx.mir.take().unwrap();
23+
let mut analyzer = LocalAnalyzer::new(fx, mir);
2424

2525
analyzer.visit_body(mir);
2626

@@ -54,11 +54,14 @@ pub fn non_ssa_locals<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
5454
}
5555
}
5656

57-
analyzer.non_ssa_locals
57+
let (mir, non_ssa_locals) = analyzer.finalize();
58+
fx.mir = Some(mir);
59+
non_ssa_locals
5860
}
5961

6062
struct LocalAnalyzer<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
6163
fx: &'mir FunctionCx<'a, 'tcx, Bx>,
64+
mir: &'a mut BodyCache<&'a Body<'tcx>>,
6265
dominators: Dominators<mir::BasicBlock>,
6366
non_ssa_locals: BitSet<mir::Local>,
6467
// The location of the first visited direct assignment to each
@@ -67,27 +70,32 @@ struct LocalAnalyzer<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
6770
}
6871

6972
impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
70-
fn new(fx: &'mir FunctionCx<'a, 'tcx, Bx>) -> Self {
73+
fn new(fx: &'mir FunctionCx<'a, 'tcx, Bx>, mir: &'a mut BodyCache<&'a Body<'tcx>>) -> Self {
7174
let invalid_location =
72-
mir::BasicBlock::new(fx.mir.basic_blocks().len()).start_location();
75+
mir::BasicBlock::new(mir.basic_blocks().len()).start_location();
7376
let mut analyzer = LocalAnalyzer {
7477
fx,
75-
dominators: fx.mir.dominators(),
76-
non_ssa_locals: BitSet::new_empty(fx.mir.local_decls.len()),
77-
first_assignment: IndexVec::from_elem(invalid_location, &fx.mir.local_decls)
78+
dominators: mir.dominators(),
79+
mir,
80+
non_ssa_locals: BitSet::new_empty(mir.local_decls.len()),
81+
first_assignment: IndexVec::from_elem(invalid_location, &mir.local_decls)
7882
};
7983

8084
// Arguments get assigned to by means of the function being called
81-
for arg in fx.mir.args_iter() {
85+
for arg in mir.args_iter() {
8286
analyzer.first_assignment[arg] = mir::START_BLOCK.start_location();
8387
}
8488

8589
analyzer
8690
}
8791

92+
fn finalize(self) -> (&'a mut BodyCache<&'a Body<'tcx>>, BitSet<mir::Local>) {
93+
(self.mir, self.non_ssa_locals)
94+
}
95+
8896
fn first_assignment(&self, local: mir::Local) -> Option<Location> {
8997
let location = self.first_assignment[local];
90-
if location.block.index() < self.fx.mir.basic_blocks().len() {
98+
if location.block.index() < self.mir.basic_blocks().len() {
9199
Some(location)
92100
} else {
93101
None
@@ -130,7 +138,7 @@ impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
130138
};
131139
if is_consume {
132140
let base_ty =
133-
mir::Place::ty_from(place_ref.base, proj_base, self.fx.mir, cx.tcx());
141+
mir::Place::ty_from(place_ref.base, proj_base, self.mir.body(), cx.tcx());
134142
let base_ty = self.fx.monomorphize(&base_ty);
135143

136144
// ZSTs don't require any actual memory access.
@@ -139,7 +147,7 @@ impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
139147
.ty;
140148
let elem_ty = self.fx.monomorphize(&elem_ty);
141149
let span = if let mir::PlaceBase::Local(index) = place_ref.base {
142-
self.fx.mir.local_decls[*index].source_info.span
150+
self.mir.local_decls[*index].source_info.span
143151
} else {
144152
DUMMY_SP
145153
};
@@ -243,7 +251,7 @@ impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx>
243251

244252
if let Some(index) = place.as_local() {
245253
self.assign(index, location);
246-
let decl_span = self.fx.mir.local_decls[index].source_info.span;
254+
let decl_span = self.mir.local_decls[index].source_info.span;
247255
if !self.fx.rvalue_creates_operand(rvalue, decl_span) {
248256
self.not_ssa(index);
249257
}
@@ -348,7 +356,7 @@ impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx>
348356
}
349357

350358
PlaceContext::MutatingUse(MutatingUseContext::Drop) => {
351-
let ty = self.fx.mir.local_decls[local].ty;
359+
let ty = self.mir.local_decls[local].ty;
352360
let ty = self.fx.monomorphize(&ty);
353361

354362
// Only need the place if we're actually dropping it.

0 commit comments

Comments
 (0)