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

Make Allocation::bytes private #63561

Merged
merged 12 commits into from
Sep 3, 2019
Merged

Conversation

HeroicKatora
Copy link
Contributor

@HeroicKatora HeroicKatora commented Aug 14, 2019

Fixes #62931.

Direct immutable access to the bytes is still possible but redirected through the new method raw_bytes_with_undef_and_ptr, similar to get_bytes_with_undef_and_ptr but without requiring an interpretation context and not doing any relocation or bounds checks. The size of the allocation is stored separately which makes access as Size and usize more ergonomic.

cc: @RalfJung

@rust-highfive
Copy link
Collaborator

r? @cramertj

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 14, 2019
@HeroicKatora
Copy link
Contributor Author

r? @RalfJung

@rust-highfive rust-highfive assigned RalfJung and unassigned cramertj Aug 14, 2019
@HeroicKatora
Copy link
Contributor Author

Changed pub(crate) to crate by amending the first commit.

@RalfJung
Copy link
Member

I edited your OP to say "Fixes: #issue"; that will make GitHub auto-close it when the PR gets merged.

@HeroicKatora
Copy link
Contributor Author

Removed crate visibility qualifier, only required rather simple changes to the stable hashing. Also removed the portion of comments on bytes and undef_mask that were not intended to be part of this PR yet.

@HeroicKatora
Copy link
Contributor Author

The size field makes it much simpler to access the allocation extent as a Size, avoiding many additional conversions from the usize length of the byte vector. So it might be worth keeping?

Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

This looks good overall, my notes are mostly editorial / ask for more comments.

Note that I'll be on conference next week and won't have time for reviews.

/// Note that the bytes of a pointer represent the offset of the pointer
pub bytes: Vec<u8>,
/// Note that the bytes of a pointer represent the offset of the pointer.
bytes: Vec<u8>,
/// Maps from byte addresses to extra data for each pointer.
/// Only the first byte of a pointer is inserted into the map; i.e.,
/// every entry in this map applies to `pointer_size` consecutive bytes starting
/// at the given offset.
pub relocations: Relocations<Tag>,
Copy link
Member

Choose a reason for hiding this comment

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

Can you make this private and provide a read-only accessor, similar to what you did for undef_mask?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There already exists a method relocations, which returns relocations within a range specified by ptr and size, so I'd name the corresponding accessor all_relocations instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, interpret/memory.rs tries to access it directly. I'd move code in a similar manner as compressed undef mask.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not available until Wednesday next week but the rest should be ready.

Copy link
Contributor

Choose a reason for hiding this comment

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

take all the time you need

Copy link
Contributor Author

@HeroicKatora HeroicKatora Aug 29, 2019

Choose a reason for hiding this comment

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

The name fn relocations for the accessor is already taken:

