Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Remove rustc's notion of "preferred" alignment AKA __alignof #141803

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
Loading
from

Conversation

workingjubilee
Copy link
Member

@workingjubilee workingjubilee commented May 31, 2025

In PR #90877 T-lang decided not to remove intrinsics::pref_align_of. However, the intrinsic and its supporting code

  1. is a nightly feature, so can be removed at compiler/libs discretion
  2. requires considerable effort in the compiler to support, as it necessarily complicates every single site reasoning about alignment
  3. has been justified based on relevance to codegen, but it is only a requirement for C++ (not C, not Rust) stack frame layout for AIX1, in ways Rust would not consider even with increased C++ interop
  4. is only used by rustc to overalign some globals, not correctness2
  5. can be adequately replaced by other rules for globals, as it mostly affects alignments for a few types under 16 bytes of alignment
  6. has only one clear beneficiary: automating C -> Rust translation for GNU extensions like __alignof3
  7. such code was likely intended to be alignof or _Alignof, because the GNU extension is a "false friend" of the C keyword, which makes the choice to support such a mapping very questionable
  8. makes it easy to do incorrect codegen in the compiler by its mere presence as usual Rust rules of alignment (e.g. size == align * N) do not hold with preferred alignment4

Despite an automated translation tool like c2rust using it, we have made multiple attempts to find a crate that actually has been committed to public repositories, like GitHub or crates.io, using such translated code. We have found none. While it is possible someone privately uses this intrinsic, it seems unlikely, and it is behind a feature gate that will warn about using the internal features of rustc.

The implementation is clearly damaging the code quality of the compiler. Thus it is within the compiler team's purview to simply rip it out. If T-lang wishes to have this intrinsic restored for c2rust's benefit, it would have to use a radically different implementation that somehow does not cause internal incorrectness.

Until then, remove the intrinsic and its supporting code, as one tool and an ill-considered GCC extension cannot justify risking correctness.

Because we touch a fair amount of the compiler to change this at all, and unfortunately the duplication of AbiAndPrefAlign is deep-rooted, we keep an "AbiAlign" type which we can wean code off later.

r? @bjorn3

Footnotes

  1. https://github.com/rust-lang/rust/issues/91971#issuecomment-2451330704

  2. as viewable in the code altered by this PR

  3. c2rust: https://github.com/immunant/c2rust/blame/3b1ec86b9b0cf363adfd3178cc45a891a970eef2/c2rust-transpile/src/translator/mod.rs#L3175

  4. https://github.com/rust-lang/rustc_codegen_cranelift/issues/1560

@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 May 31, 2025
@rustbot
Copy link
Collaborator

rustbot commented May 31, 2025

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter
gets adapted for the changes, if necessary.

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label May 31, 2025
@rustbot
Copy link
Collaborator

rustbot commented May 31, 2025

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

@@ -3489,15 +3489,6 @@ pub const fn size_of<T>() -> usize;
#[rustc_intrinsic]
pub const fn min_align_of<T>() -> usize;
Copy link
Member

Choose a reason for hiding this comment

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

Also in the future, we should then rename this to just align_of.

@RalfJung
Copy link
Member

RalfJung commented May 31, 2025

I agree with removing the intrinsic. However, the PR description is written in a style that seems somewhat antagonistic to t-lang, which is wholly unnecessary. Quoting some t-lang members (not necessarily speaking for the team) from the prior PR:

With my lang hat on, I consider intrinsics entirely a compiler-team question so long as they're not otherwise exposed. So removing something unexposed sounds fine to me, FWIW.

We discussed this both in last week's and today's @rust-lang/lang meeting. In both cases, we came to the conclusion that we're not in a hurry to remove this intrinsic, and it's not obvious that the intrinsic couldn't be stabilized in the future if it makes sense to do so, given that people are using it.

Given that, we don't want to merge this PR at this time, and we don't mind letting this sit on nightly, unless there's some other reason that we should be considering this an urgent issue.

It's not become an "urgent" issue, but the past 3 years have not shown any attempt of this moving any closer to stabilization -- and meanwhile we constantly pay the complexity cost in the compiler. An attempt at exposing this would have to include a clear idea of what "preferred alignment" even means, which is a discussion that would be good to have for a clean implementation. IMO that is enough to justify removal.

However, we could help ease the transition by marking it as deprecated for a cycle, pointing to a tracking issue so that nightly users relying on it have a chance to speak up.

@workingjubilee
Copy link
Member Author

@RalfJung To be clear, codegen backends have miscompiled Rust code by dint of this intrinsic relying on data that can undermine Rust assumptions: rust-lang/rustc_codegen_cranelift#1560

