Skip to content

Commit 9fb0416

Browse files
committed
stabilize -Cmin-function-aligmnemt
1 parent d01b56e commit 9fb0416

File tree

10 files changed

+37
-39
lines changed

10 files changed

+37
-39
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,10 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
491491
let allocated_pointer = AttributeKind::AllocatedPointer.create_attr(cx.llcx);
492492
attributes::apply_to_llfn(llfn, AttributePlace::Argument(0), &[allocated_pointer]);
493493
}
494-
// function alignment can be set globally with the `-Zmin-function-alignment=<n>` flag;
494+
// function alignment can be set globally with the `-Cmin-function-alignment=<n>` flag;
495495
// the alignment from a `#[repr(align(<n>))]` is used if it specifies a higher alignment.
496496
if let Some(align) =
497-
Ord::max(cx.tcx.sess.opts.unstable_opts.min_function_alignment, codegen_fn_attrs.alignment)
497+
Ord::max(cx.tcx.sess.opts.cg.min_function_alignment, codegen_fn_attrs.alignment)
498498
{
499499
llvm::set_alignment(llfn, align);
500500
}

compiler/rustc_codegen_ssa/src/mir/naked_asm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ fn prefix_and_suffix<'tcx>(
131131
let attrs = tcx.codegen_fn_attrs(instance.def_id());
132132
let link_section = attrs.link_section.map(|symbol| symbol.as_str().to_string());
133133

134-
// function alignment can be set globally with the `-Zmin-function-alignment=<n>` flag;
134+
// Function alignment can be set globally with the `-Cmin-function-alignment=<n>` flag;
135135
// the alignment from a `#[repr(align(<n>))]` is used if it specifies a higher alignment.
136136
// if no alignment is specified, an alignment of 4 bytes is used.
137-
let min_function_alignment = tcx.sess.opts.unstable_opts.min_function_alignment;
137+
let min_function_alignment = tcx.sess.opts.cg.min_function_alignment;
138138
let align_bytes =
139139
Ord::max(min_function_alignment, attrs.alignment).map(|a| a.bytes()).unwrap_or(4);
140140

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,10 +877,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
877877
if let Some(fn_val) = self.get_fn_alloc(id) {
878878
let align = match fn_val {
879879
FnVal::Instance(instance) => {
880-
// Function alignment can be set globally with the `-Zmin-function-alignment=<n>` flag;
880+
// Function alignment can be set globally with the `-Cmin-function-alignment=<n>` flag;
881881
// the alignment from a `#[repr(align(<n>))]` is used if it specifies a higher alignment.
882882
let fn_align = self.tcx.codegen_fn_attrs(instance.def_id()).alignment;
883-
let global_align = self.tcx.sess.opts.unstable_opts.min_function_alignment;
883+
let global_align = self.tcx.sess.opts.cg.min_function_alignment;
884884

885885
Ord::max(global_align, fn_align).unwrap_or(Align::ONE)
886886
}

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
@@ -2022,6 +2022,8 @@ options! {
20222022
"perform LLVM link-time optimizations"),
20232023
metadata: Vec<String> = (Vec::new(), parse_list, [TRACKED],
20242024
"metadata to mangle symbol names with"),
2025+
min_function_alignment: Option<Align> = (None, parse_align, [TRACKED],
2026+
"align all functions to at least this many bytes. Must be a power of 2"),
20252027
no_prepopulate_passes: bool = (false, parse_no_value, [TRACKED],
20262028
"give an empty list of passes to the pass manager"),
20272029
no_redzone: Option<bool> = (None, parse_opt_bool, [TRACKED],
@@ -2335,8 +2337,6 @@ options! {
23352337
"gather metadata statistics (default: no)"),
23362338
metrics_dir: Option<PathBuf> = (None, parse_opt_pathbuf, [UNTRACKED],
23372339
"the directory metrics emitted by rustc are dumped into (implicitly enables default set of metrics)"),
2338-
min_function_alignment: Option<Align> = (None, parse_align, [TRACKED],
2339-
"align all functions to at least this many bytes. Must be a power of 2"),
23402340
mir_emit_retag: bool = (false, parse_bool, [TRACKED],
23412341
"emit Retagging MIR statements, interpreted e.g., by miri; implies -Zmir-opt-level=0 \
23422342
(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 rust (and LLVM) is `2^29`. 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

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

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
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

@@ -32,7 +32,7 @@ pub fn higher_align() {}
3232
// cold functions follow the same rules as other functions
3333
//
3434
// in GCC, the `-falign-functions` does not apply to cold functions, but
35-
// `-Zmin-function-alignment` applies to all functions.
35+
// `-Cmin-function-alignment` applies to all functions.
3636
//
3737
// CHECK-LABEL: @no_explicit_align_cold
3838
// 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

@@ -32,7 +32,7 @@ pub extern "C" fn naked_higher_align() {
3232
// cold functions follow the same rules as other functions
3333
//
3434
// in GCC, the `-falign-functions` does not apply to cold functions, but
35-
// `-Zmin-function-alignment` applies to all functions.
35+
// `-Cmin-function-alignment` applies to all functions.
3636
//
3737
// CHECK: .balign 16
3838
#[no_mangle]

0 commit comments

Comments
 (0)