extract rustc::middle::codegen_fn_attrs

This commit is contained in:
Mazdak Farrokhzad 2019-12-24 05:30:02 +01:00
parent c19ed3bc5b
commit 66f5bf1b8b
19 changed files with 151 additions and 149 deletions

View file

@ -10,7 +10,6 @@ pub use self::UnsafeSource::*;
use crate::hir::def::{DefKind, Res};
use crate::hir::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX};
use crate::mir::mono::Linkage;
use crate::ty::query::Providers;
use crate::util::nodemap::{FxHashSet, NodeMap};
@ -29,7 +28,6 @@ use syntax::ast::{self, AsmDialect, CrateSugar, Ident, Name, NodeId};
use syntax::ast::{AttrVec, Attribute, FloatTy, IntTy, Label, LitKind, StrStyle, UintTy};
pub use syntax::ast::{BorrowKind, ImplPolarity, IsAuto};
pub use syntax::ast::{CaptureBy, Constness, Movability, Mutability, Unsafety};
use syntax::attr::{InlineAttr, OptimizeAttr};
use syntax::tokenstream::TokenStream;
use syntax::util::parser::ExprPrecedence;
@ -2668,119 +2666,6 @@ pub fn provide(providers: &mut Providers<'_>) {
upvars::provide(providers);
}
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
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.
pub link_ordinal: Option<usize>,
/// The `#[target_feature(enable = "...")]` attribute and the enabled
/// features (only enabled features are supported right now).
pub target_features: Vec<Symbol>,
/// The `#[linkage = "..."]` attribute and the value we found.
pub linkage: Option<Linkage>,
/// The `#[link_section = "..."]` attribute, or what executable section this
/// should be placed in.
pub link_section: Option<Symbol>,
}
bitflags! {
#[derive(RustcEncodable, RustcDecodable, HashStable)]
pub struct CodegenFnAttrFlags: u32 {
/// `#[cold]`: a hint to LLVM that this function, when called, is never on
/// the hot path.
const COLD = 1 << 0;
/// `#[rustc_allocator]`: a hint to LLVM that the pointer returned from this
/// function is never null.
const ALLOCATOR = 1 << 1;
/// `#[unwind]`: an indicator that this function may unwind despite what
/// its ABI signature may otherwise imply.
const UNWIND = 1 << 2;
/// `#[rust_allocator_nounwind]`, an indicator that an imported FFI
/// function will never unwind. Probably obsolete by recent changes with
/// #[unwind], but hasn't been removed/migrated yet
const RUSTC_ALLOCATOR_NOUNWIND = 1 << 3;
/// `#[naked]`: an indicator to LLVM that no function prologue/epilogue
/// should be generated.
const NAKED = 1 << 4;
/// `#[no_mangle]`: an indicator that the function's name should be the same
/// as its symbol.
const NO_MANGLE = 1 << 5;
/// `#[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.
const RUSTC_STD_INTERNAL_SYMBOL = 1 << 6;
/// `#[no_debug]`: an indicator that no debugging information should be
/// generated for this function by LLVM.
const NO_DEBUG = 1 << 7;
/// `#[thread_local]`: indicates a static is actually a thread local
/// piece of memory
const THREAD_LOCAL = 1 << 8;
/// `#[used]`: indicates that LLVM can't eliminate this function (but the
/// linker can!).
const USED = 1 << 9;
/// `#[ffi_returns_twice]`, indicates that an extern function can return
/// multiple times
const FFI_RETURNS_TWICE = 1 << 10;
/// `#[track_caller]`: allow access to the caller location
const TRACK_CALLER = 1 << 11;
}
}
impl CodegenFnAttrs {
pub fn new() -> CodegenFnAttrs {
CodegenFnAttrs {
flags: CodegenFnAttrFlags::empty(),
inline: InlineAttr::None,
optimize: OptimizeAttr::None,
export_name: None,
link_name: None,
link_ordinal: None,
target_features: vec![],
linkage: None,
link_section: None,
}
}
/// Returns `true` if `#[inline]` or `#[inline(always)]` is present.
pub fn requests_inline(&self) -> bool {
match self.inline {
InlineAttr::Hint | InlineAttr::Always => true,
InlineAttr::None | InlineAttr::Never => false,
}
}
/// 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
pub fn contains_extern_indicator(&self) -> bool {
self.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
|| self.export_name.is_some()
|| match self.linkage {
// These are private, so make sure we don't try to consider
// them external.
None | Some(Linkage::Internal) | Some(Linkage::Private) => false,
Some(_) => true,
}
}
}
#[derive(Copy, Clone, Debug)]
pub enum Node<'hir> {
Param(&'hir Param<'hir>),

