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

Extend the alignment check to borrows #137940

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

1c3t3a
Copy link
Member

@1c3t3a 1c3t3a commented Mar 3, 2025

The current alignment check does not include checks for creating misaligned references from raw pointers, which is now added in this patch.

When inserting the check we need to be careful with references to field projections (e.g. &(*ptr).a), in which case the resulting reference must be aligned according to the field type and not the type of the pointer.

r? @saethlin

cc @RalfJung, after our discussion in #134424

The current alignment check does not include checks for creating
misaligned references from raw pointers, which is now added in this
patch.

When inserting the check we need to be careful with references to
field projections (e.g. `&(*ptr).a`), in which case the resulting
reference must be aligned according to the field type and not the
type of the pointer.
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 3, 2025
@rustbot
Copy link
Collaborator

rustbot commented Mar 3, 2025

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@RalfJung
Copy link
Member

When inserting the check we need to be careful with references to field projections (e.g. &(*ptr).a), in which case the resulting reference must be aligned according to the field type and not the type of the pointer.

Yes, that is the most subtle part. Please add a test ensuring we do not complain in a case where ptr: *const Align8Struct, then we do &(*ptr.u8_field), and the ptr points to an odd address.

@1c3t3a
Copy link
Member Author

1c3t3a commented Mar 10, 2025

We actually already have a test for this (@saethlin added an amazing test-suite for this check): https://github.com/rust-lang/rust/blob/2b285cd5f0877e30ad1d83e04f8cc46254e43391/tests/ui/mir/alignment/place_computation.rs. I rename it as part of this PR to make it more clear what we actually test here in the context of the new pass.

@saethlin
Copy link
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 11, 2025
@bors
Copy link
Contributor

bors commented Mar 11, 2025

⌛ Trying commit d3ef125 with merge a7ce54d...

bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 11, 2025
Extend the alignment check to borrows

The current alignment check does not include checks for creating misaligned references from raw pointers, which is now added in this patch.

When inserting the check we need to be careful with references to field projections (e.g. `&(*ptr).a`), in which case the resulting reference must be aligned according to the field type and not the type of the pointer.

r? `@saethlin`

cc `@RalfJung,` after our discussion in rust-lang#134424
@bors
Copy link
Contributor

bors commented Mar 11, 2025

☀️ Try build successful - checks-actions
Build commit: a7ce54d (a7ce54db9b096bd1098d07c645b489195a667c9f)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (a7ce54d): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results (primary 1.8%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
4.4% [1.3%, 7.9%] 5
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.5% [-2.7%, -2.4%] 3
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.8% [-2.7%, 7.9%] 8

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

Results (primary 0.1%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.1% [0.0%, 0.5%] 35
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.2% [-0.4%, -0.0%] 6
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.1% [-0.4%, 0.5%] 41

Bootstrap: 782.898s -> 782.134s (-0.10%)
Artifact size: 365.21 MiB -> 365.22 MiB (0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 11, 2025
@1c3t3a
Copy link
Member Author

1c3t3a commented Mar 11, 2025

The perf results look good I think, especially compared to the added null-check. What do you think @saethlin, @RalfJung?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants