Skip to content

Commit 963e157

Browse files
committed
Remove in-progress allocation decoding states
1 parent eb72697 commit 963e157

File tree

1 file changed

+20
-60
lines changed
  • compiler/rustc_middle/src/mir/interpret

1 file changed

+20
-60
lines changed

compiler/rustc_middle/src/mir/interpret/mod.rs

+20-60
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ use std::fmt;
1212
use std::io;
1313
use std::io::{Read, Write};
1414
use std::num::NonZero;
15-
use std::sync::atomic::{AtomicU32, Ordering};
1615

17-
use smallvec::{smallvec, SmallVec};
1816
use tracing::{debug, trace};
1917

2018
use rustc_ast::LitKind;
@@ -159,14 +157,9 @@ pub fn specialized_encode_alloc_id<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>>(
159157
}
160158
}
161159

162-
// Used to avoid infinite recursion when decoding cyclic allocations.
163-
type DecodingSessionId = NonZero<u32>;
164-
165160
#[derive(Clone)]
166161
enum State {
167162
Empty,
168-
InProgressNonAlloc(SmallVec<[DecodingSessionId; 1]>),
169-
InProgress(SmallVec<[DecodingSessionId; 1]>, AllocId),
170163
Done(AllocId),
171164
}
172165

@@ -180,13 +173,7 @@ pub struct AllocDecodingState {
180173
impl AllocDecodingState {
181174
#[inline]
182175
pub fn new_decoding_session(&self) -> AllocDecodingSession<'_> {
183-
static DECODER_SESSION_ID: AtomicU32 = AtomicU32::new(0);
184-
let counter = DECODER_SESSION_ID.fetch_add(1, Ordering::SeqCst);
185-
186-
// Make sure this is never zero.
187-
let session_id = DecodingSessionId::new((counter & 0x7FFFFFFF) + 1).unwrap();
188-
189-
AllocDecodingSession { state: self, session_id }
176+
AllocDecodingSession { state: self }
190177
}
191178

192179
pub fn new(data_offsets: Vec<u64>) -> Self {
@@ -200,7 +187,6 @@ impl AllocDecodingState {
200187
#[derive(Copy, Clone)]
201188
pub struct AllocDecodingSession<'s> {
202189
state: &'s AllocDecodingState,
203-
session_id: DecodingSessionId,
204190
}
205191

206192
impl<'s> AllocDecodingSession<'s> {
@@ -222,52 +208,28 @@ impl<'s> AllocDecodingSession<'s> {
222208

223209
// Check the decoding state to see if it's already decoded or if we should
224210
// decode it here.
225-
let alloc_id = {
226-
let mut entry = self.state.decoding_state[idx].lock();
227-
228-
match *entry {
229-
State::Done(alloc_id) => {
230-
return alloc_id;
231-
}
232-
ref mut entry @ State::Empty => {
233-
// We are allowed to decode.
234-
match alloc_kind {
235-
AllocDiscriminant::Alloc => {
236-
// If this is an allocation, we need to reserve an
237-
// `AllocId` so we can decode cyclic graphs.
238-
let alloc_id = decoder.interner().reserve_alloc_id();
239-
*entry = State::InProgress(smallvec![self.session_id], alloc_id);
240-
Some(alloc_id)
241-
}
242-
AllocDiscriminant::Fn
243-
| AllocDiscriminant::Static
244-
| AllocDiscriminant::VTable => {
245-
// Fns and statics cannot be cyclic, and their `AllocId`
246-
// is determined later by interning.
247-
*entry = State::InProgressNonAlloc(smallvec![self.session_id]);
248-
None
249-
}
211+
let mut entry = self.state.decoding_state[idx].lock();
212+
let alloc_id = match *entry {
213+
State::Done(alloc_id) => {
214+
return alloc_id;
215+
}
216+
State::Empty => {
217+
// We are allowed to decode.
218+
match alloc_kind {
219+
AllocDiscriminant::Alloc => {
220+
// If this is an allocation, we need to reserve an
221+
// `AllocId` so we can decode cyclic graphs.
222+
let alloc_id = decoder.interner().reserve_alloc_id();
223+
Some(alloc_id)
250224
}
251-
}
252-
State::InProgressNonAlloc(ref mut sessions) => {
253-
if sessions.contains(&self.session_id) {
254-
bug!("this should be unreachable");
255-
} else {
256-
// Start decoding concurrently.
257-
sessions.push(self.session_id);
225+
AllocDiscriminant::Fn
226+
| AllocDiscriminant::Static
227+
| AllocDiscriminant::VTable => {
228+
// Fns and statics cannot be cyclic, and their `AllocId`
229+
// is determined later by interning.
258230
None
259231
}
260232
}
261-
State::InProgress(ref mut sessions, alloc_id) => {
262-
if sessions.contains(&self.session_id) {
263-
// Don't recurse.
264-
return alloc_id;
265-
} else {
266-
// Start decoding concurrently.
267-
sessions.push(self.session_id);
268-
Some(alloc_id)
269-
}
270-
}
271233
}
272234
};
273235

@@ -317,9 +279,7 @@ impl<'s> AllocDecodingSession<'s> {
317279
}
318280
});
319281

320-
self.state.decoding_state[idx].with_lock(|entry| {
321-
*entry = State::Done(alloc_id);
322-
});
282+
*entry = State::Done(alloc_id);
323283

324284
alloc_id
325285
}

0 commit comments

Comments
 (0)