rustc_middle/middle/
codegen_fn_attrs.rs

1use std::borrow::Cow;
2
3use rustc_abi::Align;
4use rustc_ast::expand::autodiff_attrs::AutoDiffAttrs;
5use rustc_hir::attrs::{InlineAttr, InstructionSetAttr, OptimizeAttr};
6use rustc_macros::{HashStable, TyDecodable, TyEncodable};
7use rustc_span::Symbol;
8use rustc_target::spec::SanitizerSet;
9
10use crate::mir::mono::Linkage;
11use crate::ty::{InstanceKind, TyCtxt};
12
13impl<'tcx> TyCtxt<'tcx> {
14    pub fn codegen_instance_attrs(
15        self,
16        instance_kind: InstanceKind<'_>,
17    ) -> Cow<'tcx, CodegenFnAttrs> {
18        let mut attrs = Cow::Borrowed(self.codegen_fn_attrs(instance_kind.def_id()));
19
20        // Drop the `#[naked]` attribute on non-item `InstanceKind`s, like the shims that
21        // are generated for indirect function calls.
22        if !matches!(instance_kind, InstanceKind::Item(_)) {
23            if attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
24                attrs.to_mut().flags.remove(CodegenFnAttrFlags::NAKED);
25            }
26        }
27
28        attrs
29    }
30}
31
32#[derive(Clone, TyEncodable, TyDecodable, HashStable, Debug)]
33pub struct CodegenFnAttrs {
34    pub flags: CodegenFnAttrFlags,
35    /// Parsed representation of the `#[inline]` attribute
36    pub inline: InlineAttr,
37    /// Parsed representation of the `#[optimize]` attribute
38    pub optimize: OptimizeAttr,
39    /// The `#[export_name = "..."]` attribute, indicating a custom symbol a
40    /// function should be exported under
41    pub export_name: Option<Symbol>,
42    /// The `#[link_name = "..."]` attribute, indicating a custom symbol an
43    /// imported function should be imported as. Note that `export_name`
44    /// probably isn't set when this is set, this is for foreign items while
45    /// `#[export_name]` is for Rust-defined functions.
46    pub link_name: Option<Symbol>,
47    /// The `#[link_ordinal = "..."]` attribute, indicating an ordinal an
48    /// imported function has in the dynamic library. Note that this must not
49    /// be set when `link_name` is set. This is for foreign items with the
50    /// "raw-dylib" kind.
51    pub link_ordinal: Option<u16>,
52    /// The `#[target_feature(enable = "...")]` attribute and the enabled
53    /// features (only enabled features are supported right now).
54    /// Implied target features have already been applied.
55    pub target_features: Vec<TargetFeature>,
56    /// Whether the function was declared safe, but has target features
57    pub safe_target_features: bool,
58    /// The `#[linkage = "..."]` attribute on Rust-defined items and the value we found.
59    pub linkage: Option<Linkage>,
60    /// The `#[linkage = "..."]` attribute on foreign items and the value we found.
61    pub import_linkage: Option<Linkage>,
62    /// The `#[link_section = "..."]` attribute, or what executable section this
63    /// should be placed in.
64    pub link_section: Option<Symbol>,
65    /// The `#[no_sanitize(...)]` attribute. Indicates sanitizers for which
66    /// instrumentation should be disabled inside the annotated function.
67    pub no_sanitize: SanitizerSet,
68    /// The `#[instruction_set(set)]` attribute. Indicates if the generated code should
69    /// be generated against a specific instruction set. Only usable on architectures which allow
70    /// switching between multiple instruction sets.
71    pub instruction_set: Option<InstructionSetAttr>,
72    /// The `#[align(...)]` attribute. Determines the alignment of the function body.
73    // FIXME(#82232, #143834): temporarily renamed to mitigate `#[align]` nameres ambiguity
74    pub alignment: Option<Align>,
75    /// The `#[patchable_function_entry(...)]` attribute. Indicates how many nops should be around
76    /// the function entry.
77    pub patchable_function_entry: Option<PatchableFunctionEntry>,
78    /// For the `#[autodiff]` macros.
79    pub autodiff_item: Option<AutoDiffAttrs>,
80}
81
82#[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, HashStable)]
83pub struct TargetFeature {
84    /// The name of the target feature (e.g. "avx")
85    pub name: Symbol,
86    /// The feature is implied by another feature, rather than explicitly added by the
87    /// `#[target_feature]` attribute
88    pub implied: bool,
89}
90
91#[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, HashStable)]
92pub struct PatchableFunctionEntry {
93    /// Nops to prepend to the function
94    prefix: u8,
95    /// Nops after entry, but before body
96    entry: u8,
97}
98
99impl PatchableFunctionEntry {
100    pub fn from_config(config: rustc_session::config::PatchableFunctionEntry) -> Self {
101        Self { prefix: config.prefix(), entry: config.entry() }
102    }
103    pub fn from_prefix_and_entry(prefix: u8, entry: u8) -> Self {
104        Self { prefix, entry }
105    }
106    pub fn prefix(&self) -> u8 {
107        self.prefix
108    }
109    pub fn entry(&self) -> u8 {
110        self.entry
111    }
112}
113
114#[derive(Clone, Copy, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
115pub struct CodegenFnAttrFlags(u32);
116bitflags::bitflags! {
117    impl CodegenFnAttrFlags: u32 {
118        /// `#[cold]`: a hint to LLVM that this function, when called, is never on
119        /// the hot path.
120        const COLD                      = 1 << 0;
121        /// `#[rustc_nounwind]`: An indicator that function will never unwind.
122        const NEVER_UNWIND              = 1 << 1;
123        /// `#[naked]`: an indicator to LLVM that no function prologue/epilogue
124        /// should be generated.
125        const NAKED                     = 1 << 2;
126        /// `#[no_mangle]`: an indicator that the function's name should be the same
127        /// as its symbol.
128        const NO_MANGLE                 = 1 << 3;
129        /// `#[rustc_std_internal_symbol]`: an indicator that this symbol is a
130        /// "weird symbol" for the standard library in that it has slightly
131        /// different linkage, visibility, and reachability rules.
132        const RUSTC_STD_INTERNAL_SYMBOL = 1 << 4;
133        /// `#[thread_local]`: indicates a static is actually a thread local
134        /// piece of memory
135        const THREAD_LOCAL              = 1 << 5;
136        /// `#[used(compiler)]`: indicates that LLVM can't eliminate this function (but the
137        /// linker can!).
138        const USED_COMPILER             = 1 << 6;
139        /// `#[used(linker)]`:
140        /// indicates that neither LLVM nor the linker will eliminate this function.
141        const USED_LINKER               = 1 << 7;
142        /// `#[track_caller]`: allow access to the caller location
143        const TRACK_CALLER              = 1 << 8;
144        /// #[ffi_pure]: applies clang's `pure` attribute to a foreign function
145        /// declaration.
146        const FFI_PURE                  = 1 << 9;
147        /// #[ffi_const]: applies clang's `const` attribute to a foreign function
148        /// declaration.
149        const FFI_CONST                 = 1 << 10;
150        /// `#[rustc_allocator]`: a hint to LLVM that the pointer returned from this
151        /// function is never null and the function has no side effects other than allocating.
152        const ALLOCATOR                 = 1 << 11;
153        /// `#[rustc_deallocator]`: a hint to LLVM that the function only deallocates memory.
154        const DEALLOCATOR               = 1 << 12;
155        /// `#[rustc_reallocator]`: a hint to LLVM that the function only reallocates memory.
156        const REALLOCATOR               = 1 << 13;
157        /// `#[rustc_allocator_zeroed]`: a hint to LLVM that the function only allocates zeroed memory.
158        const ALLOCATOR_ZEROED          = 1 << 14;
159        /// `#[no_builtins]`: indicates that disable implicit builtin knowledge of functions for the function.
160        const NO_BUILTINS               = 1 << 15;
161    }
162}
163rustc_data_structures::external_bitflags_debug! { CodegenFnAttrFlags }
164
165impl CodegenFnAttrs {
166    pub const EMPTY: &'static Self = &Self::new();
167
168    pub const fn new() -> CodegenFnAttrs {
169        CodegenFnAttrs {
170            flags: CodegenFnAttrFlags::empty(),
171            inline: InlineAttr::None,
172            optimize: OptimizeAttr::Default,
173            export_name: None,
174            link_name: None,
175            link_ordinal: None,
176            target_features: vec![],
177            safe_target_features: false,
178            linkage: None,
179            import_linkage: None,
180            link_section: None,
181            no_sanitize: SanitizerSet::empty(),
182            instruction_set: None,
183            alignment: None,
184            patchable_function_entry: None,
185            autodiff_item: None,
186        }
187    }
188
189    /// Returns `true` if it looks like this symbol needs to be exported, for example:
190    ///
191    /// * `#[no_mangle]` is present
192    /// * `#[export_name(...)]` is present
193    /// * `#[linkage]` is present
194    ///
195    /// Keep this in sync with the logic for the unused_attributes for `#[inline]` lint.
196    pub fn contains_extern_indicator(&self) -> bool {
197        self.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
198            || self.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)
199            || self.export_name.is_some()
200            || match self.linkage {
201                // These are private, so make sure we don't try to consider
202                // them external.
203                None | Some(Linkage::Internal) => false,
204                Some(_) => true,
205            }
206    }
207}