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 1 commit
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
Prev Previous commit
Next Next commit
Move provenance checks out of interning method.
  • Loading branch information
cjgillot committed Oct 25, 2023
commit ff6812cd20da84f84b2cb8540110d08bd54e8060
14 changes: 2 additions & 12 deletions compiler/rustc_const_eval/src/interpret/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ pub fn intern_const_alloc_recursive<
Ok(())
}

/// Intern `ret`, checking it references no other allocation.
/// Intern `ret`. This function assumes that `ret` references no other allocation.
#[instrument(level = "debug", skip(ecx))]
pub fn intern_const_alloc_for_constprop<
'mir,
Expand All @@ -461,17 +461,7 @@ pub fn intern_const_alloc_for_constprop<
ecx: &mut InterpCx<'mir, 'tcx, M>,
ret: &MPlaceTy<'tcx>,
) -> InterpResult<'tcx, ()> {
let Some((size, _align)) = ecx.size_and_align_of_mplace(ret)? else {
throw_inval!(ConstPropNonsense)
};

let alloc_ref = ecx.get_ptr_alloc(ret.ptr(), size)?.unwrap();
// Do not try interning a value that contains provenance.
if alloc_ref.has_provenance() {
throw_inval!(ConstPropNonsense)
}

// remove allocation
// Move allocation to `tcx`.
let alloc_id = ret.ptr().provenance.unwrap();
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
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
21 changes: 17 additions & 4 deletions compiler/rustc_mir_transform/src/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,7 @@ fn op_to_prop_const<'tcx>(
// If this constant has scalar ABI, return it as a `ConstValue::Scalar`.
if let Abi::Scalar(abi::Scalar::Initialized { .. }) = op.layout.abi
&& let Ok(scalar) = ecx.read_scalar(op)
&& scalar.try_to_int().is_ok()
{
return Some(ConstValue::Scalar(scalar));
}
Expand All @@ -840,6 +841,14 @@ fn op_to_prop_const<'tcx>(
if let Either::Left(mplace) = op.as_mplace_or_imm()
&& let MemPlaceMeta::None = mplace.meta()
cjgillot marked this conversation as resolved.
Show resolved Hide resolved
{
let (size, _align) = ecx.size_and_align_of_mplace(&mplace).ok()??;

// Do not try interning a value that contains provenance.
RalfJung marked this conversation as resolved.
Show resolved Hide resolved
let alloc_ref = ecx.get_ptr_alloc(mplace.ptr(), size).ok()??;
if alloc_ref.has_provenance() {
return None;
}

intern_const_alloc_for_constprop(ecx, &mplace).ok()?;
let pointer = mplace.ptr().into_pointer_or_addr().ok()?;
let (alloc_id, offset) = pointer.into_parts();
Expand All @@ -853,7 +862,13 @@ fn op_to_prop_const<'tcx>(
// Everything failed: create a new allocation to hold the data.
let alloc_id =
ecx.intern_with_temp_alloc(op.layout, |ecx, dest| ecx.copy_op(op, dest, false)).ok()?;
RalfJung marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

This path should be unreachable now? Why is there a fallback path here? I can't see why we'd ever want to copy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

op could be an immediate scalar pair. That wouldn't be caught by the scalar path, nor the mplace path.

Copy link
Member

Choose a reason for hiding this comment

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

Seems nicer to have a 3-way match than a series of fallbacks, then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For scalars, we may want to encode them in MIR as a ConstValue::Scalar, even if the interpreter backs it by a MPlace.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah sure. What I dislike is to have this look like "try A; try B; try C", I'd much rather have a very clear case distinction showing when we use which case. In particular since the code currently completely mixes up checks that are actually needed and errors that can never happen and certainly shouldn't lead to a fallback.

So you can do something like

  • first check if let Abi::Scalar(abi::Scalar::Initialized { .. }) = op.layout.abi. In that case always use ConstValue::Scalar, never fall back to anything else, with a comment explaining why we are doing this.
  • otherwise match on op.as_mplace_or_imm(), and if you have an mplace use the fast path, otherwise the copy, with a comment explaining this can happen for ScalarPair and ideally we'd have a more efficient representation for them but currently we don't (Cc Further possibilities for mir::Constant cleanup #115877)

Some(ConstValue::Indirect { alloc_id, offset: Size::ZERO })
let value = ConstValue::Indirect { alloc_id, offset: Size::ZERO };

if !value.has_provenance(*ecx.tcx, op.layout.size) {
Copy link
Member

Choose a reason for hiding this comment

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

Seems easier to check the allocation, given that you have an alloc_id?

Also please add comments explaining that this is just a temporary hack to work around #79738.

return Some(value);
}

None
}

impl<'tcx> VnState<'_, 'tcx> {
Expand Down Expand Up @@ -905,9 +920,7 @@ impl<'tcx> VnState<'_, 'tcx> {

// Check that we do not leak a pointer.
// Those pointers may lose part of their identity in codegen.
if value.has_provenance(self.tcx, op.layout.size) {
return None;
}
assert!(!value.has_provenance(self.tcx, op.layout.size));

let const_ = Const::Val(value, op.layout.ty);
Some(ConstOperand { span: rustc_span::DUMMY_SP, user_ty: None, const_ })
Expand Down