View file

@ -0,0 +1,116 @@
use crate::mir::mono::Linkage;
use rustc_span::symbol::Symbol;
use syntax::attr::{InlineAttr, OptimizeAttr};
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
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.
pub link_ordinal: Option<usize>,
/// The `#[target_feature(enable = "...")]` attribute and the enabled
/// features (only enabled features are supported right now).
pub target_features: Vec<Symbol>,
/// The `#[linkage = "..."]` attribute and the value we found.
pub linkage: Option<Linkage>,
/// The `#[link_section = "..."]` attribute, or what executable section this
/// should be placed in.
pub link_section: Option<Symbol>,
}
bitflags! {
#[derive(RustcEncodable, RustcDecodable, HashStable)]
pub struct CodegenFnAttrFlags: u32 {
/// `#[cold]`: a hint to LLVM that this function, when called, is never on
/// the hot path.
const COLD = 1 << 0;
/// `#[rustc_allocator]`: a hint to LLVM that the pointer returned from this
/// function is never null.
const ALLOCATOR = 1 << 1;
/// `#[unwind]`: an indicator that this function may unwind despite what
/// its ABI signature may otherwise imply.
const UNWIND = 1 << 2;
/// `#[rust_allocator_nounwind]`, an indicator that an imported FFI
/// function will never unwind. Probably obsolete by recent changes with
/// #[unwind], but hasn't been removed/migrated yet
const RUSTC_ALLOCATOR_NOUNWIND = 1 << 3;
/// `#[naked]`: an indicator to LLVM that no function prologue/epilogue
/// should be generated.
const NAKED = 1 << 4;
/// `#[no_mangle]`: an indicator that the function's name should be the same
/// as its symbol.
const NO_MANGLE = 1 << 5;
/// `#[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.
const RUSTC_STD_INTERNAL_SYMBOL = 1 << 6;
/// `#[no_debug]`: an indicator that no debugging information should be
/// generated for this function by LLVM.
const NO_DEBUG = 1 << 7;
/// `#[thread_local]`: indicates a static is actually a thread local
/// piece of memory
const THREAD_LOCAL = 1 << 8;
/// `#[used]`: indicates that LLVM can't eliminate this function (but the
/// linker can!).
const USED = 1 << 9;
/// `#[ffi_returns_twice]`, indicates that an extern function can return
/// multiple times
const FFI_RETURNS_TWICE = 1 << 10;
/// `#[track_caller]`: allow access to the caller location
const TRACK_CALLER = 1 << 11;
}
}
impl CodegenFnAttrs {
pub fn new() -> CodegenFnAttrs {
CodegenFnAttrs {
flags: CodegenFnAttrFlags::empty(),
inline: InlineAttr::None,
optimize: OptimizeAttr::None,
export_name: None,
link_name: None,
link_ordinal: None,
target_features: vec![],
linkage: None,
link_section: None,
}
}
/// Returns `true` if `#[inline]` or `#[inline(always)]` is present.
pub fn requests_inline(&self) -> bool {
match self.inline {
InlineAttr::Hint | InlineAttr::Always => true,
InlineAttr::None | InlineAttr::Never => false,
}
}
/// 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
pub fn contains_extern_indicator(&self) -> bool {
self.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
|| self.export_name.is_some()
|| match self.linkage {
// These are private, so make sure we don't try to consider
// them external.
None | Some(Linkage::Internal) | Some(Linkage::Private) => false,
Some(_) => true,
}
}
}

View file

@ -1,3 +1,4 @@
pub mod codegen_fn_attrs;
pub mod cstore;
pub mod dependency_format;
pub mod exported_symbols;

View file

@ -1,6 +1,6 @@
use crate::hir::def::Namespace;
use crate::hir::def_id::DefId;
use crate::hir::CodegenFnAttrFlags;
use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use crate::middle::lang_items::DropInPlaceFnLangItem;
use crate::traits;
use crate::ty::print::{FmtPrinter, Printer};

View file

@ -1,9 +1,10 @@
use crate::dep_graph::{self, DepNode};
use crate::hir::def::{DefKind, Export};
use crate::hir::def_id::{CrateNum, DefId, DefIndex};
use crate::hir::{self, CodegenFnAttrs, ItemLocalId, TraitCandidate};
use crate::hir::{self, ItemLocalId, TraitCandidate};
use crate::infer::canonical::{self, Canonical};
use crate::lint;
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
use crate::middle::cstore::{CrateSource, DepKind, NativeLibraryKind};
use crate::middle::cstore::{ExternCrate, ForeignModule, LinkagePreference, NativeLibrary};
use crate::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};

View file

