Skip to content

Commit 3613a1f

Browse files
committed
stabilize -Cmin-function-aligmnemt
1 parent ae9e03c commit 3613a1f

File tree

10 files changed

+38
-40
lines changed

10 files changed

+38
-40
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,10 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
475475
let allocated_pointer = AttributeKind::AllocatedPointer.create_attr(cx.llcx);
476476
attributes::apply_to_llfn(llfn, AttributePlace::Argument(0), &[allocated_pointer]);
477477
}
478-
// function alignment can be set globally with the `-Zmin-function-alignment=<n>` flag;
478+
// function alignment can be set globally with the `-Cmin-function-alignment=<n>` flag;
479479
// the alignment from a `#[repr(align(<n>))]` is used if it specifies a higher alignment.
480480
if let Some(align) =
481-
Ord::max(cx.tcx.sess.opts.unstable_opts.min_function_alignment, codegen_fn_attrs.alignment)
481+
Ord::max(cx.tcx.sess.opts.cg.min_function_alignment, codegen_fn_attrs.alignment)
482482
{
483483
llvm::set_alignment(llfn, align);
484484
}

compiler/rustc_codegen_ssa/src/mir/naked_asm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ fn prefix_and_suffix<'tcx>(
121121
let attrs = tcx.codegen_fn_attrs(instance.def_id());
122122
let link_section = attrs.link_section.map(|symbol| symbol.as_str().to_string());
123123

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

compiler/rustc_const_eval/src/interpret/memory.rs

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

883883
Ord::max(global_align, fn_align).unwrap_or(Align::ONE)
884884
}

compiler/rustc_interface/src/tests.rs

+1-1
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);
@@ -815,7 +816,6 @@ fn test_unstable_options_tracking_hash() {
815816
tracked!(location_detail, LocationDetail { file: true, line: false, column: false });
816817
tracked!(maximal_hir_to_mir_coverage, true);
817818
tracked!(merge_functions, Some(MergeFunctions::Disabled));
818-
tracked!(min_function_alignment, Some(Align::EIGHT));
819819
tracked!(mir_emit_retag, true);
820820
tracked!(mir_enable_passes, vec![("DestProp".to_string(), false)]);
821821
tracked!(mir_opt_level, Some(4));

compiler/rustc_session/src/options.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2004,6 +2004,8 @@ options! {
20042004
"perform LLVM link-time optimizations"),
20052005
metadata: Vec<String> = (Vec::new(), parse_list, [TRACKED],
20062006
"metadata to mangle symbol names with"),
2007+
min_function_alignment: Option<Align> = (None, parse_align, [TRACKED],
2008+
"align all functions to at least this many bytes. Must be a power of 2"),
20072009
no_prepopulate_passes: bool = (false, parse_no_value, [TRACKED],
20082010
"give an empty list of passes to the pass manager"),
20092011
no_redzone: Option<bool> = (None, parse_opt_bool, [TRACKED],
@@ -2309,8 +2311,6 @@ options! {
23092311
"gather metadata statistics (default: no)"),
23102312
metrics_dir: Option<PathBuf> = (None, parse_opt_pathbuf, [UNTRACKED],
23112313
"the directory metrics emitted by rustc are dumped into (implicitly enables default set of metrics)"),
2312-
min_function_alignment: Option<Align> = (None, parse_align, [TRACKED],
2313-
"align all functions to at least this many bytes. Must be a power of 2"),
23142314
mir_emit_retag: bool = (false, parse_bool, [TRACKED],
23152315
"emit Retagging MIR statements, interpreted e.g., by miri; implies -Zmir-opt-level=0 \
23162316
(default: no)"),

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

+22
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,28 @@ opt-level=0`](#opt-level)). That is:
331331

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

334+
## min-function-alignment
335+
336+
The `-Cmin-function-alignment=<align>` flag specifies the minimum alignment of functions for which code is generated.
337+
The `align` value must be a power of 2, other values are rejected.
338+
339+
Note that `-Zbuild-std` (or similar) is required to apply this minimum alignment to standard library functions.
340+
By default, these functions come precompiled and their alignments won't respect the `min-function-alignment` flag.
341+
342+
This flag is equivalent to:
343+
344+
- `-fmin-function-alignment` for [GCC](https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-fmin-function-alignment_003dn)
345+
- `-falign-functions` for [Clang](https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang1-falign-functions)
346+
347+
The specified alignment is a minimum. A higher alignment can be specified for specific functions by annotating the function with a `#[repr(align(<align>))]` attribute.
348+
The attribute's value is ignored when it is lower than the value passed to `min-function-alignment`.
349+
350+
There are two additional edge cases for this flag:
351+
352+
- targets have a minimum alignment for functions (e.g. on x86_64 the lowest that LLVM generates is 16 bytes).
353+
A `min-function-alignment` value lower than the target's minimum has no effect.
354+
- the maximum alignment supported by rust (and LLVM) is `2^29`. Trying to set a higher value results in an error.
355+
334356
## metadata
335357

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

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

-24
This file was deleted.

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

+2-2
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 `repr(align(N))`, the function address should be a multiple of `N`.
44

@@ -15,6 +15,6 @@ fn main() {
1515
assert!((foo as usize).is_multiple_of(256));
1616
assert!((bar as usize).is_multiple_of(16));
1717

18-
// The maximum of `repr(align(N))` and `-Zmin-function-alignment=N` is used.
18+
// The maximum of `repr(align(N))` and `-Cmin-function-alignment=N` is used.
1919
assert!((baz as usize).is_multiple_of(8));
2020
}

tests/codegen/min-function-alignment.rs

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

+2-2
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)