Skip to content

Commit 1f34da9

Browse files
committed
Auto merge of #96591 - b-naber:transition-to-valtrees-in-type-system, r=lcnr
Use valtrees as the type-system representation for constant values This is not quite ready yet, there are still some problems with pretty printing and symbol mangling and `deref_const` seems to not work correctly in all cases. Mainly opening now for a perf-run (which should be good to go, despite the still existing problems). r? `@oli-obk` cc `@lcnr` `@RalfJung`
2 parents 872503d + 15c1c06 commit 1f34da9

File tree

117 files changed

+1610
-1007
lines changed

Some content is hidden

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

117 files changed

+1610
-1007
lines changed

Diff for: compiler/rustc_codegen_cranelift/src/constant.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub(crate) fn codegen_constant<'tcx>(
127127
ConstantKind::Val(val, ty) => return codegen_const_value(fx, val, ty),
128128
};
129129
let const_val = match const_.kind() {
130-
ConstKind::Value(const_val) => const_val,
130+
ConstKind::Value(valtree) => fx.tcx.valtree_to_const_val((const_.ty(), valtree)),
131131
ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted })
132132
if fx.tcx.is_static(def.did) =>
133133
{
@@ -468,9 +468,10 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
468468
) -> Option<ConstValue<'tcx>> {
469469
match operand {
470470
Operand::Constant(const_) => match const_.literal {
471-
ConstantKind::Ty(const_) => {
472-
fx.monomorphize(const_).eval(fx.tcx, ParamEnv::reveal_all()).kind().try_to_value()
473-
}
471+
ConstantKind::Ty(const_) => fx
472+
.monomorphize(const_)
473+
.eval_for_mir(fx.tcx, ParamEnv::reveal_all())
474+
.try_to_value(fx.tcx),
474475
ConstantKind::Val(val, _) => Some(val),
475476
},
476477
// FIXME(rust-lang/rust#85105): Casts like `IMM8 as u32` result in the const being stored

Diff for: compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -703,15 +703,19 @@ fn push_const_param<'tcx>(tcx: TyCtxt<'tcx>, ct: ty::Const<'tcx>, output: &mut S
703703
// but we get a deterministic, virtually unique value for the constant.
704704
let hcx = &mut tcx.create_stable_hashing_context();
705705
let mut hasher = StableHasher::new();
706-
hcx.while_hashing_spans(false, |hcx| ct.kind().hash_stable(hcx, &mut hasher));
706+
let ct = ct.eval(tcx, ty::ParamEnv::reveal_all());
707+
hcx.while_hashing_spans(false, |hcx| ct.to_valtree().hash_stable(hcx, &mut hasher));
707708
// Let's only emit 64 bits of the hash value. That should be plenty for
708709
// avoiding collisions and will make the emitted type names shorter.
709-
let hash: u64 = hasher.finish();
710+
// Note: Don't use `StableHashResult` impl of `u64` here directly, since that
711+
// would lead to endianness problems.
712+
let hash: u128 = hasher.finish();
713+
let hash_short = (hash.to_le() as u64).to_le();
710714

711715
if cpp_like_debuginfo(tcx) {
712-
write!(output, "CONST${:x}", hash)
716+
write!(output, "CONST${:x}", hash_short)
713717
} else {
714-
write!(output, "{{CONST#{:x}}}", hash)
718+
write!(output, "{{CONST#{:x}}}", hash_short)
715719
}
716720
}
717721
},

