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

add -Zmin-function-alignment #134030

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

Conversation

folkertdev
Copy link
Contributor

tracking issue: #82232

This PR adds the -Zmin-function-alignment=<align> flag, that specifies a minimum alignment for all* functions.

Motivation

This feature is requested by RfL here:

i.e. the equivalents of -fmin-function-alignment (GCC, Clang does not support it) / -falign-functions (GCC, Clang).

For the Linux kernel, the behavior wanted is that of GCC's -fmin-function-alignment and Clang's -falign-functions, i.e. align all functions, including cold functions.

There is feature(fn_align), but we need to do it globally.

Behavior

The fn_align feature does not have an RFC. It was decided at the time that it would not be necessary, but maybe we feel differently about that now? In any case, here are the semantics of this flag:

  • -Zmin-function-alignment=<align> specifies the minimum alignment of all* functions
  • the #[repr(align(<align>))] attribute can be used to override the function alignment on a per-function basis: when -Zmin-function-alignment is specified, the attribute's value is only used when it is higher than the value passed to -Zmin-function-alignment.
  • the target may decide to use a higher value (e.g. on x86_64 the minimum that LLVM generates is 16)
  • The highest supported alignment in rust is 2^29: I checked a bunch of targets, and they all emit the .p2align 29 directive for targets that align functions at all (some GPU stuff does not have function alignment).

*: Only with build-std would the minimum alignment also be applied to std functions.


cc @ojeda

r? @workingjubilee you were active on the tracking issue

@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 Dec 8, 2024
@workingjubilee
Copy link
Member

Yes, the 2^29 alignment is a limit imposed by LLVM.

@folkertdev
Copy link
Contributor Author

Yeah what I can't quite figure out is when targets actually support that (e.g. on embedded targets, you probably don't have enough space for .p2align 29). Anyway I guess we're doing the best we can here, and you could already use repr(align(1 << 29)) on these targets and get a broken program.

Also I just added support for #[naked] functions. They emit their own .balign directive directly and therefore need custom logic and testing.

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.

3 participants