/// Returns all relocations overlapping with the given ptr-offset pair.
pub fn relocations(
&self,
cx: &impl HasDataLayout,
ptr: Pointer<Tag>,
size: Size,
) -> &[(Size, (Tag, AllocId))] {

I'd rename the existing method to get_relocations which would be aligned with get_bytes, check_bytes, check_relocations to be concerned with interpreter execution.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good.
Makes me wonder who will even need the general read-only general accessor. Maybe that should also be raw_relocations or so, to make it less appealing to call?

Direct access to the bytes was previously a problem (rust-lang#62931) where
components would read their contents without properly checking
relocations and/or definedness.

Making bytes private instead of purely renaming them also helps in
allowing amendments to their allocation scheme (such as eliding
allocation for undef of constant regions).
Requires a manual implementation for Relocations since dereferencing to
SortedMap is not always implemented but that one is far more trivial.
Added fields could otherwise be silently forgotten since private fields
make destructing outside the module possible only with `..` pattern
which would then also be applicable to newly added public fields.
This also means that the compressed representation chosen may be
optimized together with any changes to the undef_mask.
@HeroicKatora
Copy link
Contributor Author

Moved the HashStable implementation into the module itself, renamed the raw accessor method, added comments for calls to it.

Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

LGTM, with the ntis fixed! However, I can't tell whether the arguments made in codegen_llvm are correct. Handing over to

r? @oli-obk

@RalfJung
Copy link
Member

r? @oli-obk

(Doesn't look like the bots looks into reviews.)

@rust-highfive rust-highfive assigned oli-obk and unassigned RalfJung Aug 31, 2019
This improves the clarity of the documentation a bit since they can
reference each other when reading the member docs in sequence.
@HeroicKatora
Copy link
Contributor Author

@oli-obk I fixed the remaining nits from Ralf now, there are some unresolved review comments where you commented and I wasn't certain they were fully addressed. And the review of codegen is still oustanding as mentioned. Status should now be waiting on review.

@oli-obk
Copy link
Contributor

oli-obk commented Sep 2, 2019

@bors r+

Oh, right. I reviewed those comments when you made them. Reviewed again, still all good.

@bors
Copy link
Contributor

bors commented Sep 2, 2019

📌 Commit f3c435e has been approved by oli-obk

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 2, 2019
@bors
Copy link
Contributor

bors commented Sep 2, 2019

⌛ Testing commit f3c435e with merge b505208...

bors added a commit that referenced this pull request Sep 2, 2019
Make Allocation::bytes private

Fixes #62931.

Direct immutable access to the bytes is still possible but redirected through the new method `raw_bytes_with_undef_and_ptr`, similar to `get_bytes_with_undef_and_ptr` but without requiring an interpretation context and not doing *any* relocation or bounds checks. The `size` of the allocation is stored separately which makes access as `Size` and `usize` more ergonomic.

cc: @RalfJung
@bors
Copy link
Contributor

bors commented Sep 3, 2019

☀️ Test successful - checks-azure
Approved by: oli-obk
Pushing b505208 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Sep 3, 2019
@bors bors merged commit f3c435e into rust-lang:master Sep 3, 2019
@rust-highfive
Copy link
Collaborator

📣 Toolstate changed by #63561!

Tested on commit b505208.
Direct link to PR: #63561

💔 clippy-driver on windows: test-pass → build-fail (cc @Manishearth @llogiq @mcarton @oli-obk @phansch @flip1995, @rust-lang/infra).
💔 clippy-driver on linux: test-pass → build-fail (cc @Manishearth @llogiq @mcarton @oli-obk @phansch @flip1995, @rust-lang/infra).
💔 miri on windows: test-pass → build-fail (cc @oli-obk @RalfJung @eddyb, @rust-lang/infra).
💔 miri on linux: test-pass → build-fail (cc @oli-obk @RalfJung @eddyb, @rust-lang/infra).

rust-highfive added a commit to rust-lang-nursery/rust-toolstate that referenced this pull request Sep 3, 2019
Tested on commit rust-lang/rust@b505208.
Direct link to PR: <rust-lang/rust#63561>

💔 clippy-driver on windows: test-pass → build-fail (cc @Manishearth @llogiq @mcarton @oli-obk @phansch @flip1995, @rust-lang/infra).
💔 clippy-driver on linux: test-pass → build-fail (cc @Manishearth @llogiq @mcarton @oli-obk @phansch @flip1995, @rust-lang/infra).
💔 miri on windows: test-pass → build-fail (cc @oli-obk @RalfJung @eddyb, @rust-lang/infra).
💔 miri on linux: test-pass → build-fail (cc @oli-obk @RalfJung @eddyb, @rust-lang/infra).
bors added a commit to rust-lang/rust-clippy that referenced this pull request Sep 3, 2019
bors added a commit that referenced this pull request Oct 8, 2019
Avoid copying some undef memory in MIR

Copying memory between allocations in MIR is not required to preserve the current representation of undef bytes. When the complete source of a copy is in undef state then there are no bytes that need to be copied and the function can return nearly immediately. In some cases this completely avoids writing to the allocation's memory with should have various benefits.

This for example reduces required physical memory when interpreting `const fn` involving large uninitialized values, such as `MaybeUninit::uninit()`.

(*This description has been changed from the original one after parts have been integrated into #63561 *)
@HeroicKatora HeroicKatora deleted the alloc-private-bytes branch March 7, 2025 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allocation::bytes should be private
7 participants