store target.min_global_align as an Align

This commit is contained in:
Folkert de Vries 2025-06-07 21:31:21 +02:00
parent 44f415c1d6
commit a50bd7ca24
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
10 changed files with 37 additions and 63 deletions

View file

@ -2,9 +2,6 @@ codegen_gcc_unknown_ctarget_feature_prefix =
unknown feature specified for `-Ctarget-feature`: `{$feature}`
.note = features must begin with a `+` to enable or `-` to disable it
codegen_gcc_invalid_minimum_alignment =
invalid minimum global alignment: {$err}
codegen_gcc_forbidden_ctarget_feature =
target feature `{$feature}` cannot be toggled with `-Ctarget-feature`: {$reason}

View file

@ -18,7 +18,6 @@ use rustc_span::def_id::DefId;
use crate::base;
use crate::context::CodegenCx;
use crate::errors::InvalidMinimumAlignment;
use crate::type_of::LayoutGccExt;
fn set_global_alignment<'gcc, 'tcx>(
@ -29,13 +28,8 @@ fn set_global_alignment<'gcc, 'tcx>(
// The target may require greater alignment for globals than the type does.
// Note: GCC and Clang also allow `__attribute__((aligned))` on variables,
// which can force it to be smaller. Rust doesn't support this yet.
if let Some(min) = cx.sess().target.min_global_align {
match Align::from_bits(min) {
Ok(min) => align = align.max(min),
Err(err) => {
cx.sess().dcx().emit_err(InvalidMinimumAlignment { err: err.to_string() });
}
}
if let Some(min_global) = cx.sess().target.min_global_align {
align = Ord::max(align, min_global);
}
gv.set_alignment(align.bytes() as i32);
}

View file

@ -47,12 +47,6 @@ pub(crate) struct UnwindingInlineAsm {
pub span: Span,
}
#[derive(Diagnostic)]
#[diag(codegen_gcc_invalid_minimum_alignment)]
pub(crate) struct InvalidMinimumAlignment {
pub err: String,
}
#[derive(Diagnostic)]
#[diag(codegen_gcc_copy_bitcode)]
pub(crate) struct CopyBitcode {

View file

@ -19,12 +19,6 @@ codegen_llvm_from_llvm_diag = {$message}
codegen_llvm_from_llvm_optimization_diag = {$filename}:{$line}:{$column} {$pass_name} ({$kind}): {$message}
codegen_llvm_invalid_minimum_alignment_not_power_of_two =
invalid minimum global alignment: {$align} is not power of 2
codegen_llvm_invalid_minimum_alignment_too_large =
invalid minimum global alignment: {$align} is too large
codegen_llvm_load_bitcode = failed to load bitcode of module "{$name}"
codegen_llvm_load_bitcode_with_llvm_err = failed to load bitcode of module "{$name}": {$llvm_err}

View file

@ -1,8 +1,6 @@
use std::ops::Range;
use rustc_abi::{
Align, AlignFromBytesError, HasDataLayout, Primitive, Scalar, Size, WrappingRange,
};
use rustc_abi::{Align, HasDataLayout, Primitive, Scalar, Size, WrappingRange};
use rustc_codegen_ssa::common;
use rustc_codegen_ssa::traits::*;
use rustc_hir::LangItem;
@ -20,9 +18,7 @@ use rustc_middle::{bug, span_bug};
use tracing::{debug, instrument, trace};
use crate::common::{AsCCharPtr, CodegenCx};
use crate::errors::{
InvalidMinimumAlignmentNotPowerOfTwo, InvalidMinimumAlignmentTooLarge, SymbolAlreadyDefined,
};
use crate::errors::SymbolAlreadyDefined;
use crate::llvm::{self, True};
use crate::type_::Type;
use crate::type_of::LayoutLlvmExt;
@ -149,22 +145,10 @@ fn set_global_alignment<'ll>(cx: &CodegenCx<'ll, '_>, gv: &'ll Value, mut align:
// The target may require greater alignment for globals than the type does.
// Note: GCC and Clang also allow `__attribute__((aligned))` on variables,
// which can force it to be smaller. Rust doesn't support this yet.
if let Some(min) = cx.sess().target.min_global_align {
match Align::from_bits(min) {
Ok(min) => align = align.max(min),
Err(err) => match err {
AlignFromBytesError::NotPowerOfTwo(align) => {
cx.sess().dcx().emit_err(InvalidMinimumAlignmentNotPowerOfTwo { align });
}
AlignFromBytesError::TooLarge(align) => {
cx.sess().dcx().emit_err(InvalidMinimumAlignmentTooLarge { align });
}
},
}
}
unsafe {
llvm::LLVMSetAlignment(gv, align.bytes() as u32);
if let Some(min_global) = cx.sess().target.min_global_align {
align = Ord::max(align, min_global);
}
llvm::set_alignment(gv, align);
}
fn check_and_apply_linkage<'ll, 'tcx>(

View file

@ -57,18 +57,6 @@ pub(crate) struct SymbolAlreadyDefined<'a> {
pub symbol_name: &'a str,
}
#[derive(Diagnostic)]
#[diag(codegen_llvm_invalid_minimum_alignment_not_power_of_two)]
pub(crate) struct InvalidMinimumAlignmentNotPowerOfTwo {
pub align: u64,
}
#[derive(Diagnostic)]
#[diag(codegen_llvm_invalid_minimum_alignment_too_large)]
pub(crate) struct InvalidMinimumAlignmentTooLarge {
pub align: u64,
}
#[derive(Diagnostic)]
#[diag(codegen_llvm_sanitizer_memtag_requires_mte)]
pub(crate) struct SanitizerMemtagRequiresMte;

View file

@ -2,7 +2,7 @@ use std::borrow::Cow;
use std::collections::BTreeMap;
use std::str::FromStr;
use rustc_abi::ExternAbi;
use rustc_abi::{Align, AlignFromBytesError, ExternAbi};
use serde_json::Value;
use super::{Target, TargetKind, TargetOptions, TargetWarnings};
@ -57,6 +57,14 @@ impl Target {
base.metadata.std = metadata.remove("std").and_then(|host| host.as_bool());
}
let alignment_error = |field_name: &str, error: AlignFromBytesError| -> String {
let msg = match error {
AlignFromBytesError::NotPowerOfTwo(_) => "not a power of 2 number of bytes",
AlignFromBytesError::TooLarge(_) => "too large",
};
format!("`{}` bits is not a valid value for {field_name}: {msg}", error.align() * 8)
};
let mut incorrect_type = vec![];
macro_rules! key {
@ -111,6 +119,15 @@ impl Target {
base.$key_name = Some(s.into());
}
} );
($key_name:ident, Option<Align>) => ( {
let name = (stringify!($key_name)).replace("_", "-");
if let Some(b) = obj.remove(&name).and_then(|b| b.as_u64()) {
match Align::from_bits(b) {
Ok(align) => base.$key_name = Some(align),
Err(e) => return Err(alignment_error(&name, e)),
}
}
} );
($key_name:ident, BinaryFormat) => ( {
let name = (stringify!($key_name)).replace("_", "-");
obj.remove(&name).and_then(|f| f.as_str().and_then(|s| {
@ -617,7 +634,7 @@ impl Target {
key!(crt_static_default, bool);
key!(crt_static_respected, bool);
key!(stack_probes, StackProbeType)?;
key!(min_global_align, Option<u64>);
key!(min_global_align, Option<Align>);
key!(default_codegen_units, Option<u64>);
key!(default_codegen_backend, Option<StaticCow<str>>);
key!(trap_unreachable, bool);

View file

@ -1697,6 +1697,12 @@ impl ToJson for BinaryFormat {
}
}
impl ToJson for Align {
fn to_json(&self) -> Json {
self.bits().to_json()
}
}
macro_rules! supported_targets {
( $(($tuple:literal, $module:ident),)+ ) => {
mod targets {
@ -2513,7 +2519,7 @@ pub struct TargetOptions {
pub stack_probes: StackProbeType,
/// The minimum alignment for global symbols.
pub min_global_align: Option<u64>,
pub min_global_align: Option<Align>,
/// Default number of codegen units to use in debug mode
pub default_codegen_units: Option<u64>,

View file

@ -1,4 +1,4 @@
use rustc_abi::Endian;
use rustc_abi::{Align, Endian};
use crate::spec::{SanitizerSet, StackProbeType, Target, TargetMetadata, base};
@ -8,7 +8,7 @@ pub(crate) fn target() -> Target {
// z10 is the oldest CPU supported by LLVM
base.cpu = "z10".into();
base.max_atomic_width = Some(128);
base.min_global_align = Some(16);
base.min_global_align = Some(Align::from_bits(16).unwrap());
base.stack_probes = StackProbeType::Inline;
base.supported_sanitizers =
SanitizerSet::ADDRESS | SanitizerSet::LEAK | SanitizerSet::MEMORY | SanitizerSet::THREAD;

View file

@ -1,4 +1,4 @@
use rustc_abi::Endian;
use rustc_abi::{Align, Endian};
use crate::spec::{SanitizerSet, StackProbeType, Target, TargetMetadata, base};
@ -8,7 +8,7 @@ pub(crate) fn target() -> Target {
// z10 is the oldest CPU supported by LLVM
base.cpu = "z10".into();
base.max_atomic_width = Some(128);
base.min_global_align = Some(16);
base.min_global_align = Some(Align::from_bits(16).unwrap());
base.static_position_independent_executables = true;
base.stack_probes = StackProbeType::Inline;
base.supported_sanitizers =