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

remove pref_align_of intrinsic #90877

Closed
wants to merge 2 commits into from

Conversation

RalfJung
Copy link
Member

This intrinsic has never been stably exposed, and its existence occasionally causes confusion. Preferred alignment still exists as a concept inside the compiler that it might use e.g. for aligning globals, but there seems to be no good reason to expose it to clients. Also see this discussion on Zulip.

@rust-highfive
Copy link
Collaborator

Some changes occured to rustc_codegen_cranelift

cc @bjorn3

Some changes occured to the CTFE / Miri engine

cc @rust-lang/miri

@rust-highfive
Copy link
Collaborator

r? @jackh726

(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 Nov 13, 2021
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@nbdd0121
Copy link
Contributor

The Zulip thread does mention that this is used by c2rust though.

@RalfJung
Copy link
Member Author

RalfJung commented Nov 13, 2021

Good point, I pinged the person who wrote that on Zulip (not sure about their GH handle).

AFAIK we usually do remove features that have no path to stabilization. This one does not even have a tracking issue (to my knowledge) -- the least we should have if anyone actually needs this.

@RalfJung
Copy link
Member Author

However, https://github.com/immunant/c2rust/search?q=pref_align_to turns up no results.

@tavianator
Copy link
Contributor

@nagisa
Copy link
Member

nagisa commented Nov 13, 2021

Hm, so it seems like the __alignof/__alignof__/_alignof/__builtin_alignof keyword in clang returns this preferred alignment rather than the ABI alignment, whereas the _Alignof and alignof keywordsreturn the ABI alignment. With that in mind, it does look lie c2rust's use of the intrinsic is well warranted.

@RalfJung
Copy link
Member Author

https://github.com/immunant/c2rust/search?q=pref_align_of

oops

Hm, so it seems like the __alignof/alignof/_alignof/__builtin_alignof keyword in clang returns this preferred alignment rather than the ABI alignment, whereas the _Alignof and alignof keywordsreturn the ABI alignment. With that in mind, it does look lie c2rust's use of the intrinsic is well warranted.

By that argument, we should aim to have a function in mem that exposes this intrinsic, with an accompanying tracking issue. Using a perma-unstable feature (intrinsics) with no path towards stabilization is certainly not a solution.

@RalfJung
Copy link
Member Author

RalfJung commented Nov 14, 2021

Interesting, back in 2014, mem::align_of returned pref_align_of: https://github.com/rust-lang/rust/pull/14259/files#diff-6181b218ab2b0db797bea38c28defa9697bbb91d80a2b56b22c51015730e3ae6R79. This was changed in #25646. Ever since that PR in 2015, pref_align_of was inaccessible except via perma-unstable features.

@nagisa
Copy link
Member

nagisa commented Nov 14, 2021

Ah yeah, I meant warranted in a sense that “this will produce the expected result” and less so a justification for use of an intrinsic as an implementation strategy. None of the mechanisms to obtain the preferred alignment in C are standard either, though, so c2rust could consider not supporting preferred alignment too.

@nbdd0121
Copy link
Contributor

I assume there will be pre-C11 code that uses __alignof because there're not standard counterparts.

@RalfJung
Copy link
Member Author

Since this concerns intrinsics, Cc @rust-lang/lang

@scottmcm
Copy link
Member

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.

(I have no opinion right now on whether it should be exposed.)

@RalfJung
Copy link
Member Author

With my lang hat on, I consider intrinsics entirely a compiler-team question so long as they're not otherwise exposed.

Well, they are unstably 'exposed' and the main concern here is breaking c2rust which uses one of those intrinsics that are not really but de-facto sufficiently exposed.

@apiraino apiraino added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Nov 18, 2021
@jackh726
Copy link
Member

Even though I'm not really familiar with this intrinsic, my thoughts are: 1) What is/was its original purpose? 2) Is that purpose no longer needed or is it solved some other way? 3) If it isn't solved some other way, should it be? 4) How often is this actually used/how much breakage do we expect?

