Skip to content

Commit 7f26036

Browse files
committed
compiler: Apply target features to the entry function
1 parent 51ff895 commit 7f26036

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

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

289+
/// Get the `target-features` LLVM attribute.
290+
pub(crate) fn target_features_attr<'ll>(
291+
cx: &CodegenCx<'ll, '_>,
292+
function_features: Vec<String>,
293+
) -> Option<&'ll Attribute> {
294+
let global_features = cx.tcx.global_backend_features(()).iter().map(String::as_str);
295+
let function_features = function_features.iter().map(String::as_str);
296+
let target_features =
297+
global_features.chain(function_features).intersperse(",").collect::<String>();
298+
(!target_features.is_empty())
299+
.then(|| llvm::CreateAttrStringValue(cx.llcx, "target-features", &target_features))
300+
}
301+
289302
/// Get the `NonLazyBind` LLVM attribute,
290303
/// if the codegen options allow skipping the PLT.
291304
pub(crate) fn non_lazy_bind_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> {
@@ -513,13 +526,7 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
513526
}
514527
}
515528

516-
let global_features = cx.tcx.global_backend_features(()).iter().map(|s| s.as_str());
517-
let function_features = function_features.iter().map(|s| s.as_str());
518-
let target_features: String =
519-
global_features.chain(function_features).intersperse(",").collect();
520-
if !target_features.is_empty() {
521-
to_add.push(llvm::CreateAttrStringValue(cx.llcx, "target-features", &target_features));
522-
}
529+
to_add.extend(target_features_attr(cx, function_features));
523530

524531
attributes::apply_to_llfn(llfn, Function, &to_add);
525532
}

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,15 +849,19 @@ impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
849849
fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function> {
850850
let entry_name = self.sess().target.entry_name.as_ref();
851851
if self.get_declared_value(entry_name).is_none() {
852-
Some(self.declare_entry_fn(
852+
let llfn = self.declare_entry_fn(
853853
entry_name,
854854
llvm::CallConv::from_conv(
855855
self.sess().target.entry_abi,
856856
self.sess().target.arch.borrow(),
857857
),
858858
llvm::UnnamedAddr::Global,
859859
fn_type,
860-
))
860+
);
861+
if let Some(attr) = attributes::target_features_attr(self, vec![]) {
862+
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[attr])
863+
}
864+
Some(llfn)
861865
} else {
862866
// If the symbol already exists, it is an error: for example, the user wrote
863867
// #[no_mangle] extern "C" fn main(..) {..}

0 commit comments

Comments
 (0)