-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #134030 - folkertdev:min-fn-align, r=workingjubilee
add `-Zmin-function-alignment` 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](#128830): > i.e. the equivalents of `-fmin-function-alignment` ([GCC](https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-fmin-function-alignment_003dn), Clang does not support it) / `-falign-functions` ([GCC](https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-falign-functions), [Clang](https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang1-falign-functions)). > > 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)`](#82232), 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
- Loading branch information
Showing
8 changed files
with
145 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/doc/unstable-book/src/compiler-flags/min-function-alignment.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# `min-function-alignment` | ||
|
||
The tracking issue for this feature is: https://github.com/rust-lang/rust/issues/82232. | ||
|
||
------------------------ | ||
|
||
The `-Zmin-function-alignment=<align>` flag specifies the minimum alignment of functions for which code is generated. | ||
The `align` value must be a power of 2, other values are rejected. | ||
|
||
Note that `-Zbuild-std` (or similar) is required to apply this minimum alignment to standard library functions. | ||
By default, these functions come precompiled and their alignments won't respect the `min-function-alignment` flag. | ||
|
||
This flag is equivalent to: | ||
|
||
- `-fmin-function-alignment` for [GCC](https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-fmin-function-alignment_003dn) | ||
- `-falign-functions` for [Clang](https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang1-falign-functions) | ||
|
||
The specified alignment is a minimum. A higher alignment can be specified for specific functions by using the [`repr(align(...))`](https://github.com/rust-lang/rust/issues/82232) feature and annotating the function with a `#[repr(align(<align>))]` attribute. The attribute's value is ignored when it is lower than the value passed to `min-function-alignment`. | ||
|
||
There are two additional edge cases for this flag: | ||
|
||
- targets have a minimum alignment for functions (e.g. on x86_64 the lowest that LLVM generates is 16 bytes). | ||
A `min-function-alignment` value lower than the target's minimum has no effect. | ||
- the maximum alignment supported by rust (and LLVM) is `2^29`. Trying to set a higher value results in an error. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//@ revisions: align16 align1024 | ||
//@ compile-flags: -C no-prepopulate-passes -Z mir-opt-level=0 | ||
//@ [align16] compile-flags: -Zmin-function-alignment=16 | ||
//@ [align1024] compile-flags: -Zmin-function-alignment=1024 | ||
|
||
#![crate_type = "lib"] | ||
#![feature(fn_align)] | ||
|
||
// functions without explicit alignment use the global minimum | ||
// | ||
// CHECK-LABEL: @no_explicit_align | ||
// align16: align 16 | ||
// align1024: align 1024 | ||
#[no_mangle] | ||
pub fn no_explicit_align() {} | ||
|
||
// CHECK-LABEL: @lower_align | ||
// align16: align 16 | ||
// align1024: align 1024 | ||
#[no_mangle] | ||
#[repr(align(8))] | ||
pub fn lower_align() {} | ||
|
||
// the higher value of min-function-alignment and repr(align) wins out | ||
// | ||
// CHECK-LABEL: @higher_align | ||
// align16: align 32 | ||
// align1024: align 1024 | ||
#[no_mangle] | ||
#[repr(align(32))] | ||
pub fn higher_align() {} | ||
|
||
// cold functions follow the same rules as other functions | ||
// | ||
// in GCC, the `-falign-functions` does not apply to cold functions, but | ||
// `-Zmin-function-alignment` applies to all functions. | ||
// | ||
// CHECK-LABEL: @no_explicit_align_cold | ||
// align16: align 16 | ||
// align1024: align 1024 | ||
#[no_mangle] | ||
#[cold] | ||
pub fn no_explicit_align_cold() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//@ compile-flags: -C no-prepopulate-passes -Copt-level=0 -Zmin-function-alignment=16 | ||
//@ needs-asm-support | ||
//@ ignore-arm no "ret" mnemonic | ||
|
||
#![feature(naked_functions, fn_align)] | ||
#![crate_type = "lib"] | ||
|
||
// functions without explicit alignment use the global minimum | ||
// | ||
// CHECK: .balign 16 | ||
#[no_mangle] | ||
#[naked] | ||
pub unsafe extern "C" fn naked_no_explicit_align() { | ||
core::arch::naked_asm!("ret") | ||
} | ||
|
||
// CHECK: .balign 16 | ||
#[no_mangle] | ||
#[repr(align(8))] | ||
#[naked] | ||
pub unsafe extern "C" fn naked_lower_align() { | ||
core::arch::naked_asm!("ret") | ||
} | ||
|
||
// CHECK: .balign 32 | ||
#[no_mangle] | ||
#[repr(align(32))] | ||
#[naked] | ||
pub unsafe extern "C" fn naked_higher_align() { | ||
core::arch::naked_asm!("ret") | ||
} | ||
|
||
// cold functions follow the same rules as other functions | ||
// | ||
// in GCC, the `-falign-functions` does not apply to cold functions, but | ||
// `-Zmin-function-alignment` applies to all functions. | ||
// | ||
// CHECK: .balign 16 | ||
#[no_mangle] | ||
#[cold] | ||
#[naked] | ||
pub unsafe extern "C" fn no_explicit_align_cold() { | ||
core::arch::naked_asm!("ret") | ||
} |