Skip to content

Commit ae429e8

Browse files
oli-obkantoyo
authored andcommitted
s/eval_usize/eval_target_usize/ for clarity
1 parent 1640cca commit ae429e8

File tree

1 file changed

+111
-78
lines changed

1 file changed

+111
-78
lines changed

src/intrinsic/simd.rs

Lines changed: 111 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#[cfg_attr(not(feature="master"), allow(unused_imports))]
22
use gccjit::{ToRValue, ComparisonOp, UnaryOp};
33
use gccjit::{BinaryOp, RValue, Type};
4+
45
use rustc_codegen_ssa::base::compare_simd_types;
56
use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
67
use rustc_codegen_ssa::mir::operand::OperandRef;
@@ -10,52 +11,58 @@ use rustc_hir as hir;
1011
use rustc_middle::span_bug;
1112
use rustc_middle::ty::layout::HasTyCtxt;
1213
use rustc_middle::ty::{self, Ty};
13-
use rustc_span::{Span, Symbol, sym};
14+
use rustc_span::{sym, Span, Symbol};
1415
use rustc_target::abi::Align;
1516

1617
use crate::builder::Builder;
1718
#[cfg(feature="master")]
1819
use crate::context::CodegenCx;
1920
use crate::errors::{
20-
InvalidMonomorphizationInvalidFloatVector,
21-
InvalidMonomorphizationNotFloat,
22-
InvalidMonomorphizationUnrecognized,
23-
InvalidMonomorphizationExpectedSignedUnsigned,
24-
InvalidMonomorphizationUnsupportedElement,
25-
InvalidMonomorphizationInvalidBitmask,
26-
InvalidMonomorphizationSimdShuffle,
27-
InvalidMonomorphizationExpectedSimd,
28-
InvalidMonomorphizationMaskType,
29-
InvalidMonomorphizationReturnLength,
30-
InvalidMonomorphizationReturnLengthInputType,
31-
InvalidMonomorphizationReturnElement,
32-
InvalidMonomorphizationReturnType,
33-
InvalidMonomorphizationInsertedType,
34-
InvalidMonomorphizationReturnIntegerType,
35-
InvalidMonomorphizationMismatchedLengths,
36-
InvalidMonomorphizationUnsupportedOperation
21+
InvalidMonomorphizationExpectedSignedUnsigned, InvalidMonomorphizationExpectedSimd,
22+
InvalidMonomorphizationInsertedType, InvalidMonomorphizationInvalidBitmask,
23+
InvalidMonomorphizationInvalidFloatVector, InvalidMonomorphizationMaskType,
24+
InvalidMonomorphizationMismatchedLengths, InvalidMonomorphizationNotFloat,
25+
InvalidMonomorphizationReturnElement, InvalidMonomorphizationReturnIntegerType,
26+
InvalidMonomorphizationReturnLength, InvalidMonomorphizationReturnLengthInputType,
27+
InvalidMonomorphizationReturnType, InvalidMonomorphizationSimdShuffle,
28+
InvalidMonomorphizationUnrecognized, InvalidMonomorphizationUnsupportedElement,
29+
InvalidMonomorphizationUnsupportedOperation,
3730
};
3831

39-
pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>, name: Symbol, callee_ty: Ty<'tcx>, args: &[OperandRef<'tcx, RValue<'gcc>>], ret_ty: Ty<'tcx>, llret_ty: Type<'gcc>, span: Span) -> Result<RValue<'gcc>, ()> {
32+
pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(
33+
bx: &mut Builder<'a, 'gcc, 'tcx>,
34+
name: Symbol,
35+
callee_ty: Ty<'tcx>,
36+
args: &[OperandRef<'tcx, RValue<'gcc>>],
37+
ret_ty: Ty<'tcx>,
38+
llret_ty: Type<'gcc>,
39+
span: Span,
40+
) -> Result<RValue<'gcc>, ()> {
4041
// macros for error handling:
4142
macro_rules! return_error {
42-
($err:expr) => {
43-
{
44-
bx.sess().emit_err($err);
45-
return Err(());
46-
}
47-
}
43+
($err:expr) => {{
44+
bx.sess().emit_err($err);
45+
return Err(());
46+
}};
4847
}
4948
macro_rules! require {
5049
($cond:expr, $err:expr) => {
5150
if !$cond {
5251
return_error!($err);
5352
}
54-
}
53+
};
5554
}
5655
macro_rules! require_simd {
5756
($ty: expr, $position: expr) => {
58-
require!($ty.is_simd(), InvalidMonomorphizationExpectedSimd { span, name, position: $position, found_ty: $ty })
57+
require!(
58+
$ty.is_simd(),
59+
InvalidMonomorphizationExpectedSimd {
60+
span,
61+
name,
62+
position: $position,
63+
found_ty: $ty
64+
}
65+
)
5966
};
6067
}
6168

