Skip to content

Commit 0153001

Browse files
author
Ariel Ben-Yehuda
committed
cache Ty::is_simd
1 parent 612760b commit 0153001

File tree

10 files changed

+45
-36
lines changed

10 files changed

+45
-36
lines changed

src/librustc/middle/ty.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -3179,11 +3179,12 @@ impl<'tcx> TraitDef<'tcx> {
31793179
bitflags! {
31803180
flags ADTFlags: u32 {
31813181
const NO_ADT_FLAGS = 0,
3182-
const IS_FUNDAMENTAL = 1 << 0,
3183-
const IS_PHANTOM_DATA = 1 << 1,
3184-
const IS_DTORCK = 1 << 2, // is this a dtorck type?
3185-
const IS_DTORCK_VALID = 1 << 3,
3186-
const IS_ENUM = 1 << 4
3182+
const IS_ENUM = 1 << 0,
3183+
const IS_DTORCK = 1 << 1, // is this a dtorck type?
3184+
const IS_DTORCK_VALID = 1 << 2,
3185+
const IS_PHANTOM_DATA = 1 << 3,
3186+
const IS_SIMD = 1 << 4,
3187+
const IS_FUNDAMENTAL = 1 << 5,
31873188
}
31883189
}
31893190

@@ -3244,9 +3245,13 @@ impl<'tcx, 'lt> ADTDef_<'tcx, 'lt> {
32443245
kind: ADTKind,
32453246
variants: Vec<VariantDef_<'tcx, 'lt>>) -> Self {
32463247
let mut flags = ADTFlags::NO_ADT_FLAGS;
3247-
if tcx.has_attr(did, "fundamental") {
3248+
let attrs = tcx.get_attrs(did);
3249+
if attrs.iter().any(|item| item.check_name("fundamental")) {
32483250
flags = flags | ADTFlags::IS_FUNDAMENTAL;
32493251
}
3252+
if attrs.iter().any(|item| item.check_name("simd")) {
3253+
flags = flags | ADTFlags::IS_SIMD;
3254+
}
32503255
if Some(did) == tcx.lang_items.phantom_data() {
32513256
flags = flags | ADTFlags::IS_PHANTOM_DATA;
32523257
}
@@ -3289,6 +3294,11 @@ impl<'tcx, 'lt> ADTDef_<'tcx, 'lt> {
32893294
self.flags.get().intersects(ADTFlags::IS_FUNDAMENTAL)
32903295
}
32913296

3297+
#[inline]
3298+
pub fn is_simd(&self) -> bool {
3299+
self.flags.get().intersects(ADTFlags::IS_SIMD)
3300+
}
3301+
32923302
#[inline]
32933303
pub fn is_phantom_data(&self) -> bool {
32943304
self.flags.get().intersects(ADTFlags::IS_PHANTOM_DATA)
@@ -4203,9 +4213,10 @@ impl<'tcx> TyS<'tcx> {
42034213
}
42044214
}
42054215

4206-
pub fn is_simd(&self, cx: &ctxt) -> bool {
4216+
#[inline]
4217+
pub fn is_simd(&self) -> bool {
42074218
match self.sty {
4208-
TyStruct(def, _) => cx.lookup_simd(def.did),
4219+
TyStruct(def, _) => def.is_simd(),
42094220
_ => false
42104221
}
42114222
}
@@ -5979,7 +5990,6 @@ impl<'tcx> ctxt<'tcx> {
59795990

59805991
/// Obtain the representation annotation for a struct definition.
59815992
pub fn lookup_repr_hints(&self, did: DefId) -> Rc<Vec<attr::ReprAttr>> {
5982-
// TODO: remove
59835993
memoized(&self.repr_hint_cache, did, |did: DefId| {
59845994
Rc::new(if did.krate == LOCAL_CRATE {
59855995
self.get_attrs(did).iter().flat_map(|meta| {

src/librustc_trans/trans/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ pub fn fail_if_zero_or_overflows<'blk, 'tcx>(
621621
let zero = C_integral(Type::uint_from_ty(cx.ccx(), t), 0, false);
622622
(ICmp(cx, llvm::IntEQ, rhs, zero, debug_loc), false)
623623
}
624-
ty::TyStruct(_, _) if rhs_t.is_simd(cx.tcx()) => {
624+
ty::TyStruct(def, _) if def.is_simd() => {
625625
let mut res = C_bool(cx.ccx(), false);
626626
for i in 0 .. rhs_t.simd_size(cx.tcx()) {
627627
res = Or(cx, res,

src/librustc_trans/trans/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ pub fn type_is_immediate<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -
192192
let simple = ty.is_scalar() ||
193193
ty.is_unique() || ty.is_region_ptr() ||
194194
type_is_newtype_immediate(ccx, ty) ||
195-
ty.is_simd(tcx);
195+
ty.is_simd();
196196
if simple && !type_is_fat_ptr(tcx, ty) {
197197
return true;
198198
}

src/librustc_trans/trans/consts.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
500500
debug!("const_expr_unadjusted: te1={}, ty={:?}",
501501
cx.tn().val_to_string(te1),
502502
ty);
503-
let is_simd = ty.is_simd(cx.tcx());
503+
let is_simd = ty.is_simd();
504504
let intype = if is_simd {
505505
ty.simd_type(cx.tcx())
506506
} else {
@@ -754,7 +754,7 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
754754
(_, None) => cx.sess().span_bug(e.span, "missing struct field"),
755755
}
756756
}).collect::<Vec<_>>();
757-
if ety.is_simd(cx.tcx()) {
757+
if ety.is_simd() {
758758
C_vector(&cs[..])
759759
} else {
760760
adt::trans_const(cx, &*repr, discr, &cs[..])
@@ -850,7 +850,7 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
850850
const_fn_call(cx, ExprId(callee.id), did, &arg_vals, param_substs)
851851
}
852852
def::DefStruct(_) => {
853-
if ety.is_simd(cx.tcx()) {
853+
if ety.is_simd() {
854854
C_vector(&arg_vals[..])
855855
} else {
856856
let repr = adt::represent_type(cx, ety);

src/librustc_trans/trans/debuginfo/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,7 @@ fn prepare_struct_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
11751175
StructMDF(StructMemberDescriptionFactory {
11761176
variant: variant,
11771177
substs: substs,
1178-
is_simd: struct_type.is_simd(cx.tcx()),
1178+
is_simd: struct_type.is_simd(),
11791179
span: span,
11801180
})
11811181
)

src/librustc_trans/trans/expr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1453,7 +1453,7 @@ pub fn trans_adt<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
14531453
// panic occur before the ADT as a whole is ready.
14541454
let custom_cleanup_scope = fcx.push_custom_cleanup_scope();
14551455

1456-
if ty.is_simd(bcx.tcx()) {
1456+
if ty.is_simd() {
14571457
// Issue 23112: The original logic appeared vulnerable to same
14581458
// order-of-eval bug. But, SIMD values are tuple-structs;
14591459
// i.e. functional record update (FRU) syntax is unavailable.
@@ -1697,7 +1697,7 @@ fn trans_eager_binop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
16971697
let _icx = push_ctxt("trans_eager_binop");
16981698

16991699
let tcx = bcx.tcx();
1700-
let is_simd = lhs_t.is_simd(tcx);
1700+
let is_simd = lhs_t.is_simd();
17011701
let intype = if is_simd {
17021702
lhs_t.simd_type(tcx)
17031703
} else {
@@ -2502,7 +2502,7 @@ fn build_unchecked_rshift<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
25022502
// #1877, #10183: Ensure that input is always valid
25032503
let rhs = shift_mask_rhs(bcx, rhs, binop_debug_loc);
25042504
let tcx = bcx.tcx();
2505-
let is_simd = lhs_t.is_simd(tcx);
2505+
let is_simd = lhs_t.is_simd();
25062506
let intype = if is_simd {
25072507
lhs_t.simd_type(tcx)
25082508
} else {

src/librustc_trans/trans/foreign.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ pub fn trans_native_call<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
449449
fn gate_simd_ffi(tcx: &ty::ctxt, decl: &ast::FnDecl, ty: &ty::BareFnTy) {
450450
if !tcx.sess.features.borrow().simd_ffi {
451451
let check = |ast_ty: &ast::Ty, ty: ty::Ty| {
452-
if ty.is_simd(tcx) {
452+
if ty.is_simd() {
453453
tcx.sess.span_err(ast_ty.span,
454454
&format!("use of SIMD type `{}` in FFI is highly experimental and \
455455
may result in invalid code",

src/librustc_trans/trans/glue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ pub fn size_and_align_of_dst<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, t: Ty<'tcx>, in
419419
let ccx = bcx.ccx();
420420
// First get the size of all statically known fields.
421421
// Don't use type_of::sizing_type_of because that expects t to be sized.
422-
assert!(!t.is_simd(bcx.tcx()));
422+
assert!(!t.is_simd());
423423
let repr = adt::represent_type(ccx, t);
424424
let sizing_type = adt::sizing_type_context_of(ccx, &*repr, true);
425425
debug!("DST {} sizing_type: {}", t, sizing_type.to_string());

src/librustc_trans/trans/type_of.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ pub fn sizing_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> Typ
222222
}
223223

224224
ty::TyStruct(..) => {
225-
if t.is_simd(cx.tcx()) {
225+
if t.is_simd() {
226226
let llet = type_of(cx, t.simd_type(cx.tcx()));
227227
let n = t.simd_size(cx.tcx()) as u64;
228228
ensure_array_fits_in_address_space(cx, llet, n, t);
@@ -404,7 +404,7 @@ pub fn in_memory_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) ->
404404
adt::type_of(cx, &*repr)
405405
}
406406
ty::TyStruct(def, ref substs) => {
407-
if t.is_simd(cx.tcx()) {
407+
if t.is_simd() {
408408
let llet = in_memory_type_of(cx, t.simd_type(cx.tcx()));
409409
let n = t.simd_size(cx.tcx()) as u64;
410410
ensure_array_fits_in_address_space(cx, llet, n, t);
@@ -436,7 +436,7 @@ pub fn in_memory_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) ->
436436
// If this was an enum or struct, fill in the type now.
437437
match t.sty {
438438
ty::TyEnum(..) | ty::TyStruct(..) | ty::TyClosure(..)
439-
if !t.is_simd(cx.tcx()) => {
439+
if !t.is_simd() => {
440440
let repr = adt::represent_type(cx, t);
441441
adt::finish_type_of(cx, &*repr, &mut llty);
442442
}

src/librustc_typeck/check/op.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use super::{
2121
structurally_resolved_type,
2222
};
2323
use middle::traits;
24-
use middle::ty::{self, Ty, HasTypeFlags};
24+
use middle::ty::{Ty, HasTypeFlags};
2525
use syntax::ast;
2626
use syntax::ast_util;
2727
use syntax::parse::token;
@@ -41,7 +41,7 @@ pub fn check_binop_assign<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>,
4141
let lhs_ty = structurally_resolved_type(fcx, lhs_expr.span, fcx.expr_ty(lhs_expr));
4242
let rhs_ty = structurally_resolved_type(fcx, rhs_expr.span, fcx.expr_ty(rhs_expr));
4343

44-
if is_builtin_binop(fcx.tcx(), lhs_ty, rhs_ty, op) {
44+
if is_builtin_binop(lhs_ty, rhs_ty, op) {
4545
enforce_builtin_binop_types(fcx, lhs_expr, lhs_ty, rhs_expr, rhs_ty, op);
4646
fcx.write_nil(expr.id);
4747
} else {
@@ -86,7 +86,7 @@ pub fn check_binop<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
8686
// traits, because their return type is not bool. Perhaps this
8787
// should change, but for now if LHS is SIMD we go down a
8888
// different path that bypassess all traits.
89-
if lhs_ty.is_simd(fcx.tcx()) {
89+
if lhs_ty.is_simd() {
9090
check_expr_coercable_to_type(fcx, rhs_expr, lhs_ty);
9191
let rhs_ty = fcx.resolve_type_vars_if_possible(fcx.expr_ty(lhs_expr));
9292
let return_ty = enforce_builtin_binop_types(fcx, lhs_expr, lhs_ty, rhs_expr, rhs_ty, op);
@@ -123,7 +123,7 @@ pub fn check_binop<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
123123
let rhs_ty = fcx.resolve_type_vars_if_possible(rhs_ty);
124124
if
125125
!lhs_ty.is_ty_var() && !rhs_ty.is_ty_var() &&
126-
is_builtin_binop(fcx.tcx(), lhs_ty, rhs_ty, op)
126+
is_builtin_binop(lhs_ty, rhs_ty, op)
127127
{
128128
let builtin_return_ty =
129129
enforce_builtin_binop_types(fcx, lhs_expr, lhs_ty, rhs_expr, rhs_ty, op);
@@ -143,7 +143,7 @@ fn enforce_builtin_binop_types<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
143143
op: ast::BinOp)
144144
-> Ty<'tcx>
145145
{
146-
debug_assert!(is_builtin_binop(fcx.tcx(), lhs_ty, rhs_ty, op));
146+
debug_assert!(is_builtin_binop(lhs_ty, rhs_ty, op));
147147

148148
let tcx = fcx.tcx();
149149
match BinOpCategory::from(op) {
@@ -156,7 +156,7 @@ fn enforce_builtin_binop_types<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
156156
BinOpCategory::Shift => {
157157
// For integers, the shift amount can be of any integral
158158
// type. For simd, the type must match exactly.
159-
if lhs_ty.is_simd(tcx) {
159+
if lhs_ty.is_simd() {
160160
demand::suptype(fcx, rhs_expr.span, lhs_ty, rhs_ty);
161161
}
162162

@@ -176,7 +176,7 @@ fn enforce_builtin_binop_types<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
176176
demand::suptype(fcx, rhs_expr.span, lhs_ty, rhs_ty);
177177

178178
// if this is simd, result is same as lhs, else bool
179-
if lhs_ty.is_simd(tcx) {
179+
if lhs_ty.is_simd() {
180180
let unit_ty = lhs_ty.simd_type(tcx);
181181
debug!("enforce_builtin_binop_types: lhs_ty={:?} unit_ty={:?}",
182182
lhs_ty,
@@ -415,8 +415,7 @@ impl BinOpCategory {
415415
/// Reason #2 is the killer. I tried for a while to always use
416416
/// overloaded logic and just check the types in constants/trans after
417417
/// the fact, and it worked fine, except for SIMD types. -nmatsakis
418-
fn is_builtin_binop<'tcx>(cx: &ty::ctxt<'tcx>,
419-
lhs: Ty<'tcx>,
418+
fn is_builtin_binop<'tcx>(lhs: Ty<'tcx>,
420419
rhs: Ty<'tcx>,
421420
op: ast::BinOp)
422421
-> bool
@@ -429,28 +428,28 @@ fn is_builtin_binop<'tcx>(cx: &ty::ctxt<'tcx>,
429428
BinOpCategory::Shift => {
430429
lhs.references_error() || rhs.references_error() ||
431430
lhs.is_integral() && rhs.is_integral() ||
432-
lhs.is_simd(cx) && rhs.is_simd(cx)
431+
lhs.is_simd() && rhs.is_simd()
433432
}
434433

435434
BinOpCategory::Math => {
436435
lhs.references_error() || rhs.references_error() ||
437436
lhs.is_integral() && rhs.is_integral() ||
438437
lhs.is_floating_point() && rhs.is_floating_point() ||
439-
lhs.is_simd(cx) && rhs.is_simd(cx)
438+
lhs.is_simd() && rhs.is_simd()
440439
}
441440

442441
BinOpCategory::Bitwise => {
443442
lhs.references_error() || rhs.references_error() ||
444443
lhs.is_integral() && rhs.is_integral() ||
445444
lhs.is_floating_point() && rhs.is_floating_point() ||
446-
lhs.is_simd(cx) && rhs.is_simd(cx) ||
445+
lhs.is_simd() && rhs.is_simd() ||
447446
lhs.is_bool() && rhs.is_bool()
448447
}
449448

450449
BinOpCategory::Comparison => {
451450
lhs.references_error() || rhs.references_error() ||
452451
lhs.is_scalar() && rhs.is_scalar() ||
453-
lhs.is_simd(cx) && rhs.is_simd(cx)
452+
lhs.is_simd() && rhs.is_simd()
454453
}
455454
}
456455
}

0 commit comments

Comments
 (0)