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

See through aggregates in GVN #116270

Merged
merged 28 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d284059
Do not remove unused definitions inside GVN.
cjgillot Sep 16, 2023
afd631c
Do not visit rvalues twice.
cjgillot Sep 23, 2023
38c86b0
Evaluate computed values to constants.
cjgillot Sep 19, 2023
db9bd9b
Do not intern too large aggregates.
cjgillot Sep 23, 2023
9389373
Do not transmute immediates to non-immediates.
cjgillot Sep 23, 2023
692e528
Simplify projections in GVN.
cjgillot Sep 16, 2023
48d2157
Simplify aggregate projections.
cjgillot Sep 16, 2023
f110f22
Simplify repeat expressions.
cjgillot Sep 16, 2023
23d4857
Do not compute actual aggregate type.
cjgillot May 3, 2023
80a5e85
Extract simplify_aggregate.
cjgillot May 21, 2023
dbf9ea3
Transform large arrays into Repeat expressions when possible.
cjgillot Sep 23, 2023
8162dc2
Do not intern GVN temps.
cjgillot Oct 3, 2023
ebc87bf
Directly intern values instead of copying them.
cjgillot Oct 7, 2023
ff6812c
Move provenance checks out of interning method.
cjgillot Oct 10, 2023
fbf0a0c
Explain why we check variant equality.
cjgillot Oct 11, 2023
59235a7
Fortify transmute check.
cjgillot Oct 12, 2023
e3538d1
Do not require absence of metadata.
cjgillot Oct 12, 2023
f08dc9b
Take an AllocId in intern_const_alloc_for_constprop.
cjgillot Oct 12, 2023
5e78b9c
Disambiguate non-deterministic constants.
cjgillot Oct 14, 2023
f6aa3ee
Complete comments.
cjgillot Oct 14, 2023
50559ce
Valtrees for primitive types are fine.
cjgillot Oct 14, 2023
ac0228d
FileCheck gvn.
cjgillot Oct 16, 2023
c4cc9ca
Do not merge fn pointer casts.
cjgillot Oct 21, 2023
d80eb3a
Verify that the alloc_id is Memory.
cjgillot Oct 22, 2023
eda1928
Typo.
cjgillot Oct 23, 2023
72f0e0e
Rename has_provance and tweaks comments.
cjgillot Oct 23, 2023
8561618
Directly check provenance from the AllocId.
cjgillot Oct 23, 2023
24be433
Apply suggestions from code review
cjgillot Oct 27, 2023
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
11 changes: 6 additions & 5 deletions compiler/rustc_const_eval/src/interpret/discriminant.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Functions for reading and writing discriminants of multi-variant layouts (enums and coroutines).

use rustc_middle::ty::layout::{LayoutOf, PrimitiveExt, TyAndLayout};
use rustc_middle::{mir, ty};
use rustc_middle::mir;
use rustc_middle::ty::layout::{LayoutOf, PrimitiveExt};
use rustc_middle::ty::{self, Ty};
use rustc_target::abi::{self, TagEncoding};
use rustc_target::abi::{VariantIdx, Variants};

