Skip to content

Commit f8fc695

Browse files
committed
Auto merge of #116707 - cjgillot:slice-id, r=<try>
Create an `AllocId` for `ConstValue::Slice`. r? `@ghost`
2 parents 0fb279b + 47bdbd3 commit f8fc695

File tree

82 files changed

+618
-185
lines changed

Some content is hidden

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

82 files changed

+618
-185
lines changed

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub(crate) fn codegen_tls_ref<'tcx>(
7474
pub(crate) fn eval_mir_constant<'tcx>(
7575
fx: &FunctionCx<'_, '_, 'tcx>,
7676
constant: &ConstOperand<'tcx>,
77-
) -> (ConstValue<'tcx>, Ty<'tcx>) {
77+
) -> (ConstValue, Ty<'tcx>) {
7878
let cv = fx.monomorphize(constant.const_);
7979
// This cannot fail because we checked all required_consts in advance.
8080
let val = cv
@@ -93,7 +93,7 @@ pub(crate) fn codegen_constant_operand<'tcx>(
9393

9494
pub(crate) fn codegen_const_value<'tcx>(
9595
fx: &mut FunctionCx<'_, '_, 'tcx>,
96-
const_val: ConstValue<'tcx>,
96+
const_val: ConstValue,
9797
ty: Ty<'tcx>,
9898
) -> CValue<'tcx> {
9999
let layout = fx.layout_of(ty);
@@ -210,8 +210,7 @@ pub(crate) fn codegen_const_value<'tcx>(
210210
.offset_i64(fx, i64::try_from(offset.bytes()).unwrap()),
211211
layout,
212212
),
213-
ConstValue::Slice { data, meta } => {
214-
let alloc_id = fx.tcx.reserve_and_set_memory_alloc(data);
213+
ConstValue::Slice { alloc_id, meta } => {
215214
let ptr = pointer_for_allocation(fx, alloc_id).get_addr(fx);
216215
let len = fx.bcx.ins().iconst(fx.pointer_type, meta as i64);
217216
CValue::by_val_pair(ptr, len, layout)

compiler/rustc_codegen_ssa/src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub(crate) fn shift_mask_val<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
148148
pub fn asm_const_to_str<'tcx>(
149149
tcx: TyCtxt<'tcx>,
150150
sp: Span,
151-
const_value: mir::ConstValue<'tcx>,
151+
const_value: mir::ConstValue,
152152
ty_and_layout: TyAndLayout<'tcx>,
153153
) -> String {
154154
let mir::ConstValue::Scalar(scalar) = const_value else {

compiler/rustc_codegen_ssa/src/mir/constant.rs

Lines changed: 1 addition & 1 deletion
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 required_consts before codegen began, so if we got here
2525
// there can be no more constants that fail to evaluate.
2626
self.monomorphize(constant.const_)

compiler/rustc_codegen_ssa/src/mir/operand.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
140140

141141
pub(crate) fn from_const<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
142142
bx: &mut Bx,
143-
val: mir::ConstValue<'tcx>,
143+
val: mir::ConstValue,
144144
ty: Ty<'tcx>,
145145
) -> Self {
146146
let layout = bx.layout_of(ty);
@@ -154,14 +154,11 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
154154
OperandValue::Immediate(llval)
155155
}
156156
ConstValue::ZeroSized => return OperandRef::zero_sized(layout),
157-
ConstValue::Slice { data, meta } => {
157+
ConstValue::Slice { alloc_id, meta } => {
158158
let BackendRepr::ScalarPair(a_scalar, _) = layout.backend_repr else {
159159
bug!("from_const: invalid ScalarPair layout: {:#?}", layout);
160160
};
161-
let a = Scalar::from_pointer(
162-
Pointer::new(bx.tcx().reserve_and_set_memory_alloc(data).into(), Size::ZERO),
163-
&bx.tcx(),
164-
);
161+
let a = Scalar::from_pointer(Pointer::new(alloc_id.into(), Size::ZERO), &bx.tcx());
165162
let a_llval = bx.scalar_to_backend(
166163
a,
167164
a_scalar,

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ pub(crate) fn mk_eval_cx_to_read_const_val<'tcx>(
147147
pub fn mk_eval_cx_for_const_val<'tcx>(
148148
tcx: TyCtxtAt<'tcx>,
149149
typing_env: ty::TypingEnv<'tcx>,
150-
val: mir::ConstValue<'tcx>,
150+
val: mir::ConstValue,
151151
ty: Ty<'tcx>,
152152
) -> Option<(CompileTimeInterpCx<'tcx>, OpTy<'tcx>)> {
153153
let ecx = mk_eval_cx_to_read_const_val(tcx.tcx, tcx.span, typing_env, CanAccessMutGlobal::No);
@@ -167,7 +167,7 @@ pub(super) fn op_to_const<'tcx>(
167167
ecx: &CompileTimeInterpCx<'tcx>,
168168
op: &OpTy<'tcx>,
169169
for_diagnostics: bool,
170-
) -> ConstValue<'tcx> {
170+
) -> ConstValue {
171171
// Handle ZST consistently and early.
172172
if op.layout.is_zst() {
173173
return ConstValue::ZeroSized;
@@ -236,10 +236,9 @@ pub(super) fn op_to_const<'tcx>(
236236
let (prov, offset) =
237237
ptr.into_pointer_or_addr().expect(msg).prov_and_relative_offset();
238238
let alloc_id = prov.alloc_id();
239-
let data = ecx.tcx.global_alloc(alloc_id).unwrap_memory();
240239
assert!(offset == abi::Size::ZERO, "{}", msg);
241240
let meta = b.to_target_usize(ecx).expect(msg);
242-
ConstValue::Slice { data, meta }
241+
ConstValue::Slice { alloc_id, meta }
243242
}
244243
Immediate::Uninit => bug!("`Uninit` is not a valid value for {}", op.layout.ty),
245244
},
@@ -251,7 +250,7 @@ pub(crate) fn turn_into_const_value<'tcx>(
251250
tcx: TyCtxt<'tcx>,
252251
constant: ConstAlloc<'tcx>,
253252
key: ty::PseudoCanonicalInput<'tcx, GlobalId<'tcx>>,
254-
) -> ConstValue<'tcx> {
253+
) -> ConstValue {
255254
let cid = key.value;
256255
let def_id = cid.instance.def.def_id();
257256
let is_static = tcx.is_static(def_id);

compiler/rustc_const_eval/src/const_eval/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const VALTREE_MAX_NODES: usize = 100000;
2828
#[instrument(skip(tcx), level = "debug")]
2929
pub(crate) fn try_destructure_mir_constant_for_user_output<'tcx>(
3030
tcx: TyCtxt<'tcx>,
31-
val: mir::ConstValue<'tcx>,
31+
val: mir::ConstValue,
3232
ty: Ty<'tcx>,
3333
) -> Option<mir::DestructuredConstant<'tcx>> {
3434
let typing_env = ty::TypingEnv::fully_monomorphized();

compiler/rustc_const_eval/src/const_eval/valtrees.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ pub fn valtree_to_const_value<'tcx>(
259259
tcx: TyCtxt<'tcx>,
260260
typing_env: ty::TypingEnv<'tcx>,
261261
cv: ty::Value<'tcx>,
262-
) -> mir::ConstValue<'tcx> {
262+
) -> mir::ConstValue {
263263
// Basic idea: We directly construct `Scalar` values from trivial `ValTree`s
264264
// (those for constants with type bool, int, uint, float or char).
265265
// For all other types we create an `MPlace` and fill that by walking

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::assert_matches::assert_matches;
66

77
use rustc_abi::{FieldIdx, Size};
88
use rustc_apfloat::ieee::{Double, Half, Quad, Single};
9+
use rustc_middle::mir::interpret::CTFE_ALLOC_SALT;
910
use rustc_middle::mir::{self, BinOp, ConstValue, NonDivergingIntrinsic};
1011
use rustc_middle::ty::layout::TyAndLayout;
1112
use rustc_middle::ty::{Ty, TyCtxt};
@@ -16,17 +17,18 @@ use tracing::trace;
1617
use super::memory::MemoryKind;
1718
use super::util::ensure_monomorphic_enough;
1819
use super::{
19-
Allocation, CheckInAllocMsg, ConstAllocation, ImmTy, InterpCx, InterpResult, Machine, OpTy,
20-
PlaceTy, Pointer, PointerArithmetic, Provenance, Scalar, err_ub_custom, err_unsup_format,
21-
interp_ok, throw_inval, throw_ub_custom, throw_ub_format,
20+
AllocId, CheckInAllocMsg, ImmTy, InterpCx, InterpResult, Machine, OpTy, PlaceTy, Pointer,
21+
PointerArithmetic, Provenance, Scalar, err_ub_custom, err_unsup_format, interp_ok, throw_inval,
22+
throw_ub_custom, throw_ub_format,
2223
};
2324
use crate::fluent_generated as fluent;
2425

2526
/// Directly returns an `Allocation` containing an absolute path representation of the given type.
26-
pub(crate) fn alloc_type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ConstAllocation<'tcx> {
27+
pub(crate) fn alloc_type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> (AllocId, u64) {
2728
let path = crate::util::type_name(tcx, ty);
28-
let alloc = Allocation::from_bytes_byte_aligned_immutable(path.into_bytes(), ());
29-
tcx.mk_const_alloc(alloc)
29+
let bytes = path.into_bytes();
30+
let len = bytes.len().try_into().unwrap();
31+
(tcx.allocate_bytes_dedup(bytes, CTFE_ALLOC_SALT), len)
3032
}
3133
impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
3234
/// Generates a value of `TypeId` for `ty` in-place.
@@ -75,8 +77,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
7577
sym::type_name => {
7678
let tp_ty = instance.args.type_at(0);
7779
ensure_monomorphic_enough(tcx, tp_ty)?;
78-
let alloc = alloc_type_name(tcx, tp_ty);
79-
let val = ConstValue::Slice { data: alloc, meta: alloc.inner().size().bytes() };
80+
let (alloc_id, meta) = alloc_type_name(tcx, tp_ty);
81+
let val = ConstValue::Slice { alloc_id, meta };
8082
let val = self.const_val_to_op(val, dest.layout.ty, Some(dest.layout))?;
8183
self.copy_op(&val, dest)?;
8284
}

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
836836

837837
pub(crate) fn const_val_to_op(
838838
&self,
839-
val_val: mir::ConstValue<'tcx>,
839+
val_val: mir::ConstValue,
840840
ty: Ty<'tcx>,
841841
layout: Option<TyAndLayout<'tcx>>,
842842
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
@@ -860,9 +860,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
860860
}
861861
mir::ConstValue::Scalar(x) => adjust_scalar(x)?.into(),
862862
mir::ConstValue::ZeroSized => Immediate::Uninit,
863-
mir::ConstValue::Slice { data, meta } => {
863+
mir::ConstValue::Slice { alloc_id, meta } => {
864864
// This is const data, no mutation allowed.
865-
let alloc_id = self.tcx.reserve_and_set_memory_alloc(data);
866865
let ptr = Pointer::new(CtfeProvenance::from(alloc_id).as_immutable(), Size::ZERO);
867866
Immediate::new_slice(self.global_root_pointer(ptr)?.into(), meta, self)
868867
}

compiler/rustc_const_eval/src/util/caller_location.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub(crate) fn const_caller_location_provider(
5757
file: Symbol,
5858
line: u32,
5959
col: u32,
60-
) -> mir::ConstValue<'_> {
60+
) -> mir::ConstValue {
6161
trace!("const_caller_location: {}:{}:{}", file, line, col);
6262
let mut ecx = mk_eval_cx_to_read_const_val(
6363
tcx,

0 commit comments

Comments
 (0)