Skip to content

Commit b49a1f7

Browse files
committed
rustc_target: introduce Arch
Improve type safety by using an enum rather than strings.
1 parent b548b29 commit b49a1f7

File tree

4 files changed

+30
-24
lines changed

4 files changed

+30
-24
lines changed

src/abi/mod.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc_middle::ty::print::with_no_trimmed_paths;
2020
use rustc_session::Session;
2121
use rustc_span::source_map::Spanned;
2222
use rustc_target::callconv::{FnAbi, PassMode};
23+
use rustc_target::spec::Arch;
2324
use smallvec::SmallVec;
2425

2526
use self::pass_mode::*;
@@ -155,7 +156,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
155156
let ret = self.lib_call_unadjusted(name, params, returns, &args)[0];
156157

157158
Cow::Owned(vec![codegen_bitcast(self, types::I128, ret)])
158-
} else if ret_single_i128 && self.tcx.sess.target.arch == "s390x" {
159+
} else if ret_single_i128 && self.tcx.sess.target.arch == Arch::S390x {
159160
// Return i128 using a return area pointer on s390x.
160161
let mut params = params;
161162
let mut args = args.to_vec();
@@ -641,7 +642,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
641642
.flat_map(|arg_abi| arg_abi.get_abi_param(fx.tcx).into_iter()),
642643
);
643644

