2024-02-28 22:06:24 +00:00
|
|
|
#[cfg(feature = "master")]
|
2023-03-05 17:03:19 +00:00
|
|
|
use gccjit::FnAttribute;
|
|
|
|
use gccjit::Function;
|
2024-02-28 22:06:24 +00:00
|
|
|
#[cfg(feature = "master")]
|
2024-12-13 13:47:11 +00:00
|
|
|
use rustc_attr_parsing::InlineAttr;
|
|
|
|
use rustc_attr_parsing::InstructionSetAttr;
|
2024-02-28 22:06:24 +00:00
|
|
|
#[cfg(feature = "master")]
|
2023-06-19 22:51:02 +00:00
|
|
|
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
|
2024-02-28 22:06:24 +00:00
|
|
|
use rustc_middle::ty;
|
2023-03-05 17:03:19 +00:00
|
|
|
|
2024-07-28 22:13:50 +00:00
|
|
|
use crate::context::CodegenCx;
|
2024-09-11 12:57:12 +00:00
|
|
|
use crate::gcc_util::to_gcc_features;
|
2023-03-05 17:03:19 +00:00
|
|
|
|
2023-06-19 22:51:02 +00:00
|
|
|
/// Get GCC attribute for the provided inline heuristic.
|
2024-02-28 22:06:24 +00:00
|
|
|
#[cfg(feature = "master")]
|
2023-06-19 22:51:02 +00:00
|
|
|
#[inline]
|
2024-03-16 13:26:16 +00:00
|
|
|
fn inline_attr<'gcc, 'tcx>(
|
|
|
|
cx: &CodegenCx<'gcc, 'tcx>,
|
|
|
|
inline: InlineAttr,
|
|
|
|
) -> Option<FnAttribute<'gcc>> {
|
2023-06-19 22:51:02 +00:00
|
|
|
match inline {
|
|
|
|
InlineAttr::Hint => Some(FnAttribute::Inline),
|
|
|
|
InlineAttr::Always => Some(FnAttribute::AlwaysInline),
|
|
|
|
InlineAttr::Never => {
|
|
|
|
if cx.sess().target.arch != "amdgpu" {
|
|
|
|
Some(FnAttribute::NoInline)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
InlineAttr::None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 17:03:19 +00:00
|
|
|
/// Composite function which sets GCC attributes for function depending on its AST (`#[attribute]`)
|
|
|
|
/// attributes.
|
|
|
|
pub fn from_fn_attrs<'gcc, 'tcx>(
|
|
|
|
cx: &CodegenCx<'gcc, 'tcx>,
|
2024-02-28 22:06:24 +00:00
|
|
|
#[cfg_attr(not(feature = "master"), allow(unused_variables))] func: Function<'gcc>,
|
2023-03-05 17:03:19 +00:00
|
|
|
instance: ty::Instance<'tcx>,
|
|
|
|
) {
|
|
|
|
let codegen_fn_attrs = cx.tcx.codegen_fn_attrs(instance.def_id());
|
|
|
|
|
2024-02-28 22:06:24 +00:00
|
|
|
#[cfg(feature = "master")]
|
2023-06-19 22:51:02 +00:00
|
|
|
{
|
2024-02-28 22:06:24 +00:00
|
|
|
let inline = if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
|
|
|
|
InlineAttr::Never
|
|
|
|
} else if codegen_fn_attrs.inline == InlineAttr::None
|
|
|
|
&& instance.def.requires_inline(cx.tcx)
|
|
|
|
{
|
|
|
|
InlineAttr::Hint
|
|
|
|
} else {
|
|
|
|
codegen_fn_attrs.inline
|
|
|
|
};
|
2023-06-19 22:51:02 +00:00
|
|
|
if let Some(attr) = inline_attr(cx, inline) {
|
2023-10-26 21:42:02 +00:00
|
|
|
if let FnAttribute::AlwaysInline = attr {
|
|
|
|
func.add_attribute(FnAttribute::Inline);
|
|
|
|
}
|
2023-06-19 22:51:02 +00:00
|
|
|
func.add_attribute(attr);
|
|
|
|
}
|
2023-10-09 19:53:34 +00:00
|
|
|
|
|
|
|
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
|
|
|
|
func.add_attribute(FnAttribute::Cold);
|
|
|
|
}
|
|
|
|
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::FFI_PURE) {
|
|
|
|
func.add_attribute(FnAttribute::Pure);
|
|
|
|
}
|
|
|
|
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::FFI_CONST) {
|
|
|
|
func.add_attribute(FnAttribute::Const);
|
|
|
|
}
|
2023-06-19 22:51:02 +00:00
|
|
|
}
|
|
|
|
|
2024-09-11 12:57:12 +00:00
|
|
|
let mut function_features = codegen_fn_attrs
|
2024-02-28 22:06:24 +00:00
|
|
|
.target_features
|
|
|
|
.iter()
|
2024-08-05 03:51:59 +00:00
|
|
|
.map(|features| features.name.as_str())
|
2023-03-05 17:03:19 +00:00
|
|
|
.flat_map(|feat| to_gcc_features(cx.tcx.sess, feat).into_iter())
|
2024-03-08 10:47:20 +00:00
|
|
|
.chain(codegen_fn_attrs.instruction_set.iter().map(|x| match *x {
|
2023-03-05 17:03:19 +00:00
|
|
|
InstructionSetAttr::ArmA32 => "-thumb-mode", // TODO(antoyo): support removing feature.
|
|
|
|
InstructionSetAttr::ArmT32 => "thumb-mode",
|
|
|
|
}))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2023-10-09 19:53:34 +00:00
|
|
|
// TODO(antoyo): cg_llvm adds global features to each function so that LTO keep them.
|
|
|
|
// Check if GCC requires the same.
|
2023-03-05 17:03:19 +00:00
|
|
|
let mut global_features = cx.tcx.global_backend_features(()).iter().map(|s| s.as_str());
|
|
|
|
function_features.extend(&mut global_features);
|
2023-10-09 19:53:34 +00:00
|
|
|
let target_features = function_features
|
|
|
|
.iter()
|
|
|
|
.filter_map(|feature| {
|
|
|
|
// FIXME(antoyo): for some reasons, disabling SSE results in the following error when
|
|
|
|
// compiling Rust for Linux:
|
|
|
|
// SSE register return with SSE disabled
|
|
|
|
// TODO(antoyo): support soft-float and retpoline-external-thunk.
|
2024-02-28 22:06:24 +00:00
|
|
|
if feature.contains("soft-float")
|
|
|
|
|| feature.contains("retpoline-external-thunk")
|
|
|
|
|| *feature == "-sse"
|
|
|
|
{
|
2023-10-09 19:53:34 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if feature.starts_with('-') {
|
|
|
|
Some(format!("no{}", feature))
|
2024-03-08 10:47:20 +00:00
|
|
|
} else if let Some(stripped) = feature.strip_prefix('+') {
|
|
|
|
Some(stripped.to_string())
|
2024-02-28 22:06:24 +00:00
|
|
|
} else {
|
2023-10-09 19:53:34 +00:00
|
|
|
Some(feature.to_string())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(",");
|
2023-03-05 17:03:19 +00:00
|
|
|
if !target_features.is_empty() {
|
2024-02-28 22:06:24 +00:00
|
|
|
#[cfg(feature = "master")]
|
2024-04-25 00:01:57 +00:00
|
|
|
match cx.sess().target.arch.as_ref() {
|
|
|
|
"x86" | "x86_64" | "powerpc" => {
|
|
|
|
func.add_attribute(FnAttribute::Target(&target_features))
|
|
|
|
}
|
|
|
|
// The target attribute is not supported on other targets in GCC.
|
|
|
|
_ => (),
|
|
|
|
}
|
2023-03-05 17:03:19 +00:00
|
|
|
}
|
|
|
|
}
|