@ -3,7 +3,7 @@
use std::ffi::CString;
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
use rustc::hir::CodegenFnAttrFlags;
use rustc::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc::session::config::{OptLevel, Sanitizer};
use rustc::session::Session;
use rustc::ty::layout::HasTyCtxt;

View file

@ -14,33 +14,32 @@
//! int)` and `rec(x=int, y=int, z=int)` will have the same `llvm::Type`.
use super::{LlvmCodegenBackend, ModuleLlvm};
use rustc_codegen_ssa::base::maybe_create_entry_wrapper;
use rustc_codegen_ssa::{ModuleCodegen, ModuleKind};
use crate::builder::Builder;
use crate::common;
use crate::context::CodegenCx;
use crate::llvm;
use crate::metadata;
use crate::value::Value;
use rustc::dep_graph;
use rustc::middle::codegen_fn_attrs::CodegenFnAttrs;
use rustc::middle::cstore::EncodedMetadata;
use rustc::middle::exported_symbols;
use rustc::mir::mono::{Linkage, Visibility};
use rustc::session::config::DebugInfo;
use rustc::ty::TyCtxt;
use rustc_codegen_ssa::mono_item::MonoItemExt;
use rustc_data_structures::small_c_str::SmallCStr;
use rustc_codegen_ssa::back::write::submit_codegened_module_to_llvm;
use rustc_codegen_ssa::base::maybe_create_entry_wrapper;
use rustc_codegen_ssa::mono_item::MonoItemExt;
use rustc_codegen_ssa::traits::*;
use rustc::hir::CodegenFnAttrs;
use rustc_codegen_ssa::{ModuleCodegen, ModuleKind};
use rustc_data_structures::small_c_str::SmallCStr;
use rustc_span::symbol::Symbol;
use std::ffi::CString;
use std::time::Instant;
use crate::value::Value;
pub fn write_compressed_metadata<'tcx>(
tcx: TyCtxt<'tcx>,
metadata: &EncodedMetadata,

View file

@ -8,9 +8,11 @@ use crate::value::Value;
use libc::c_uint;
use log::debug;
use rustc::hir::def_id::DefId;
use rustc::hir::Node;
use rustc::hir::{self, Node};
use rustc::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
use rustc::mir::interpret::{read_target_uint, Allocation, ConstValue, ErrorHandled, Pointer};
use rustc::mir::mono::MonoItem;
use rustc::ty::layout::{self, Align, LayoutOf, Size};
use rustc::ty::{self, Instance, Ty};
use rustc::{bug, span_bug};
use rustc_codegen_ssa::traits::*;
@ -18,10 +20,6 @@ use rustc_span::symbol::{sym, Symbol};
use rustc_span::Span;
use rustc_target::abi::HasDataLayout;
use rustc::ty::layout::{self, Align, LayoutOf, Size};
use rustc::hir::{self, CodegenFnAttrFlags, CodegenFnAttrs};
use std::ffi::CStr;
pub fn const_alloc_to_llvm(cx: &CodegenCx<'ll, '_>, alloc: &Allocation) -> &'ll Value {

View file

@ -22,8 +22,8 @@ use crate::value::Value;
use log::debug;
use rustc::hir::def::CtorKind;
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use rustc::hir::CodegenFnAttrFlags;
use rustc::ich::NodeIdHashingMode;
use rustc::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc::mir::interpret::truncate;
use rustc::mir::{self, Field, GeneratorLayout};
use rustc::session::config::{self, DebugInfo};

View file

@ -14,7 +14,7 @@ use crate::llvm::debuginfo::{
DIArray, DIBuilder, DIFile, DIFlags, DILexicalBlock, DISPFlags, DIScope, DIType,
};
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use rustc::hir::CodegenFnAttrFlags;
use rustc::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc::ty::subst::{GenericArgKind, SubstsRef};
use crate::abi::FnAbi;

View file

@ -3,8 +3,8 @@ use std::sync::Arc;
use rustc::hir;
use rustc::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc::hir::CodegenFnAttrFlags;
use rustc::hir::Node;
use rustc::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc::middle::exported_symbols::{metadata_symbol_name, ExportedSymbol, SymbolExportLevel};
use rustc::session::config;
use rustc::ty::query::Providers;

View file

