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

impl more traits for ptr::Alignment, add mask method #115249

Merged
merged 1 commit into from
Nov 18, 2023

Conversation

clarfonthey
Copy link
Contributor

@clarfonthey clarfonthey commented Aug 26, 2023

Changes:

  • Adds rustc_const_unstable attributes where missing
  • Makes log2 method const
  • Adds mask method
  • Implements Default, which is equivalent to Alignment::MIN

No longer included in PR:

  • Removes indirection of AlignmentEnum type alias (this was intentional)
  • Implements Display, Binary, Octal, LowerHex, and UpperHex (should go through libs-api instead)
  • Controversially implements LowerExp and UpperExp using p instead of e to indicate a power of 2 (also should go through libs-api)

Tracking issue for ptr::Alignment: #102070

@rustbot
Copy link
Collaborator

rustbot commented Aug 26, 2023

r? @cuviper

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

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 26, 2023
}

impl Alignment {
// longest string: 1p-128
Copy link
Contributor Author

Choose a reason for hiding this comment

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

even though no targets have 128-bit usize, I figured I might as well future-proof it

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

#[inline]
pub const fn mask(self) -> usize {
// SAFETY: The alignment is always nonzero, and therefore decrementing won't overflow.
!(unsafe { self.as_usize().unchecked_sub(1) })
Copy link
Member

Choose a reason for hiding this comment

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

Sadly, unchecked_sub(1) is useless on unsigned types llvm/llvm-project#53377

I guess it's fine to leave it, though, since it's clearly correct. And trying to do it in isize would be UB.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, that's unfortunate. I even was considering at some point adding a decrement() method to NonZeroU* that could use unchecked_sub since it would never overflow, but I guess that that also wouldn't work.

#[unstable(feature = "ptr_alignment_type", issue = "102070")]
impl fmt::Display for Alignment {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.as_nonzero(), f)
Copy link
Member

Choose a reason for hiding this comment

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

Pondering: I have no idea when we should be including Display on types. I guess it's not a problem, but it feels like alignment isn't something I'd usually be showing to people. But showing it in binary or hex seems logical, so maybe display is ok too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that was my thought too. It's less that it's necessarily a common operation, but more that it's well-defined and not a whole lot of extra code to do. I'm fine removing it if it seems too weird.

@clarfonthey clarfonthey force-pushed the alignment branch 2 times, most recently from ac74183 to f8cd316 Compare August 27, 2023 20:10
@rust-log-analyzer

This comment has been minimized.

@scottmcm
Copy link
Member

r? @scottmcm

Would you be willing to split this change? I feel like it's got a bunch of things that are easy to take, but also some unprecedented things (as you note in the op), and I'd be happy to just merge the former to try to make progress here for you, since this has been open a while.

If you make this PR be these bits:

  • Adds rustc_const_unstable attributes where missing
  • Makes log2 method const
  • Adds mask method
  • Implements Default, which is equivalent to Alignment::MIN
    • Please write a doc comment on it saying that it's the same as MIN, though

Then I'd be happy to just r+ that, and we can leave the question of Display and Exp and such to libs-api for their opinions in another PR.

@rustbot author

@rustbot rustbot assigned scottmcm and unassigned cuviper Oct 13, 2023
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 13, 2023
@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-review Status: Awaiting review from the assignee but also interested parties. labels Oct 13, 2023
@bors
Copy link
Contributor

bors commented Oct 14, 2023

⌛ Testing commit 5dda0c9 with merge 2c4b73c...

bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 14, 2023
impl more traits for ptr::Alignment, add mask method

Changes:

* Adds `rustc_const_unstable` attributes where missing
* Makes `log2` method const
* Adds `mask` method
* Implements `Default`, which is equivalent to `Alignment::MIN`

No longer included in PR:

* Removes indirection of `AlignmentEnum` type alias (this was intentional)
* Implements `Display`, `Binary`, `Octal`, `LowerHex`, and `UpperHex` (should go through libs-api instead)
* Controversially implements `LowerExp` and `UpperExp` using `p` instead of `e` to indicate a power of 2 (also should go through libs-api)

Tracking issue for `ptr::Alignment`: rust-lang#102070
@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Oct 14, 2023

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 14, 2023
/// assert_eq!(four.mask(Alignment::of::<u8>().mask()), four);
/// assert_eq!(four.mask(Alignment::of::<u16>().mask()), four);
/// assert_eq!(four.mask(Alignment::of::<u32>().mask()), four);
/// assert_ne!(four.mask(Alignment::of::<u64>().mask()), four);
Copy link
Member

Choose a reason for hiding this comment

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

Looks like you need to not make assumptions about the alignment of primitives like this in these tests. There's no guarantee for alignof(u64), and on i686 it looks like it's 4. Maybe these demos should use Alignment::new or have specific repr(align(…)) types, rather than get them from primitives?

@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 14, 2023
@clarfonthey
Copy link
Contributor Author

I don't like it, but I updated the example to have explicitly-aligned structs.

Right now, it's operating way more as a test than an example of behaviour, but that can be resolved during stabilisation.

@clarfonthey
Copy link
Contributor Author

Ping @scottmcm when you have time to take a look at this

@Dylan-DPC Dylan-DPC added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 6, 2023
@clarfonthey
Copy link
Contributor Author

Ping @scottmcm again when you're free.

@scottmcm
Copy link
Member

Oops, thanks for the ping!

@bors r+ rollup=iffy (had a per-target failure last time)

@bors
Copy link
Contributor

bors commented Nov 18, 2023

📌 Commit 114873d has been approved by scottmcm

It is now in the queue for this repository.

@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-review Status: Awaiting review from the assignee but also interested parties. labels Nov 18, 2023
@bors
Copy link
Contributor

bors commented Nov 18, 2023

⌛ Testing commit 114873d with merge 61d3b26...

@bors
Copy link
Contributor

bors commented Nov 18, 2023

☀️ Test successful - checks-actions
Approved by: scottmcm
Pushing 61d3b26 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Nov 18, 2023
@bors bors merged commit 61d3b26 into rust-lang:master Nov 18, 2023
12 checks passed
@rustbot rustbot added this to the 1.76.0 milestone Nov 18, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (61d3b26): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

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

Max RSS (memory usage)

Results

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
Regressions ❌
(secondary)
3.9% [2.4%, 4.9%] 6
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Cycles

Results

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
Regressions ❌
(secondary)
2.4% [1.8%, 2.9%] 10
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

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

Bootstrap: 675.975s -> 678.192s (0.33%)
Artifact size: 313.82 MiB -> 313.84 MiB (0.01%)

@clarfonthey clarfonthey deleted the alignment branch November 18, 2023 16:58
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. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants