Skip to content

Commit 9ef21e1

Browse files
committed
Auto merge of #116707 - cjgillot:slice-id, r=<try>
Create an `AllocId` for `ConstValue::Slice`. r? `@ghost`
2 parents 57ef889 + 334753f commit 9ef21e1

File tree

55 files changed

+305
-158
lines changed

Some content is hidden

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

55 files changed

+305
-158
lines changed

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 4 additions & 5 deletions
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

Lines changed: 0 additions & 9 deletions
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

Lines changed: 1 addition & 1 deletion
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

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 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

Lines changed: 3 additions & 6 deletions
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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub(super) fn mk_eval_cx<'mir, 'tcx>(
109109
pub(super) fn op_to_const<'tcx>(
110110
ecx: &CompileTimeEvalContext<'_, 'tcx>,
111111
op: &OpTy<'tcx>,
112-
) -> ConstValue<'tcx> {
112+
) -> ConstValue {
113113
// Handle ZST consistently and early.
114114
if op.layout.is_zst() {
115115
return ConstValue::ZeroSized;
@@ -167,10 +167,9 @@ pub(super) fn op_to_const<'tcx>(
167167
// We know `offset` is relative to the allocation, so we can use `into_parts`.
168168
let (alloc_id, offset) = a.to_pointer(ecx).expect(msg).into_parts();
169169
let alloc_id = alloc_id.expect(msg);
170-
let data = ecx.tcx.global_alloc(alloc_id).unwrap_memory();
171170
assert!(offset == abi::Size::ZERO, "{}", msg);
172171
let meta = b.to_target_usize(ecx).expect(msg);
173-
ConstValue::Slice { data, meta }
172+
ConstValue::Slice { alloc_id, meta }
174173
}
175174
Immediate::Uninit => bug!("`Uninit` is not a valid value for {}", op.layout.ty),
176175
},
@@ -182,7 +181,7 @@ pub(crate) fn turn_into_const_value<'tcx>(
182181
tcx: TyCtxt<'tcx>,
183182
constant: ConstAlloc<'tcx>,
184183
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
185-
) -> ConstValue<'tcx> {
184+
) -> ConstValue {
186185
let cid = key.value;
187186
let def_id = cid.instance.def.def_id();
188187
let is_static = tcx.is_static(def_id);
@@ -211,7 +210,7 @@ pub(crate) fn turn_into_const_value<'tcx>(
211210
pub fn eval_to_const_value_raw_provider<'tcx>(
212211
tcx: TyCtxt<'tcx>,
213212
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
214-
) -> ::rustc_middle::mir::interpret::EvalToConstValueResult<'tcx> {
213+
) -> ::rustc_middle::mir::interpret::EvalToConstValueResult {
215214
// see comment in eval_to_allocation_raw_provider for what we're doing here
216215
if key.param_env.reveal() == Reveal::All {
217216
let mut key = key;

compiler/rustc_const_eval/src/const_eval/mod.rs

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 8 additions & 7 deletions
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

Lines changed: 3 additions & 3 deletions
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,

0 commit comments

Comments
 (0)