|
1 | | -From 3f3b1288db7f4e21337ddfa107e42211dafebee0 Mon Sep 17 00:00:00 2001 |
| 1 | +From 874b53e68456b421b3a0bf7e2f48a2390758e71d Mon Sep 17 00:00:00 2001 |
2 | 2 | From: WANG Rui < [email protected]> |
3 | 3 | Date: Wed, 3 Sep 2025 11:34:15 +0800 |
4 | 4 | Subject: [PATCH 4/4] compiler: Apply target features to the entry function |
5 | 5 |
|
6 | 6 | --- |
7 | | - compiler/rustc_codegen_gcc/src/context.rs | 4 ++++ |
8 | | - compiler/rustc_codegen_llvm/src/context.rs | 10 ++++++++++ |
9 | | - compiler/rustc_codegen_ssa/src/base.rs | 1 + |
10 | | - compiler/rustc_codegen_ssa/src/traits/misc.rs | 2 ++ |
11 | | - 4 files changed, 17 insertions(+) |
| 7 | + compiler/rustc_codegen_llvm/src/attributes.rs | 21 ++++++++++++------- |
| 8 | + compiler/rustc_codegen_llvm/src/context.rs | 8 +++++-- |
| 9 | + 2 files changed, 20 insertions(+), 9 deletions(-) |
12 | 10 |
|
13 | | -diff --git a/compiler/rustc_codegen_gcc/src/context.rs b/compiler/rustc_codegen_gcc/src/context.rs |
14 | | -index ff141ad365..bf873cd156 100644 |
15 | | ---- a/compiler/rustc_codegen_gcc/src/context.rs |
16 | | -+++ b/compiler/rustc_codegen_gcc/src/context.rs |
17 | | -@@ -489,6 +489,10 @@ impl<'gcc, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> { |
18 | | - // TODO(antoyo) |
19 | | - } |
| 11 | +diff --git a/compiler/rustc_codegen_llvm/src/attributes.rs b/compiler/rustc_codegen_llvm/src/attributes.rs |
| 12 | +index adb53e0b66..3313b1f4c4 100644 |
| 13 | +--- a/compiler/rustc_codegen_llvm/src/attributes.rs |
| 14 | ++++ b/compiler/rustc_codegen_llvm/src/attributes.rs |
| 15 | +@@ -302,6 +302,19 @@ pub(crate) fn tune_cpu_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribu |
| 16 | + .map(|tune_cpu| llvm::CreateAttrStringValue(cx.llcx, "tune-cpu", tune_cpu)) |
| 17 | + } |
20 | 18 |
|
21 | | -+ fn apply_target_features(&self, _llfn: Function<'gcc>) { |
22 | | -+ // TODO(antoyo) |
23 | | -+ } |
| 19 | ++/// Get the `target-features` LLVM attribute. |
| 20 | ++pub(crate) fn target_features_attr<'ll>( |
| 21 | ++ cx: &CodegenCx<'ll, '_>, |
| 22 | ++ function_features: Vec<String>, |
| 23 | ++) -> Option<&'ll Attribute> { |
| 24 | ++ let global_features = cx.tcx.global_backend_features(()).iter().map(String::as_str); |
| 25 | ++ let function_features = function_features.iter().map(String::as_str); |
| 26 | ++ let target_features = |
| 27 | ++ global_features.chain(function_features).intersperse(",").collect::<String>(); |
| 28 | ++ (!target_features.is_empty()) |
| 29 | ++ .then(|| llvm::CreateAttrStringValue(cx.llcx, "target-features", &target_features)) |
| 30 | ++} |
24 | 31 | + |
25 | | - fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function> { |
26 | | - let entry_name = self.sess().target.entry_name.as_ref(); |
27 | | - if !self.functions.borrow().contains_key(entry_name) { |
| 32 | + /// Get the `NonLazyBind` LLVM attribute, |
| 33 | + /// if the codegen options allow skipping the PLT. |
| 34 | + pub(crate) fn non_lazy_bind_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> { |
| 35 | +@@ -535,13 +548,7 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>( |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | +- let global_features = cx.tcx.global_backend_features(()).iter().map(|s| s.as_str()); |
| 40 | +- let function_features = function_features.iter().map(|s| s.as_str()); |
| 41 | +- let target_features: String = |
| 42 | +- global_features.chain(function_features).intersperse(",").collect(); |
| 43 | +- if !target_features.is_empty() { |
| 44 | +- to_add.push(llvm::CreateAttrStringValue(cx.llcx, "target-features", &target_features)); |
| 45 | +- } |
| 46 | ++ to_add.extend(target_features_attr(cx, function_features)); |
| 47 | + |
| 48 | + attributes::apply_to_llfn(llfn, Function, &to_add); |
| 49 | + } |
28 | 50 | diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs |
29 | | -index 0324dff6ff..029855700d 100644 |
| 51 | +index 0324dff6ff..55d335923d 100644 |
30 | 52 | --- a/compiler/rustc_codegen_llvm/src/context.rs |
31 | 53 | +++ b/compiler/rustc_codegen_llvm/src/context.rs |
32 | | -@@ -819,6 +819,16 @@ impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> { |
33 | | - attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &attrs); |
34 | | - } |
35 | | - |
36 | | -+ fn apply_target_features(&self, llfn: &'ll Value) { |
37 | | -+ let mut attrs = SmallVec::<[_; 2]>::new(); |
38 | | -+ let global_features = self.tcx.global_backend_features(()).iter().map(|s| s.as_str()); |
39 | | -+ let target_features: String = global_features.intersperse(",").collect(); |
40 | | -+ if !target_features.is_empty() { |
41 | | -+ attrs.push(llvm::CreateAttrStringValue(self.llcx, "target-features", &target_features)); |
42 | | -+ } |
43 | | -+ attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &attrs); |
44 | | -+ } |
45 | | -+ |
| 54 | +@@ -822,7 +822,7 @@ impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> { |
46 | 55 | fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function> { |
47 | 56 | let entry_name = self.sess().target.entry_name.as_ref(); |
48 | 57 | if self.get_declared_value(entry_name).is_none() { |
49 | | -diff --git a/compiler/rustc_codegen_ssa/src/base.rs b/compiler/rustc_codegen_ssa/src/base.rs |
50 | | -index a3d6c73ba8..9b1a734c6b 100644 |
51 | | ---- a/compiler/rustc_codegen_ssa/src/base.rs |
52 | | -+++ b/compiler/rustc_codegen_ssa/src/base.rs |
53 | | -@@ -546,6 +546,7 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( |
54 | | - // `main` should respect same config for frame pointer elimination as rest of code |
55 | | - cx.set_frame_pointer_type(llfn); |
56 | | - cx.apply_target_cpu_attr(llfn); |
57 | | -+ cx.apply_target_features(llfn); |
58 | | - |
59 | | - let llbb = Bx::append_block(cx, llfn, "top"); |
60 | | - let mut bx = Bx::build(cx, llbb); |
61 | | -diff --git a/compiler/rustc_codegen_ssa/src/traits/misc.rs b/compiler/rustc_codegen_ssa/src/traits/misc.rs |
62 | | -index 710fab2790..24218d4ed0 100644 |
63 | | ---- a/compiler/rustc_codegen_ssa/src/traits/misc.rs |
64 | | -+++ b/compiler/rustc_codegen_ssa/src/traits/misc.rs |
65 | | -@@ -23,6 +23,8 @@ pub trait MiscCodegenMethods<'tcx>: BackendTypes { |
66 | | - fn sess(&self) -> &Session; |
67 | | - fn set_frame_pointer_type(&self, llfn: Self::Function); |
68 | | - fn apply_target_cpu_attr(&self, llfn: Self::Function); |
69 | | -+ /// Apply global target features to a given LLVM function. |
70 | | -+ fn apply_target_features(&self, llfn: Self::Function); |
71 | | - /// Declares the extern "C" main function for the entry point. Returns None if the symbol |
72 | | - /// already exists. |
73 | | - fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function>; |
| 58 | +- Some(self.declare_entry_fn( |
| 59 | ++ let llfn = self.declare_entry_fn( |
| 60 | + entry_name, |
| 61 | + llvm::CallConv::from_conv( |
| 62 | + self.sess().target.entry_abi, |
| 63 | +@@ -830,7 +830,11 @@ impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> { |
| 64 | + ), |
| 65 | + llvm::UnnamedAddr::Global, |
| 66 | + fn_type, |
| 67 | +- )) |
| 68 | ++ ); |
| 69 | ++ if let Some(attr) = attributes::target_features_attr(self, vec![]) { |
| 70 | ++ attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[attr]) |
| 71 | ++ } |
| 72 | ++ Some(llfn) |
| 73 | + } else { |
| 74 | + // If the symbol already exists, it is an error: for example, the user wrote |
| 75 | + // #[no_mangle] extern "C" fn main(..) {..} |
74 | 76 | -- |
75 | 77 | 2.51.0 |
76 | 78 |
|
0 commit comments