Skip to content

Commit e8cd6cc

Browse files
committed
Add query for trans fn attributes
Part of #47320
1 parent 2789b06 commit e8cd6cc

File tree

7 files changed

+98
-15
lines changed

7 files changed

+98
-15
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ define_dep_nodes!( <'tcx>
559559
[] IsReachableNonGeneric(DefId),
560560
[] IsMirAvailable(DefId),
561561
[] ItemAttrs(DefId),
562+
[] TransFnAttrs(DefId),
562563
[] FnArgNames(DefId),
563564
[] DylibDepFormats(CrateNum),
564565
[] IsPanicRuntime(CrateNum),

src/librustc/hir/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,3 +2210,28 @@ pub type GlobMap = NodeMap<FxHashSet<Name>>;
22102210
pub fn provide(providers: &mut Providers) {
22112211
providers.describe_def = map::describe_def;
22122212
}
2213+
2214+
#[derive(Clone, RustcEncodable, RustcDecodable, Hash)]
2215+
pub struct TransFnAttrs {
2216+
pub flags: TransFnAttrFlags,
2217+
}
2218+
2219+
bitflags! {
2220+
#[derive(RustcEncodable, RustcDecodable)]
2221+
pub struct TransFnAttrFlags: u8 {
2222+
const COLD = 0b0000_0001;
2223+
const ALLOCATOR = 0b0000_0010;
2224+
const UNWIND = 0b0000_0100;
2225+
const RUSTC_ALLOCATOR_NOUNWIND = 0b0000_1000;
2226+
const NAKED = 0b0001_0000;
2227+
}
2228+
}
2229+
2230+
impl TransFnAttrs {
2231+
pub fn new() -> TransFnAttrs {
2232+
TransFnAttrs {
2233+
flags: TransFnAttrFlags::empty(),
2234+
}
2235+
}
2236+
}
2237+

src/librustc/ich/impls_hir.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,27 @@ impl<'gcx> ToStableHashKey<StableHashingContext<'gcx>> for hir::TraitCandidate {
11381138
}
11391139
}
11401140

1141+
impl<'hir> HashStable<StableHashingContext<'hir>> for hir::TransFnAttrs
1142+
{
1143+
fn hash_stable<W: StableHasherResult>(&self,
1144+
hcx: &mut StableHashingContext<'hir>,
1145+
hasher: &mut StableHasher<W>) {
1146+
let hir::TransFnAttrs {
1147+
flags,
1148+
} = *self;
1149+
1150+
flags.hash_stable(hcx, hasher);
1151+
}
1152+
}
1153+
1154+
impl<'hir> HashStable<StableHashingContext<'hir>> for hir::TransFnAttrFlags
1155+
{
1156+
fn hash_stable<W: StableHasherResult>(&self,
1157+
hcx: &mut StableHashingContext<'hir>,
1158+
hasher: &mut StableHasher<W>) {
1159+
self.bits().hash_stable(hcx, hasher);
1160+
}
1161+
}
11411162

11421163
impl_stable_hash_for!(struct hir::Freevar {
11431164
def,

src/librustc/ty/maps/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use dep_graph::{DepConstructor, DepNode};
1212
use errors::DiagnosticBuilder;
1313
use hir::def_id::{CrateNum, DefId, DefIndex};
1414
use hir::def::{Def, Export};
15-
use hir::{self, TraitCandidate, ItemLocalId};
15+
use hir::{self, TraitCandidate, ItemLocalId, TransFnAttrs};
1616
use hir::svh::Svh;
1717
use lint;
1818
use middle::borrowck::BorrowCheckResult;
@@ -235,6 +235,7 @@ define_maps! { <'tcx>
235235
[] fn lookup_stability: LookupStability(DefId) -> Option<&'tcx attr::Stability>,
236236
[] fn lookup_deprecation_entry: LookupDeprecationEntry(DefId) -> Option<DeprecationEntry>,
237237
[] fn item_attrs: ItemAttrs(DefId) -> Lrc<[ast::Attribute]>,
238+
[] fn trans_fn_attrs: trans_fn_attrs(DefId) -> TransFnAttrs,
238239
[] fn fn_arg_names: FnArgNames(DefId) -> Vec<ast::Name>,
239240
[] fn impl_parent: ImplParent(DefId) -> Option<DefId>,
240241
[] fn trait_of_item: TraitOfItem(DefId) -> Option<DefId>,
@@ -403,6 +404,10 @@ fn features_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
403404
DepConstructor::Features
404405
}
405406

407+
fn trans_fn_attrs<'tcx>(id: DefId) -> DepConstructor<'tcx> {
408+
DepConstructor::TransFnAttrs { 0: id }
409+
}
410+
406411
fn erase_regions_ty<'tcx>(ty: Ty<'tcx>) -> DepConstructor<'tcx> {
407412
DepConstructor::EraseRegionsTy { ty }
408413
}

