|
| 1 | +From 3f3b1288db7f4e21337ddfa107e42211dafebee0 Mon Sep 17 00:00:00 2001 |
| 2 | +From: WANG Rui < [email protected]> |
| 3 | +Date: Wed, 3 Sep 2025 11:34:15 +0800 |
| 4 | +Subject: [PATCH 4/4] compiler: Apply target features to the entry function |
| 5 | + |
| 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(+) |
| 12 | + |
| 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 | + } |
| 20 | + |
| 21 | ++ fn apply_target_features(&self, _llfn: Function<'gcc>) { |
| 22 | ++ // TODO(antoyo) |
| 23 | ++ } |
| 24 | ++ |
| 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) { |
| 28 | +diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs |
| 29 | +index 0324dff6ff..029855700d 100644 |
| 30 | +--- a/compiler/rustc_codegen_llvm/src/context.rs |
| 31 | ++++ 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 | ++ |
| 46 | + fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function> { |
| 47 | + let entry_name = self.sess().target.entry_name.as_ref(); |
| 48 | + 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>; |
| 74 | +-- |
| 75 | +2.51.0 |
| 76 | + |
0 commit comments