@ -27,6 +27,7 @@ use crate::{CachedModuleCodegen, CrateInfo, MemFlags, ModuleCodegen, ModuleKind}
use rustc::hir;
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
use rustc::middle::codegen_fn_attrs::CodegenFnAttrs;
use rustc::middle::cstore::EncodedMetadata;
use rustc::middle::cstore::{self, LinkagePreference};
use rustc::middle::lang_items::StartFnLangItem;
@ -811,7 +812,7 @@ pub fn provide_both(providers: &mut Providers<'_>) {
let (defids, _) = tcx.collect_and_partition_mono_items(cratenum);
for id in &*defids {
let hir::CodegenFnAttrs { optimize, .. } = tcx.codegen_fn_attrs(*id);
let CodegenFnAttrs { optimize, .. } = tcx.codegen_fn_attrs(*id);
match optimize {
attr::OptimizeAttr::None => continue,
attr::OptimizeAttr::Size => continue,

View file

@ -88,8 +88,8 @@
//! DefPaths which are much more robust in the face of changes to the code base.
use rustc::hir::def_id::LOCAL_CRATE;
use rustc::hir::CodegenFnAttrFlags;
use rustc::hir::Node;
use rustc::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc::mir::mono::{InstantiationMode, MonoItem};
use rustc::session::config::SymbolManglingVersion;
use rustc::ty::query::Providers;

View file

@ -176,9 +176,10 @@
use crate::monomorphize;
use rustc::hir;
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
use rustc::hir::itemlikevisit::ItemLikeVisitor;
use rustc::hir::{self, CodegenFnAttrFlags};
use rustc::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc::middle::lang_items::{ExchangeMallocFnLangItem, StartFnLangItem};
use rustc::mir::interpret::{AllocId, ConstValue};
use rustc::mir::interpret::{ErrorHandled, GlobalAlloc, Scalar};

View file

@ -98,7 +98,7 @@ use std::sync::Arc;
use rustc::hir::def::DefKind;
use rustc::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc::hir::CodegenFnAttrFlags;
use rustc::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc::middle::exported_symbols::SymbolExportLevel;
use rustc::mir::mono::{CodegenUnit, CodegenUnitNameBuilder, Linkage, Visibility};
use rustc::mir::mono::{InstantiationMode, MonoItem};

View file

@ -1,11 +1,11 @@
//! Inlining pass for MIR functions
use rustc::hir::def_id::DefId;
use rustc::hir::CodegenFnAttrFlags;
use rustc_index::bit_set::BitSet;
use rustc_index::vec::{Idx, IndexVec};
use rustc::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc::mir::visit::*;
use rustc::mir::*;
use rustc::ty::subst::{InternalSubsts, Subst, SubstsRef};

View file

@ -9,8 +9,8 @@ use rustc::hir::{self, PatKind, TyKind};
use rustc::hir::def::{CtorOf, DefKind, Res};
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
use rustc::hir::CodegenFnAttrFlags;
use rustc::lint;
use rustc::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc::middle::privacy;
use rustc::ty::{self, DefIdTree, TyCtxt};
use rustc::util::nodemap::FxHashSet;

View file

@ -5,22 +5,21 @@
// makes all other generics or inline functions that it references
// reachable as well.
use rustc::hir;
use rustc::hir::def::{DefKind, Res};
use rustc::hir::def_id::LOCAL_CRATE;
use rustc::hir::def_id::{CrateNum, DefId};
use rustc::hir::intravisit;
use rustc::hir::intravisit::{NestedVisitorMap, Visitor};
use rustc::hir::itemlikevisit::ItemLikeVisitor;
use rustc::hir::Node;
use rustc::hir::{CodegenFnAttrFlags, CodegenFnAttrs};
use rustc::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
use rustc::middle::privacy;
use rustc::session::config;
use rustc::ty::query::Providers;
use rustc::ty::{self, TyCtxt};
use rustc::util::nodemap::{FxHashSet, HirIdSet};
use rustc_data_structures::sync::Lrc;
use rustc::hir;
use rustc::hir::def_id::LOCAL_CRATE;
use rustc::hir::intravisit;
use rustc::hir::intravisit::{NestedVisitorMap, Visitor};
use rustc::hir::itemlikevisit::ItemLikeVisitor;
use rustc_target::spec::abi::Abi;
// Returns true if the given item must be inlined because it may be

View file

@ -20,6 +20,7 @@ use crate::constrained_generic_params as cgp;
use crate::lint;
use crate::middle::resolve_lifetime as rl;
use crate::middle::weak_lang_items;
use rustc::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
use rustc::mir::mono::Linkage;
use rustc::ty::query::Providers;
use rustc::ty::subst::GenericArgKind;
@ -44,7 +45,7 @@ use rustc::hir::def_id::{DefId, LOCAL_CRATE};
use rustc::hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc::hir::GenericParamKind;
use rustc::hir::Node;
use rustc::hir::{self, CodegenFnAttrFlags, CodegenFnAttrs, Unsafety};
use rustc::hir::{self, Unsafety};
use errors::{Applicability, StashKey};