Diff for: compiler/rustc_codegen_ssa/src/mir/constant.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
3838
self.cx.tcx().sess.span_err(constant.span, "erroneous constant encountered");
3939
err
4040
}),
41-
ty::ConstKind::Value(value) => Ok(value),
41+
ty::ConstKind::Value(val) => Ok(self.cx.tcx().valtree_to_const_val((ct.ty(), val))),
4242
err => span_bug!(
4343
constant.span,
4444
"encountered bad ConstKind after monomorphizing: {:?}",
@@ -58,14 +58,14 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
5858
constant
5959
.map(|val| {
6060
let field_ty = ty.builtin_index().unwrap();
61-
let c = ty::Const::from_value(bx.tcx(), val, ty);
61+
let c = mir::ConstantKind::from_value(val, ty);
6262
let values: Vec<_> = bx
6363
.tcx()
64-
.destructure_const(ty::ParamEnv::reveal_all().and(c))
64+
.destructure_mir_constant(ty::ParamEnv::reveal_all(), c)
6565
.fields
6666
.iter()
6767
.map(|field| {
68-
if let Some(prim) = field.kind().try_to_scalar() {
68+
if let Some(prim) = field.try_to_scalar() {
6969
let layout = bx.layout_of(field_ty);
7070
let Abi::Scalar(scalar) = layout.abi else {
7171
bug!("from_const: invalid ByVal layout: {:#?}", layout);

Diff for: compiler/rustc_const_eval/src/const_eval/error.rs

+2
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ impl<'tcx> ConstEvalErr<'tcx> {
140140
///
141141
/// If `lint_root.is_some()` report it as a lint, else report it as a hard error.
142142
/// (Except that for some errors, we ignore all that -- see `must_error` below.)
143+
#[instrument(skip(self, tcx, decorate, lint_root), level = "debug")]
143144
fn struct_generic(
144145
&self,
145146
tcx: TyCtxtAt<'tcx>,
@@ -190,6 +191,7 @@ impl<'tcx> ConstEvalErr<'tcx> {
190191
decorate(err);
191192
};
192193

194+
debug!("self.error: {:?}", self.error);
193195
// Special handling for certain errors
194196
match &self.error {
195197
// Don't emit a new diagnostic for these errors

Diff for: compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ pub(super) fn op_to_const<'tcx>(
196196
}
197197

198198
#[instrument(skip(tcx), level = "debug")]
199-
fn turn_into_const_value<'tcx>(
199+
pub(crate) fn turn_into_const_value<'tcx>(
200200
tcx: TyCtxt<'tcx>,
201201
constant: ConstAlloc<'tcx>,
202202
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
@@ -222,6 +222,7 @@ fn turn_into_const_value<'tcx>(
222222
const_val
223223
}
224224

225+
#[instrument(skip(tcx), level = "debug")]
225226
pub fn eval_to_const_value_raw_provider<'tcx>(
226227
tcx: TyCtxt<'tcx>,
227228
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
@@ -256,6 +257,7 @@ pub fn eval_to_const_value_raw_provider<'tcx>(
256257
tcx.eval_to_allocation_raw(key).map(|val| turn_into_const_value(tcx, val, key))
257258
}
258259

260+
#[instrument(skip(tcx), level = "debug")]
259261
pub fn eval_to_allocation_raw_provider<'tcx>(
260262
tcx: TyCtxt<'tcx>,
261263
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,

Diff for: compiler/rustc_const_eval/src/const_eval/mod.rs

+81-76
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// Not in interpret to make sure we do not use private implementation details
22

3-
use std::convert::TryFrom;
4-
53
use rustc_hir::Mutability;
64
use rustc_middle::mir;
75
use rustc_middle::mir::interpret::{EvalToValTreeResult, GlobalId};
86
use rustc_middle::ty::{self, TyCtxt};
97
use rustc_span::{source_map::DUMMY_SP, symbol::Symbol};
8+
use rustc_target::abi::VariantIdx;
109

1110
use crate::interpret::{
1211
intern_const_alloc_recursive, ConstValue, InternKind, InterpCx, InterpResult, MemPlaceMeta,
@@ -40,7 +39,7 @@ pub(crate) fn const_caller_location(
4039
}
4140

4241
// We forbid type-level constants that contain more than `VALTREE_MAX_NODES` nodes.
43-
const VALTREE_MAX_NODES: usize = 1000;
42+
const VALTREE_MAX_NODES: usize = 100000;
4443

4544
pub(crate) enum ValTreeCreationError {
4645
NodesOverflow,
@@ -56,6 +55,8 @@ pub(crate) fn eval_to_valtree<'tcx>(
5655
cid: GlobalId<'tcx>,
5756
) -> EvalToValTreeResult<'tcx> {
5857
let const_alloc = tcx.eval_to_allocation_raw(param_env.and(cid))?;
58+
59+
// FIXME Need to provide a span to `eval_to_valtree`
5960
let ecx = mk_eval_cx(
6061
tcx, DUMMY_SP, param_env,
6162
// It is absolutely crucial for soundness that
@@ -90,40 +91,81 @@ pub(crate) fn eval_to_valtree<'tcx>(
9091
}
9192
}
9293

93-
/// This function should never fail for validated constants. However, it is also invoked from the
94-
/// pretty printer which might attempt to format invalid constants and in that case it might fail.
94+
/// Tries to destructure constants of type Array or Adt into the constants
95+
/// of its fields.
9596
pub(crate) fn try_destructure_const<'tcx>(
9697
tcx: TyCtxt<'tcx>,
97-
param_env: ty::ParamEnv<'tcx>,
98-
val: ty::Const<'tcx>,
99-
) -> InterpResult<'tcx, mir::DestructuredConst<'tcx>> {
100-
trace!("destructure_const: {:?}", val);
101-
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
102-
let op = ecx.const_to_op(val, None)?;
103-
// We go to `usize` as we cannot allocate anything bigger anyway.
104-
let (field_count, variant, down) = match val.ty().kind() {
105-
ty::Array(_, len) => (usize::try_from(len.eval_usize(tcx, param_env)).unwrap(), None, op),
106-
// Checks if we have any variants, to avoid downcasting to a non-existing variant (when
107-
// there are no variants `read_discriminant` successfully returns a non-existing variant
108-
// index).
109-
ty::Adt(def, _) if def.variants().is_empty() => throw_ub!(Unreachable),
110-
ty::Adt(def, _) => {
111-
let variant = ecx.read_discriminant(&op)?.1;
112-
let down = ecx.operand_downcast(&op, variant)?;
113-
(def.variant(variant).fields.len(), Some(variant), down)
114-
}
115-
ty::Tuple(substs) => (substs.len(), None, op),
116-
_ => bug!("cannot destructure constant {:?}", val),
117-
};
118-
let fields = (0..field_count)
119-
.map(|i| {
120-
let field_op = ecx.operand_field(&down, i)?;
121-
let val = op_to_const(&ecx, &field_op);
122-
Ok(ty::Const::from_value(tcx, val, field_op.layout.ty))
123-
})
124-
.collect::<InterpResult<'tcx, Vec<_>>>()?;
125-
let fields = tcx.arena.alloc_from_iter(fields);
126-
Ok(mir::DestructuredConst { variant, fields })
98+
const_: ty::Const<'tcx>,
99+
) -> Option<ty::DestructuredConst<'tcx>> {
100+
if let ty::ConstKind::Value(valtree) = const_.kind() {
101+
let branches = match valtree {
102+
ty::ValTree::Branch(b) => b,
103+
_ => return None,
104+
};
105+
106+
let (fields, variant) = match const_.ty().kind() {
107+
ty::Array(inner_ty, _) | ty::Slice(inner_ty) => {
108+
// construct the consts for the elements of the array/slice
109+
let field_consts = branches
110+
.iter()
111+
.map(|b| {
112+
tcx.mk_const(ty::ConstS { kind: ty::ConstKind::Value(*b), ty: *inner_ty })
113+
})
114+
.collect::<Vec<_>>();
115+
debug!(?field_consts);
116+
117+
(field_consts, None)
118+
}
119+
ty::Adt(def, _) if def.variants().is_empty() => bug!("unreachable"),
120+
ty::Adt(def, substs) => {
121+
let variant_idx = if def.is_enum() {
122+
VariantIdx::from_u32(branches[0].unwrap_leaf().try_to_u32().ok()?)
123+
} else {
124+
VariantIdx::from_u32(0)
125+
};
126+
let fields = &def.variant(variant_idx).fields;
127+
let mut field_consts = Vec::with_capacity(fields.len());
128+
129+
// Note: First element inValTree corresponds to variant of enum
130+
let mut valtree_idx = if def.is_enum() { 1 } else { 0 };
131+
for field in fields {
132+
let field_ty = field.ty(tcx, substs);
133+
let field_valtree = branches[valtree_idx]; // first element of branches is variant
134+
let field_const = tcx.mk_const(ty::ConstS {
135+
kind: ty::ConstKind::Value(field_valtree),
136+
ty: field_ty,
137+
});
138+
field_consts.push(field_const);
139+
valtree_idx += 1;
140+
}
141+
debug!(?field_consts);
142+
143+
(field_consts, Some(variant_idx))
144+
}
145+
ty::Tuple(elem_tys) => {
146+
let fields = elem_tys
147+
.iter()
148+
.enumerate()
149+
.map(|(i, elem_ty)| {
150+
let elem_valtree = branches[i];
151+
tcx.mk_const(ty::ConstS {
152+
kind: ty::ConstKind::Value(elem_valtree),
153+
ty: elem_ty,
154+
})
155+
})
156+
.collect::<Vec<_>>();
157+
158+
(fields, None)
159+
}
160+
_ => bug!("cannot destructure constant {:?}", const_),
161+
};
162+
163+
let fields = tcx.arena.alloc_from_iter(fields.into_iter());
164+
165+
Some(ty::DestructuredConst { variant, fields })
166+
} else {
167+
None
168+
}
127169
}
128170

129171
#[instrument(skip(tcx), level = "debug")]
@@ -143,8 +185,8 @@ pub(crate) fn try_destructure_mir_constant<'tcx>(
143185
throw_ub!(Unreachable)
144186
}
145187
ty::Adt(def, _) => {
146-
let variant = ecx.read_discriminant(&op).unwrap().1;
147-
let down = ecx.operand_downcast(&op, variant).unwrap();
188+
let variant = ecx.read_discriminant(&op)?.1;
189+
let down = ecx.operand_downcast(&op, variant)?;
148190
(def.variants()[variant].fields.len(), Some(variant), down)
149191
}
150192
ty::Tuple(substs) => (substs.len(), None, op),
@@ -163,43 +205,6 @@ pub(crate) fn try_destructure_mir_constant<'tcx>(
163205
Ok(mir::DestructuredMirConstant { variant, fields })
164206
}
165207

166-
#[instrument(skip(tcx), level = "debug")]
167-
pub(crate) fn deref_const<'tcx>(
168-
tcx: TyCtxt<'tcx>,
169-
param_env: ty::ParamEnv<'tcx>,
170-
val: ty::Const<'tcx>,
171-
) -> ty::Const<'tcx> {
172-
trace!("deref_const: {:?}", val);
173-
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
174-
let op = ecx.const_to_op(val, None).unwrap();
175-
let mplace = ecx.deref_operand(&op).unwrap();
176-
if let Some(alloc_id) = mplace.ptr.provenance {
177-
assert_eq!(
178-
tcx.get_global_alloc(alloc_id).unwrap().unwrap_memory().inner().mutability,
179-
Mutability::Not,
180-
"deref_const cannot be used with mutable allocations as \
181-
that could allow pattern matching to observe mutable statics",
182-
);
183-
}
184-
185-
let ty = match mplace.meta {
186-
MemPlaceMeta::None => mplace.layout.ty,
187-
MemPlaceMeta::Poison => bug!("poison metadata in `deref_const`: {:#?}", mplace),
188-
// In case of unsized types, figure out the real type behind.
189-
MemPlaceMeta::Meta(scalar) => match mplace.layout.ty.kind() {
190-
ty::Str => bug!("there's no sized equivalent of a `str`"),
191-
ty::Slice(elem_ty) => tcx.mk_array(*elem_ty, scalar.to_machine_usize(&tcx).unwrap()),
192-
_ => bug!(
193-
"type {} should not have metadata, but had {:?}",
194-
mplace.layout.ty,
195-
mplace.meta
196-
),
197-
},
198-
};
199-
200-
tcx.mk_const(ty::ConstS { kind: ty::ConstKind::Value(op_to_const(&ecx, &mplace.into())), ty })
201-
}
202-
203208
#[instrument(skip(tcx), level = "debug")]
204209
pub(crate) fn deref_mir_constant<'tcx>(
205210
tcx: TyCtxt<'tcx>,
@@ -213,14 +218,14 @@ pub(crate) fn deref_mir_constant<'tcx>(
213218
assert_eq!(
214219
tcx.get_global_alloc(alloc_id).unwrap().unwrap_memory().0.0.mutability,
215220
Mutability::Not,
216-
"deref_const cannot be used with mutable allocations as \
221+
"deref_mir_constant cannot be used with mutable allocations as \
217222
that could allow pattern matching to observe mutable statics",
218223
);
219224
}
220225

221226
let ty = match mplace.meta {
222227
MemPlaceMeta::None => mplace.layout.ty,
223-
MemPlaceMeta::Poison => bug!("poison metadata in `deref_const`: {:#?}", mplace),
228+
MemPlaceMeta::Poison => bug!("poison metadata in `deref_mir_constant`: {:#?}", mplace),
224229
// In case of unsized types, figure out the real type behind.
225230
MemPlaceMeta::Meta(scalar) => match mplace.layout.ty.kind() {
226231
ty::Str => bug!("there's no sized equivalent of a `str`"),

0 commit comments

Comments
 (0)