diff --git a/compiler/rustc_codegen_cranelift/src/lib.rs b/compiler/rustc_codegen_cranelift/src/lib.rs index 07ea29f3024ef..3bc1a18e46dfa 100644 --- a/compiler/rustc_codegen_cranelift/src/lib.rs +++ b/compiler/rustc_codegen_cranelift/src/lib.rs @@ -309,7 +309,7 @@ fn build_isa(sess: &Session, jit: bool) -> Arc { flags_builder.set("enable_llvm_abi_extensions", "true").unwrap(); - if let Some(align) = sess.opts.unstable_opts.min_function_alignment { + if let Some(align) = sess.opts.cg.min_function_alignment { flags_builder .set("log2_min_function_alignment", &align.bytes().ilog2().to_string()) .unwrap(); diff --git a/compiler/rustc_codegen_llvm/src/attributes.rs b/compiler/rustc_codegen_llvm/src/attributes.rs index 27fd09745ff08..8dd01cec95a4c 100644 --- a/compiler/rustc_codegen_llvm/src/attributes.rs +++ b/compiler/rustc_codegen_llvm/src/attributes.rs @@ -491,10 +491,10 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>( let allocated_pointer = AttributeKind::AllocatedPointer.create_attr(cx.llcx); attributes::apply_to_llfn(llfn, AttributePlace::Argument(0), &[allocated_pointer]); } - // function alignment can be set globally with the `-Zmin-function-alignment=` flag; - // the alignment from a `#[repr(align())]` is used if it specifies a higher alignment. + // function alignment can be set globally with the `-Cmin-function-alignment=` flag; + // the alignment from a `#[align()]` is used if it specifies a higher alignment. if let Some(align) = - Ord::max(cx.tcx.sess.opts.unstable_opts.min_function_alignment, codegen_fn_attrs.alignment) + Ord::max(cx.tcx.sess.opts.cg.min_function_alignment, codegen_fn_attrs.alignment) { llvm::set_alignment(llfn, align); } diff --git a/compiler/rustc_codegen_ssa/src/mir/naked_asm.rs b/compiler/rustc_codegen_ssa/src/mir/naked_asm.rs index 9f66457a74005..6f7bd5a301064 100644 --- a/compiler/rustc_codegen_ssa/src/mir/naked_asm.rs +++ b/compiler/rustc_codegen_ssa/src/mir/naked_asm.rs @@ -131,10 +131,10 @@ fn prefix_and_suffix<'tcx>( let attrs = tcx.codegen_fn_attrs(instance.def_id()); let link_section = attrs.link_section.map(|symbol| symbol.as_str().to_string()); - // function alignment can be set globally with the `-Zmin-function-alignment=` flag; - // the alignment from a `#[repr(align())]` is used if it specifies a higher alignment. + // Function alignment can be set globally with the `-Cmin-function-alignment=` flag; + // the alignment from a `#[align()]` is used if it specifies a higher alignment. // if no alignment is specified, an alignment of 4 bytes is used. - let min_function_alignment = tcx.sess.opts.unstable_opts.min_function_alignment; + let min_function_alignment = tcx.sess.opts.cg.min_function_alignment; let align_bytes = Ord::max(min_function_alignment, attrs.alignment).map(|a| a.bytes()).unwrap_or(4); diff --git a/compiler/rustc_const_eval/src/interpret/memory.rs b/compiler/rustc_const_eval/src/interpret/memory.rs index 36d1a413598db..a218016ac9a2c 100644 --- a/compiler/rustc_const_eval/src/interpret/memory.rs +++ b/compiler/rustc_const_eval/src/interpret/memory.rs @@ -877,10 +877,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { if let Some(fn_val) = self.get_fn_alloc(id) { let align = match fn_val { FnVal::Instance(instance) => { - // Function alignment can be set globally with the `-Zmin-function-alignment=` flag; - // the alignment from a `#[repr(align())]` is used if it specifies a higher alignment. + // Function alignment can be set globally with the `-Cmin-function-alignment=` flag; + // the alignment from a `#[align()]` is used if it specifies a higher alignment. let fn_align = self.tcx.codegen_fn_attrs(instance.def_id()).alignment; - let global_align = self.tcx.sess.opts.unstable_opts.min_function_alignment; + let global_align = self.tcx.sess.opts.cg.min_function_alignment; Ord::max(global_align, fn_align).unwrap_or(Align::ONE) } diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs index a0012b04c4f29..e0c33cc5644cf 100644 --- a/compiler/rustc_interface/src/tests.rs +++ b/compiler/rustc_interface/src/tests.rs @@ -624,6 +624,7 @@ fn test_codegen_options_tracking_hash() { tracked!(llvm_args, vec![String::from("1"), String::from("2")]); tracked!(lto, LtoCli::Fat); tracked!(metadata, vec![String::from("A"), String::from("B")]); + tracked!(min_function_alignment, Some(Align::EIGHT)); tracked!(no_prepopulate_passes, true); tracked!(no_redzone, Some(true)); tracked!(no_vectorize_loops, true); @@ -818,7 +819,6 @@ fn test_unstable_options_tracking_hash() { tracked!(location_detail, LocationDetail { file: true, line: false, column: false }); tracked!(maximal_hir_to_mir_coverage, true); tracked!(merge_functions, Some(MergeFunctions::Disabled)); - tracked!(min_function_alignment, Some(Align::EIGHT)); tracked!(mir_emit_retag, true); tracked!(mir_enable_passes, vec![("DestProp".to_string(), false)]); tracked!(mir_opt_level, Some(4)); diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 7fef942525b96..9cad7517996a3 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -811,7 +811,7 @@ mod desc { pub(crate) const parse_wasm_c_abi: &str = "`spec`"; pub(crate) const parse_mir_include_spans: &str = "either a boolean (`yes`, `no`, `on`, `off`, etc), or `nll` (default: `nll`)"; - pub(crate) const parse_align: &str = "a number that is a power of 2 between 1 and 2^29"; + pub(crate) const parse_align: &str = "a number that is a power of 2 between 1 and 8192"; } pub mod parse { @@ -1925,6 +1925,12 @@ pub mod parse { return false; } + // Limit the alignment to 8192 (i.e. 0x2000, or 1 << 13) bytes. It is the highest function + // alignment that works on all target platforms. COFF does not support higher alignments. + if bytes > 8192 { + return false; + } + let Ok(align) = Align::from_bytes(bytes) else { return false; }; @@ -2014,6 +2020,8 @@ options! { "perform LLVM link-time optimizations"), metadata: Vec = (Vec::new(), parse_list, [TRACKED], "metadata to mangle symbol names with"), + min_function_alignment: Option = (None, parse_align, [TRACKED], + "align all functions to at least this many bytes. Must be a power of 2"), no_prepopulate_passes: bool = (false, parse_no_value, [TRACKED], "give an empty list of passes to the pass manager"), no_redzone: Option = (None, parse_opt_bool, [TRACKED], @@ -2327,8 +2335,6 @@ options! { "gather metadata statistics (default: no)"), metrics_dir: Option = (None, parse_opt_pathbuf, [UNTRACKED], "the directory metrics emitted by rustc are dumped into (implicitly enables default set of metrics)"), - min_function_alignment: Option = (None, parse_align, [TRACKED], - "align all functions to at least this many bytes. Must be a power of 2"), mir_emit_retag: bool = (false, parse_bool, [TRACKED], "emit Retagging MIR statements, interpreted e.g., by miri; implies -Zmir-opt-level=0 \ (default: no)"), diff --git a/src/doc/rustc/src/codegen-options/index.md b/src/doc/rustc/src/codegen-options/index.md index bb109adf76f41..20937aaba93e3 100644 --- a/src/doc/rustc/src/codegen-options/index.md +++ b/src/doc/rustc/src/codegen-options/index.md @@ -346,6 +346,28 @@ opt-level=0`](#opt-level)). That is: See also [linker-plugin-lto](#linker-plugin-lto) for cross-language LTO. +## min-function-alignment + +The `-Cmin-function-alignment=` 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 annotating the function with a `#[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 this flag is `8192`. Trying to set a higher value results in an error. + ## metadata This option allows you to control the metadata used for symbol mangling. This diff --git a/src/doc/unstable-book/src/compiler-flags/min-function-alignment.md b/src/doc/unstable-book/src/compiler-flags/min-function-alignment.md deleted file mode 100644 index 03e576e3e300f..0000000000000 --- a/src/doc/unstable-book/src/compiler-flags/min-function-alignment.md +++ /dev/null @@ -1,24 +0,0 @@ -# `min-function-alignment` - -The tracking issue for this feature is: https://github.com/rust-lang/rust/issues/82232. - ------------------------- - -The `-Zmin-function-alignment=` 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 [`align(...)`](https://github.com/rust-lang/rust/issues/82232) feature and annotating the function with a `#[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. diff --git a/src/tools/miri/tests/pass/fn_align.rs b/src/tools/miri/tests/pass/fn_align.rs index 28f9299588003..6c07abc369e92 100644 --- a/src/tools/miri/tests/pass/fn_align.rs +++ b/src/tools/miri/tests/pass/fn_align.rs @@ -1,4 +1,4 @@ -//@compile-flags: -Zmin-function-alignment=8 +//@compile-flags: -Cmin-function-alignment=8 #![feature(fn_align)] // When a function uses `align(N)`, the function address should be a multiple of `N`. diff --git a/tests/codegen/min-function-alignment.rs b/tests/codegen/min-function-alignment.rs index 75f845572a4af..6e6da9eb4b8e0 100644 --- a/tests/codegen/min-function-alignment.rs +++ b/tests/codegen/min-function-alignment.rs @@ -1,7 +1,7 @@ //@ 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 +//@ [align16] compile-flags: -Cmin-function-alignment=16 +//@ [align1024] compile-flags: -Cmin-function-alignment=1024 #![crate_type = "lib"] #![feature(fn_align)] @@ -33,7 +33,7 @@ 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. +// `-Cmin-function-alignment` applies to all functions. // // CHECK-LABEL: @no_explicit_align_cold // align16: align 16 diff --git a/tests/codegen/naked-fn/min-function-alignment.rs b/tests/codegen/naked-fn/min-function-alignment.rs index 59554c1cae554..8a7cf43bcb92a 100644 --- a/tests/codegen/naked-fn/min-function-alignment.rs +++ b/tests/codegen/naked-fn/min-function-alignment.rs @@ -1,4 +1,4 @@ -//@ compile-flags: -C no-prepopulate-passes -Copt-level=0 -Zmin-function-alignment=16 +//@ compile-flags: -C no-prepopulate-passes -Copt-level=0 -Cmin-function-alignment=16 //@ needs-asm-support //@ ignore-arm no "ret" mnemonic @@ -33,7 +33,7 @@ pub extern "C" fn naked_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. +// `-Cmin-function-alignment` applies to all functions. // // CHECK: .balign 16 #[no_mangle] diff --git a/tests/ui/invalid-compile-flags/min-function-alignment.not-power-of-2.stderr b/tests/ui/invalid-compile-flags/min-function-alignment.not-power-of-2.stderr new file mode 100644 index 0000000000000..f3cb5e99a7687 --- /dev/null +++ b/tests/ui/invalid-compile-flags/min-function-alignment.not-power-of-2.stderr @@ -0,0 +1,2 @@ +error: incorrect value `3` for codegen option `min-function-alignment` - a number that is a power of 2 between 1 and 8192 was expected + diff --git a/tests/ui/invalid-compile-flags/min-function-alignment.rs b/tests/ui/invalid-compile-flags/min-function-alignment.rs new file mode 100644 index 0000000000000..8a1bc76e6b678 --- /dev/null +++ b/tests/ui/invalid-compile-flags/min-function-alignment.rs @@ -0,0 +1,6 @@ +//@ revisions: too-high not-power-of-2 +// +//@ [too-high] compile-flags: -Cmin-function-alignment=16384 +//@ [not-power-of-2] compile-flags: -Cmin-function-alignment=3 + +//~? ERROR a number that is a power of 2 between 1 and 8192 was expected diff --git a/tests/ui/invalid-compile-flags/min-function-alignment.too-high.stderr b/tests/ui/invalid-compile-flags/min-function-alignment.too-high.stderr new file mode 100644 index 0000000000000..4bc7b219c6690 --- /dev/null +++ b/tests/ui/invalid-compile-flags/min-function-alignment.too-high.stderr @@ -0,0 +1,2 @@ +error: incorrect value `16384` for codegen option `min-function-alignment` - a number that is a power of 2 between 1 and 8192 was expected +