Skip to content

Commit eafbd55

Browse files
committed
Auto merge of #116707 - cjgillot:slice-id, r=<try>
Create an `AllocId` for `ConstValue::Slice`. r? `@ghost`
2 parents 481d45a + e195fe8 commit eafbd55

File tree

92 files changed

+580
-350
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+580
-350
lines changed

compiler/rustc_codegen_cranelift/src/constant.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub(crate) fn codegen_tls_ref<'tcx>(
6464
pub(crate) fn eval_mir_constant<'tcx>(
6565
fx: &FunctionCx<'_, '_, 'tcx>,
6666
constant: &ConstOperand<'tcx>,
67-
) -> (ConstValue<'tcx>, Ty<'tcx>) {
67+
) -> (ConstValue, Ty<'tcx>) {
6868
let cv = fx.monomorphize(constant.const_);
6969
// This cannot fail because we checked all required_consts in advance.
7070
let val = cv
@@ -83,7 +83,7 @@ pub(crate) fn codegen_constant_operand<'tcx>(
8383

8484
pub(crate) fn codegen_const_value<'tcx>(
8585
fx: &mut FunctionCx<'_, '_, 'tcx>,
86-
const_val: ConstValue<'tcx>,
86+
const_val: ConstValue,
8787
ty: Ty<'tcx>,
8888
) -> CValue<'tcx> {
8989
let layout = fx.layout_of(ty);
@@ -183,8 +183,7 @@ pub(crate) fn codegen_const_value<'tcx>(
183183
.offset_i64(fx, i64::try_from(offset.bytes()).unwrap()),
184184
layout,
185185
),
186-
ConstValue::Slice { data, meta } => {
187-
let alloc_id = fx.tcx.reserve_and_set_memory_alloc(data);
186+
ConstValue::Slice { alloc_id, meta } => {
188187
let ptr = pointer_for_allocation(fx, alloc_id).get_addr(fx);
189188
let len = fx.bcx.ins().iconst(fx.pointer_type, meta as i64);
190189
CValue::by_val_pair(ptr, len, layout)
@@ -430,7 +429,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
430429
pub(crate) fn mir_operand_get_const_val<'tcx>(
431430
fx: &FunctionCx<'_, '_, 'tcx>,
432431
operand: &Operand<'tcx>,
433-
) -> Option<ConstValue<'tcx>> {
432+
) -> Option<ConstValue> {
434433
match operand {
435434
Operand::Constant(const_) => Some(eval_mir_constant(fx, const_).0),
436435
// FIXME(rust-lang/rust#85105): Casts like `IMM8 as u32` result in the const being stored

compiler/rustc_codegen_llvm/src/common.rs

-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::value::Value;
88

99
use rustc_ast::Mutability;
1010
use rustc_codegen_ssa::traits::*;
11-
use rustc_data_structures::stable_hasher::{Hash128, HashStable, StableHasher};
1211
use rustc_hir::def_id::DefId;
1312
use rustc_middle::bug;
1413
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
@@ -254,14 +253,6 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
254253
Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None),
255254
_ => self.static_addr_of(init, alloc.align, None),
256255
};
257-
if !self.sess().fewer_names() && llvm::get_value_name(value).is_empty() {
258-
let hash = self.tcx.with_stable_hashing_context(|mut hcx| {
259-
let mut hasher = StableHasher::new();
260-
alloc.hash_stable(&mut hcx, &mut hasher);
261-
hasher.finish::<Hash128>()
262-
});
263-
llvm::set_value_name(value, format!("alloc_{hash:032x}").as_bytes());
264-
}
265256
(value, AddressSpace::DATA)
266257
}
267258
GlobalAlloc::Function(fn_instance) => (

compiler/rustc_codegen_ssa/src/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ pub fn shift_mask_val<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
194194
pub fn asm_const_to_str<'tcx>(
195195
tcx: TyCtxt<'tcx>,
196196
sp: Span,
197-
const_value: mir::ConstValue<'tcx>,
197+
const_value: mir::ConstValue,
198198
ty_and_layout: TyAndLayout<'tcx>,
199199
) -> String {
200200
let mir::ConstValue::Scalar(scalar) = const_value else {

compiler/rustc_codegen_ssa/src/mir/constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
2020
OperandRef::from_const(bx, val, ty)
2121
}
2222

23-
pub fn eval_mir_constant(&self, constant: &mir::ConstOperand<'tcx>) -> mir::ConstValue<'tcx> {
23+
pub fn eval_mir_constant(&self, constant: &mir::ConstOperand<'tcx>) -> mir::ConstValue {
2424
// `MirUsedCollector` visited all constants before codegen began, so if we got here there
2525
// can be no more constants that fail to evaluate.
2626
self.monomorphize(constant.const_)

compiler/rustc_codegen_ssa/src/mir/operand.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
8686

8787
pub fn from_const<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
8888
bx: &mut Bx,
89-
val: mir::ConstValue<'tcx>,
89+
val: mir::ConstValue,
9090
ty: Ty<'tcx>,
9191
) -> Self {
9292
let layout = bx.layout_of(ty);
@@ -100,14 +100,11 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
100100
OperandValue::Immediate(llval)
101101
}
102102
ConstValue::ZeroSized => return OperandRef::zero_sized(layout),
103-
ConstValue::Slice { data, meta } => {
103+
ConstValue::Slice { alloc_id, meta } => {
104104
let Abi::ScalarPair(a_scalar, _) = layout.abi else {
105105
bug!("from_const: invalid ScalarPair layout: {:#?}", layout);
106106
};
107-
let a = Scalar::from_pointer(
108-
Pointer::new(bx.tcx().reserve_and_set_memory_alloc(data), Size::ZERO),
109-
&bx.tcx(),
110-
);
107+
let a = Scalar::from_pointer(Pointer::new(alloc_id, Size::ZERO), &bx.tcx());
111108
let a_llval = bx.scalar_to_backend(
112109
a,
113110
a_scalar,

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub(super) fn mk_eval_cx<'mir, 'tcx>(
108108
pub(super) fn op_to_const<'tcx>(
109109
ecx: &CompileTimeEvalContext<'_, 'tcx>,
110110
op: &OpTy<'tcx>,
111-
) -> ConstValue<'tcx> {
111+
) -> ConstValue {
112112
// Handle ZST consistently and early.
113113
if op.layout.is_zst() {
114114
return ConstValue::ZeroSized;
@@ -166,10 +166,9 @@ pub(super) fn op_to_const<'tcx>(
166166
// We know `offset` is relative to the allocation, so we can use `into_parts`.
167167
let (alloc_id, offset) = a.to_pointer(ecx).expect(msg).into_parts();
168168
let alloc_id = alloc_id.expect(msg);
169-
let data = ecx.tcx.global_alloc(alloc_id).unwrap_memory();
170169
assert!(offset == abi::Size::ZERO, "{}", msg);
171170
let meta = b.to_target_usize(ecx).expect(msg);
172-
ConstValue::Slice { data, meta }
171+
ConstValue::Slice { alloc_id, meta }
173172
}
174173
Immediate::Uninit => bug!("`Uninit` is not a valid value for {}", op.layout.ty),
175174
},
@@ -181,7 +180,7 @@ pub(crate) fn turn_into_const_value<'tcx>(
181180
tcx: TyCtxt<'tcx>,
182181
constant: ConstAlloc<'tcx>,
183182
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
184-
) -> ConstValue<'tcx> {
183+
) -> ConstValue {
185184
let cid = key.value;
186185
let def_id = cid.instance.def.def_id();
187186
let is_static = tcx.is_static(def_id);
@@ -210,7 +209,7 @@ pub(crate) fn turn_into_const_value<'tcx>(
210209
pub fn eval_to_const_value_raw_provider<'tcx>(
211210
tcx: TyCtxt<'tcx>,
212211
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
213-
) -> ::rustc_middle::mir::interpret::EvalToConstValueResult<'tcx> {
212+
) -> ::rustc_middle::mir::interpret::EvalToConstValueResult {
214213
// see comment in eval_to_allocation_raw_provider for what we're doing here
215214
if key.param_env.reveal() == Reveal::All {
216215
let mut key = key;

compiler/rustc_const_eval/src/const_eval/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub(crate) use valtrees::{const_to_valtree_inner, valtree_to_const_value};
2323
pub(crate) fn const_caller_location(
2424
tcx: TyCtxt<'_>,
2525
(file, line, col): (Symbol, u32, u32),
26-
) -> mir::ConstValue<'_> {
26+
) -> mir::ConstValue {
2727
trace!("const_caller_location: {}:{}:{}", file, line, col);
2828
let mut ecx = mk_eval_cx(tcx, DUMMY_SP, ty::ParamEnv::reveal_all(), CanAccessStatics::No);
2929

@@ -88,7 +88,7 @@ pub(crate) fn eval_to_valtree<'tcx>(
8888
#[instrument(skip(tcx), level = "debug")]
8989
pub(crate) fn try_destructure_mir_constant_for_diagnostics<'tcx>(
9090
tcx: TyCtxtAt<'tcx>,
91-
val: mir::ConstValue<'tcx>,
91+
val: mir::ConstValue,
9292
ty: Ty<'tcx>,
9393
) -> Option<mir::DestructuredConstant<'tcx>> {
9494
let param_env = ty::ParamEnv::reveal_all();

compiler/rustc_const_eval/src/const_eval/valtrees.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ pub fn valtree_to_const_value<'tcx>(
207207
tcx: TyCtxt<'tcx>,
208208
param_env_ty: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
209209
valtree: ty::ValTree<'tcx>,
210-
) -> mir::ConstValue<'tcx> {
210+
) -> mir::ConstValue {
211211
// Basic idea: We directly construct `Scalar` values from trivial `ValTree`s
212212
// (those for constants with type bool, int, uint, float or char).
213213
// For all other types we create an `MPlace` and fill that by walking

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use rustc_hir::def_id::DefId;
66
use rustc_middle::mir::{
77
self,
8-
interpret::{Allocation, ConstAllocation, GlobalId, InterpResult, PointerArithmetic, Scalar},
8+
interpret::{AllocId, GlobalId, InterpResult, PointerArithmetic, Scalar},
99
BinOp, ConstValue, NonDivergingIntrinsic,
1010
};
1111
use rustc_middle::ty;
@@ -42,10 +42,11 @@ fn numeric_intrinsic<Prov>(name: Symbol, bits: u128, kind: Primitive) -> Scalar<
4242
}
4343

4444
/// Directly returns an `Allocation` containing an absolute path representation of the given type.
45-
pub(crate) fn alloc_type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ConstAllocation<'tcx> {
45+
pub(crate) fn alloc_type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> (AllocId, u64) {
4646
let path = crate::util::type_name(tcx, ty);
47-
let alloc = Allocation::from_bytes_byte_aligned_immutable(path.into_bytes());
48-
tcx.mk_const_alloc(alloc)
47+
let bytes = path.into_bytes();
48+
let len = bytes.len().try_into().unwrap();
49+
(tcx.allocate_bytes(bytes), len)
4950
}
5051

5152
/// The logic for all nullary intrinsics is implemented here. These intrinsics don't get evaluated
@@ -55,14 +56,14 @@ pub(crate) fn eval_nullary_intrinsic<'tcx>(
5556
param_env: ty::ParamEnv<'tcx>,
5657
def_id: DefId,
5758
args: GenericArgsRef<'tcx>,
58-
) -> InterpResult<'tcx, ConstValue<'tcx>> {
59+
) -> InterpResult<'tcx, ConstValue> {
5960
let tp_ty = args.type_at(0);
6061
let name = tcx.item_name(def_id);
6162
Ok(match name {
6263
sym::type_name => {
6364
ensure_monomorphic_enough(tcx, tp_ty)?;
64-
let alloc = alloc_type_name(tcx, tp_ty);
65-
ConstValue::Slice { data: alloc, meta: alloc.inner().size().bytes() }
65+
let (alloc_id, len) = alloc_type_name(tcx, tp_ty);
66+
ConstValue::Slice { alloc_id, meta: len }
6667
}
6768
sym::needs_drop => {
6869
ensure_monomorphic_enough(tcx, tp_ty)?;

compiler/rustc_const_eval/src/interpret/operand.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
722722

723723
pub(crate) fn const_val_to_op(
724724
&self,
725-
val_val: mir::ConstValue<'tcx>,
725+
val_val: mir::ConstValue,
726726
ty: Ty<'tcx>,
727727
layout: Option<TyAndLayout<'tcx>>,
728728
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
@@ -743,10 +743,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
743743
}
744744
mir::ConstValue::Scalar(x) => Operand::Immediate(adjust_scalar(x)?.into()),
745745
mir::ConstValue::ZeroSized => Operand::Immediate(Immediate::Uninit),
746-
mir::ConstValue::Slice { data, meta } => {
746+
mir::ConstValue::Slice { alloc_id, meta } => {
747747
// We rely on mutability being set correctly in `data` to prevent writes
748748
// where none should happen.
749-
let ptr = Pointer::new(self.tcx.reserve_and_set_memory_alloc(data), Size::ZERO);
749+
let ptr = Pointer::new(alloc_id, Size::ZERO);
750750
Operand::Immediate(Immediate::new_slice(
751751
self.global_base_pointer(ptr)?.into(),
752752
meta,

compiler/rustc_middle/src/hooks/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,5 @@ macro_rules! declare_hooks {
6666
declare_hooks! {
6767
/// Tries to destructure an `mir::Const` ADT or array into its variant index
6868
/// and its field values. This should only be used for pretty printing.
69-
hook try_destructure_mir_constant_for_diagnostics(val: mir::ConstValue<'tcx>, ty: Ty<'tcx>) -> Option<mir::DestructuredConstant<'tcx>>;
69+
hook try_destructure_mir_constant_for_diagnostics(val: mir::ConstValue, ty: Ty<'tcx>) -> Option<mir::DestructuredConstant<'tcx>>;
7070
}

compiler/rustc_middle/src/mir/consts.rs

+21-17
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_hir::{self as hir};
66
use rustc_span::Span;
77
use rustc_target::abi::{HasDataLayout, Size};
88

9-
use crate::mir::interpret::{alloc_range, AllocId, ConstAllocation, ErrorHandled, Scalar};
9+
use crate::mir::interpret::{alloc_range, AllocId, ErrorHandled, Scalar};
1010
use crate::mir::{pretty_print_const_value, Promoted};
1111
use crate::ty::ScalarInt;
1212
use crate::ty::{self, print::pretty_print_const, List, Ty, TyCtxt};
@@ -29,8 +29,8 @@ pub struct ConstAlloc<'tcx> {
2929
/// Represents a constant value in Rust. `Scalar` and `Slice` are optimizations for
3030
/// array length computations, enum discriminants and the pattern matching logic.
3131
#[derive(Copy, Clone, Debug, Eq, PartialEq, TyEncodable, TyDecodable, Hash)]
32-
#[derive(HashStable, Lift)]
33-
pub enum ConstValue<'tcx> {
32+
#[derive(HashStable)]
33+
pub enum ConstValue {
3434
/// Used for types with `layout::abi::Scalar` ABI.
3535
///
3636
/// Not using the enum `Value` to encode that this must not be `Uninit`.
@@ -48,7 +48,7 @@ pub enum ConstValue<'tcx> {
4848
Slice {
4949
/// The allocation storing the slice contents.
5050
/// This always points to the beginning of the allocation.
51-
data: ConstAllocation<'tcx>,
51+
alloc_id: AllocId,
5252
/// The metadata field of the reference.
5353
/// This is a "target usize", so we use `u64` as in the interpreter.
5454
meta: u64,
@@ -71,9 +71,9 @@ pub enum ConstValue<'tcx> {
7171
}
7272

7373
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
74-
static_assert_size!(ConstValue<'_>, 24);
74+
static_assert_size!(ConstValue, 24);
7575

76-
impl<'tcx> ConstValue<'tcx> {
76+
impl ConstValue {
7777
#[inline]
7878
pub fn try_to_scalar(&self) -> Option<Scalar<AllocId>> {
7979
match *self {
@@ -94,11 +94,11 @@ impl<'tcx> ConstValue<'tcx> {
9494
self.try_to_scalar_int()?.try_into().ok()
9595
}
9696

97-
pub fn try_to_target_usize(&self, tcx: TyCtxt<'tcx>) -> Option<u64> {
97+
pub fn try_to_target_usize(&self, tcx: TyCtxt<'_>) -> Option<u64> {
9898
self.try_to_scalar_int()?.try_to_target_usize(tcx).ok()
9999
}
100100

101-
pub fn try_to_bits_for_ty(
101+
pub fn try_to_bits_for_ty<'tcx>(
102102
&self,
103103
tcx: TyCtxt<'tcx>,
104104
param_env: ty::ParamEnv<'tcx>,
@@ -125,12 +125,15 @@ impl<'tcx> ConstValue<'tcx> {
125125
}
126126

127127
/// Must only be called on constants of type `&str` or `&[u8]`!
128-
pub fn try_get_slice_bytes_for_diagnostics(&self, tcx: TyCtxt<'tcx>) -> Option<&'tcx [u8]> {
129-
let (data, start, end) = match self {
128+
pub fn try_get_slice_bytes_for_diagnostics<'tcx>(
129+
&self,
130+
tcx: TyCtxt<'tcx>,
131+
) -> Option<&'tcx [u8]> {
132+
let (alloc_id, start, len) = match self {
130133
ConstValue::Scalar(_) | ConstValue::ZeroSized => {
131134
bug!("`try_get_slice_bytes` on non-slice constant")
132135
}
133-
&ConstValue::Slice { data, meta } => (data, 0, meta),
136+
&ConstValue::Slice { alloc_id, meta } => (alloc_id, 0, meta),
134137
&ConstValue::Indirect { alloc_id, offset } => {
135138
// The reference itself is stored behind an indirection.
136139
// Load the reference, and then load the actual slice contents.
@@ -162,14 +165,15 @@ impl<'tcx> ConstValue<'tcx> {
162165
}
163166
// Non-empty slice, must have memory. We know this is a relative pointer.
164167
let (inner_alloc_id, offset) = ptr.into_parts();
165-
let data = tcx.global_alloc(inner_alloc_id?).unwrap_memory();
166-
(data, offset.bytes(), offset.bytes() + len)
168+
(inner_alloc_id?, offset.bytes(), len)
167169
}
168170
};
169171

172+
let data = tcx.global_alloc(alloc_id).unwrap_memory();
173+
170174
// This is for diagnostics only, so we are okay to use `inspect_with_uninit_and_ptr_outside_interpreter`.
171175
let start = start.try_into().unwrap();
172-
let end = end.try_into().unwrap();
176+
let end = start + usize::try_from(len).unwrap();
173177
Some(data.inner().inspect_with_uninit_and_ptr_outside_interpreter(start..end))
174178
}
175179
}
@@ -197,7 +201,7 @@ pub enum Const<'tcx> {
197201

198202
/// This constant cannot go back into the type system, as it represents
199203
/// something the type system cannot handle (e.g. pointers).
200-
Val(ConstValue<'tcx>, Ty<'tcx>),
204+
Val(ConstValue, Ty<'tcx>),
201205
}
202206

203207
impl<'tcx> Const<'tcx> {
@@ -245,7 +249,7 @@ impl<'tcx> Const<'tcx> {
245249
tcx: TyCtxt<'tcx>,
246250
param_env: ty::ParamEnv<'tcx>,
247251
span: Option<Span>,
248-
) -> Result<ConstValue<'tcx>, ErrorHandled> {
252+
) -> Result<ConstValue, ErrorHandled> {
249253
match self {
250254
Const::Ty(c) => {
251255
// We want to consistently have a "clean" value for type system constants (i.e., no
@@ -337,7 +341,7 @@ impl<'tcx> Const<'tcx> {
337341
}
338342

339343
#[inline]
340-
pub fn from_value(val: ConstValue<'tcx>, ty: Ty<'tcx>) -> Self {
344+
pub fn from_value(val: ConstValue, ty: Ty<'tcx>) -> Self {
341345
Self::Val(val, ty)
342346
}
343347

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl Into<ErrorGuaranteed> for ReportedErrorInfo {
8585
TrivialTypeTraversalImpls! { ErrorHandled }
8686

8787
pub type EvalToAllocationRawResult<'tcx> = Result<ConstAlloc<'tcx>, ErrorHandled>;
88-
pub type EvalToConstValueResult<'tcx> = Result<ConstValue<'tcx>, ErrorHandled>;
88+
pub type EvalToConstValueResult = Result<ConstValue, ErrorHandled>;
8989
/// `Ok(None)` indicates the constant was fine, but the valtree couldn't be constructed.
9090
/// This is needed in `thir::pattern::lower_inline_const`.
9191
pub type EvalToValTreeResult<'tcx> = Result<Option<ValTree<'tcx>>, ErrorHandled>;

0 commit comments

Comments
 (0)