Skip to content

make -Zmin-function-alignment a target modifier #143323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2340,7 +2340,7 @@ options! {
"gather metadata statistics (default: no)"),
metrics_dir: Option<PathBuf> = (None, parse_opt_pathbuf, [UNTRACKED],
"the directory metrics emitted by rustc are dumped into (implicitly enables default set of metrics)"),
min_function_alignment: Option<Align> = (None, parse_align, [TRACKED],
min_function_alignment: Option<Align> = (None, parse_align, [TRACKED TARGET_MODIFIER],
"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 \
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/pass/fn_align.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//@compile-flags: -Zmin-function-alignment=8
//@compile-flags: -Zmin-function-alignment=8 -Cunsafe-allow-abi-mismatch=min-function-alignment
#![feature(fn_align)]

// When a function uses `align(N)`, the function address should be a multiple of `N`.
Expand Down
9 changes: 8 additions & 1 deletion tests/codegen/min-function-alignment.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
//@ add-core-stubs
//@ revisions: align16 align1024
//@ compile-flags: -C no-prepopulate-passes -Z mir-opt-level=0 -Clink-dead-code
//@ [align16] compile-flags: -Zmin-function-alignment=16
//@ [align1024] compile-flags: -Zmin-function-alignment=1024

#![crate_type = "lib"]
#![feature(no_core)]
#![feature(fn_align)]
#![crate_type = "lib"]
#![no_std]
#![no_core]

extern crate minicore;
use minicore::*;

// Functions without explicit alignment use the global minimum.
//
Expand Down
15 changes: 11 additions & 4 deletions tests/codegen/naked-fn/min-function-alignment.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
//@ add-core-stubs
//@ compile-flags: -C no-prepopulate-passes -Copt-level=0 -Zmin-function-alignment=16
//@ needs-asm-support
//@ ignore-arm no "ret" mnemonic

#![feature(no_core)]
#![feature(fn_align)]
#![crate_type = "lib"]
#![no_std]
#![no_core]

extern crate minicore;
use minicore::*;

// functions without explicit alignment use the global minimum
//
// CHECK: .balign 16
#[no_mangle]
#[unsafe(naked)]
pub extern "C" fn naked_no_explicit_align() {
core::arch::naked_asm!("ret")
naked_asm!("ret")
}

// CHECK: .balign 16
#[no_mangle]
#[align(8)]
#[unsafe(naked)]
pub extern "C" fn naked_lower_align() {
core::arch::naked_asm!("ret")
naked_asm!("ret")
}

// CHECK: .balign 32
#[no_mangle]
#[align(32)]
#[unsafe(naked)]
pub extern "C" fn naked_higher_align() {
core::arch::naked_asm!("ret")
naked_asm!("ret")
}

// cold functions follow the same rules as other functions
Expand All @@ -40,5 +47,5 @@ pub extern "C" fn naked_higher_align() {
#[cold]
#[unsafe(naked)]
pub extern "C" fn no_explicit_align_cold() {
core::arch::naked_asm!("ret")
naked_asm!("ret")
}
7 changes: 7 additions & 0 deletions tests/ui/target_modifiers/auxiliary/min_function_alignment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//@ no-prefer-dynamic
//@ compile-flags: --target aarch64-unknown-none -Zmin-function-alignment=32
//@ needs-llvm-components: aarch64

#![feature(no_core)]
#![crate_type = "rlib"]
#![no_core]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error: mixing `-Zmin-function-alignment` will cause an ABI mismatch in crate `incompatible_min_function_alignment`
Copy link
Member

@jieyouxu jieyouxu Jul 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussion: when this isn't a language guarantee, it's almost like this should be a warning/warn-by-default lint and not an error, right?

the -Zmin-function-alignment flag modifies the ABI so Rust crates compiled with different values of this flag cannot be used together safely

When this is a hint, then the current target modifier infra/error message might be a bit confusing

--> $DIR/incompatible_min_function_alignment.rs:13:1
|
LL | #![feature(no_core)]
| ^
|
= help: the `-Zmin-function-alignment` flag modifies the ABI so Rust crates compiled with different values of this flag cannot be used together safely
= note: `-Zmin-function-alignment=4` in this crate is incompatible with `-Zmin-function-alignment=32` in dependency `min_function_alignment`
= help: set `-Zmin-function-alignment=32` in this crate or `-Zmin-function-alignment=4` in `min_function_alignment`
= help: if you are sure this will not cause problems, you may use `-Cunsafe-allow-abi-mismatch=min-function-alignment` to silence this error

error: aborting due to 1 previous error

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error: mixing `-Zmin-function-alignment` will cause an ABI mismatch in crate `incompatible_min_function_alignment`
--> $DIR/incompatible_min_function_alignment.rs:13:1
|
LL | #![feature(no_core)]
| ^
|
= help: the `-Zmin-function-alignment` flag modifies the ABI so Rust crates compiled with different values of this flag cannot be used together safely
= note: unset `-Zmin-function-alignment` in this crate is incompatible with `-Zmin-function-alignment=32` in dependency `min_function_alignment`
= help: set `-Zmin-function-alignment=32` in this crate or unset `-Zmin-function-alignment` in `min_function_alignment`
= help: if you are sure this will not cause problems, you may use `-Cunsafe-allow-abi-mismatch=min-function-alignment` to silence this error

error: aborting due to 1 previous error

18 changes: 18 additions & 0 deletions tests/ui/target_modifiers/incompatible_min_function_alignment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//@ aux-build:min_function_alignment.rs
//@ compile-flags: --target aarch64-unknown-none
//@ needs-llvm-components: aarch64

//@ revisions:allow_match allow_mismatch error_mismatch error_omitted
//@[allow_match] compile-flags: -Zmin-function-alignment=32
//@[allow_mismatch] compile-flags: -Cunsafe-allow-abi-mismatch=min-function-alignment
//@[error_mismatch] compile-flags: -Zmin-function-alignment=4
//@[error_omitted] compile-flags:
//@[allow_mismatch] check-pass
//@[allow_match] check-pass

#![feature(no_core)]
//[error_mismatch,error_omitted]~^ ERROR mixing `-Zmin-function-alignment` will cause an ABI mismatch in crate `incompatible_min_function_alignment`
#![crate_type = "rlib"]
#![no_core]

extern crate min_function_alignment;
Loading