Skip to content

Commit d03a825

Browse files
committed
Intern ConstValue.
1 parent 5e71913 commit d03a825

File tree

36 files changed

+273
-182
lines changed

36 files changed

+273
-182
lines changed

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
44
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
55
use rustc_middle::mir::interpret::{
6-
read_target_uint, AllocId, ConstValue, ErrorHandled, GlobalAlloc, Scalar,
6+
read_target_uint, AllocId, ConstValue, ConstValueKind, ErrorHandled, GlobalAlloc, Scalar,
77
};
88

99
use cranelift_module::*;
@@ -115,9 +115,9 @@ pub(crate) fn codegen_const_value<'tcx>(
115115
return CValue::by_ref(crate::Pointer::dangling(layout.align.pref), layout);
116116
}
117117

118-
match const_val {
119-
ConstValue::ZeroSized => unreachable!(), // we already handled ZST above
120-
ConstValue::Scalar(x) => match x {
118+
match *const_val.kind() {
119+
ConstValueKind::ZeroSized => unreachable!(), // we already handled ZST above
120+
ConstValueKind::Scalar(x) => match x {
121121
Scalar::Int(int) => {
122122
if fx.clif_type(layout.ty).is_some() {
123123
return CValue::const_val(fx, layout, int);
@@ -200,12 +200,12 @@ pub(crate) fn codegen_const_value<'tcx>(
200200
CValue::by_val(val, layout)
201201
}
202202
},
203-
ConstValue::Indirect { alloc_id, offset } => CValue::by_ref(
203+
ConstValueKind::Indirect { alloc_id, offset } => CValue::by_ref(
204204
pointer_for_allocation(fx, alloc_id)
205205
.offset_i64(fx, i64::try_from(offset.bytes()).unwrap()),
206206
layout,
207207
),
208-
ConstValue::Slice { data, start, end } => {
208+
ConstValueKind::Slice { data, start, end } => {
209209
let alloc_id = fx.tcx.reserve_and_set_memory_alloc(data);
210210
let ptr = pointer_for_allocation(fx, alloc_id)
211211
.offset_i64(fx, i64::try_from(start).unwrap())

compiler/rustc_codegen_cranelift/src/intrinsics/simd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
171171
let idx_const = crate::constant::mir_operand_get_const_val(fx, idx)
172172
.expect("simd_shuffle idx not const");
173173

174-
let idx_bytes = match idx_const {
175-
ConstValue::Indirect { alloc_id, offset } => {
174+
let idx_bytes = match *idx_const.kind() {
175+
ConstValueKind::Indirect { alloc_id, offset } => {
176176
let alloc = fx.tcx.global_alloc(alloc_id).unwrap_memory();
177177
let size = Size::from_bytes(
178178
4 * ret_lane_count, /* size_of([u32; ret_lane_count]) */

compiler/rustc_codegen_ssa/src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ pub fn asm_const_to_str<'tcx>(
197197
const_value: ConstValue<'tcx>,
198198
ty_and_layout: TyAndLayout<'tcx>,
199199
) -> String {
200-
let ConstValue::Scalar(scalar) = const_value else {
200+
let Some(scalar) = const_value.try_to_scalar() else {
201201
span_bug!(sp, "expected Scalar for promoted asm const, but got {:#?}", const_value)
202202
};
203203
let value = scalar.assert_bits(ty_and_layout.size);

compiler/rustc_codegen_ssa/src/mir/operand.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::traits::*;
77
use crate::MemFlags;
88

99
use rustc_middle::mir;
10-
use rustc_middle::mir::interpret::{alloc_range, ConstValue, Pointer, Scalar};
10+
use rustc_middle::mir::interpret::{alloc_range, ConstValue, ConstValueKind, Pointer, Scalar};
1111
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
1212
use rustc_middle::ty::Ty;
1313
use rustc_target::abi::{self, Abi, Align, Size};
@@ -91,16 +91,16 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
9191
) -> Self {
9292
let layout = bx.layout_of(ty);
9393

94-
let val = match val {
95-
ConstValue::Scalar(x) => {
94+
let val = match *val.kind() {
95+
ConstValueKind::Scalar(x) => {
9696
let Abi::Scalar(scalar) = layout.abi else {
9797
bug!("from_const: invalid ByVal layout: {:#?}", layout);
9898
};
9999
let llval = bx.scalar_to_backend(x, scalar, bx.immediate_backend_type(layout));
100100
OperandValue::Immediate(llval)
101101
}
102-
ConstValue::ZeroSized => return OperandRef::zero_sized(layout),
103-
ConstValue::Slice { data, start, end } => {
102+
ConstValueKind::ZeroSized => return OperandRef::zero_sized(layout),
103+
ConstValueKind::Slice { data, start, end } => {
104104
let Abi::ScalarPair(a_scalar, _) = layout.abi else {
105105
bug!("from_const: invalid ScalarPair layout: {:#?}", layout);
106106
};
@@ -119,7 +119,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
119119
let b_llval = bx.const_usize((end - start) as u64);
120120
OperandValue::Pair(a_llval, b_llval)
121121
}
122-
ConstValue::Indirect { alloc_id, offset } => {
122+
ConstValueKind::Indirect { alloc_id, offset } => {
123123
let alloc = bx.tcx().global_alloc(alloc_id).unwrap_memory();
124124
return Self::from_const_alloc(bx, layout, alloc, offset);
125125
}

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,24 @@ pub(super) fn op_to_const<'tcx>(
111111
ecx: &CompileTimeEvalContext<'_, 'tcx>,
112112
op: &OpTy<'tcx>,
113113
) -> ConstValue<'tcx> {
114+
let tcx = *ecx.tcx;
114115
// Handle ZST consistently and early.
115116
if op.layout.is_zst() {
116-
return ConstValue::ZeroSized;
117+
return ConstValue::zero_sized(tcx);
117118
}
118119

119-
// All scalar types should be stored as `ConstValue::Scalar`. This is needed to make
120-
// `ConstValue::try_to_scalar` efficient; we want that to work for *all* constants of scalar
120+
// All scalar types should be stored as `ConstValueKind::Scalar`. This is needed to make
121+
// `ConstValueKind::try_to_scalar` efficient; we want that to work for *all* constants of scalar
121122
// type (it's used throughout the compiler and having it work just on literals is not enough)
122123
// and we want it to be fast (i.e., don't go to an `Allocation` and reconstruct the `Scalar`
123124
// from its byte-serialized form).
124125
let force_as_immediate = match op.layout.abi {
125126
Abi::Scalar(abi::Scalar::Initialized { .. }) => true,
126-
// We don't *force* `ConstValue::Slice` for `ScalarPair`. This has the advantage that if the
127+
// We don't *force* `ConstValueKind::Slice` for `ScalarPair`. This has the advantage that if the
127128
// input `op` is a place, then turning it into a `ConstValue` and back into a `OpTy` will
128129
// not have to generate any duplicate allocations (we preserve the original `AllocId` in
129-
// `ConstValue::Indirect`). It means accessing the contents of a slice can be slow (since
130-
// they can be stored as `ConstValue::Indirect`), but that's not relevant since we barely
130+
// `ConstValueKind::Indirect`). It means accessing the contents of a slice can be slow (since
131+
// they can be stored as `ConstValueKind::Indirect`), but that's not relevant since we barely
131132
// ever have to do this. (`try_get_slice_bytes_for_diagnostics` exists to provide this
132133
// functionality.)
133134
_ => false,
@@ -145,26 +146,26 @@ pub(super) fn op_to_const<'tcx>(
145146
// We know `offset` is relative to the allocation, so we can use `into_parts`.
146147
let (alloc_id, offset) = mplace.ptr().into_parts();
147148
let alloc_id = alloc_id.expect("cannot have `fake` place fot non-ZST type");
148-
ConstValue::Indirect { alloc_id, offset }
149+
ConstValue::from_memory(tcx, alloc_id, offset)
149150
}
150151
// see comment on `let force_as_immediate` above
151152
Right(imm) => match *imm {
152-
Immediate::Scalar(x) => ConstValue::Scalar(x),
153+
Immediate::Scalar(x) => ConstValue::from_scalar(tcx, x),
153154
Immediate::ScalarPair(a, b) => {
154155
debug!("ScalarPair(a: {:?}, b: {:?})", a, b);
155156
// FIXME: assert that this has an appropriate type.
156157
// Currently we actually get here for non-[u8] slices during valtree construction!
157158
let msg = "`op_to_const` on an immediate scalar pair must only be used on slice references to actually allocated memory";
158159
// We know `offset` is relative to the allocation, so we can use `into_parts`.
159-
// We use `ConstValue::Slice` so that we don't have to generate an allocation for
160-
// `ConstValue::Indirect` here.
160+
// We use `ConstValueKind::Slice` so that we don't have to generate an allocation for
161+
// `ConstValueKind::Indirect` here.
161162
let (alloc_id, offset) = a.to_pointer(ecx).expect(msg).into_parts();
162163
let alloc_id = alloc_id.expect(msg);
163164
let data = ecx.tcx.global_alloc(alloc_id).unwrap_memory();
164165
let start = offset.bytes_usize();
165166
let len = b.to_target_usize(ecx).expect(msg);
166167
let len: usize = len.try_into().unwrap();
167-
ConstValue::Slice { data, start, end: start + len }
168+
ConstValue::from_slice(tcx, data, start, start + len)
168169
}
169170
Immediate::Uninit => bug!("`Uninit` is not a valid value for {}", op.layout.ty),
170171
},

compiler/rustc_const_eval/src/const_eval/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub(crate) fn const_caller_location(
3030
if intern_const_alloc_recursive(&mut ecx, InternKind::Constant, &loc_place).is_err() {
3131
bug!("intern_const_alloc_recursive should not error in this case")
3232
}
33-
ConstValue::Scalar(Scalar::from_maybe_pointer(loc_place.ptr(), &tcx))
33+
ConstValue::from_scalar(tcx, Scalar::from_maybe_pointer(loc_place.ptr(), &tcx))
3434
}
3535

3636
// We forbid type-level constants that contain more than `VALTREE_MAX_NODES` nodes.

compiler/rustc_const_eval/src/const_eval/valtrees.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,10 @@ pub fn valtree_to_const_value<'tcx>(
225225
match ty.kind() {
226226
ty::FnDef(..) => {
227227
assert!(valtree.unwrap_branch().is_empty());
228-
ConstValue::ZeroSized
228+
ConstValue::zero_sized(tcx)
229229
}
230230
ty::Bool | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Char => match valtree {
231-
ty::ValTree::Leaf(scalar_int) => ConstValue::Scalar(Scalar::Int(scalar_int)),
231+
ty::ValTree::Leaf(scalar_int) => ConstValue::from_scalar(tcx, Scalar::Int(scalar_int)),
232232
ty::ValTree::Branch(_) => bug!(
233233
"ValTrees for Bool, Int, Uint, Float or Char should have the form ValTree::Leaf"
234234
),

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,24 @@ pub(crate) fn eval_nullary_intrinsic<'tcx>(
6464
sym::type_name => {
6565
ensure_monomorphic_enough(tcx, tp_ty)?;
6666
let alloc = alloc_type_name(tcx, tp_ty);
67-
ConstValue::Slice { data: alloc, start: 0, end: alloc.inner().len() }
67+
ConstValue::from_slice(tcx, alloc, 0, alloc.inner().len())
6868
}
6969
sym::needs_drop => {
7070
ensure_monomorphic_enough(tcx, tp_ty)?;
71-
ConstValue::from_bool(tp_ty.needs_drop(tcx, param_env))
71+
ConstValue::from_bool(tcx, tp_ty.needs_drop(tcx, param_env))
7272
}
7373
sym::pref_align_of => {
7474
// Correctly handles non-monomorphic calls, so there is no need for ensure_monomorphic_enough.
7575
let layout = tcx.layout_of(param_env.and(tp_ty)).map_err(|e| err_inval!(Layout(*e)))?;
76-
ConstValue::from_target_usize(layout.align.pref.bytes(), &tcx)
76+
ConstValue::from_target_usize(tcx, layout.align.pref.bytes())
7777
}
7878
sym::type_id => {
7979
ensure_monomorphic_enough(tcx, tp_ty)?;
80-
ConstValue::from_u128(tcx.type_id_hash(tp_ty).as_u128())
80+
ConstValue::from_u128(tcx, tcx.type_id_hash(tp_ty).as_u128())
8181
}
8282
sym::variant_count => match tp_ty.kind() {
8383
// Correctly handles non-monomorphic calls, so there is no need for ensure_monomorphic_enough.
84-
ty::Adt(adt, _) => ConstValue::from_target_usize(adt.variants().len() as u64, &tcx),
84+
ty::Adt(adt, _) => ConstValue::from_target_usize(tcx, adt.variants().len() as u64),
8585
ty::Alias(..) | ty::Param(_) | ty::Placeholder(_) | ty::Infer(_) => {
8686
throw_inval!(TooGeneric)
8787
}
@@ -106,7 +106,7 @@ pub(crate) fn eval_nullary_intrinsic<'tcx>(
106106
| ty::GeneratorWitnessMIR(_, _)
107107
| ty::Never
108108
| ty::Tuple(_)
109-
| ty::Error(_) => ConstValue::from_target_usize(0u64, &tcx),
109+
| ty::Error(_) => ConstValue::from_target_usize(tcx, 0u64),
110110
},
111111
other => bug!("`{}` is not a zero arg intrinsic", other),
112112
})

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use rustc_span::Span;
1414
use rustc_target::abi::{self, Abi, Align, HasDataLayout, Size};
1515

1616
use super::{
17-
alloc_range, from_known_layout, mir_assign_valid_types, AllocId, ConstValue, Frame, GlobalId,
18-
InterpCx, InterpResult, MPlaceTy, Machine, MemPlace, MemPlaceMeta, PlaceTy, Pointer,
19-
Projectable, Provenance, Scalar,
17+
alloc_range, from_known_layout, mir_assign_valid_types, AllocId, ConstValue, ConstValueKind,
18+
Frame, GlobalId, InterpCx, InterpResult, MPlaceTy, Machine, MemPlace, MemPlaceMeta, PlaceTy,
19+
Pointer, Projectable, Provenance, Scalar,
2020
};
2121

2222
/// An `Immediate` represents a single immediate self-contained Rust value.
@@ -755,16 +755,16 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
755755
})
756756
};
757757
let layout = from_known_layout(self.tcx, self.param_env, layout, || self.layout_of(ty))?;
758-
let op = match val_val {
759-
ConstValue::Indirect { alloc_id, offset } => {
758+
let op = match *val_val.kind() {
759+
ConstValueKind::Indirect { alloc_id, offset } => {
760760
// We rely on mutability being set correctly in that allocation to prevent writes
761761
// where none should happen.
762762
let ptr = self.global_base_pointer(Pointer::new(alloc_id, offset))?;
763763
Operand::Indirect(MemPlace::from_ptr(ptr.into()))
764764
}
765-
ConstValue::Scalar(x) => Operand::Immediate(adjust_scalar(x)?.into()),
766-
ConstValue::ZeroSized => Operand::Immediate(Immediate::Uninit),
767-
ConstValue::Slice { data, start, end } => {
765+
ConstValueKind::Scalar(x) => Operand::Immediate(adjust_scalar(x)?.into()),
766+
ConstValueKind::ZeroSized => Operand::Immediate(Immediate::Uninit),
767+
ConstValueKind::Slice { data, start, end } => {
768768
// We rely on mutability being set correctly in `data` to prevent writes
769769
// where none should happen.
770770
let ptr = Pointer::new(

compiler/rustc_const_eval/src/transform/promote_consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
769769
let unit = Rvalue::Use(Operand::Constant(Box::new(Constant {
770770
span: statement.source_info.span,
771771
user_ty: None,
772-
literal: ConstantKind::zero_sized(self.tcx.types.unit),
772+
literal: ConstantKind::zero_sized(self.tcx, self.tcx.types.unit),
773773
})));
774774
mem::replace(rhs, unit)
775775
},

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pub use self::error::{
149149
UnsupportedOpInfo, ValidationErrorInfo, ValidationErrorKind,
150150
};
151151

152-
pub use self::value::{ConstAlloc, ConstValue, Scalar};
152+
pub use self::value::{ConstAlloc, ConstValue, ConstValueKind, Scalar};
153153

154154
pub use self::allocation::{
155155
alloc_range, AllocBytes, AllocError, AllocRange, AllocResult, Allocation, ConstAllocation,

0 commit comments

Comments
 (0)