Skip to content

Commit 57abad8

Browse files
authored
Rollup merge of #142854 - folkertdev:centralize-min-function-alignment, r=workingjubilee
centralize `-Zmin-function-alignment` logic tracking issue: #82232 discussed in: #142824 (comment) Apply the `-Zmin-function-alignment` value to the alignment field of the function attributes when those are created, so that individual backends don't need to consider it. The one exception right now is cranelift, because it can't yet set the alignment for individual functions, but it can (and does) set the global minimum function alignment. cc ``@RalfJung`` I think this is an improvement regardless, is there anything else that should be done for miri?
2 parents 2357fb0 + a123a36 commit 57abad8

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
@@ -145,6 +145,12 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
145145
}
146146
}
147147

148+
// Apply the minimum function alignment here, so that individual backends don't have to.
149+
codegen_fn_attrs.alignment = Ord::max(
150+
codegen_fn_attrs.alignment,
151+
tcx.sess.opts.unstable_opts.min_function_alignment,
152+
);
153+
148154
let Some(Ident { name, .. }) = attr.ident() else {
149155
continue;
150156
};

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)