Skip to content

Commit a123a36

Browse files
committed
centralize -Zmin-function-alignment logic
1 parent 6d0c9e2 commit a123a36

File tree

4 files changed

+10
-17
lines changed

4 files changed

+10
-17
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,11 +491,7 @@ 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;
495-
// the alignment from a `#[repr(align(<n>))]` is used if it specifies a higher alignment.
496-
if let Some(align) =
497-
Ord::max(cx.tcx.sess.opts.unstable_opts.min_function_alignment, codegen_fn_attrs.alignment)
498-
{
494+
if let Some(align) = codegen_fn_attrs.alignment {
499495
llvm::set_alignment(llfn, align);
500496
}
501497
if let Some(backchain) = backchain_attr(cx) {

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
126126
}
127127
}
128128

129+
// Apply the minimum function alignment here, so that individual backends don't have to.
130+
codegen_fn_attrs.alignment = Ord::max(
131+
codegen_fn_attrs.alignment,
132+
tcx.sess.opts.unstable_opts.min_function_alignment,
133+
);
134+
129135
let Some(Ident { name, .. }) = attr.ident() else {
130136
continue;
131137
};

compiler/rustc_codegen_ssa/src/mir/naked_asm.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,8 @@ 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;
135-
// the alignment from a `#[repr(align(<n>))]` is used if it specifies a higher alignment.
136-
// 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;
138-
let align_bytes =
139-
Ord::max(min_function_alignment, attrs.alignment).map(|a| a.bytes()).unwrap_or(4);
134+
// If no alignment is specified, an alignment of 4 bytes is used.
135+
let align_bytes = attrs.alignment.map(|a| a.bytes()).unwrap_or(4);
140136

141137
// In particular, `.arm` can also be written `.code 32` and `.thumb` as `.code 16`.
142138
let (arch_prefix, arch_suffix) = if is_arm {

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -877,12 +877,7 @@ 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;
881-
// the alignment from a `#[repr(align(<n>))]` is used if it specifies a higher alignment.
882-
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;
884-
885-
Ord::max(global_align, fn_align).unwrap_or(Align::ONE)
880+
self.tcx.codegen_fn_attrs(instance.def_id()).alignment.unwrap_or(Align::ONE)
886881
}
887882
// Machine-specific extra functions currently do not support alignment restrictions.
888883
FnVal::Other(_) => Align::ONE,

0 commit comments

Comments
 (0)