Skip to content

Commit 6c7556f

Browse files
committed
Change intrinsic types
1 parent 63f70b3 commit 6c7556f

File tree

6 files changed

+96
-21
lines changed

6 files changed

+96
-21
lines changed

compiler/rustc_codegen_llvm/src/intrinsic.rs

+4
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,10 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
355355
// rotate = funnel shift with first two args the same
356356
let llvm_name =
357357
&format!("llvm.fsh{}.i{}", if is_left { 'l' } else { 'r' }, width);
358+
359+
// llvm expects shift to be the same type as the values, but rust always uses `u32`
360+
let raw_shift = self.intcast(raw_shift, self.val_ty(val), false);
361+
358362
self.call_intrinsic(llvm_name, &[val, val, raw_shift])
359363
}
360364
sym::saturating_add | sym::saturating_sub => {

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
173173
let ty = instance_args.type_at(0);
174174
let layout = self.layout_of(ty)?;
175175
let val = self.read_scalar(&args[0])?;
176-
let out_val = self.numeric_intrinsic(intrinsic_name, val, layout)?;
176+
177+
let out_val = self.numeric_intrinsic(intrinsic_name, val, layout, dest.layout)?;
177178
self.write_scalar(out_val, dest)?;
178179
}
179180
sym::saturating_add | sym::saturating_sub => {
@@ -200,21 +201,24 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
200201
sym::rotate_left | sym::rotate_right => {
201202
// rotate_left: (X << (S % BW)) | (X >> ((BW - S) % BW))
202203
// rotate_right: (X << ((BW - S) % BW)) | (X >> (S % BW))
203-
let layout = self.layout_of(instance_args.type_at(0))?;
204+
let layout_val = self.layout_of(instance_args.type_at(0))?;
204205
let val = self.read_scalar(&args[0])?;
205-
let val_bits = val.to_bits(layout.size)?;
206+
let val_bits = val.to_bits(layout_val.size)?;
207+
208+
let layout_raw_shift = self.layout_of(instance_args.type_at(1))?;
206209
let raw_shift = self.read_scalar(&args[1])?;
207-
let raw_shift_bits = raw_shift.to_bits(layout.size)?;
208-
let width_bits = u128::from(layout.size.bits());
210+
let raw_shift_bits = raw_shift.to_bits(layout_raw_shift.size)?;
211+
212+
let width_bits = u128::from(layout_val.size.bits());
209213
let shift_bits = raw_shift_bits % width_bits;
210214
let inv_shift_bits = (width_bits - shift_bits) % width_bits;
211215
let result_bits = if intrinsic_name == sym::rotate_left {
212216
(val_bits << shift_bits) | (val_bits >> inv_shift_bits)
213217
} else {
214218
(val_bits >> shift_bits) | (val_bits << inv_shift_bits)
215219
};
216-
let truncated_bits = self.truncate(result_bits, layout);
217-
let result = Scalar::from_uint(truncated_bits, layout.size);
220+
let truncated_bits = self.truncate(result_bits, layout_val);
221+
let result = Scalar::from_uint(truncated_bits, layout_val.size);
218222
self.write_scalar(result, dest)?;
219223
}
220224
sym::copy => {
@@ -472,6 +476,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
472476
name: Symbol,
473477
val: Scalar<M::Provenance>,
474478
layout: TyAndLayout<'tcx>,
479+
ret_layout: TyAndLayout<'tcx>,
475480
) -> InterpResult<'tcx, Scalar<M::Provenance>> {
476481
assert!(layout.ty.is_integral(), "invalid type for numeric intrinsic: {}", layout.ty);
477482
let bits = val.to_bits(layout.size)?;
@@ -487,7 +492,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
487492
sym::bitreverse => (bits << extra).reverse_bits(),
488493
_ => bug!("not a numeric intrinsic: {}", name),
489494
};
490-
Ok(Scalar::from_uint(bits_out, layout.size))
495+
Ok(Scalar::from_uint(bits_out, ret_layout.size))
491496
}
492497