@@ -77,7 +84,7 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>,
7784
ty::Uint(i) if i.bit_width() == Some(expected_int_bits) => args[0].immediate(),
7885
ty::Array(elem, len)
7986
if matches!(elem.kind(), ty::Uint(ty::UintTy::U8))
80-
&& len.try_eval_usize(bx.tcx, ty::ParamEnv::reveal_all())
87+
&& len.try_eval_target_usize(bx.tcx, ty::ParamEnv::reveal_all())
8188
== Some(expected_bytes) =>
8289
{
8390
let place = PlaceRef::alloca(bx, args[0].layout);
@@ -86,9 +93,13 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>,
8693
let ptr = bx.pointercast(place.llval, bx.cx.type_ptr_to(int_ty));
8794
bx.load(int_ty, ptr, Align::ONE)
8895
}
89-
_ => return_error!(
90-
InvalidMonomorphizationInvalidBitmask { span, name, ty: mask_ty, expected_int_bits, expected_bytes }
91-
),
96+
_ => return_error!(InvalidMonomorphizationInvalidBitmask {
97+
span,
98+
name,
99+
ty: mask_ty,
100+
expected_int_bits,
101+
expected_bytes
102+
}),
92103
};
93104

94105
let arg1 = args[1].immediate();
@@ -134,11 +145,18 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>,
134145
let (out_len, out_ty) = ret_ty.simd_size_and_type(bx.tcx());
135146
require!(
136147
in_len == out_len,
137-
InvalidMonomorphizationReturnLengthInputType { span, name, in_len, in_ty, ret_ty, out_len }
148+
InvalidMonomorphizationReturnLengthInputType {
149+
span,
150+
name,
151+
in_len,
152+
in_ty,
153+
ret_ty,
154+
out_len
155+
}
138156
);
139157
require!(
140158
bx.type_kind(bx.element_type(llret_ty)) == TypeKind::Integer,
141-
InvalidMonomorphizationReturnIntegerType {span, name, ret_ty, out_ty}
159+
InvalidMonomorphizationReturnIntegerType { span, name, ret_ty, out_ty }
142160
);
143161