src/librustc/ty/maps/plumbing.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
854854
DepKind::IsReachableNonGeneric => { force!(is_reachable_non_generic, def_id!()); }
855855
DepKind::IsMirAvailable => { force!(is_mir_available, def_id!()); }
856856
DepKind::ItemAttrs => { force!(item_attrs, def_id!()); }
857+
DepKind::TransFnAttrs => { force!(trans_fn_attrs, def_id!()); }
857858
DepKind::FnArgNames => { force!(fn_arg_names, def_id!()); }
858859
DepKind::DylibDepFormats => { force!(dylib_dependency_formats, krate!()); }
859860
DepKind::IsPanicRuntime => { force!(is_panic_runtime, krate!()); }

src/librustc_trans/attributes.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
1212
use std::ffi::{CStr, CString};
1313

14+
use rustc::hir::TransFnAttrFlags;
1415
use rustc::hir::Unsafety;
1516
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
1617
use rustc::session::config::Sanitizer;
@@ -109,22 +110,27 @@ pub fn from_fn_attrs(cx: &CodegenCx, llfn: ValueRef, id: DefId) {
109110
set_frame_pointer_elimination(cx, llfn);
110111
set_probestack(cx, llfn);
111112

112-
for attr in attrs.iter() {
113-
if attr.check_name("cold") {
114-
Attribute::Cold.apply_llfn(Function, llfn);
115-
} else if attr.check_name("naked") {
116-
naked(llfn, true);
117-
} else if attr.check_name("allocator") {
118-
Attribute::NoAlias.apply_llfn(
119-
llvm::AttributePlace::ReturnValue, llfn);
120-
} else if attr.check_name("unwind") {
121-
unwind(llfn, true);
122-
} else if attr.check_name("rustc_allocator_nounwind") {
123-
unwind(llfn, false);
124-
}
113+
let trans_fn_attrs = cx.tcx.trans_fn_attrs(id);
114+
115+
if trans_fn_attrs.flags.contains(TransFnAttrFlags::COLD) {
116+
Attribute::Cold.apply_llfn(Function, llfn);
117+
}
118+
if trans_fn_attrs.flags.contains(TransFnAttrFlags::NAKED) {
119+
naked(llfn, true);
120+
}
121+
if trans_fn_attrs.flags.contains(TransFnAttrFlags::ALLOCATOR) {
122+
Attribute::NoAlias.apply_llfn(
123+
llvm::AttributePlace::ReturnValue, llfn);
124+
}
125+
if trans_fn_attrs.flags.contains(TransFnAttrFlags::UNWIND) {
126+
unwind(llfn, true);
127+
}
128+
if trans_fn_attrs.flags.contains(TransFnAttrFlags::RUSTC_ALLOCATOR_NOUNWIND) {
129+
unwind(llfn, false);
125130
}
126131

127132
let target_features = cx.tcx.target_features_enabled(id);
133+
128134
if !target_features.is_empty() {
129135
let val = CString::new(target_features.join(",")).unwrap();
130136
llvm::AddFunctionAttrStringValue(

src/librustc_typeck/collect.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use syntax::codemap::Spanned;
4545
use syntax::symbol::{Symbol, keywords};
4646
use syntax_pos::{Span, DUMMY_SP};
4747

48-
use rustc::hir::{self, map as hir_map};
48+
use rustc::hir::{self, map as hir_map, TransFnAttrs, TransFnAttrFlags};
4949
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
5050
use rustc::hir::def::{Def, CtorKind};
5151
use rustc::hir::def_id::DefId;
@@ -71,6 +71,7 @@ pub fn provide(providers: &mut Providers) {
7171
impl_trait_ref,
7272
impl_polarity,
7373
is_foreign_item,
74+
trans_fn_attrs,
7475
..*providers
7576
};
7677
}
@@ -1723,3 +1724,26 @@ fn is_foreign_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
17231724
_ => bug!("is_foreign_item applied to non-local def-id {:?}", def_id)
17241725
}
17251726
}
1727+
1728+
fn trans_fn_attrs<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, id: DefId) -> TransFnAttrs {
1729+
let attrs = tcx.get_attrs(id);
1730+
1731+
let mut trans_fn_attrs = TransFnAttrs::new();
1732+
1733+
for attr in attrs.iter() {
1734+
if attr.check_name("cold") {
1735+
trans_fn_attrs.flags |= TransFnAttrFlags::COLD;
1736+
} else if attr.check_name("allocator") {
1737+
trans_fn_attrs.flags |= TransFnAttrFlags::ALLOCATOR;
1738+
} else if attr.check_name("unwind") {
1739+
trans_fn_attrs.flags |= TransFnAttrFlags::UNWIND;
1740+
} else if attr.check_name("rustc_allocator_nounwind") {
1741+
trans_fn_attrs.flags |= TransFnAttrFlags::RUSTC_ALLOCATOR_NOUNWIND;
1742+
} else if attr.check_name("naked") {
1743+
trans_fn_attrs.flags |= TransFnAttrFlags::NAKED;
1744+
} else if attr.check_name("inline") {
1745+
}
1746+
}
1747+
1748+
trans_fn_attrs
1749+
}

0 commit comments

Comments
 (0)