Skip to content

Commit 0a911ec

Browse files
committed
Stop counting opaques.
1 parent 7f34f6e commit 0a911ec

File tree

1 file changed

+24
-25
lines changed
  • compiler/rustc_mir_transform/src

1 file changed

+24
-25
lines changed

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ use rustc_data_structures::fx::FxHasher;
100100
use rustc_data_structures::graph::dominators::Dominators;
101101
use rustc_hir::def::DefKind;
102102
use rustc_index::bit_set::DenseBitSet;
103-
use rustc_index::{Idx, IndexVec, newtype_index};
103+
use rustc_index::{IndexVec, newtype_index};
104104
use rustc_middle::bug;
105105
use rustc_middle::mir::interpret::GlobalAlloc;
106106
use rustc_middle::mir::visit::*;
@@ -158,11 +158,16 @@ newtype_index! {
158158
struct VnIndex {}
159159
}
160160

161-
newtype_index! {
162-
/// Counter type to ensure that all unique values are created using `insert_unique`.
163-
#[debug_format = "_o{}"]
164-
struct VnOpaque {
165-
const DETERMINISTIC = 0;
161+
#[derive(Copy, Clone, Debug, Eq)]
162+
struct VnOpaque;
163+
impl PartialEq for VnOpaque {
164+
fn eq(&self, _: &VnOpaque) -> bool {
165+
unreachable!()
166+
}
167+
}
168+
impl Hash for VnOpaque {
169+
fn hash<T: Hasher>(&self, _: &mut T) {
170+
unreachable!()
166171
}
167172
}
168173

@@ -183,8 +188,8 @@ enum Value<'tcx> {
183188
value: Const<'tcx>,
184189
/// Some constants do not have a deterministic value. To avoid merging two instances of the
185190
/// same `Const`, we assign them an additional integer index.
186-
// `disambiguator` is `DETERMINISTIC` iff the constant is deterministic.
187-
disambiguator: VnOpaque,
191+
// `disambiguator` is `None` iff the constant is deterministic.
192+
disambiguator: Option<VnOpaque>,
188193
},
189194
/// An aggregate value, either tuple/closure/struct/enum.
190195
/// This does not contain unions, as we cannot reason with the value.
@@ -234,8 +239,6 @@ struct ValueSet<'tcx> {
234239
hashes: IndexVec<VnIndex, u64>,
235240
values: IndexVec<VnIndex, Value<'tcx>>,
236241
types: IndexVec<VnIndex, Ty<'tcx>>,
237-
/// Counter to generate different values.
238-
next_opaque: VnOpaque,
239242
}
240243

241244
impl<'tcx> ValueSet<'tcx> {
@@ -245,8 +248,6 @@ impl<'tcx> ValueSet<'tcx> {
245248
hashes: IndexVec::with_capacity(num_values),
246249
values: IndexVec::with_capacity(num_values),
247250
types: IndexVec::with_capacity(num_values),
248-
// The first opaque is 1, as 0 means deterministic constant.
249-
next_opaque: VnOpaque::from_u32(1),
250251
}
251252
}
252253

@@ -257,12 +258,11 @@ impl<'tcx> ValueSet<'tcx> {
257258
ty: Ty<'tcx>,
258259
value: impl FnOnce(VnOpaque) -> Value<'tcx>,
259260
) -> VnIndex {
260-
let value = value(self.next_opaque);
261-
self.next_opaque.increment_by(1);
261+
let value = value(VnOpaque);
262262

263263
debug_assert!(match value {
264264
Value::Opaque(_) | Value::Address { .. } => true,
265-
Value::Constant { disambiguator, .. } => disambiguator != DETERMINISTIC,
265+
Value::Constant { disambiguator, .. } => disambiguator.is_some(),
266266
_ => false,
267267
});
268268

@@ -280,7 +280,7 @@ impl<'tcx> ValueSet<'tcx> {
280280
fn insert(&mut self, ty: Ty<'tcx>, value: Value<'tcx>) -> (VnIndex, bool) {
281281
debug_assert!(match value {
282282
Value::Opaque(_) | Value::Address { .. } => false,
283-
Value::Constant { disambiguator, .. } => disambiguator == DETERMINISTIC,
283+
Value::Constant { disambiguator, .. } => disambiguator.is_none(),
284284
_ => true,
285285
});
286286

@@ -325,8 +325,7 @@ impl<'tcx> ValueSet<'tcx> {
325325
/// Replace the value associated with `index` with an opaque value.
326326
#[inline]
327327
fn forget(&mut self, index: VnIndex) {
328-
self.values[index] = Value::Opaque(self.next_opaque);
329-
self.next_opaque.increment_by(1);
328+
self.values[index] = Value::Opaque(VnOpaque);
330329
}
331330
}
332331

@@ -436,14 +435,14 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
436435
fn insert_constant(&mut self, value: Const<'tcx>) -> VnIndex {
437436
let (index, new) = if value.is_deterministic() {
438437
// The constant is deterministic, no need to disambiguate.
439-
let constant = Value::Constant { value, disambiguator: DETERMINISTIC };
438+
let constant = Value::Constant { value, disambiguator: None };
440439
self.values.insert(value.ty(), constant)
441440
} else {
442441
// Multiple mentions of this constant will yield different values,
443442
// so assign a different `disambiguator` to ensure they do not get the same `VnIndex`.
444-
let index = self.values.insert_unique(value.ty(), |disambiguator| {
445-
debug_assert_ne!(disambiguator, DETERMINISTIC);
446-
Value::Constant { value, disambiguator }
443+
let index = self.values.insert_unique(value.ty(), |disambiguator| Value::Constant {
444+
value,
445+
disambiguator: Some(disambiguator),
447446
});
448447
(index, true)
449448
};
@@ -479,14 +478,14 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
479478
// Booleans are deterministic.
480479
let value = Const::from_bool(self.tcx, flag);
481480
debug_assert!(value.is_deterministic());
482-
self.insert(self.tcx.types.bool, Value::Constant { value, disambiguator: DETERMINISTIC })
481+
self.insert(self.tcx.types.bool, Value::Constant { value, disambiguator: None })
483482
}
484483

485484
fn insert_scalar(&mut self, ty: Ty<'tcx>, scalar: Scalar) -> VnIndex {
486485
// Scalars are deterministic.
487486
let value = Const::from_scalar(self.tcx, scalar, ty);
488487
debug_assert!(value.is_deterministic());
489-
self.insert(ty, Value::Constant { value, disambiguator: DETERMINISTIC })
488+
self.insert(ty, Value::Constant { value, disambiguator: None })
490489
}
491490

492491
fn insert_tuple(&mut self, ty: Ty<'tcx>, values: Vec<VnIndex>) -> VnIndex {
@@ -1731,7 +1730,7 @@ impl<'tcx> VnState<'_, 'tcx> {
17311730
// This was already constant in MIR, do not change it. If the constant is not
17321731
// deterministic, adding an additional mention of it in MIR will not give the same value as
17331732
// the former mention.
1734-
if let Value::Constant { value, disambiguator: DETERMINISTIC } = *self.get(index) {
1733+
if let Value::Constant { value, disambiguator: None } = *self.get(index) {
17351734
debug_assert!(value.is_deterministic());
17361735
return Some(ConstOperand { span: DUMMY_SP, user_ty: None, const_: value });
17371736
}

0 commit comments

Comments
 (0)