Skip to content

Commit 2010f9a

Browse files
committed
stabilize -Cmin-function-aligmnent
1 parent ae18cc4 commit 2010f9a

File tree

12 files changed

+37
-39
lines changed

12 files changed

+37
-39
lines changed

compiler/rustc_codegen_cranelift/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ fn build_isa(sess: &Session, jit: bool) -> Arc<dyn TargetIsa + 'static> {
309309

310310
flags_builder.set("enable_llvm_abi_extensions", "true").unwrap();
311311

312-
if let Some(align) = sess.opts.unstable_opts.min_function_alignment {
312+
if let Some(align) = sess.opts.cg.min_function_alignment {
313313
flags_builder
314314
.set("log2_min_function_alignment", &align.bytes().ilog2().to_string())
315315
.unwrap();

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
450450

451451
// Apply the minimum function alignment here, so that individual backends don't have to.
452452
codegen_fn_attrs.alignment =
453-
Ord::max(codegen_fn_attrs.alignment, tcx.sess.opts.unstable_opts.min_function_alignment);
453+
Ord::max(codegen_fn_attrs.alignment, tcx.sess.opts.cg.min_function_alignment);
454454

455455
let inline_span;
456456
(codegen_fn_attrs.inline, inline_span) = if let Some((inline_attr, span)) =

compiler/rustc_interface/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@ fn test_codegen_options_tracking_hash() {
624624
tracked!(llvm_args, vec![String::from("1"), String::from("2")]);
625625
tracked!(lto, LtoCli::Fat);
626626
tracked!(metadata, vec![String::from("A"), String::from("B")]);
627+
tracked!(min_function_alignment, Some(Align::EIGHT));
627628
tracked!(no_prepopulate_passes, true);
628629
tracked!(no_redzone, Some(true));
629630
tracked!(no_vectorize_loops, true);
@@ -818,7 +819,6 @@ fn test_unstable_options_tracking_hash() {
818819
tracked!(location_detail, LocationDetail { file: true, line: false, column: false });
819820
tracked!(maximal_hir_to_mir_coverage, true);
820821
tracked!(merge_functions, Some(MergeFunctions::Disabled));
821-
tracked!(min_function_alignment, Some(Align::EIGHT));
822822
tracked!(mir_emit_retag, true);
823823
tracked!(mir_enable_passes, vec![("DestProp".to_string(), false)]);
824824
tracked!(mir_opt_level, Some(4));

compiler/rustc_session/src/options.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,6 +2020,8 @@ options! {
20202020
"perform LLVM link-time optimizations"),
20212021
metadata: Vec<String> = (Vec::new(), parse_list, [TRACKED],
20222022
"metadata to mangle symbol names with"),
2023+
min_function_alignment: Option<Align> = (None, parse_align, [TRACKED],
2024+
"align all functions to at least this many bytes. Must be a power of 2"),
20232025
no_prepopulate_passes: bool = (false, parse_no_value, [TRACKED],
20242026
"give an empty list of passes to the pass manager"),
20252027
no_redzone: Option<bool> = (None, parse_opt_bool, [TRACKED],
@@ -2333,8 +2335,6 @@ options! {
23332335
"gather metadata statistics (default: no)"),
23342336
metrics_dir: Option<PathBuf> = (None, parse_opt_pathbuf, [UNTRACKED],
23352337
"the directory metrics emitted by rustc are dumped into (implicitly enables default set of metrics)"),
2336-
min_function_alignment: Option<Align> = (None, parse_align, [TRACKED],
2337-
"align all functions to at least this many bytes. Must be a power of 2"),
23382338
mir_emit_retag: bool = (false, parse_bool, [TRACKED],
23392339
"emit Retagging MIR statements, interpreted e.g., by miri; implies -Zmir-opt-level=0 \
23402340
(default: no)"),

src/doc/rustc/src/codegen-options/index.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,28 @@ opt-level=0`](#opt-level)). That is:
346346

347347
See also [linker-plugin-lto](#linker-plugin-lto) for cross-language LTO.
348348

349+
## min-function-alignment
350+
351+
The `-Cmin-function-alignment=<align>` flag specifies the minimum alignment of functions for which code is generated.
352+
The `align` value must be a power of 2, other values are rejected.
353+
354+
Note that `-Zbuild-std` (or similar) is required to apply this minimum alignment to standard library functions.
355+
By default, these functions come precompiled and their alignments won't respect the `min-function-alignment` flag.
356+
357+
This flag is equivalent to:
358+
359+
- `-fmin-function-alignment` for [GCC](https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-fmin-function-alignment_003dn)
360+
- `-falign-functions` for [Clang](https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang1-falign-functions)
361+
362+
The specified alignment is a minimum. A higher alignment can be specified for specific functions by annotating the function with a `#[align(<align>)]` attribute.
363+
The attribute's value is ignored when it is lower than the value passed to `min-function-alignment`.
364+
365+
There are two additional edge cases for this flag:
366+
367+
- targets have a minimum alignment for functions (e.g. on x86_64 the lowest that LLVM generates is 16 bytes).
368+
A `min-function-alignment` value lower than the target's minimum has no effect.
369+
- the maximum alignment supported by this flag is `8192`. Trying to set a higher value results in an error.
370+
349371
## metadata
350372

351373
This option allows you to control the metadata used for symbol mangling. This

src/doc/unstable-book/src/compiler-flags/min-function-alignment.md

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/tools/miri/tests/pass/fn_align.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@compile-flags: -Zmin-function-alignment=8
1+
//@compile-flags: -Cmin-function-alignment=8
22
#![feature(fn_align)]
33

44
// When a function uses `align(N)`, the function address should be a multiple of `N`.

tests/codegen/min-function-alignment.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@ revisions: align16 align1024
22
//@ compile-flags: -C no-prepopulate-passes -Z mir-opt-level=0 -Clink-dead-code
3-
//@ [align16] compile-flags: -Zmin-function-alignment=16
4-
//@ [align1024] compile-flags: -Zmin-function-alignment=1024
3+
//@ [align16] compile-flags: -Cmin-function-alignment=16
4+
//@ [align1024] compile-flags: -Cmin-function-alignment=1024
55

66
#![crate_type = "lib"]
77
#![feature(fn_align)]
@@ -35,7 +35,7 @@ pub fn higher_align() {}
3535
// cold functions follow the same rules as other functions
3636
//
3737
// in GCC, the `-falign-functions` does not apply to cold functions, but
38-
// `-Zmin-function-alignment` applies to all functions.
38+
// `-Cmin-function-alignment` applies to all functions.
3939
//
4040
// CHECK-LABEL: @no_explicit_align_cold
4141
// align16: align 16

tests/codegen/naked-fn/min-function-alignment.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ compile-flags: -C no-prepopulate-passes -Copt-level=0 -Zmin-function-alignment=16
1+
//@ compile-flags: -C no-prepopulate-passes -Copt-level=0 -Cmin-function-alignment=16
22
//@ needs-asm-support
33
//@ ignore-arm no "ret" mnemonic
44

@@ -33,7 +33,7 @@ pub extern "C" fn naked_higher_align() {
3333
// cold functions follow the same rules as other functions
3434
//
3535
// in GCC, the `-falign-functions` does not apply to cold functions, but
36-
// `-Zmin-function-alignment` applies to all functions.
36+
// `-Cmin-function-alignment` applies to all functions.
3737
//
3838
// CHECK: .balign 16
3939
#[no_mangle]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
error: incorrect value `3` for unstable option `min-function-alignment` - a number that is a power of 2 between 1 and 8192 was expected
1+
error: incorrect value `3` for codegen option `min-function-alignment` - a number that is a power of 2 between 1 and 8192 was expected
22

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ revisions: too-high not-power-of-2
22
//
3-
//@ [too-high] compile-flags: -Zmin-function-alignment=16384
4-
//@ [not-power-of-2] compile-flags: -Zmin-function-alignment=3
3+
//@ [too-high] compile-flags: -Cmin-function-alignment=16384
4+
//@ [not-power-of-2] compile-flags: -Cmin-function-alignment=3
55

66
//~? ERROR a number that is a power of 2 between 1 and 8192 was expected
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
error: incorrect value `16384` for unstable option `min-function-alignment` - a number that is a power of 2 between 1 and 8192 was expected
1+
error: incorrect value `16384` for codegen option `min-function-alignment` - a number that is a power of 2 between 1 and 8192 was expected
22

0 commit comments

Comments
 (0)