Skip to content

Commit 27ca099

Browse files
committed
add llvm_floatabi field to target spec that controls FloatABIType
1 parent fff026c commit 27ca099

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_session::config::{
2626
self, Lto, OutputType, Passes, RemapPathScopeComponents, SplitDwarfKind, SwitchWithOptPath,
2727
};
2828
use rustc_span::{BytePos, InnerSpan, Pos, SpanData, SyntaxContext, sym};
29-
use rustc_target::spec::{CodeModel, RelocModel, SanitizerSet, SplitDebuginfo, TlsModel};
29+
use rustc_target::spec::{CodeModel, FloatAbi, RelocModel, SanitizerSet, SplitDebuginfo, TlsModel};
3030
use tracing::debug;
3131

3232
use crate::back::lto::ThinBuffer;
@@ -40,7 +40,7 @@ use crate::errors::{
4040
WithLlvmError, WriteBytecode,
4141
};
4242
use crate::llvm::diagnostic::OptimizationDiagnosticKind::*;
43-
use crate::llvm::{self, DiagnosticInfo, FloatAbi, PassManager};
43+
use crate::llvm::{self, DiagnosticInfo, PassManager};
4444
use crate::type_::Type;
4545
use crate::{LlvmCodegenBackend, ModuleLlvm, base, common, llvm_util};
4646

@@ -181,6 +181,14 @@ pub(crate) fn to_llvm_code_model(code_model: Option<CodeModel>) -> llvm::CodeMod
181181
}
182182
}
183183

