2024-11-02 19:33:00 -07:00
|
|
|
use rustc_abi::Align;
|
2025-01-29 21:31:13 -05:00
|
|
|
use rustc_ast::expand::autodiff_attrs::AutoDiffAttrs;
|
2025-02-12 15:04:26 +01:00
|
|
|
use rustc_attr_data_structures::{InlineAttr, InstructionSetAttr, OptimizeAttr};
|
2024-04-29 11:14:55 +10:00
|
|
|
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
|
2024-12-13 10:29:23 +11:00
|
|
|
use rustc_span::Symbol;
|
2021-02-07 23:47:03 +02:00
|
|
|
use rustc_target::spec::SanitizerSet;
|
2019-12-24 05:30:02 +01:00
|
|
|
|
|
|
|
|
use crate::mir::mono::Linkage;
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2021-01-03 09:19:16 -05:00
|
|
|
#[derive(Clone, TyEncodable, TyDecodable, HashStable, Debug)]
|
2019-12-24 05:30:02 +01:00
|
|
|
pub struct CodegenFnAttrs {
|
|
|
|
|
pub flags: CodegenFnAttrFlags,
|
|
|
|
|
/// Parsed representation of the `#[inline]` attribute
|
|
|
|
|
pub inline: InlineAttr,
|
|
|
|
|
/// Parsed representation of the `#[optimize]` attribute
|
|
|
|
|
pub optimize: OptimizeAttr,
|
|
|
|
|
/// The `#[export_name = "..."]` attribute, indicating a custom symbol a
|
|
|
|
|
/// function should be exported under
|
|
|
|
|
pub export_name: Option<Symbol>,
|
|
|
|
|
/// The `#[link_name = "..."]` attribute, indicating a custom symbol an
|
|
|
|
|
/// imported function should be imported as. Note that `export_name`
|
|
|
|
|
/// probably isn't set when this is set, this is for foreign items while
|
|
|
|
|
/// `#[export_name]` is for Rust-defined functions.
|
|
|
|
|
pub link_name: Option<Symbol>,
|
|
|
|
|
/// The `#[link_ordinal = "..."]` attribute, indicating an ordinal an
|
|
|
|
|
/// imported function has in the dynamic library. Note that this must not
|
|
|
|
|
/// be set when `link_name` is set. This is for foreign items with the
|
|
|
|
|
/// "raw-dylib" kind.
|
2021-09-10 17:34:09 -07:00
|
|
|
pub link_ordinal: Option<u16>,
|
2024-09-01 16:35:53 +02:00
|
|
|
/// The `#[target_feature(enable = "...")]` attribute and the enabled
|
|
|
|
|
/// features (only enabled features are supported right now).
|
2024-12-26 18:32:22 +01:00
|
|
|
/// Implied target features have already been applied.
|
2024-08-04 23:51:59 -04:00
|
|
|
pub target_features: Vec<TargetFeature>,
|
2024-12-13 12:19:46 +00:00
|
|
|
/// Whether the function was declared safe, but has target features
|
|
|
|
|
pub safe_target_features: bool,
|
Move linkage type check to HIR analysis and fix semantics issues.
This ensures that the error is printed even for unused variables,
as well as unifying the handling between the LLVM and GCC backends.
This also fixes unusual behavior around exported Rust-defined variables
with linkage attributes. With the previous behavior, it appears to be
impossible to define such a variable such that it can actually be imported
and used by another crate. This is because on the importing side, the
variable is required to be a pointer, but on the exporting side, the
type checker rejects static variables of pointer type because they do
not implement `Sync`. Even if it were possible to import such a type, it
appears that code generation on the importing side would add an unexpected
additional level of pointer indirection, which would break type safety.
This highlighted that the semantics of linkage on Rust-defined variables
is different to linkage on foreign items. As such, we now model the
difference with two different codegen attributes: linkage for Rust-defined
variables, and import_linkage for foreign items.
This change gives semantics to the test
src/test/ui/linkage-attr/auxiliary/def_illtyped_external.rs which was
previously expected to fail to compile. Therefore, convert it into a
test that is expected to successfully compile.
The update to the GCC backend is speculative and untested.
2022-11-23 18:13:30 -08:00
|
|
|
/// The `#[linkage = "..."]` attribute on Rust-defined items and the value we found.
|
2019-12-24 05:30:02 +01:00
|
|
|
pub linkage: Option<Linkage>,
|
Move linkage type check to HIR analysis and fix semantics issues.
This ensures that the error is printed even for unused variables,
as well as unifying the handling between the LLVM and GCC backends.
This also fixes unusual behavior around exported Rust-defined variables
with linkage attributes. With the previous behavior, it appears to be
impossible to define such a variable such that it can actually be imported
and used by another crate. This is because on the importing side, the
variable is required to be a pointer, but on the exporting side, the
type checker rejects static variables of pointer type because they do
not implement `Sync`. Even if it were possible to import such a type, it
appears that code generation on the importing side would add an unexpected
additional level of pointer indirection, which would break type safety.
This highlighted that the semantics of linkage on Rust-defined variables
is different to linkage on foreign items. As such, we now model the
difference with two different codegen attributes: linkage for Rust-defined
variables, and import_linkage for foreign items.
This change gives semantics to the test
src/test/ui/linkage-attr/auxiliary/def_illtyped_external.rs which was
previously expected to fail to compile. Therefore, convert it into a
test that is expected to successfully compile.
The update to the GCC backend is speculative and untested.
2022-11-23 18:13:30 -08:00
|
|
|
/// The `#[linkage = "..."]` attribute on foreign items and the value we found.
|
|
|
|
|
pub import_linkage: Option<Linkage>,
|
2019-12-24 05:30:02 +01:00
|
|
|
/// The `#[link_section = "..."]` attribute, or what executable section this
|
|
|
|
|
/// should be placed in.
|
|
|
|
|
pub link_section: Option<Symbol>,
|
2020-06-14 00:00:00 +00:00
|
|
|
/// The `#[no_sanitize(...)]` attribute. Indicates sanitizers for which
|
|
|
|
|
/// instrumentation should be disabled inside the annotated function.
|
|
|
|
|
pub no_sanitize: SanitizerSet,
|
2020-10-08 23:23:27 +01:00
|
|
|
/// The `#[instruction_set(set)]` attribute. Indicates if the generated code should
|
|
|
|
|
/// be generated against a specific instruction set. Only usable on architectures which allow
|
|
|
|
|
/// switching between multiple instruction sets.
|
|
|
|
|
pub instruction_set: Option<InstructionSetAttr>,
|
2021-01-20 21:49:04 -05:00
|
|
|
/// The `#[repr(align(...))]` attribute. Indicates the value of which the function should be
|
|
|
|
|
/// aligned to.
|
2024-03-24 01:03:39 +00:00
|
|
|
pub alignment: Option<Align>,
|
2023-12-12 13:37:04 -08:00
|
|
|
/// The `#[patchable_function_entry(...)]` attribute. Indicates how many nops should be around
|
|
|
|
|
/// the function entry.
|
|
|
|
|
pub patchable_function_entry: Option<PatchableFunctionEntry>,
|
2025-01-29 21:31:13 -05:00
|
|
|
/// For the `#[autodiff]` macros.
|
|
|
|
|
pub autodiff_item: Option<AutoDiffAttrs>,
|
2019-12-24 05:30:02 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-12 13:32:43 -08:00
|
|
|
#[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, HashStable)]
|
2024-08-04 23:51:59 -04:00
|
|
|
pub struct TargetFeature {
|
|
|
|
|
/// The name of the target feature (e.g. "avx")
|
|
|
|
|
pub name: Symbol,
|
2024-09-01 16:35:53 +02:00
|
|
|
/// The feature is implied by another feature, rather than explicitly added by the
|
|
|
|
|
/// `#[target_feature]` attribute
|
2024-08-04 23:51:59 -04:00
|
|
|
pub implied: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, HashStable)]
|
2023-12-12 13:32:43 -08:00
|
|
|
pub struct PatchableFunctionEntry {
|
|
|
|
|
/// Nops to prepend to the function
|
|
|
|
|
prefix: u8,
|
|
|
|
|
/// Nops after entry, but before body
|
|
|
|
|
entry: u8,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PatchableFunctionEntry {
|
|
|
|
|
pub fn from_config(config: rustc_session::config::PatchableFunctionEntry) -> Self {
|
|
|
|
|
Self { prefix: config.prefix(), entry: config.entry() }
|
|
|
|
|
}
|
|
|
|
|
pub fn from_prefix_and_entry(prefix: u8, entry: u8) -> Self {
|
|
|
|
|
Self { prefix, entry }
|
|
|
|
|
}
|
|
|
|
|
pub fn prefix(&self) -> u8 {
|
|
|
|
|
self.prefix
|
|
|
|
|
}
|
|
|
|
|
pub fn entry(&self) -> u8 {
|
|
|
|
|
self.entry
|
|
|
|
|
}
|
2019-12-24 05:30:02 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-30 17:09:02 +01:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
|
|
|
|
|
pub struct CodegenFnAttrFlags(u32);
|
2024-04-29 16:11:04 +10:00
|
|
|
bitflags::bitflags! {
|
2023-12-30 17:09:02 +01:00
|
|
|
impl CodegenFnAttrFlags: u32 {
|
2019-12-24 05:30:02 +01:00
|
|
|
/// `#[cold]`: a hint to LLVM that this function, when called, is never on
|
|
|
|
|
/// the hot path.
|
|
|
|
|
const COLD = 1 << 0;
|
2025-05-24 19:50:11 +02:00
|
|
|
/// `#[rustc_nounwind]`: An indicator that function will never unwind.
|
|
|
|
|
const NEVER_UNWIND = 1 << 1;
|
2019-12-24 05:30:02 +01:00
|
|
|
/// `#[naked]`: an indicator to LLVM that no function prologue/epilogue
|
|
|
|
|
/// should be generated.
|
2025-05-24 19:50:11 +02:00
|
|
|
const NAKED = 1 << 2;
|
2019-12-24 05:30:02 +01:00
|
|
|
/// `#[no_mangle]`: an indicator that the function's name should be the same
|
|
|
|
|
/// as its symbol.
|
2025-05-24 19:50:11 +02:00
|
|
|
const NO_MANGLE = 1 << 3;
|
2019-12-24 05:30:02 +01:00
|
|
|
/// `#[rustc_std_internal_symbol]`: an indicator that this symbol is a
|
|
|
|
|
/// "weird symbol" for the standard library in that it has slightly
|
|
|
|
|
/// different linkage, visibility, and reachability rules.
|
2025-05-24 19:50:11 +02:00
|
|
|
const RUSTC_STD_INTERNAL_SYMBOL = 1 << 4;
|
2019-12-24 05:30:02 +01:00
|
|
|
/// `#[thread_local]`: indicates a static is actually a thread local
|
|
|
|
|
/// piece of memory
|
2025-05-24 19:50:11 +02:00
|
|
|
const THREAD_LOCAL = 1 << 5;
|
|
|
|
|
/// `#[used(compiler)]`: indicates that LLVM can't eliminate this function (but the
|
2019-12-24 05:30:02 +01:00
|
|
|
/// linker can!).
|
2025-05-24 19:50:11 +02:00
|
|
|
const USED_COMPILER = 1 << 6;
|
|
|
|
|
/// `#[used(linker)]`:
|
|
|
|
|
/// indicates that neither LLVM nor the linker will eliminate this function.
|
|
|
|
|
const USED_LINKER = 1 << 7;
|
2019-12-24 05:30:02 +01:00
|
|
|
/// `#[track_caller]`: allow access to the caller location
|
2025-05-24 19:50:11 +02:00
|
|
|
const TRACK_CALLER = 1 << 8;
|
2020-04-14 00:19:46 +02:00
|
|
|
/// #[ffi_pure]: applies clang's `pure` attribute to a foreign function
|
|
|
|
|
/// declaration.
|
2025-05-24 19:50:11 +02:00
|
|
|
const FFI_PURE = 1 << 9;
|
2020-04-14 00:19:46 +02:00
|
|
|
/// #[ffi_const]: applies clang's `const` attribute to a foreign function
|
|
|
|
|
/// declaration.
|
2025-05-24 19:50:11 +02:00
|
|
|
const FFI_CONST = 1 << 10;
|
|
|
|
|
/// `#[rustc_allocator]`: a hint to LLVM that the pointer returned from this
|
|
|
|
|
/// function is never null and the function has no side effects other than allocating.
|
|
|
|
|
const ALLOCATOR = 1 << 11;
|
2022-03-21 15:30:54 -04:00
|
|
|
/// `#[rustc_deallocator]`: a hint to LLVM that the function only deallocates memory.
|
2025-05-24 19:50:11 +02:00
|
|
|
const DEALLOCATOR = 1 << 12;
|
2022-03-21 15:30:54 -04:00
|
|
|
/// `#[rustc_reallocator]`: a hint to LLVM that the function only reallocates memory.
|
2025-05-24 19:50:11 +02:00
|
|
|
const REALLOCATOR = 1 << 13;
|
2022-03-21 15:30:54 -04:00
|
|
|
/// `#[rustc_allocator_zeroed]`: a hint to LLVM that the function only allocates zeroed memory.
|
2025-05-24 19:50:11 +02:00
|
|
|
const ALLOCATOR_ZEROED = 1 << 14;
|
2023-07-18 22:15:47 +08:00
|
|
|
/// `#[no_builtins]`: indicates that disable implicit builtin knowledge of functions for the function.
|
2025-05-24 19:50:11 +02:00
|
|
|
const NO_BUILTINS = 1 << 15;
|
2019-12-24 05:30:02 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-12-30 17:09:02 +01:00
|
|
|
rustc_data_structures::external_bitflags_debug! { CodegenFnAttrFlags }
|
2019-12-24 05:30:02 +01:00
|
|
|
|
|
|
|
|
impl CodegenFnAttrs {
|
2022-05-04 11:18:37 +02:00
|
|
|
pub const EMPTY: &'static Self = &Self::new();
|
|
|
|
|
|
|
|
|
|
pub const fn new() -> CodegenFnAttrs {
|
2019-12-24 05:30:02 +01:00
|
|
|
CodegenFnAttrs {
|
|
|
|
|
flags: CodegenFnAttrFlags::empty(),
|
|
|
|
|
inline: InlineAttr::None,
|
2025-01-24 19:34:01 +00:00
|
|
|
optimize: OptimizeAttr::Default,
|
2019-12-24 05:30:02 +01:00
|
|
|
export_name: None,
|
|
|
|
|
link_name: None,
|
|
|
|
|
link_ordinal: None,
|
|
|
|
|
target_features: vec![],
|
2024-12-13 12:19:46 +00:00
|
|
|
safe_target_features: false,
|
2019-12-24 05:30:02 +01:00
|
|
|
linkage: None,
|
Move linkage type check to HIR analysis and fix semantics issues.
This ensures that the error is printed even for unused variables,
as well as unifying the handling between the LLVM and GCC backends.
This also fixes unusual behavior around exported Rust-defined variables
with linkage attributes. With the previous behavior, it appears to be
impossible to define such a variable such that it can actually be imported
and used by another crate. This is because on the importing side, the
variable is required to be a pointer, but on the exporting side, the
type checker rejects static variables of pointer type because they do
not implement `Sync`. Even if it were possible to import such a type, it
appears that code generation on the importing side would add an unexpected
additional level of pointer indirection, which would break type safety.
This highlighted that the semantics of linkage on Rust-defined variables
is different to linkage on foreign items. As such, we now model the
difference with two different codegen attributes: linkage for Rust-defined
variables, and import_linkage for foreign items.
This change gives semantics to the test
src/test/ui/linkage-attr/auxiliary/def_illtyped_external.rs which was
previously expected to fail to compile. Therefore, convert it into a
test that is expected to successfully compile.
The update to the GCC backend is speculative and untested.
2022-11-23 18:13:30 -08:00
|
|
|
import_linkage: None,
|
2019-12-24 05:30:02 +01:00
|
|
|
link_section: None,
|
2020-06-14 00:00:00 +00:00
|
|
|
no_sanitize: SanitizerSet::empty(),
|
2020-10-08 23:23:27 +01:00
|
|
|
instruction_set: None,
|
2021-01-20 21:49:04 -05:00
|
|
|
alignment: None,
|
2023-12-12 13:37:04 -08:00
|
|
|
patchable_function_entry: None,
|
2025-01-29 21:31:13 -05:00
|
|
|
autodiff_item: None,
|
2019-12-24 05:30:02 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns `true` if it looks like this symbol needs to be exported, for example:
|
|
|
|
|
///
|
|
|
|
|
/// * `#[no_mangle]` is present
|
|
|
|
|
/// * `#[export_name(...)]` is present
|
|
|
|
|
/// * `#[linkage]` is present
|
2025-03-22 22:29:23 +01:00
|
|
|
///
|
|
|
|
|
/// Keep this in sync with the logic for the unused_attributes for `#[inline]` lint.
|
2019-12-24 05:30:02 +01:00
|
|
|
pub fn contains_extern_indicator(&self) -> bool {
|
|
|
|
|
self.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
|
2023-12-02 13:50:44 +00:00
|
|
|
|| self.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)
|
2019-12-24 05:30:02 +01:00
|
|
|
|| self.export_name.is_some()
|
|
|
|
|
|| match self.linkage {
|
|
|
|
|
// These are private, so make sure we don't try to consider
|
|
|
|
|
// them external.
|
2025-02-07 14:47:21 +00:00
|
|
|
None | Some(Linkage::Internal) => false,
|
2019-12-24 05:30:02 +01:00
|
|
|
Some(_) => true,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|