Skip to content

store target.min_global_align as an Align #142179

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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions compiler/rustc_codegen_gcc/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
10 changes: 2 additions & 8 deletions compiler/rustc_codegen_gcc/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(
Expand All @@ -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);
}
Expand Down
6 changes: 0 additions & 6 deletions compiler/rustc_codegen_gcc/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 0 additions & 6 deletions compiler/rustc_codegen_llvm/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
26 changes: 5 additions & 21 deletions compiler/rustc_codegen_llvm/src/consts.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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>(
Expand Down
12 changes: 0 additions & 12 deletions compiler/rustc_codegen_llvm/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 19 additions & 2 deletions compiler/rustc_target/src/spec/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_abi::Endian;
use rustc_abi::{Align, Endian};

use crate::spec::{SanitizerSet, StackProbeType, Target, TargetMetadata, base};

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_abi::Endian;
use rustc_abi::{Align, Endian};

use crate::spec::{SanitizerSet, StackProbeType, Target, TargetMetadata, base};

Expand All @@ -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 =
Expand Down
Loading