Skip to content

Commit a50bd7c

Browse files
committed
store target.min_global_align as an Align
1 parent 44f415c commit a50bd7c

File tree

10 files changed

+37
-63
lines changed

10 files changed

+37
-63
lines changed

compiler/rustc_codegen_gcc/messages.ftl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ codegen_gcc_unknown_ctarget_feature_prefix =
22
unknown feature specified for `-Ctarget-feature`: `{$feature}`
33
.note = features must begin with a `+` to enable or `-` to disable it
44
5-
codegen_gcc_invalid_minimum_alignment =
6-
invalid minimum global alignment: {$err}
7-
85
codegen_gcc_forbidden_ctarget_feature =
96
target feature `{$feature}` cannot be toggled with `-Ctarget-feature`: {$reason}
107

compiler/rustc_codegen_gcc/src/consts.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use rustc_span::def_id::DefId;
1818

1919
use crate::base;
2020
use crate::context::CodegenCx;
21-
use crate::errors::InvalidMinimumAlignment;
2221
use crate::type_of::LayoutGccExt;
2322

2423
fn set_global_alignment<'gcc, 'tcx>(
@@ -29,13 +28,8 @@ fn set_global_alignment<'gcc, 'tcx>(
2928
// The target may require greater alignment for globals than the type does.
3029
// Note: GCC and Clang also allow `__attribute__((aligned))` on variables,
3130
// which can force it to be smaller. Rust doesn't support this yet.
32-
if let Some(min) = cx.sess().target.min_global_align {
33-
match Align::from_bits(min) {
34-
Ok(min) => align = align.max(min),
35-
Err(err) => {
36-
cx.sess().dcx().emit_err(InvalidMinimumAlignment { err: err.to_string() });
37-
}
38-
}
31+
if let Some(min_global) = cx.sess().target.min_global_align {
32+
align = Ord::max(align, min_global);
3933
}
4034
gv.set_alignment(align.bytes() as i32);
4135
}

compiler/rustc_codegen_gcc/src/errors.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@ pub(crate) struct UnwindingInlineAsm {
4747
pub span: Span,
4848
}
4949

50-
#[derive(Diagnostic)]
51-
#[diag(codegen_gcc_invalid_minimum_alignment)]
52-
pub(crate) struct InvalidMinimumAlignment {
53-
pub err: String,
54-
}
55-
5650
#[derive(Diagnostic)]
5751
#[diag(codegen_gcc_copy_bitcode)]
5852
pub(crate) struct CopyBitcode {

compiler/rustc_codegen_llvm/messages.ftl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ codegen_llvm_from_llvm_diag = {$message}
1919
2020
codegen_llvm_from_llvm_optimization_diag = {$filename}:{$line}:{$column} {$pass_name} ({$kind}): {$message}
2121
22-
codegen_llvm_invalid_minimum_alignment_not_power_of_two =
23-
invalid minimum global alignment: {$align} is not power of 2
24-
25-
codegen_llvm_invalid_minimum_alignment_too_large =
26-
invalid minimum global alignment: {$align} is too large
27-
2822
codegen_llvm_load_bitcode = failed to load bitcode of module "{$name}"
2923
codegen_llvm_load_bitcode_with_llvm_err = failed to load bitcode of module "{$name}": {$llvm_err}
3024

compiler/rustc_codegen_llvm/src/consts.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use std::ops::Range;
22

3-
use rustc_abi::{
4-
Align, AlignFromBytesError, HasDataLayout, Primitive, Scalar, Size, WrappingRange,
5-
};
3+
use rustc_abi::{Align, HasDataLayout, Primitive, Scalar, Size, WrappingRange};
64
use rustc_codegen_ssa::common;
75
use rustc_codegen_ssa::traits::*;
86
use rustc_hir::LangItem;
@@ -20,9 +18,7 @@ use rustc_middle::{bug, span_bug};
2018
use tracing::{debug, instrument, trace};
2119

2220
use crate::common::{AsCCharPtr, CodegenCx};
23-
use crate::errors::{
24-
InvalidMinimumAlignmentNotPowerOfTwo, InvalidMinimumAlignmentTooLarge, SymbolAlreadyDefined,
25-
};
21+
use crate::errors::SymbolAlreadyDefined;
2622
use crate::llvm::{self, True};
2723
use crate::type_::Type;
2824
use crate::type_of::LayoutLlvmExt;
@@ -149,22 +145,10 @@ fn set_global_alignment<'ll>(cx: &CodegenCx<'ll, '_>, gv: &'ll Value, mut align:
149145
// The target may require greater alignment for globals than the type does.
150146
// Note: GCC and Clang also allow `__attribute__((aligned))` on variables,
151147
// which can force it to be smaller. Rust doesn't support this yet.
152-
if let Some(min) = cx.sess().target.min_global_align {
153-
match Align::from_bits(min) {
154-
Ok(min) => align = align.max(min),
155-
Err(err) => match err {
156-
AlignFromBytesError::NotPowerOfTwo(align) => {
157-
cx.sess().dcx().emit_err(InvalidMinimumAlignmentNotPowerOfTwo { align });
158-
}
159-
AlignFromBytesError::TooLarge(align) => {
160-
cx.sess().dcx().emit_err(InvalidMinimumAlignmentTooLarge { align });
161-
}
162-
},
163-
}
164-
}
165-
unsafe {
166-
llvm::LLVMSetAlignment(gv, align.bytes() as u32);
148+
if let Some(min_global) = cx.sess().target.min_global_align {
149+
align = Ord::max(align, min_global);
167150
}
151+
llvm::set_alignment(gv, align);
168152
}
169153

170154
fn check_and_apply_linkage<'ll, 'tcx>(

compiler/rustc_codegen_llvm/src/errors.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,6 @@ pub(crate) struct SymbolAlreadyDefined<'a> {
5757
pub symbol_name: &'a str,
5858
}
5959

60-
#[derive(Diagnostic)]
61-
#[diag(codegen_llvm_invalid_minimum_alignment_not_power_of_two)]
62-
pub(crate) struct InvalidMinimumAlignmentNotPowerOfTwo {
63-
pub align: u64,
64-
}
65-
66-
#[derive(Diagnostic)]
67-
#[diag(codegen_llvm_invalid_minimum_alignment_too_large)]
68-
pub(crate) struct InvalidMinimumAlignmentTooLarge {
69-
pub align: u64,
70-
}
71-
7260
#[derive(Diagnostic)]
7361
#[diag(codegen_llvm_sanitizer_memtag_requires_mte)]
7462
pub(crate) struct SanitizerMemtagRequiresMte;

compiler/rustc_target/src/spec/json.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::borrow::Cow;
22
use std::collections::BTreeMap;
33
use std::str::FromStr;
44

5-
use rustc_abi::ExternAbi;
5+
use rustc_abi::{Align, AlignFromBytesError, ExternAbi};
66
use serde_json::Value;
77

88
use super::{Target, TargetKind, TargetOptions, TargetWarnings};
@@ -57,6 +57,14 @@ impl Target {
5757
base.metadata.std = metadata.remove("std").and_then(|host| host.as_bool());
5858
}
5959

60+
let alignment_error = |field_name: &str, error: AlignFromBytesError| -> String {
61+
let msg = match error {
62+
AlignFromBytesError::NotPowerOfTwo(_) => "not a power of 2 number of bytes",
63+
AlignFromBytesError::TooLarge(_) => "too large",
64+
};
65+
format!("`{}` bits is not a valid value for {field_name}: {msg}", error.align() * 8)
66+
};
67+
6068
let mut incorrect_type = vec![];
6169

6270
macro_rules! key {
@@ -111,6 +119,15 @@ impl Target {
111119
base.$key_name = Some(s.into());
112120
}
113121
} );
122+
($key_name:ident, Option<Align>) => ( {
123+
let name = (stringify!($key_name)).replace("_", "-");
124+
if let Some(b) = obj.remove(&name).and_then(|b| b.as_u64()) {
125+
match Align::from_bits(b) {
126+
Ok(align) => base.$key_name = Some(align),
127+
Err(e) => return Err(alignment_error(&name, e)),
128+
}
129+
}
130+
} );
114131
($key_name:ident, BinaryFormat) => ( {
115132
let name = (stringify!($key_name)).replace("_", "-");
116133
obj.remove(&name).and_then(|f| f.as_str().and_then(|s| {
@@ -617,7 +634,7 @@ impl Target {
617634
key!(crt_static_default, bool);
618635
key!(crt_static_respected, bool);
619636
key!(stack_probes, StackProbeType)?;
620-
key!(min_global_align, Option<u64>);
637+
key!(min_global_align, Option<Align>);
621638
key!(default_codegen_units, Option<u64>);
622639
key!(default_codegen_backend, Option<StaticCow<str>>);
623640
key!(trap_unreachable, bool);

compiler/rustc_target/src/spec/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1697,6 +1697,12 @@ impl ToJson for BinaryFormat {
16971697
}
16981698
}
16991699

1700+
impl ToJson for Align {
1701+
fn to_json(&self) -> Json {
1702+
self.bits().to_json()
1703+
}
1704+
}
1705+
17001706
macro_rules! supported_targets {
17011707
( $(($tuple:literal, $module:ident),)+ ) => {
17021708
mod targets {
@@ -2513,7 +2519,7 @@ pub struct TargetOptions {
25132519
pub stack_probes: StackProbeType,
25142520

25152521
/// The minimum alignment for global symbols.
2516-
pub min_global_align: Option<u64>,
2522+
pub min_global_align: Option<Align>,
25172523

25182524
/// Default number of codegen units to use in debug mode
25192525
pub default_codegen_units: Option<u64>,

compiler/rustc_target/src/spec/targets/s390x_unknown_linux_gnu.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_abi::Endian;
1+
use rustc_abi::{Align, Endian};
22

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

@@ -8,7 +8,7 @@ pub(crate) fn target() -> Target {
88
// z10 is the oldest CPU supported by LLVM
99
base.cpu = "z10".into();
1010
base.max_atomic_width = Some(128);
11-
base.min_global_align = Some(16);
11+
base.min_global_align = Some(Align::from_bits(16).unwrap());
1212
base.stack_probes = StackProbeType::Inline;
1313
base.supported_sanitizers =
1414
SanitizerSet::ADDRESS | SanitizerSet::LEAK | SanitizerSet::MEMORY | SanitizerSet::THREAD;

compiler/rustc_target/src/spec/targets/s390x_unknown_linux_musl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_abi::Endian;
1+
use rustc_abi::{Align, Endian};
22

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

@@ -8,7 +8,7 @@ pub(crate) fn target() -> Target {
88
// z10 is the oldest CPU supported by LLVM
99
base.cpu = "z10".into();
1010
base.max_atomic_width = Some(128);
11-
base.min_global_align = Some(16);
11+
base.min_global_align = Some(Align::from_bits(16).unwrap());
1212
base.static_position_independent_executables = true;
1313
base.stack_probes = StackProbeType::Inline;
1414
base.supported_sanitizers =

0 commit comments

Comments
 (0)