@jackh726
Copy link
Member

@RalfJung what do you think the next step here is?

I don't feel comfortable approving this alone (at least not without the questions above answered, maybe also with lang FCP given this is unstably exposed).

@RalfJung
Copy link
Member Author

Looks like some team needs to decide if we want to keep an unexposed un-RFCd no-tracking-issue intrinsic around because it has a downstream user. I don't think this is a good situation. If we didn't already have it, we certainly would not add it like that. Ideally, someone who needs that intrinsic would work towards exposing it, unstably at least.

@scottmcm scottmcm added the I-lang-nominated Nominated for discussion during a lang team meeting. label Nov 30, 2021
@scottmcm
Copy link
Member

It definitely doesn't need an FCP, since it's a reversible decision that doesn't affect stable.

I've nominated for lang to try to get unstuck.

@RalfJung
Copy link
Member Author

Cc @thedataking for c2rust input.

@thedataking
Copy link

Appreciate the ping @RalfJung. The code which may emit this intrinsic was added to support LLVM 8 (commit).

The LLVM source code for getPreferredTypeAlign mentions that:

despite the name, the preferred alignment is ABI-impacting, and not an optimization.

Which sounds like we'd lose the ability to correctly transpile some code if preferred alignment is no longer exposed; we would have to emit an error if the input source uses this intrinsic.

cc @rinon @ahomescu

@tavianator
Copy link
Contributor

Here are the relevant GCC bug reports about this:

I'm not sure exactly the sense in which the preferred alignment is ABI impacting. It will of course affect the ABI of things like char buffer[__alignof__(double)]. I thing GCC/clang will also align global/stack variables to the preferred alignment, and perhaps accesses to them will assume that alignment.

Emitting an error on __alignof__() seems too restrictive, there's lots of code using it. Maybe c2rust could compile it as if it were _Alignof() and raise a warning?

@jackh726 jackh726 added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 2, 2021
@joshtriplett
Copy link
Member

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.

@RalfJung
Copy link
Member Author

Okay, I opened a tracking issue at #91971 then.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 15, 2021
…lacrum

link to pref_align_of tracking issue