Expand Down Expand Up @@ -244,11 +245,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {

pub fn discriminant_for_variant(
&self,
layout: TyAndLayout<'tcx>,
ty: Ty<'tcx>,
variant: VariantIdx,
) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> {
let discr_layout = self.layout_of(layout.ty.discriminant_ty(*self.tcx))?;
let discr_value = match layout.ty.discriminant_for_variant(*self.tcx, variant) {
let discr_layout = self.layout_of(ty.discriminant_ty(*self.tcx))?;
let discr_value = match ty.discriminant_for_variant(*self.tcx, variant) {
Some(discr) => {
// This type actually has discriminants.
assert_eq!(discr.ty, discr_layout.ty);
Expand Down
36 changes: 36 additions & 0 deletions compiler/rustc_const_eval/src/interpret/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,42 @@ pub fn intern_const_alloc_recursive<
Ok(())
}

/// Intern `ret`. This function assumes that `ret` references no other allocation.
#[instrument(level = "debug", skip(ecx))]
pub fn intern_const_alloc_for_constprop<
'mir,
'tcx: 'mir,
T,
M: CompileTimeMachine<'mir, 'tcx, T>,
>(
ecx: &mut InterpCx<'mir, 'tcx, M>,
alloc_id: AllocId,
) -> InterpResult<'tcx, ()> {
// Move allocation to `tcx`.
let Some((_, mut alloc)) = ecx.memory.alloc_map.remove(&alloc_id) else {
// Pointer not found in local memory map. It is either a pointer to the global
// map, or dangling.
if ecx.tcx.try_get_global_alloc(alloc_id).is_none() {
throw_ub!(DeadLocal)
}
// The constant is already in global memory. Do nothing.
return Ok(());
};

alloc.mutability = Mutability::Not;

// We are not doing recursive interning, so we don't currently support provenance.
// (If this assertion ever triggers, we should just implement a
// proper recursive interning loop.)
assert!(alloc.provenance().ptrs().is_empty());

// Link the alloc id to the actual allocation
let alloc = ecx.tcx.mk_const_alloc(alloc);
ecx.tcx.set_alloc_id_memory(alloc_id, alloc);

Ok(())
}

impl<'mir, 'tcx: 'mir, M: super::intern::CompileTimeMachine<'mir, 'tcx, !>>
InterpCx<'mir, 'tcx, M>
{
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/interpret/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
sym::discriminant_value => {
let place = self.deref_pointer(&args[0])?;
let variant = self.read_discriminant(&place)?;
let discr = self.discriminant_for_variant(place.layout, variant)?;
let discr = self.discriminant_for_variant(place.layout.ty, variant)?;
self.write_immediate(*discr, dest)?;
}
sym::exact_div => {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/interpret/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ impl<'tcx, 'a, Prov: Provenance, Extra, Bytes: AllocBytes> AllocRef<'a, 'tcx, Pr
}

/// Returns whether the allocation has provenance anywhere in the range of the `AllocRef`.
pub(crate) fn has_provenance(&self) -> bool {
pub fn has_provenance(&self) -> bool {
!self.alloc.provenance().range_empty(self.range, &self.tcx)
}
}
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_const_eval/src/interpret/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ mod visitor;
pub use rustc_middle::mir::interpret::*; // have all the `interpret` symbols in one place: here

pub use self::eval_context::{Frame, FrameInfo, InterpCx, StackPopCleanup};
pub use self::intern::{intern_const_alloc_recursive, InternKind};
pub use self::intern::{
intern_const_alloc_for_constprop, intern_const_alloc_recursive, InternKind,
};
pub use self::machine::{compile_time_machine, AllocMap, Machine, MayLeak, StackPopJump};
pub use self::memory::{AllocKind, AllocRef, AllocRefMut, FnVal, Memory, MemoryKind};
pub use self::operand::{ImmTy, Immediate, OpTy, Readable};
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_const_eval/src/interpret/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ impl<'tcx, Prov: Provenance> ImmTy<'tcx, Prov> {
ImmTy { imm: val.into(), layout }
}

#[inline]
pub fn from_scalar_pair(a: Scalar<Prov>, b: Scalar<Prov>, layout: TyAndLayout<'tcx>) -> Self {
debug_assert!(
matches!(layout.abi, Abi::ScalarPair(..)),
"`ImmTy::from_scalar_pair` on non-scalar-pair layout"
);
let imm = Immediate::ScalarPair(a, b);
ImmTy { imm, layout }
}

#[inline(always)]
pub fn from_immediate(imm: Immediate<Prov>, layout: TyAndLayout<'tcx>) -> Self {
debug_assert!(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/interpret/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
Discriminant(place) => {
let op = self.eval_place_to_op(place, None)?;
let variant = self.read_discriminant(&op)?;
let discr = self.discriminant_for_variant(op.layout, variant)?;
let discr = self.discriminant_for_variant(op.layout.ty, variant)?;
self.write_immediate(*discr, &dest)?;
}
}
Expand Down
47 changes: 47 additions & 0 deletions compiler/rustc_middle/src/mir/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,21 @@ impl<'tcx> ConstValue<'tcx> {
let end = end.try_into().unwrap();
Some(data.inner().inspect_with_uninit_and_ptr_outside_interpreter(start..end))
}

/// Check if a constant may contain provenance information. This is used by MIR opts.
cjgillot marked this conversation as resolved.
Show resolved Hide resolved
pub fn may_have_provenance(&self, tcx: TyCtxt<'tcx>, size: Size) -> bool {
match *self {
ConstValue::ZeroSized | ConstValue::Scalar(Scalar::Int(_)) => return false,
ConstValue::Scalar(Scalar::Ptr(..)) => return true,
ConstValue::Slice { data, meta: _ } => !data.inner().provenance().ptrs().is_empty(),
cjgillot marked this conversation as resolved.
Show resolved Hide resolved
ConstValue::Indirect { alloc_id, offset } => !tcx
.global_alloc(alloc_id)
.unwrap_memory()
.inner()
.provenance()
.range_empty(super::AllocRange::from(offset..offset + size), &tcx),
}
}
}

///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -485,6 +500,38 @@ impl<'tcx> Const<'tcx> {
_ => Self::Ty(c),
}
}

/// Return true if any evaluation of this constant always returns the same value,
/// taking into account even pointer identity tests.
pub fn is_deterministic(&self) -> bool {
// Some constants may contain pointers. We need to preserve the provenance of these
// pointers, but not all constants guarantee this:
// - valtrees purposefully do not;
// - ConstValue::Slice does not either.
cjgillot marked this conversation as resolved.
Show resolved Hide resolved
match self {
Const::Ty(c) => match c.kind() {
ty::ConstKind::Param(..) => true,
// A valtree may be a reference. Valtree references correspond to a
// different allocation each time they are evaluated. Valtrees for primitive
// types are fine though.
ty::ConstKind::Value(_) => c.ty().is_primitive(),
ty::ConstKind::Unevaluated(..) | ty::ConstKind::Expr(..) => false,
// Should not appear in runtime MIR.
ty::ConstKind::Infer(..)
| ty::ConstKind::Bound(..)
| ty::ConstKind::Placeholder(..)
| ty::ConstKind::Error(..) => bug!(),
},
Const::Unevaluated(..) => false,
// If the same slice appears twice in the MIR, we cannot guarantee that we will
// give the same `AllocId` to the data.
Const::Val(ConstValue::Slice { .. }, _) => false,
Const::Val(
ConstValue::ZeroSized | ConstValue::Scalar(_) | ConstValue::Indirect { .. },
_,
) => true,
}
}
}

/// An unevaluated (potentially generic) constant used in MIR.
Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_mir_transform/src/dataflow_const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,8 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
TrackElem::Variant(idx) => self.ecx.project_downcast(op, idx).ok(),
TrackElem::Discriminant => {
let variant = self.ecx.read_discriminant(op).ok()?;
let discr_value = self.ecx.discriminant_for_variant(op.layout, variant).ok()?;
let discr_value =
self.ecx.discriminant_for_variant(op.layout.ty, variant).ok()?;
Some(discr_value.into())
}
TrackElem::DerefLen => {
Expand Down Expand Up @@ -507,7 +508,8 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
return None;
}
let enum_ty_layout = self.tcx.layout_of(self.param_env.and(enum_ty)).ok()?;
let discr_value = self.ecx.discriminant_for_variant(enum_ty_layout, variant_index).ok()?;
let discr_value =
self.ecx.discriminant_for_variant(enum_ty_layout.ty, variant_index).ok()?;
Some(discr_value.to_scalar())
}

Expand Down Expand Up @@ -854,7 +856,7 @@ impl<'tcx> Visitor<'tcx> for OperandCollector<'tcx, '_, '_, '_> {
}
}

struct DummyMachine;
pub(crate) struct DummyMachine;

impl<'mir, 'tcx: 'mir> rustc_const_eval::interpret::Machine<'mir, 'tcx> for DummyMachine {
rustc_const_eval::interpret::compile_time_machine!(<'mir, 'tcx>);
Expand Down
Loading
Loading