Skip to content

Commit 75f4028

Browse files
committed
rustc_target: introduce Arch
Improve type safety by using an enum rather than strings.
1 parent 0f5c80a commit 75f4028

File tree

6 files changed

+55
-51
lines changed

6 files changed

+55
-51
lines changed

src/abi.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use rustc_middle::ty::layout::LayoutOf;
1212
#[cfg(feature = "master")]
1313
use rustc_session::config;
1414
use rustc_target::callconv::{ArgAttributes, CastTarget, FnAbi, PassMode};
15+
#[cfg(feature = "master")]
16+
use rustc_target::spec::Arch;
1517

1618
use crate::builder::Builder;
1719
use crate::context::CodegenCx;
@@ -233,12 +235,12 @@ impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
233235

234236
#[cfg(feature = "master")]
235237
fn gcc_cconv(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Option<FnAttribute<'gcc>> {
236-
conv_to_fn_attribute(self.conv, &cx.tcx.sess.target.arch)
238+
conv_to_fn_attribute(self.conv, cx.tcx.sess.target.arch)
237239
}
238240
}
239241

240242
#[cfg(feature = "master")]
241-
pub fn conv_to_fn_attribute<'gcc>(conv: CanonAbi, arch: &str) -> Option<FnAttribute<'gcc>> {
243+
pub fn conv_to_fn_attribute<'gcc>(conv: CanonAbi, arch: Arch) -> Option<FnAttribute<'gcc>> {
242244
let attribute = match conv {
243245
CanonAbi::C | CanonAbi::Rust => return None,
244246
CanonAbi::RustCold => FnAttribute::Cold,
@@ -251,15 +253,11 @@ pub fn conv_to_fn_attribute<'gcc>(conv: CanonAbi, arch: &str) -> Option<FnAttrib
251253
ArmCall::CCmseNonSecureEntry => FnAttribute::ArmCmseNonsecureEntry,
252254
ArmCall::Aapcs => FnAttribute::ArmPcs("aapcs"),
253255
},
254-
CanonAbi::GpuKernel => {
255-
if arch == "amdgpu" {
256-
FnAttribute::GcnAmdGpuHsaKernel
257-
} else if arch == "nvptx64" {
258-
FnAttribute::NvptxKernel
259-
} else {
260-
panic!("Architecture {} does not support GpuKernel calling convention", arch);
261-
}
262-
}
256+
CanonAbi::GpuKernel => match arch {
257+
Arch::AmdGpu => FnAttribute::GcnAmdGpuHsaKernel,
258+
Arch::Nvptx64 => FnAttribute::NvptxKernel,
259+
arch => panic!("Arch {arch} does not support GpuKernel calling convention"),
260+
},
263261
// TODO(antoyo): check if those AVR attributes are mapped correctly.
264262
CanonAbi::Interrupt(interrupt_kind) => match interrupt_kind {
265263
InterruptKind::Avr => FnAttribute::AvrSignal,

src/attributes.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
99
#[cfg(feature = "master")]
1010
use rustc_middle::mir::TerminatorKind;
1111
use rustc_middle::ty;
12+
#[cfg(feature = "master")]
13+
use rustc_target::spec::Arch;
1214

1315
use crate::context::CodegenCx;
1416
use crate::gcc_util::to_gcc_features;
@@ -70,7 +72,7 @@ fn inline_attr<'gcc, 'tcx>(
7072
InlineAttr::Hint => Some(FnAttribute::Inline),
7173
InlineAttr::Force { .. } => Some(FnAttribute::AlwaysInline),
7274
InlineAttr::Never => {
73-
if cx.sess().target.arch != "amdgpu" {
75+
if cx.sess().target.arch != Arch::AmdGpu {
7476
Some(FnAttribute::NoInline)
7577
} else {
7678
None
@@ -153,8 +155,8 @@ pub fn from_fn_attrs<'gcc, 'tcx>(
153155
.join(",");
154156
if !target_features.is_empty() {
155157
#[cfg(feature = "master")]
156-
match cx.sess().target.arch.as_ref() {
157-
"x86" | "x86_64" | "powerpc" => {
158+
match cx.sess().target.arch {
159+
Arch::X86 | Arch::X86_64 | Arch::PowerPC => {
158160
func.add_attribute(FnAttribute::Target(&target_features))
159161
}
160162
// The target attribute is not supported on other targets in GCC.

src/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ use rustc_middle::mir::mono::Visibility;
1515
use rustc_middle::ty::TyCtxt;
1616
use rustc_session::config::DebugInfo;
1717
use rustc_span::Symbol;
18-
use rustc_target::spec::RelocModel;
1918
#[cfg(feature = "master")]
2019
use rustc_target::spec::SymbolVisibility;
20+
use rustc_target::spec::{Arch, RelocModel};
2121

2222
use crate::builder::Builder;
2323
use crate::context::CodegenCx;
@@ -116,7 +116,7 @@ pub fn compile_codegen_unit(
116116
.map(|string| &string[1..])
117117
.collect();
118118

119-
if !disabled_features.contains("avx") && tcx.sess.target.arch == "x86_64" {
119+
if !disabled_features.contains("avx") && tcx.sess.target.arch == Arch::X86_64 {
120120
// NOTE: we always enable AVX because the equivalent of llvm.x86.sse2.cmp.pd in GCC for
121121
// SSE2 is multiple builtins, so we use the AVX __builtin_ia32_cmppd instead.
122122
// FIXME(antoyo): use the proper builtins for llvm.x86.sse2.cmp.pd and similar.

src/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ impl<'gcc, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
487487
let entry_name = self.sess().target.entry_name.as_ref();
488488
if !self.functions.borrow().contains_key(entry_name) {
489489
#[cfg(feature = "master")]
490-
let conv = conv_to_fn_attribute(self.sess().target.entry_abi, &self.sess().target.arch);
490+
let conv = conv_to_fn_attribute(self.sess().target.entry_abi, self.sess().target.arch);
491491
#[cfg(not(feature = "master"))]
492492
let conv = None;
493493
Some(self.declare_entry_fn(entry_name, fn_type, conv))

src/gcc_util.rs

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use gccjit::Context;
33
use rustc_codegen_ssa::target_features;
44
use rustc_data_structures::smallvec::{SmallVec, smallvec};
55
use rustc_session::Session;
6+
use rustc_target::spec::Arch;
67

78
fn gcc_features_by_flags(sess: &Session, features: &mut Vec<String>) {
89
target_features::retpoline_features_by_flags(sess, features);
@@ -65,44 +66,47 @@ pub(crate) fn global_gcc_features(sess: &Session, diagnostics: bool) -> Vec<Stri
6566

6667
// To find a list of GCC's names, check https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
6768
pub fn to_gcc_features<'a>(sess: &Session, s: &'a str) -> SmallVec<[&'a str; 2]> {
68-
let arch = if sess.target.arch == "x86_64" { "x86" } else { &*sess.target.arch };
6969
// cSpell:disable
70-
match (arch, s) {
70+
match (sess.target.arch, s) {
7171
// FIXME: seems like x87 does not exist?
72-
("x86", "x87") => smallvec![],
73-
("x86", "sse4.2") => smallvec!["sse4.2", "crc32"],
74-
("x86", "pclmulqdq") => smallvec!["pclmul"],
75-
("x86", "rdrand") => smallvec!["rdrnd"],
76-
("x86", "bmi1") => smallvec!["bmi"],
77-
("x86", "cmpxchg16b") => smallvec!["cx16"],
78-
("x86", "avx512vaes") => smallvec!["vaes"],
79-
("x86", "avx512gfni") => smallvec!["gfni"],
80-
("x86", "avx512vpclmulqdq") => smallvec!["vpclmulqdq"],
72+
(Arch::X86 | Arch::X86_64, "x87") => smallvec![],
73+
(Arch::X86 | Arch::X86_64, "sse4.2") => smallvec!["sse4.2", "crc32"],
74+
(Arch::X86 | Arch::X86_64, "pclmulqdq") => smallvec!["pclmul"],
75+
(Arch::X86 | Arch::X86_64, "rdrand") => smallvec!["rdrnd"],
76+
(Arch::X86 | Arch::X86_64, "bmi1") => smallvec!["bmi"],
77+
(Arch::X86 | Arch::X86_64, "cmpxchg16b") => smallvec!["cx16"],
78+
(Arch::X86 | Arch::X86_64, "avx512vaes") => smallvec!["vaes"],
79+
(Arch::X86 | Arch::X86_64, "avx512gfni") => smallvec!["gfni"],
80+
(Arch::X86 | Arch::X86_64, "avx512vpclmulqdq") => smallvec!["vpclmulqdq"],
8181
// NOTE: seems like GCC requires 'avx512bw' for 'avx512vbmi2'.
82-
("x86", "avx512vbmi2") => smallvec!["avx512vbmi2", "avx512bw"],
82+
(Arch::X86 | Arch::X86_64, "avx512vbmi2") => {
83+
smallvec!["avx512vbmi2", "avx512bw"]
84+
}
8385
// NOTE: seems like GCC requires 'avx512bw' for 'avx512bitalg'.
84-
("x86", "avx512bitalg") => smallvec!["avx512bitalg", "avx512bw"],
85-
("aarch64", "rcpc2") => smallvec!["rcpc-immo"],
86-
("aarch64", "dpb") => smallvec!["ccpp"],
87-
("aarch64", "dpb2") => smallvec!["ccdp"],
88-
("aarch64", "frintts") => smallvec!["fptoint"],
89-
("aarch64", "fcma") => smallvec!["complxnum"],
90-
("aarch64", "pmuv3") => smallvec!["perfmon"],
91-
("aarch64", "paca") => smallvec!["pauth"],
92-
("aarch64", "pacg") => smallvec!["pauth"],
86+
(Arch::X86 | Arch::X86_64, "avx512bitalg") => {
87+
smallvec!["avx512bitalg", "avx512bw"]
88+
}
89+
(Arch::AArch64, "rcpc2") => smallvec!["rcpc-immo"],
90+
(Arch::AArch64, "dpb") => smallvec!["ccpp"],
91+
(Arch::AArch64, "dpb2") => smallvec!["ccdp"],
92+
(Arch::AArch64, "frintts") => smallvec!["fptoint"],
93+
(Arch::AArch64, "fcma") => smallvec!["complxnum"],
94+
(Arch::AArch64, "pmuv3") => smallvec!["perfmon"],
95+
(Arch::AArch64, "paca") => smallvec!["pauth"],
96+
(Arch::AArch64, "pacg") => smallvec!["pauth"],
9397
// Rust ties fp and neon together. In GCC neon implicitly enables fp,
9498
// but we manually enable neon when a feature only implicitly enables fp
95-
("aarch64", "f32mm") => smallvec!["f32mm", "neon"],
96-
("aarch64", "f64mm") => smallvec!["f64mm", "neon"],
97-
("aarch64", "fhm") => smallvec!["fp16fml", "neon"],
98-
("aarch64", "fp16") => smallvec!["fullfp16", "neon"],
99-
("aarch64", "jsconv") => smallvec!["jsconv", "neon"],
100-
("aarch64", "sve") => smallvec!["sve", "neon"],
101-
("aarch64", "sve2") => smallvec!["sve2", "neon"],
102-
("aarch64", "sve2-aes") => smallvec!["sve2-aes", "neon"],
103-
("aarch64", "sve2-sm4") => smallvec!["sve2-sm4", "neon"],
104-
("aarch64", "sve2-sha3") => smallvec!["sve2-sha3", "neon"],
105-
("aarch64", "sve2-bitperm") => smallvec!["sve2-bitperm", "neon"],
99+
(Arch::AArch64, "f32mm") => smallvec!["f32mm", "neon"],
100+
(Arch::AArch64, "f64mm") => smallvec!["f64mm", "neon"],
101+
(Arch::AArch64, "fhm") => smallvec!["fp16fml", "neon"],
102+
(Arch::AArch64, "fp16") => smallvec!["fullfp16", "neon"],
103+
(Arch::AArch64, "jsconv") => smallvec!["jsconv", "neon"],
104+
(Arch::AArch64, "sve") => smallvec!["sve", "neon"],
105+
(Arch::AArch64, "sve2") => smallvec!["sve2", "neon"],
106+
(Arch::AArch64, "sve2-aes") => smallvec!["sve2-aes", "neon"],
107+
(Arch::AArch64, "sve2-sm4") => smallvec!["sve2-sm4", "neon"],
108+
(Arch::AArch64, "sve2-sha3") => smallvec!["sve2-sha3", "neon"],
109+
(Arch::AArch64, "sve2-bitperm") => smallvec!["sve2-bitperm", "neon"],
106110
(_, s) => smallvec![s],
107111
}
108112
// cSpell:enable

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ use rustc_middle::util::Providers;
103103
use rustc_session::Session;
104104
use rustc_session::config::{OptLevel, OutputFilenames};
105105
use rustc_span::Symbol;
106-
use rustc_target::spec::RelocModel;
106+
use rustc_target::spec::{Arch, RelocModel};
107107
use tempfile::TempDir;
108108

109109
use crate::back::lto::ModuleBuffer;
@@ -249,7 +249,7 @@ impl CodegenBackend for GccCodegenBackend {
249249

250250
fn new_context<'gcc, 'tcx>(tcx: TyCtxt<'tcx>) -> Context<'gcc> {
251251
let context = Context::default();
252-
if tcx.sess.target.arch == "x86" || tcx.sess.target.arch == "x86_64" {
252+
if matches!(tcx.sess.target.arch, Arch::X86 | Arch::X86_64) {
253253
context.add_command_line_option("-masm=intel");
254254
}
255255
#[cfg(feature = "master")]

0 commit comments

Comments
 (0)