Stabilize fn_align
: #[repr(align(N))]
on functions and -Zmin-function-alignment
#140261
+64
−113
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
tracking issue: #82232
closes #82232
Request for Stabilization
Summary
The
fn_align
feature has two parts:#[repr(align(N))]
function attribute specifies the alignment of the generated code of a particular function-Zmin-function-alignment
command line flag specifies the minimal alignment of the generated code of all compiled functions.These could be stabilized separately, but share a lot of the same logic in the compiler and are tracked by the same tracking issue.
An example of specifying function alignment:
Specifying the alignment of a specific function occasionally comes up in embedded development. For instance in risc-v vector tables, the function pointers stored in it must be aligned to at least 4 bytes (see #75072).
Specifying a global alignment is more commonly done as a performance optimization. This functionality is used in the linux kernel and this feature flag was requested by Rust for Linux #128830.
Semantics
The
-Zmin-function-alignment
flag is a global mimimum function alignment. It is semantically equivalent to annotating every function with a#[repr(align(N))]
. However, this minimum is only applied to functions that are actually codegened, so without-Zbuild-std
or similar, this minimum alignment is not applied to standard library functions.The function alignment is forwarded to the code generation backend, and behaves as follows:
2.pow(29)
.Documentation
Pending resolving some of the open questions.
Tests
-Zmin-function-alignment
flag and how it interacts with#[repr(align(N))]
.History
repr(align = x)
on inherent methods #110313-Zmin-function-alignment
#134030The
-Zmin-function-alignment
flag was requested by rust-for-linux #128830. It will be used soon (see #t-compiler/help > ✔ Alignment for function addresses).Miri supports function alignment since #140072. In const-eval there is no way to observe the address of a function pointer, so no special attention is needed there (see #t-compiler/const-eval > function address alignment).
Notes
unresolved questions
#[repr(align(N))]
the right syntax? Perhaps#[align(N)]
would be better (suggested at https://internals.rust-lang.org/t/pre-rfc-align-attribute/21004/27)-Zmin-function-alignment
flag does work. If that's a problem, should we pull apart the global and per-function alignment?#[repr(align(N))]
do for anasync fn
, maybe this should not be supported for now?r? @traviscross
@rustbot label +I-lang-nominated