144162
return Ok(compare_simd_types(
@@ -152,26 +170,26 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>,
152170
}
153171

154172
if let Some(stripped) = name.as_str().strip_prefix("simd_shuffle") {
155-
let n: u64 =
156-
if stripped.is_empty() {
157-
// Make sure this is actually an array, since typeck only checks the length-suffixed
158-
// version of this intrinsic.
159-
match args[2].layout.ty.kind() {
160-
ty::Array(ty, len) if matches!(ty.kind(), ty::Uint(ty::UintTy::U32)) => {
161-
len.try_eval_usize(bx.cx.tcx, ty::ParamEnv::reveal_all()).unwrap_or_else(|| {
162-
span_bug!(span, "could not evaluate shuffle index array length")
163-
})
164-
}
165-
_ => return_error!(
166-
InvalidMonomorphizationSimdShuffle { span, name, ty: args[2].layout.ty }
167-
),
173+
let n: u64 = if stripped.is_empty() {
174+
// Make sure this is actually an array, since typeck only checks the length-suffixed
175+
// version of this intrinsic.
176+
match args[2].layout.ty.kind() {
177+
ty::Array(ty, len) if matches!(ty.kind(), ty::Uint(ty::UintTy::U32)) => {
178+
len.try_eval_target_usize(bx.cx.tcx, ty::ParamEnv::reveal_all()).unwrap_or_else(
179+
|| span_bug!(span, "could not evaluate shuffle index array length"),
180+
)
168181
}
182+
_ => return_error!(InvalidMonomorphizationSimdShuffle {
183+
span,
184+
name,
185+
ty: args[2].layout.ty
186+
}),
169187
}
170-
else {
171-
stripped.parse().unwrap_or_else(|_| {
172-
span_bug!(span, "bad `simd_shuffle` instruction only caught in codegen?")
173-
})
174-
};
188+
} else {
189+
stripped.parse().unwrap_or_else(|_| {
190+
span_bug!(span, "bad `simd_shuffle` instruction only caught in codegen?")
191+
})
192+
};
175193

176194
require_simd!(ret_ty, "return");
177195

@@ -187,14 +205,10 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>,
187205

188206
let vector = args[2].immediate();
189207

190-
return Ok(bx.shuffle_vector(
191-
args[0].immediate(),
192-
args[1].immediate(),
193-
vector,
194-
));
208+
return Ok(bx.shuffle_vector(args[0].immediate(), args[1].immediate(), vector));
195209
}
196210

197-
#[cfg(feature="master")]
211+
#[cfg(feature = "master")]
198212
if name == sym::simd_insert {
199213
require!(
200214
in_elem == arg_tys[2],
@@ -211,7 +225,7 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>,
211225
return Ok(variable.to_rvalue());
212226
}
213227

214-
#[cfg(feature="master")]
228+
#[cfg(feature = "master")]
215229
if name == sym::simd_extract {
216230
require!(
217231
ret_ty == in_elem,
@@ -243,7 +257,14 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>,
243257
let (out_len, out_elem) = ret_ty.simd_size_and_type(bx.tcx());
244258
require!(
245259
in_len == out_len,
246-
InvalidMonomorphizationReturnLengthInputType { span, name, in_len, in_ty, ret_ty, out_len }
260+
InvalidMonomorphizationReturnLengthInputType {
261+
span,
262+
name,
263+
in_len,
264+
in_ty,
265+
ret_ty,
266+
out_len
267+
}
247268
);
248269
// casting cares about nominal type, not just structural type
249270
if in_elem == out_elem {
@@ -373,12 +394,10 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>,
373394
args: &[OperandRef<'tcx, RValue<'gcc>>],
374395
) -> Result<RValue<'gcc>, ()> {
375396
macro_rules! return_error {
376-
($err:expr) => {
377-
{
378-
bx.sess().emit_err($err);
379-
return Err(());
380-
}
381-
}
397+
($err:expr) => {{
398+
bx.sess().emit_err($err);
399+
return Err(());
400+
}};
382401
}
383402
let (elem_ty_str, elem_ty) =
384403
if let ty::Float(f) = in_elem.kind() {
@@ -391,9 +410,9 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>,
391410
}
392411
}
393412
}
394-
else {
395-
return_error!(InvalidMonomorphizationNotFloat { span, name, ty: in_ty });
396-
};
413+
} else {
414+
return_error!(InvalidMonomorphizationNotFloat { span, name, ty: in_ty });
415+
};
397416

398417
let vec_ty = bx.cx.type_vector(elem_ty, in_len);
399418

@@ -778,7 +797,7 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>,
778797
simd_neg: Int => neg, Float => fneg;
779798
}
780799

781-
#[cfg(feature="master")]
800+
#[cfg(feature = "master")]
782801
if name == sym::simd_saturating_add || name == sym::simd_saturating_sub {
783802
let lhs = args[0].immediate();
784803
let rhs = args[1].immediate();
@@ -898,8 +917,7 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>,
898917
// if overflow occurs, the result is the
899918
// mathematical result modulo 2^n:
900919
Ok(bx.$op(args[1].immediate(), r))
901-
}
902-
else {
920+
} else {
903921
Ok(bx.vector_reduce_op(args[0].immediate(), $vec_op))
904922
}
905923
}
@@ -908,12 +926,17 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>,
908926
// ordered arithmetic reductions take an accumulator
909927
let acc = args[1].immediate();
910928
Ok(bx.$float_reduce(acc, args[0].immediate()))
911-
}
912-
else {
929+
} else {
913930
Ok(bx.vector_reduce_op(args[0].immediate(), $vec_op))
914931
}
915932
}
916-
_ => return_error!(InvalidMonomorphizationUnsupportedElement { span, name, in_ty, elem_ty: in_elem, ret_ty }),
933+
_ => return_error!(InvalidMonomorphizationUnsupportedElement {
934+
span,
935+
name,
936+
in_ty,
937+
elem_ty: in_elem,
938+
ret_ty
939+
}),
917940
};
918941
}
919942
};
@@ -983,7 +1006,13 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>,
9831006
} else {
9841007
match in_elem.kind() {
9851008
ty::Int(_) | ty::Uint(_) => {}
986-
_ => return_error!(InvalidMonomorphizationUnsupportedElement { span, name, in_ty, elem_ty: in_elem, ret_ty }),
1009+
_ => return_error!(InvalidMonomorphizationUnsupportedElement {
1010+
span,
1011+
name,
1012+
in_ty,
1013+
elem_ty: in_elem,
1014+
ret_ty
1015+
}),
9871016
}
9881017

9891018
args[0].immediate()
@@ -993,9 +1022,13 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>,
9931022
let r = bx.vector_reduce_op(input, $op);
9941023
Ok(if !$boolean { r } else { bx.icmp(IntPredicate::IntNE, r, bx.context.new_rvalue_zero(r.get_type())) })
9951024
}
996-
_ => return_error!(
997-
InvalidMonomorphizationUnsupportedElement { span, name, in_ty, elem_ty: in_elem, ret_ty }
998-
),
1025+
_ => return_error!(InvalidMonomorphizationUnsupportedElement {
1026+
span,
1027+
name,
1028+
in_ty,
1029+
elem_ty: in_elem,
1030+
ret_ty
1031+
}),
9991032
};
10001033
}
10011034
};

0 commit comments

Comments
 (0)