Skip to content

Commit f028b9b

Browse files
committed
compiler: Apply target features to the entry function
1 parent 033c0a4 commit f028b9b

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,19 @@ pub(crate) fn tune_cpu_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribu
296296
.map(|tune_cpu| llvm::CreateAttrStringValue(cx.llcx, "tune-cpu", tune_cpu))
297297
}
298298

299+
/// Get the `target-features` LLVM attribute.
300+
pub(crate) fn target_features_attr<'ll>(
301+
cx: &CodegenCx<'ll, '_>,
302+
function_features: Vec<String>,
303+
) -> Option<&'ll Attribute> {
304+
let global_features = cx.tcx.global_backend_features(()).iter().map(String::as_str);
305+
let function_features = function_features.iter().map(String::as_str);
306+
let target_features =
307+
global_features.chain(function_features).intersperse(",").collect::<String>();
308+
(!target_features.is_empty())
309+
.then(|| llvm::CreateAttrStringValue(cx.llcx, "target-features", &target_features))
310+
}
311+
299312
/// Get the `NonLazyBind` LLVM attribute,
300313
/// if the codegen options allow skipping the PLT.
301314
pub(crate) fn non_lazy_bind_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> {
@@ -523,14 +536,7 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
523536
}
524537
}
525538

526-
let global_features = cx.tcx.global_backend_features(()).iter().map(|s| s.as_str());
527-
let function_features = function_features.iter().map(|s| s.as_str());
528-
let target_features: String =
529-
global_features.chain(function_features).intersperse(",").collect();
530-
531-
if !target_features.is_empty() {
532-
to_add.push(llvm::CreateAttrStringValue(cx.llcx, "target-features", &target_features));
533-
}
539+
to_add.extend(target_features_attr(cx, function_features));
534540

535541
attributes::apply_to_llfn(llfn, Function, &to_add);
536542
}

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -853,15 +853,19 @@ impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
853853
fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function> {
854854
let entry_name = self.sess().target.entry_name.as_ref();
855855
if self.get_declared_value(entry_name).is_none() {
856-
Some(self.declare_entry_fn(
856+
let llfn = self.declare_entry_fn(
857857
entry_name,
858858
llvm::CallConv::from_conv(
859859
self.sess().target.entry_abi,
860860
self.sess().target.arch.borrow(),
861861
),
862862
llvm::UnnamedAddr::Global,
863863
fn_type,
864-
))
864+
);
865+
if let Some(attr) = attributes::target_features_attr(self, vec![]) {
866+
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[attr])
867+
}
868+
Some(llfn)
865869
} else {
866870
// If the symbol already exists, it is an error: for example, the user wrote
867871
// #[no_mangle] extern "C" fn main(..) {..}

0 commit comments

Comments
 (0)