184+
fn to_llvm_float_abi(float_abi: Option<FloatAbi>) -> llvm::FloatAbi {
185+
match float_abi {
186+
None => llvm::FloatAbi::Default,
187+
Some(FloatAbi::Soft) => llvm::FloatAbi::Soft,
188+
Some(FloatAbi::Hard) => llvm::FloatAbi::Hard,
189+
}
190+
}
191+
184192
pub(crate) fn target_machine_factory(
185193
sess: &Session,
186194
optlvl: config::OptLevel,
@@ -190,11 +198,11 @@ pub(crate) fn target_machine_factory(
190198

191199
let (opt_level, _) = to_llvm_opt_settings(optlvl);
192200
let float_abi = if sess.target.arch == "arm" && sess.opts.cg.soft_float {
193-
FloatAbi::Soft
201+
llvm::FloatAbi::Soft
194202
} else {
195203
// `validate_commandline_args_with_session_available` has already warned about this being
196204
// ignored. Let's make sure LLVM doesn't suddenly start using this flag on more targets.
197-
FloatAbi::Default
205+
to_llvm_float_abi(sess.target.llvm_floatabi)
198206
};
199207

200208
let ffunction_sections =

compiler/rustc_target/src/spec/json.rs

+14
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,18 @@ impl Target {
116116
Some(Ok(()))
117117
})).unwrap_or(Ok(()))
118118
} );
119+
($key_name:ident, FloatAbi) => ( {
120+
let name = (stringify!($key_name)).replace("_", "-");
121+
obj.remove(&name).and_then(|o| o.as_str().and_then(|s| {
122+
match s.parse::<super::FloatAbi>() {
123+
Ok(float_abi) => base.$key_name = Some(float_abi),
124+
_ => return Some(Err(format!("'{}' is not a valid value for \
125+
llvm-floatabi. Use 'soft' or 'hard'.",
126+
s))),
127+
}
128+
Some(Ok(()))
129+
})).unwrap_or(Ok(()))
130+
} );
119131
($key_name:ident, RelocModel) => ( {
120132
let name = (stringify!($key_name)).replace("_", "-");
121133
obj.remove(&name).and_then(|o| o.as_str().and_then(|s| {
@@ -598,6 +610,7 @@ impl Target {
598610
key!(mcount = "target-mcount");
599611
key!(llvm_mcount_intrinsic, optional);
600612
key!(llvm_abiname);
613+
key!(llvm_floatabi, FloatAbi)?;
601614
key!(relax_elf_relocations, bool);
602615
key!(llvm_args, list);
603616
key!(use_ctors_section, bool);
@@ -772,6 +785,7 @@ impl ToJson for Target {
772785
target_option_val!(mcount, "target-mcount");
773786
target_option_val!(llvm_mcount_intrinsic);
774787
target_option_val!(llvm_abiname);
788+
target_option_val!(llvm_floatabi);
775789
target_option_val!(relax_elf_relocations);
776790
target_option_val!(llvm_args);
777791
target_option_val!(use_ctors_section);

compiler/rustc_target/src/spec/mod.rs

+41
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,35 @@ impl ToJson for CodeModel {
10851085
}
10861086
}
10871087

1088+
/// The float ABI setting to be configured in the LLVM target machine.
1089+
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
1090+
pub enum FloatAbi {
1091+
Soft,
1092+
Hard,
1093+
}
1094+
1095+
impl FromStr for FloatAbi {
1096+
type Err = ();
1097+
1098+
fn from_str(s: &str) -> Result<FloatAbi, ()> {
1099+
Ok(match s {
1100+
"soft" => FloatAbi::Soft,
1101+
"hard" => FloatAbi::Hard,
1102+
_ => return Err(()),
1103+
})
1104+
}
1105+
}
1106+
1107+
impl ToJson for FloatAbi {
1108+
fn to_json(&self) -> Json {
1109+
match *self {
1110+
FloatAbi::Soft => "soft",
1111+
FloatAbi::Hard => "hard",
1112+
}
1113+
.to_json()
1114+
}
1115+
}
1116+
10881117
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
10891118
pub enum TlsModel {
10901119
GeneralDynamic,
@@ -2150,6 +2179,8 @@ pub struct TargetOptions {
21502179
pub env: StaticCow<str>,
21512180
/// ABI name to distinguish multiple ABIs on the same OS and architecture. For instance, `"eabi"`
21522181
/// or `"eabihf"`. Defaults to "".
2182+
/// This field is *not* forwarded directly to LLVM; its primary purpose is `cfg(target_abi)`.
2183+
/// However, parts of the backend do check this field for specific values to enable special behavior.
21532184
pub abi: StaticCow<str>,
21542185
/// Vendor name to use for conditional compilation (`target_vendor`). Defaults to "unknown".
21552186
pub vendor: StaticCow<str>,
@@ -2446,8 +2477,17 @@ pub struct TargetOptions {
24462477
pub llvm_mcount_intrinsic: Option<StaticCow<str>>,
24472478

24482479
/// LLVM ABI name, corresponds to the '-mabi' parameter available in multilib C compilers
2480+
/// and the `-target-abi` flag in llc. In the LLVM API this is `MCOptions.ABIName`.
24492481
pub llvm_abiname: StaticCow<str>,
24502482

2483+
/// Control the float ABI to use, for architectures that support it. The only architecture we
2484+
/// currently use this for is ARM. Corresponds to the `-float-abi` flag in llc. In the LLVM ABI
2485+
/// this is `FloatABIType`. (clang's `-mfloat-abi` is similar but more complicated since it
2486+
/// can also affect the `soft-float` target feature.)
2487+
///
2488+
/// If not provided, LLVM will infer the float ABI from the target triple (`llvm_target`).
2489+
pub llvm_floatabi: Option<FloatAbi>,
2490+
24512491
/// Whether or not RelaxElfRelocation flag will be passed to the linker
24522492
pub relax_elf_relocations: bool,
24532493

@@ -2719,6 +2759,7 @@ impl Default for TargetOptions {
27192759
mcount: "mcount".into(),
27202760
llvm_mcount_intrinsic: None,
27212761
llvm_abiname: "".into(),
2762+
llvm_floatabi: None,
27222763
relax_elf_relocations: false,
27232764
llvm_args: cvs![],
27242765
use_ctors_section: false,

0 commit comments

Comments
 (0)