Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include Refs in Valtree Creation #95426

Merged
merged 6 commits into from
Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 113 additions & 25 deletions compiler/rustc_const_eval/src/const_eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use rustc_hir::Mutability;
use rustc_middle::ty::{self, TyCtxt};
use rustc_middle::{
mir::{self, interpret::ConstAlloc},
ty::ScalarInt,
ty::{ScalarInt, Ty},
};
use rustc_span::{source_map::DUMMY_SP, symbol::Symbol};
use rustc_target::abi::VariantIdx;

use crate::interpret::{
intern_const_alloc_recursive, ConstValue, InternKind, InterpCx, InterpResult, MPlaceTy,
Expand Down Expand Up @@ -55,28 +56,43 @@ pub(crate) fn const_to_valtree<'tcx>(
const_to_valtree_inner(&ecx, &place)
}

#[instrument(skip(ecx), level = "debug")]
fn branches<'tcx>(
ecx: &CompileTimeEvalContext<'tcx, 'tcx>,
place: &MPlaceTy<'tcx>,
n: usize,
variant: Option<VariantIdx>,
) -> Option<ty::ValTree<'tcx>> {
let place = match variant {
Some(variant) => ecx.mplace_downcast(&place, variant).unwrap(),
None => *place,
};
let variant = variant.map(|variant| Some(ty::ValTree::Leaf(ScalarInt::from(variant.as_u32()))));
debug!(?place, ?variant);

let fields = (0..n).map(|i| {
let field = ecx.mplace_field(&place, i).unwrap();
const_to_valtree_inner(ecx, &field)
});
// For enums, we preped their variant index before the variant's fields so we can figure out
b-naber marked this conversation as resolved.
Show resolved Hide resolved
// the variant again when just seeing a valtree.
let branches = variant.into_iter().chain(fields);
Some(ty::ValTree::Branch(ecx.tcx.arena.alloc_from_iter(branches.collect::<Option<Vec<_>>>()?)))
}

#[instrument(skip(ecx), level = "debug")]
fn const_to_valtree_inner<'tcx>(
ecx: &CompileTimeEvalContext<'tcx, 'tcx>,
place: &MPlaceTy<'tcx>,
) -> Option<ty::ValTree<'tcx>> {
let branches = |n, variant| {
let place = match variant {
Some(variant) => ecx.mplace_downcast(&place, variant).unwrap(),
None => *place,
};
let variant =
variant.map(|variant| Some(ty::ValTree::Leaf(ScalarInt::from(variant.as_u32()))));
let fields = (0..n).map(|i| {
let field = ecx.mplace_field(&place, i).unwrap();
const_to_valtree_inner(ecx, &field)
});
// For enums, we preped their variant index before the variant's fields so we can figure out
// the variant again when just seeing a valtree.
let branches = variant.into_iter().chain(fields);
Some(ty::ValTree::Branch(
ecx.tcx.arena.alloc_from_iter(branches.collect::<Option<Vec<_>>>()?),
))
// We only want to use raw bytes in ValTrees for string slices or &[<integer_ty>]
let use_bytes_for_ref = |ty: Ty<'tcx>| -> bool {
match ty.kind() {
ty::Str | ty::Char | ty::Uint(_) | ty::Int(_) | ty::Bool => true,
_ => false,
}
};

