Skip to content

intrinsics, ScalarInt: minor cleanup #141582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/interpret/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ impl<'tcx, Prov: Provenance> ImmTy<'tcx, Prov> {
let ty = tcx.ty_ordering_enum(None);
let layout =
tcx.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(ty)).unwrap();
Self::from_scalar(Scalar::from_i8(c as i8), layout)
Self::from_scalar(Scalar::Int(c.into()), layout)
}

pub fn from_pair(a: Self, b: Self, cx: &(impl HasTypingEnv<'tcx> + HasTyCtxt<'tcx>)) -> Self {
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_middle/src/mir/interpret/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,27 +180,27 @@ impl<Prov> Scalar<Prov> {

#[inline]
pub fn from_i8(i: i8) -> Self {
Self::from_int(i, Size::from_bits(8))
Self::Int(i.into())
}

#[inline]
pub fn from_i16(i: i16) -> Self {
Self::from_int(i, Size::from_bits(16))
Self::Int(i.into())
}

#[inline]
pub fn from_i32(i: i32) -> Self {
Self::from_int(i, Size::from_bits(32))
Self::Int(i.into())
}

#[inline]
pub fn from_i64(i: i64) -> Self {
Self::from_int(i, Size::from_bits(64))
Self::Int(i.into())
}

#[inline]
pub fn from_i128(i: i128) -> Self {
Self::from_int(i, Size::from_bits(128))
Self::Int(i.into())
}

#[inline]
Expand Down
47 changes: 45 additions & 2 deletions compiler/rustc_middle/src/ty/consts/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,9 @@ macro_rules! from_scalar_int_for_x {
impl From<ScalarInt> for $ty {
#[inline]
fn from(int: ScalarInt) -> Self {
// The `unwrap` cannot fail because to_bits (if it succeeds)
// The `unwrap` cannot fail because to_uint (if it succeeds)
// is guaranteed to return a value that fits into the size.
int.to_bits(Size::from_bytes(size_of::<$ty>()))
int.to_uint(Size::from_bytes(size_of::<$ty>()))
.try_into().unwrap()
}
}
Expand All @@ -450,6 +450,49 @@ impl From<char> for ScalarInt {
}
}

macro_rules! from_x_for_scalar_int_signed {
($($ty:ty),*) => {
$(
impl From<$ty> for ScalarInt {
#[inline]
fn from(u: $ty) -> Self {
Self {
data: u128::from(u.cast_unsigned()), // go via the unsigned type of the same size
size: NonZero::new(size_of::<$ty>() as u8).unwrap(),
}
}
}
)*
}
}

macro_rules! from_scalar_int_for_x_signed {
($($ty:ty),*) => {
$(
impl From<ScalarInt> for $ty {
#[inline]
fn from(int: ScalarInt) -> Self {
// The `unwrap` cannot fail because to_int (if it succeeds)
// is guaranteed to return a value that fits into the size.
int.to_int(Size::from_bytes(size_of::<$ty>()))
.try_into().unwrap()
}
}
)*
}
}

from_x_for_scalar_int_signed!(i8, i16, i32, i64, i128);
from_scalar_int_for_x_signed!(i8, i16, i32, i64, i128);

impl From<std::cmp::Ordering> for ScalarInt {
#[inline]
fn from(c: std::cmp::Ordering) -> Self {
// Here we rely on `Ordering` having the same values in host and target!
ScalarInt::from(c as i8)
}
}

/// Error returned when a conversion from ScalarInt to char fails.
#[derive(Debug)]
pub struct CharTryFromScalarInt;
Expand Down
29 changes: 9 additions & 20 deletions library/core/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! Compiler intrinsics.
//!
//! The corresponding definitions are in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_llvm/src/intrinsic.rs>.
//! The corresponding const implementations are in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_const_eval/src/interpret/intrinsics.rs>.
//! These are the imports making intrinsics available to Rust code. The actual implementations live in the compiler.
//! Some of these intrinsics are lowered to MIR in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_mir_transform/src/lower_intrinsics.rs>.
//! The remaining intrinsics are implemented for the LLVM backend in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_ssa/src/mir/intrinsic.rs>
//! and <https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_llvm/src/intrinsic.rs>,
//! and for const evaluation in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_const_eval/src/interpret/intrinsics.rs>.
//!
//! # Const intrinsics
//!
Expand All @@ -20,28 +23,14 @@
//!
//! The volatile intrinsics provide operations intended to act on I/O
//! memory, which are guaranteed to not be reordered by the compiler
//! across other volatile intrinsics. See the LLVM documentation on
//! [[volatile]].
//!
//! [volatile]: https://llvm.org/docs/LangRef.html#volatile-memory-accesses
//! across other volatile intrinsics. See [`read_volatile`][ptr::read_volatile]
//! and [`write_volatile`][ptr::write_volatile].
//!
//! # Atomics
//!
//! The atomic intrinsics provide common atomic operations on machine
//! words, with multiple possible memory orderings. They obey the same
//! semantics as C++11. See the LLVM documentation on [[atomics]].
//!
//! [atomics]: https://llvm.org/docs/Atomics.html
//!
//! A quick refresher on memory ordering:
//!
//! * Acquire - a barrier for acquiring a lock. Subsequent reads and writes
//! take place after the barrier.
//! * Release - a barrier for releasing a lock. Preceding reads and writes
//! take place before the barrier.
//! * Sequentially consistent - sequentially consistent operations are
//! guaranteed to happen in order. This is the standard mode for working
//! with atomic types and is equivalent to Java's `volatile`.
//! words, with multiple possible memory orderings. See the
//! [atomic types][crate::sync::atomic] docs for details.
//!
//! # Unwinding
//!
Expand Down
Loading