644-
if fx.tcx.sess.target.is_like_darwin && fx.tcx.sess.target.arch == "aarch64" {
645+
if fx.tcx.sess.target.is_like_darwin && fx.tcx.sess.target.arch == Arch::AArch64 {
645646
// Add any padding arguments needed for Apple AArch64.
646647
// There's no need to pad the argument list unless variadic arguments are actually being
647648
// passed.
@@ -787,25 +788,25 @@ pub(crate) fn codegen_drop<'tcx>(
787788
pub(crate) fn lib_call_arg_param(tcx: TyCtxt<'_>, ty: Type, is_signed: bool) -> AbiParam {
788789
let param = AbiParam::new(ty);
789790
if ty.is_int() && u64::from(ty.bits()) < tcx.data_layout.pointer_size().bits() {
790-
match (&*tcx.sess.target.arch, &*tcx.sess.target.vendor) {
791-
("x86_64", _) | ("aarch64", "apple") => match (ty, is_signed) {
791+
match (tcx.sess.target.arch, tcx.sess.target.vendor.as_ref()) {
792+
(Arch::X86_64, _) | (Arch::AArch64, "apple") => match (ty, is_signed) {
792793
(types::I8 | types::I16, true) => param.sext(),
793794
(types::I8 | types::I16, false) => param.uext(),
794795
_ => param,
795796
},
796-
("aarch64", _) => param,
797-
("riscv64", _) => match (ty, is_signed) {
797+
(Arch::AArch64, _) => param,
798+
(Arch::RiscV64, _) => match (ty, is_signed) {
798799
(types::I32, _) | (_, true) => param.sext(),
799800
_ => param.uext(),
800801
},
801-
("s390x", _) => {
802+
(Arch::S390x, _) => {
802803
if is_signed {
803804
param.sext()
804805
} else {
805806
param.uext()
806807
}
807808
}
808-
_ => unimplemented!("{:?}", tcx.sess.target.arch),
809+
(arch, _) => unimplemented!("{arch:?}"),
809810
}
810811
} else {
811812
param

src/codegen_f16_f128.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
use rustc_target::spec::Arch;
2+
13
use crate::prelude::*;
24

35
pub(crate) fn f16_to_f32(fx: &mut FunctionCx<'_, '_, '_>, value: Value) -> Value {
46
let (value, arg_ty) =
5-
if fx.tcx.sess.target.vendor == "apple" && fx.tcx.sess.target.arch == "x86_64" {
7+
if fx.tcx.sess.target.vendor == "apple" && fx.tcx.sess.target.arch == Arch::X86_64 {
68
(
79
fx.bcx.ins().bitcast(types::I16, MemFlags::new(), value),
810
lib_call_arg_param(fx.tcx, types::I16, false),
@@ -19,7 +21,8 @@ fn f16_to_f64(fx: &mut FunctionCx<'_, '_, '_>, value: Value) -> Value {
1921
}
2022

2123
pub(crate) fn f32_to_f16(fx: &mut FunctionCx<'_, '_, '_>, value: Value) -> Value {
22-
let ret_ty = if fx.tcx.sess.target.vendor == "apple" && fx.tcx.sess.target.arch == "x86_64" {
24+
let ret_ty = if fx.tcx.sess.target.vendor == "apple" && fx.tcx.sess.target.arch == Arch::X86_64
25+
{
2326
types::I16
2427
} else {
2528
types::F16
@@ -34,7 +37,8 @@ pub(crate) fn f32_to_f16(fx: &mut FunctionCx<'_, '_, '_>, value: Value) -> Value
3437
}
3538

3639
fn f64_to_f16(fx: &mut FunctionCx<'_, '_, '_>, value: Value) -> Value {
37-
let ret_ty = if fx.tcx.sess.target.vendor == "apple" && fx.tcx.sess.target.arch == "x86_64" {
40+
let ret_ty = if fx.tcx.sess.target.vendor == "apple" && fx.tcx.sess.target.arch == Arch::X86_64
41+
{
3842
types::I16
3943
} else {
4044
types::F16

src/common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::ty::layout::{
88
};
99
use rustc_span::source_map::Spanned;
1010
use rustc_target::callconv::FnAbi;
11-
use rustc_target::spec::{HasTargetSpec, Target};
11+
use rustc_target::spec::{Arch, HasTargetSpec, Target};
1212

1313
use crate::constant::ConstantCx;
1414
use crate::debuginfo::FunctionDebugContext;
@@ -373,7 +373,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
373373
"size must be a multiple of alignment (size={size}, align={align})"
374374
);
375375

376-
let abi_align = if self.tcx.sess.target.arch == "s390x" { 8 } else { 16 };
376+
let abi_align = if self.tcx.sess.target.arch == Arch::S390x { 8 } else { 16 };
377377
if align <= abi_align {
378378
let stack_slot = self.bcx.create_sized_stack_slot(StackSlotData {
379379
kind: StackSlotKind::ExplicitSlot,

src/lib.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
5050
use rustc_session::Session;
5151
use rustc_session::config::OutputFilenames;
5252
use rustc_span::{Symbol, sym};
53+
use rustc_target::spec::Arch;
5354

5455
pub use crate::config::*;
5556
use crate::prelude::*;
@@ -186,20 +187,20 @@ impl CodegenBackend for CraneliftCodegenBackend {
186187

187188
fn target_config(&self, sess: &Session) -> TargetConfig {
188189
// FIXME return the actually used target features. this is necessary for #[cfg(target_feature)]
189-
let target_features = if sess.target.arch == "x86_64" && sess.target.os != "none" {
190-
// x86_64 mandates SSE2 support and rustc requires the x87 feature to be enabled
191-
vec![sym::fxsr, sym::sse, sym::sse2, Symbol::intern("x87")]
192-
} else if sess.target.arch == "aarch64" {
193-
match &*sess.target.os {
190+
let target_features = match sess.target.arch {
191+
Arch::X86_64 if sess.target.os != "none" => {
192+
// x86_64 mandates SSE2 support and rustc requires the x87 feature to be enabled
193+
vec![sym::fxsr, sym::sse, sym::sse2, Symbol::intern("x87")]
194+
}
195+
Arch::AArch64 => match &*sess.target.os {
194196
"none" => vec![],
195197
// On macOS the aes, sha2 and sha3 features are enabled by default and ring
196198
// fails to compile on macOS when they are not present.
197199
"macos" => vec![sym::neon, sym::aes, sym::sha2, sym::sha3],
198200
// AArch64 mandates Neon support
199201
_ => vec![sym::neon],
200-
}
201-
} else {
202-
vec![]
202+
},
203+
_ => vec![],
203204
};
204205
// FIXME do `unstable_target_features` properly
205206
let unstable_target_features = target_features.clone();
@@ -208,14 +209,14 @@ impl CodegenBackend for CraneliftCodegenBackend {
208209
// Windows, whereas LLVM 21+ and Cranelift pass it indirectly. This means that `f128` won't
209210
// work when linking against a LLVM-built sysroot.
210211
let has_reliable_f128 = !sess.target.is_like_windows;
211-
let has_reliable_f16 = match &*sess.target.arch {
212+
let has_reliable_f16 = match sess.target.arch {
212213
// FIXME(f16_f128): LLVM 20 does not support `f16` on s390x, meaning the required
213214
// builtins are not available in `compiler-builtins`.
214-
"s390x" => false,
215+
Arch::S390x => false,
215216
// FIXME(f16_f128): `rustc_codegen_llvm` currently disables support on Windows GNU
216217
// targets due to GCC using a different ABI than LLVM. Therefore `f16` won't be
217218
// available when using a LLVM-built sysroot.
218-
"x86_64"
219+
Arch::X86_64
219220
if sess.target.os == "windows"
220221
&& sess.target.env == "gnu"
221222
&& sess.target.abi != "llvm" =>

0 commit comments

Comments
 (0)