match place.layout.ty.kind() {
ty::FnDef(..) => Some(ty::ValTree::zst()),
ty::Bool | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Char => {
Expand All @@ -90,19 +106,91 @@ fn const_to_valtree_inner<'tcx>(
// Technically we could allow function pointers (represented as `ty::Instance`), but this is not guaranteed to
// agree with runtime equality tests.
ty::FnPtr(_) | ty::RawPtr(_) => None,
ty::Ref(..) => unimplemented!("need to use deref_const"),

ty::Ref(_, ref_ty, _) if place.layout.ty.is_slice() => {
match ecx.try_read_immediate_from_mplace(&place) {
b-naber marked this conversation as resolved.
Show resolved Hide resolved
Ok(Some(imm)) => {
// `imm` is a ScalarPair. We try to get the underlying bytes behind that
// fat pointer for string slices and slices of integer types. For any other
// slice types we use `branches` to recursively construct the Valtree.

if use_bytes_for_ref(*ref_ty) {
b-naber marked this conversation as resolved.
Show resolved Hide resolved
let (alloc, range) = ecx.get_alloc_from_imm_scalar_pair(imm);
let alloc_bytes = match alloc.get_bytes(&ecx.tcx, range) {
Ok(bytes) => bytes,
Err(_e) => return None,
};
debug!(?alloc_bytes);
b-naber marked this conversation as resolved.
Show resolved Hide resolved

let bytes = ecx.tcx.arena.alloc_slice(alloc_bytes);
let len = bytes.len();
debug!(?bytes, ?len);

let slice = ty::ValSlice { bytes};

Some(ty::ValTree::SliceOrStr(slice))
} else {
let derefd = ecx.deref_operand(&imm.into()).expect(&format!("couldnt deref {:?}", imm));
debug!("derefd: {:?}", derefd);

let derefd_imm = match ecx.try_read_immediate_from_mplace(&derefd) {
Ok(Some(imm)) => imm,
_ => return None,
};
debug!(?derefd_imm);

let tcx = ecx.tcx.tcx;
let scalar_len= derefd.meta.unwrap_meta();
let len = match scalar_len {
Scalar::Int(int) => {
int.try_to_machine_usize(tcx).expect(&format!("Expected a valid ScalarInt in {:?}", scalar_len))
}
_ => bug!("expected a ScalarInt in meta data for {:?}", place),
};
debug!(?len);

let valtree = branches(ecx, place, len.try_into().expect("BLA"), None);
debug!(?valtree);

valtree
}
}
_ => {
None
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, now the Ref case looks like I hoped it would. :)


ty::Ref(_, inner_ty, _) => {
debug!("Ref with inner_ty: {:?}", inner_ty);
let imm = ecx.try_read_immediate_from_mplace(&place).unwrap_or_else(|e| bug!("couldnt read immediate from {:?}, error: {:?}", place, e));
b-naber marked this conversation as resolved.
Show resolved Hide resolved
match imm {
Some(imm) => {
debug!(?imm);

let derefd_place = ecx.deref_mplace(place).unwrap_or_else(|e| bug!("couldn't deref {:?}, error: {:?}", place, e));
debug!(?derefd_place);

const_to_valtree_inner(ecx, &derefd_place)
}
None => None,
}
}
ty::Str => {
bug!("ty::Str should have been handled in ty::Ref branch that uses raw bytes");
}
ty::Slice(_) => {
bug!("should have been handled in the Ref arm");
}

// Trait objects are not allowed in type level constants, as we have no concept for
// resolving their backing type, even if we can do that at const eval time. We may
// hypothetically be able to allow `dyn StructuralEq` trait objects in the future,
// but it is unclear if this is useful.
ty::Dynamic(..) => None,

ty::Slice(_) | ty::Str => {
unimplemented!("need to find the backing data of the slice/str and recurse on that")
}
ty::Tuple(substs) => branches(substs.len(), None),
ty::Array(_, len) => branches(usize::try_from(len.eval_usize(ecx.tcx.tcx, ecx.param_env)).unwrap(), None),
ty::Tuple(substs) => branches(ecx, place, substs.len(), None),
ty::Array(_, len) => branches(ecx, place, usize::try_from(len.eval_usize(ecx.tcx.tcx, ecx.param_env)).unwrap(), None),
b-naber marked this conversation as resolved.
Show resolved Hide resolved

ty::Adt(def, _) => {
if def.variants().is_empty() {
Expand All @@ -111,7 +199,7 @@ fn const_to_valtree_inner<'tcx>(

let variant = ecx.read_discriminant(&place.into()).unwrap().1;

branches(def.variant(variant).fields.len(), def.is_enum().then_some(variant))
branches(ecx, place, def.variant(variant).fields.len(), def.is_enum().then_some(variant))
}

ty::Never
Expand Down
37 changes: 33 additions & 4 deletions compiler/rustc_const_eval/src/interpret/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use rustc_target::abi::{Abi, HasDataLayout, Size, TagEncoding};
use rustc_target::abi::{VariantIdx, Variants};

use super::{
alloc_range, from_known_layout, mir_assign_valid_types, AllocId, ConstValue, GlobalId,
InterpCx, InterpResult, MPlaceTy, Machine, MemPlace, Place, PlaceTy, Pointer, Provenance,
Scalar, ScalarMaybeUninit,
alloc_range, from_known_layout, mir_assign_valid_types, AllocId, AllocRange, Allocation,
ConstValue, GlobalId, InterpCx, InterpResult, MPlaceTy, Machine, MemPlace, Place, PlaceTy,
Pointer, Provenance, Scalar, ScalarMaybeUninit,
};

/// An `Immediate` represents a single immediate self-contained Rust value.
Expand Down Expand Up @@ -248,7 +248,7 @@ impl<'tcx, Tag: Provenance> ImmTy<'tcx, Tag> {
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
/// Try reading an immediate in memory; this is interesting particularly for `ScalarPair`.
/// Returns `None` if the layout does not permit loading this as a value.
fn try_read_immediate_from_mplace(
pub(crate) fn try_read_immediate_from_mplace(
&self,
mplace: &MPlaceTy<'tcx, M::PointerTag>,
) -> InterpResult<'tcx, Option<ImmTy<'tcx, M::PointerTag>>> {
Expand Down Expand Up @@ -777,3 +777,32 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
})
}
}

impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx, PointerTag = AllocId>> InterpCx<'mir, 'tcx, M> {
pub fn get_alloc_from_imm_scalar_pair(
b-naber marked this conversation as resolved.
Show resolved Hide resolved
&self,
imm: ImmTy<'tcx, M::PointerTag>,
) -> (&Allocation, AllocRange) {
match imm.imm {
Immediate::ScalarPair(a, b) => {
// We know `offset` is relative to the allocation, so we can use `into_parts`.
let (data, start) = match self.scalar_to_ptr(a.check_init().unwrap()).into_parts() {
(Some(alloc_id), offset) => {
(self.tcx.global_alloc(alloc_id).unwrap_memory(), offset.bytes())
}
(None, _offset) => (
self.tcx.intern_const_alloc(Allocation::from_bytes_byte_aligned_immutable(
b"" as &[u8],
)),
0,
),
};
let len = b.to_machine_usize(self).unwrap();
let size = Size::from_bytes(len);
let start = Size::from_bytes(start);
(data.inner(), AllocRange { start, size })
}
_ => bug!("{:?} not a ScalarPair", imm),
}
}
}
12 changes: 12 additions & 0 deletions compiler/rustc_const_eval/src/interpret/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,18 @@ where
Ok(mplace)
}

#[instrument(skip(self), level = "debug")]
pub fn deref_mplace(
b-naber marked this conversation as resolved.
Show resolved Hide resolved
&self,
src: &MPlaceTy<'tcx, M::PointerTag>,
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
let val = self.try_read_immediate_from_mplace(src)?;
let mplace = self.ref_to_mplace(&val.unwrap())?;
self.check_mplace_access(mplace, CheckInAllocMsg::DerefTest)?;

Ok(mplace)
}

#[inline]
pub(super) fn get_alloc(
&self,
Expand Down