493498
pub fn exact_div(

compiler/rustc_hir_analysis/src/check/intrinsic.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -411,13 +411,11 @@ pub fn check_intrinsic_type(
411411
(1, 0, vec![Ty::new_mut_ptr(tcx, param(0)), param(0)], Ty::new_unit(tcx))
412412
}
413413

414-
sym::ctpop
415-
| sym::ctlz
416-
| sym::ctlz_nonzero
417-
| sym::cttz
418-
| sym::cttz_nonzero
419-
| sym::bswap
420-
| sym::bitreverse => (1, 0, vec![param(0)], param(0)),
414+
sym::ctpop | sym::ctlz | sym::ctlz_nonzero | sym::cttz | sym::cttz_nonzero => {
415+
(1, 0, vec![param(0)], tcx.types.u32)
416+
}
417+
418+
sym::bswap | sym::bitreverse => (1, 0, vec![param(0)], param(0)),
421419

422420
sym::three_way_compare => {
423421
(1, 0, vec![param(0), param(0)], tcx.ty_ordering_enum(Some(span)))
@@ -460,7 +458,7 @@ pub fn check_intrinsic_type(
460458
(1, 0, vec![param(0), param(0)], param(0))
461459
}
462460
sym::unchecked_shl | sym::unchecked_shr => (2, 0, vec![param(0), param(1)], param(0)),
463-
sym::rotate_left | sym::rotate_right => (1, 0, vec![param(0), param(0)], param(0)),
461+
sym::rotate_left | sym::rotate_right => (1, 0, vec![param(0), tcx.types.u32], param(0)),
464462
sym::unchecked_add | sym::unchecked_sub | sym::unchecked_mul => {
465463
(1, 0, vec![param(0), param(0)], param(0))
466464
}

library/core/src/intrinsics.rs

+47
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,13 @@ extern "rust-intrinsic" {
19871987
/// The stabilized versions of this intrinsic are available on the integer
19881988
/// primitives via the `count_ones` method. For example,
19891989
/// [`u32::count_ones`]
1990+
#[cfg(not(bootstrap))]
1991+
#[rustc_const_stable(feature = "const_ctpop", since = "1.40.0")]
1992+
#[rustc_safe_intrinsic]
1993+
#[rustc_nounwind]
1994+
pub fn ctpop<T: Copy>(x: T) -> u32;
1995+
1996+
#[cfg(bootstrap)]
19901997
#[rustc_const_stable(feature = "const_ctpop", since = "1.40.0")]
19911998
#[rustc_safe_intrinsic]
19921999
#[rustc_nounwind]
@@ -2028,6 +2035,13 @@ extern "rust-intrinsic" {
20282035
/// let num_leading = ctlz(x);
20292036
/// assert_eq!(num_leading, 16);
20302037
/// ```
2038+
#[cfg(not(bootstrap))]
2039+
#[rustc_const_stable(feature = "const_ctlz", since = "1.40.0")]
2040+
#[rustc_safe_intrinsic]
2041+
#[rustc_nounwind]
2042+
pub fn ctlz<T: Copy>(x: T) -> u32;
2043+
2044+
#[cfg(bootstrap)]
20312045
#[rustc_const_stable(feature = "const_ctlz", since = "1.40.0")]
20322046
#[rustc_safe_intrinsic]
20332047
#[rustc_nounwind]
@@ -2050,6 +2064,12 @@ extern "rust-intrinsic" {
20502064
/// let num_leading = unsafe { ctlz_nonzero(x) };
20512065
/// assert_eq!(num_leading, 3);
20522066
/// ```
2067+
#[cfg(not(bootstrap))]
2068+
#[rustc_const_stable(feature = "constctlz", since = "1.50.0")]
2069+
#[rustc_nounwind]
2070+
pub fn ctlz_nonzero<T: Copy>(x: T) -> u32;
2071+
2072+
#[cfg(bootstrap)]
20532073
#[rustc_const_stable(feature = "constctlz", since = "1.50.0")]
20542074
#[rustc_nounwind]
20552075
pub fn ctlz_nonzero<T: Copy>(x: T) -> T;
@@ -2090,6 +2110,13 @@ extern "rust-intrinsic" {
20902110
/// let num_trailing = cttz(x);
20912111
/// assert_eq!(num_trailing, 16);
20922112
/// ```
2113+
#[cfg(not(bootstrap))]
2114+
#[rustc_const_stable(feature = "const_cttz", since = "1.40.0")]
2115+
#[rustc_safe_intrinsic]
2116+
#[rustc_nounwind]
2117+
pub fn cttz<T: Copy>(x: T) -> u32;
2118+
2119+
#[cfg(bootstrap)]
20932120
#[rustc_const_stable(feature = "const_cttz", since = "1.40.0")]
20942121
#[rustc_safe_intrinsic]
20952122
#[rustc_nounwind]
@@ -2112,6 +2139,12 @@ extern "rust-intrinsic" {
21122139
/// let num_trailing = unsafe { cttz_nonzero(x) };
21132140
/// assert_eq!(num_trailing, 3);
21142141
/// ```
2142+
#[cfg(not(bootstrap))]
2143+
#[rustc_const_stable(feature = "const_cttz_nonzero", since = "1.53.0")]
2144+
#[rustc_nounwind]
2145+
pub fn cttz_nonzero<T: Copy>(x: T) -> u32;
2146+
2147+
#[cfg(bootstrap)]
21152148
#[rustc_const_stable(feature = "const_cttz_nonzero", since = "1.53.0")]
21162149
#[rustc_nounwind]
21172150
pub fn cttz_nonzero<T: Copy>(x: T) -> T;
@@ -2288,6 +2321,13 @@ extern "rust-intrinsic" {
22882321
/// The stabilized versions of this intrinsic are available on the integer
22892322
/// primitives via the `rotate_left` method. For example,
22902323
/// [`u32::rotate_left`]
2324+
#[cfg(not(bootstrap))]
2325+
#[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
2326+
#[rustc_safe_intrinsic]
2327+
#[rustc_nounwind]
2328+
pub fn rotate_left<T: Copy>(x: T, shift: u32) -> T;
2329+
2330+
#[cfg(bootstrap)]
22912331
#[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
22922332
#[rustc_safe_intrinsic]
22932333
#[rustc_nounwind]
@@ -2303,6 +2343,13 @@ extern "rust-intrinsic" {
23032343
/// The stabilized versions of this intrinsic are available on the integer
23042344
/// primitives via the `rotate_right` method. For example,
23052345
/// [`u32::rotate_right`]
2346+
#[cfg(not(bootstrap))]
2347+
#[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
2348+
#[rustc_safe_intrinsic]
2349+
#[rustc_nounwind]
2350+
pub fn rotate_right<T: Copy>(x: T, shift: u32) -> T;
2351+
2352+
#[cfg(bootstrap)]
23062353
#[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
23072354
#[rustc_safe_intrinsic]
23082355
#[rustc_nounwind]

library/core/src/num/nonzero.rs

+6
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,9 @@ macro_rules! nonzero_integer {
528528
#[inline]
529529
pub const fn leading_zeros(self) -> u32 {
530530
// SAFETY: since `self` cannot be zero, it is safe to call `ctlz_nonzero`.
531+
#[cfg(not(bootstrap))]
532+
unsafe { intrinsics::ctlz_nonzero(self.get() as $UnsignedPrimitive) }
533+
#[cfg(bootstrap)]
531534
unsafe { intrinsics::ctlz_nonzero(self.get() as $UnsignedPrimitive) as u32 }
532535
}
533536

@@ -552,6 +555,9 @@ macro_rules! nonzero_integer {
552555
#[inline]
553556
pub const fn trailing_zeros(self) -> u32 {
554557
// SAFETY: since `self` cannot be zero, it is safe to call `cttz_nonzero`.
558+
#[cfg(not(bootstrap))]
559+
unsafe { intrinsics::cttz_nonzero(self.get() as $UnsignedPrimitive) }
560+
#[cfg(bootstrap)]
555561
unsafe { intrinsics::cttz_nonzero(self.get() as $UnsignedPrimitive) as u32 }
556562
}
557563

library/core/src/num/uint_macros.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ macro_rules! uint_impl {
7777
without modifying the original"]
7878
#[inline(always)]
7979
pub const fn count_ones(self) -> u32 {
80-
intrinsics::ctpop(self as $ActualT) as u32
80+
#[cfg(not(bootstrap))]
81+
return intrinsics::ctpop(self as $ActualT);
82+
#[cfg(bootstrap)]
83+
return intrinsics::ctpop(self as $ActualT) as u32;
8184
}
8285

8386
/// Returns the number of zeros in the binary representation of `self`.
@@ -119,7 +122,10 @@ macro_rules! uint_impl {
119122
without modifying the original"]
120123
#[inline(always)]
121124
pub const fn leading_zeros(self) -> u32 {
122-
intrinsics::ctlz(self as $ActualT) as u32
125+
#[cfg(not(bootstrap))]
126+
return intrinsics::ctlz(self as $ActualT);
127+
#[cfg(bootstrap)]
128+
return intrinsics::ctlz(self as $ActualT) as u32;
123129
}
124130

125131
/// Returns the number of trailing zeros in the binary representation
@@ -140,7 +146,10 @@ macro_rules! uint_impl {
140146
without modifying the original"]
141147
#[inline(always)]
142148
pub const fn trailing_zeros(self) -> u32 {
143-
intrinsics::cttz(self) as u32
149+
#[cfg(not(bootstrap))]
150+
return intrinsics::cttz(self);
151+
#[cfg(bootstrap)]
152+
return intrinsics::cttz(self) as u32;
144153
}
145154

146155
/// Returns the number of leading ones in the binary representation of `self`.
@@ -205,7 +214,10 @@ macro_rules! uint_impl {
205214
without modifying the original"]
206215
#[inline(always)]
207216
pub const fn rotate_left(self, n: u32) -> Self {
208-
intrinsics::rotate_left(self, n as $SelfT)
217+
#[cfg(not(bootstrap))]
218+
return intrinsics::rotate_left(self, n);
219+
#[cfg(bootstrap)]
220+
return intrinsics::rotate_left(self, n as $SelfT);
209221
}
210222

211223
/// Shifts the bits to the right by a specified amount, `n`,
@@ -230,7 +242,10 @@ macro_rules! uint_impl {
230242
without modifying the original"]
231243
#[inline(always)]
232244
pub const fn rotate_right(self, n: u32) -> Self {
233-
intrinsics::rotate_right(self, n as $SelfT)
245+
#[cfg(not(bootstrap))]
246+
return intrinsics::rotate_right(self, n);
247+
#[cfg(bootstrap)]
248+
return intrinsics::rotate_right(self, n as $SelfT);
234249
}
235250

236251
/// Reverses the byte order of the integer.

0 commit comments

Comments
 (0)