I am not sure "urgent" is the word but perhaps "should have been fixed yesterday"?

@RalfJung
Copy link
Member

I am not sure "urgent" is the word but perhaps "should have been fixed yesterday"?

"Should have been fixed some time in the last 10 years" :D (but waiting another few weeks to give people a heads-up is not a big deal)

@workingjubilee
Copy link
Member Author

I... don't know if it is indeed not a big deal?

I opened this because I remembered that I have to fix it before I do much more meaningful work on layout-related code. We've waited three years for someone to explain why they really need this and nothing has happened.

@workingjubilee
Copy link
Member Author

I would rather you just close my PR than pretend we're actually helping anyone by implementing a deprecation cycle for a feature no one really wants.

@RalfJung
Copy link
Member

All I did was raise a concern that given how long this intrinsic was around, people might be relying on it, so it might be courteous to give them a heads-up. So far, we have not given a clear signal that this will disappear. OTOH it is guarded by an "internal" feature gate so whoever was using this should be aware they are on thin ice.

If others don't think we need to be this careful, that's fine for me as well, but I think this option should at least be considered.

@jieyouxu
Copy link
Member

jieyouxu commented May 31, 2025

OTOH it is guarded by an "internal" feature gate so whoever was using this should be aware they are on thin ice.

I find this is sufficient signal to not need a deprecation cycle TBH. If the codegen reviewer agrees with removing this instrinsic, I'd say we should just rid of it especially if it's blocking further changes.

@bjorn3
Copy link
Member

bjorn3 commented May 31, 2025

The only uses on github are either declaring the intrinsics, copies of the rust repo or the sole usage inside c2rust itself for __alignof: https://github.com/search?q=%2Fpref_align_of%3A%3A%2F+-is%3Afork&type=code and https://grep.app/search?q=pref_align_of No actual user code seems to use it, so removing it immediately is fine by me.

compiler/rustc_abi/src/lib.rs Show resolved Hide resolved
compiler/rustc_ty_utils/src/layout.rs Outdated Show resolved Hide resolved
@workingjubilee
Copy link
Member Author

I'm not particularly trying to give T-lang a super-hard time here, but they made a decision, and I don't think this PR makes sense without including a rebuttal of that decision. If anyone should get an earful, it's whoever working on GCC thought it was a good idea to retain __alignof instead of harmonizing it with alignof.

@bors
Copy link
Collaborator

bors commented Jun 4, 2025

☔ The latest upstream changes (presumably #142003) made this pull request unmergeable. Please resolve the merge conflicts.

@rust-log-analyzer

This comment has been minimized.

In PR 90877 T-lang decided not to remove `intrinsics::pref_align_of`.
However, the intrinsic and its supporting code
1.  is a nightly feature, so can be removed at compiler/libs discretion
2.  requires considerable effort in the compiler to support, as it
    necessarily complicates every single site reasoning about alignment
3.  has been justified based on relevance to codegen, but it is only a
    requirement for C++ (not C, not Rust) stack frame layout for AIX,
    in ways Rust would not consider even with increased C++ interop
4.  is only used by rustc to overalign some globals, not correctness
5.  can be adequately replaced by other rules for globals, as it mostly
    affects alignments for a few types under 16 bytes of alignment
6.  has only one clear benefactor: automating C -> Rust translation
    for GNU extensions like `__alignof`
7.  such code was likely intended to be `alignof` or `_Alignof`,
    because the GNU extension is a "false friend" of the C keyword,
    which makes the choice to support such a mapping very questionable
8.  makes it easy to do incorrect codegen in the compiler by its mere
    presence as usual Rust rules of alignment (e.g. `size == align * N`)
    do not hold with preferred alignment

The implementation is clearly damaging the code quality of the compiler.
Thus it is within the compiler team's purview to simply rip it out.
If T-lang wishes to have this intrinsic restored for c2rust's benefit,
it would have to use a radically different implementation that somehow
does not cause internal incorrectness.

Until then, remove the intrinsic and its supporting code, as one tool
and an ill-considered GCC extension cannot justify risking correctness.

Because we touch a fair amount of the compiler to change this at all,
and unfortunately the duplication of AbiAndPrefAlign is deep-rooted,
we keep an "AbiAlign" type which we can wean code off later.
We will want to remove many cases of `.abi`, including `.abi.thing`,
so this may simplify future PRs and certainly doesn't hurt.

We omit DerefMut because mutation is much rarer and localized.
@workingjubilee
Copy link
Member Author

rebased because conflict and also "x.py wonky" and also pushed another commit motivated by making the future transition smoother.

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. 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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.