If we are not going to remove this intrinsic (rust-lang#90877), I think we should at least have a place to centralize discussion around it, so here we go. Intrinsics don't have their own separate features and usually we instead use the public method for tracking it, but this one does not have such a method... so the tracking issue is just a regular link. (And then we sue it for the const part as well.)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 16, 2021
…lacrum

link to pref_align_of tracking issue

If we are not going to remove this intrinsic (rust-lang#90877), I think we should at least have a place to centralize discussion around it, so here we go. Intrinsics don't have their own separate features and usually we instead use the public method for tracking it, but this one does not have such a method... so the tracking issue is just a regular link. (And then we sue it for the const part as well.)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 16, 2021
…lacrum

link to pref_align_of tracking issue

If we are not going to remove this intrinsic (rust-lang#90877), I think we should at least have a place to centralize discussion around it, so here we go. Intrinsics don't have their own separate features and usually we instead use the public method for tracking it, but this one does not have such a method... so the tracking issue is just a regular link. (And then we sue it for the const part as well.)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 16, 2021
…lacrum

link to pref_align_of tracking issue

If we are not going to remove this intrinsic (rust-lang#90877), I think we should at least have a place to centralize discussion around it, so here we go. Intrinsics don't have their own separate features and usually we instead use the public method for tracking it, but this one does not have such a method... so the tracking issue is just a regular link. (And then we sue it for the const part as well.)
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Feb 18, 2025
In rust-lang#90877 (comment)
T-lang decided they did not wish to remove `intrinsics::pref_align_of`.
However, the intrinsic
1. is a nightly feature, so can be removed at compiler/libs discretion
2. requires considerable effort in the compiler to support
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[^1],
   in ways Rust would not interact with, even with increased C++ interop
4. the AIX XLC compiler's layout rules for C have unrelated concerns[^2]
5. otherwise it is used to pad static layout[^3], not correctness
6. these static align-up clauses can be replaced by other rules
7. there is only one clear benefactor we are aware of:
   tools automating C -> Rust translation handling GNU extensions[^4].
8. because the GNU extension is a "false friend" of a standard C form,
   `alignof` or `_Alignof`, the C code likely meant `mem::align_of`,
   which makes the choice to support such a mapping very questionable
9. its presence makes it easy to do incorrect codegen with the compiler,
   as Rust rules regarding alignment (e.g. size == align * N) do not hold
   with preferred alignment, and this actually happened[^5]

Thus remove the intrinsic and supporting code, as a cost-benefit analysis
cannot possibly justify going to such lengths and risking correctness
just to help automated translation of an ill-considered GNU extension.

[^1]: cite AIX cookie layout
[^2]: cite AIX "power alignment" issues
[^3]: cite static alignment usages
[^4]: cite c2rust commit
[^5]: cite bjorn3's PR
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Feb 18, 2025
In rust-lang#90877 (comment)
T-lang decided they did not wish to remove `intrinsics::pref_align_of`.
However, the intrinsic
1. is a nightly feature, so can be removed at compiler/libs discretion
2. requires considerable effort in the compiler to support
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[^1],
   in ways Rust would not interact with, even with increased C++ interop
4. the AIX XLC compiler's layout rules for C have unrelated concerns[^2]
5. otherwise it is used to pad static layout[^3], not correctness
6. these static align-up clauses can be replaced by other rules
7. there is only one clear benefactor we are aware of:
   tools automating C -> Rust translation handling GNU extensions[^4].
8. because the GNU extension is a "false friend" of a standard C form,
   `alignof` or `_Alignof`, the C code likely meant `mem::align_of`,
   which makes the choice to support such a mapping very questionable
9. its presence makes it easy to do incorrect codegen with the compiler,
   as Rust rules regarding alignment (e.g. size == align * N) do not hold
   with preferred alignment, and this actually happened[^5]

Thus remove the intrinsic and supporting code, as a cost-benefit analysis
cannot possibly justify going to such lengths and risking correctness
just to help automated translation of an ill-considered GNU extension.

[^1]: cite AIX cookie layout
[^2]: cite AIX "power alignment" issues
[^3]: cite static alignment usages
[^4]: cite c2rust commit
[^5]: cite bjorn3's PR
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Feb 18, 2025
In rust-lang#90877 (comment)
T-lang decided they did not wish 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
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[^1],
    in ways Rust would not consider even with increased C++ interop
4.  is dwarfed by unrelated concerns regarding the AIX XLC compiler's
    layout rules for C which make it difficult to support[^2]
5.  is only used by rustc to pad global layouts[^3], not correctness
6.  can be replaced by other rules concerning globals
7.  has only one clear benefactor: automating C -> Rust translation
    for GNU extensions[^4] like `__alignof`.
8.  was likely intended to be `alignof`, `_Alignof`, AKA `mem::align_of`,
    because the GNU extensions were "false friends" of Standard C forms,
    which makes the choice to support such a mapping very questionable
9.  makes it easy to do incorrect codegen with the compiler by its mere
    presence, as usual Rust rules of alignment (e.g. size == align * N)
    do not hold with preferred alignment aand this actually happened[^5]

Thus remove the intrinsic and supporting code, as a cost-benefit analysis
cannot possibly justify going to such lengths and risking correctness
just to help automated translation of an ill-considered GNU extension.

[^1]: cite AIX cookie layout
[^2]: cite AIX "power alignment" issues
[^3]: cite static alignment usages
[^4]: cite c2rust commit
[^5]: cite bjorn3's PR
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Feb 18, 2025
In rust-lang#90877 (comment)
T-lang decided they did not wish 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
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[^1],
    in ways Rust would not consider even with increased C++ interop
4.  is dwarfed by unrelated concerns regarding the AIX XLC compiler's
    layout rules for C which make it difficult to support[^2]
5.  is only used by rustc to pad global layouts[^3], not correctness
6.  can be replaced by other rules concerning globals, as it only affects
    alignments for a few types under 16 bytes of alignment
7.  has only one clear benefactor: automating C -> Rust translation
    for GNU extensions[^4] like `__alignof`
8.  was likely intended to be `alignof`, `_Alignof`, AKA `mem::align_of`,
    because the GNU extensions were "false friends" of Standard C forms,
    which makes the choice to support such a mapping very questionable
9.  makes it easy to do incorrect codegen with the compiler by its mere
    presence, as usual Rust rules of alignment (e.g. size == align * N)
    do not hold with preferred alignment[^5]

Thus remove the intrinsic and supporting code, as a cost-benefit analysis
cannot possibly justify going to such lengths and risking correctness
just to help automated translation of an ill-considered GNU extension.

[^1]: cite AIX cookie layout
[^2]: cite AIX "power alignment" issues
[^3]: cite static alignment usages
[^4]: cite c2rust commit
[^5]: cite bjorn3's PR
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Feb 18, 2025
In rust-lang#90877 (comment)
T-lang decided they did not wish 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[^1],
    in ways Rust would not consider even with increased C++ interop
4.  is dwarfed by unrelated concerns regarding the AIX XLC compiler's
    layout rules for C which make it difficult to support[^2]
5.  is only used by rustc to pad global layouts[^3], not correctness
6.  can be adequately replaced by other rules for globals, as it mostly
    affects alignments for a few types under 16 bytes of alignment
7.  has only one clear benefactor: automating C -> Rust translation
    for GNU extensions[^4] like `__alignof`
8.  was likely intended to be `alignof`, `_Alignof`, AKA `mem::align_of`,
    because the GNU extensions were "false friends" of Standard C forms,
    which makes the choice to support such a mapping very questionable
9.  makes it easy to do incorrect codegen with the compiler by its mere
    presence, as usual Rust rules of alignment (e.g. size == align * N)
    do not hold with preferred alignment[^5]

Thus remove the intrinsic and supporting code, as a cost-benefit analysis
cannot possibly justify going to such lengths and risking correctness
just to help automated translation of an ill-considered GNU extension.

[^1]: cite AIX cookie layout
[^2]: cite AIX "power alignment" issues
[^3]: cite static alignment usages
[^4]: cite c2rust commit
[^5]: cite bjorn3's PR
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Feb 18, 2025
In rust-lang#90877 (comment)
T-lang decided they did not wish 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[^1],
    in ways Rust would not consider even with increased C++ interop
4.  is overshadowed by the AIX XLC compiler's "power alignment" rules
    which make even "C" compatibility difficult to support[^2]
5.  is only used by rustc to pad global layouts[^3], not correctness
6.  can be adequately replaced by other rules for globals, as it mostly
    affects alignments for a few types under 16 bytes of alignment
7.  has only one clear benefactor: automating C -> Rust translation
    for GNU extensions[^4] like `__alignof`
8.  was likely intended to be `alignof`, `_Alignof`, AKA `mem::align_of`,
    because the GNU extensions were "false friends" of Standard C forms,
    which makes the choice to support such a mapping very questionable
9.  makes it easy to do incorrect codegen with the compiler by its mere
    presence, as usual Rust rules of alignment (e.g. size == align * N)
    do not hold with preferred alignment[^5]

Thus remove the intrinsic and supporting code, as a cost-benefit analysis
cannot possibly justify going to such lengths and risking correctness
just to help automated translation of an ill-considered GNU extension.

[^1]: cite AIX cookie layout
[^2]: cite AIX "power alignment" issues
[^3]: cite static alignment usages
[^4]: cite c2rust commit
[^5]: cite bjorn3's PR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-lang-nominated Nominated for discussion during a lang team meeting. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). 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.