core_intrinsics)Expand description
Compiler intrinsics.
The functions in this module are implementation details of core and should
not be used outside of the standard library. We generally provide access to
intrinsics via stable wrapper functions. Use these instead.
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/HEAD/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/HEAD/compiler/rustc_codegen_ssa/src/mir/intrinsic.rs and https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_codegen_llvm/src/intrinsic.rs, and for const evaluation in https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_const_eval/src/interpret/intrinsics.rs.
§Const intrinsics
In order to make an intrinsic unstable usable at compile-time, copy the implementation from
https://github.com/rust-lang/miri/blob/master/src/intrinsics to
https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_const_eval/src/interpret/intrinsics.rs
and make the intrinsic declaration below a const fn. This should be done in coordination with
wg-const-eval.
If an intrinsic is supposed to be used from a const fn with a rustc_const_stable attribute,
#[rustc_intrinsic_const_stable_indirect] needs to be added to the intrinsic. Such a change requires
T-lang approval, because it may bake a feature into the language that cannot be replicated in
user code without compiler support.
§Volatiles
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 read_volatile
and write_volatile.
§Atomics
The atomic intrinsics provide common atomic operations on machine words, with multiple possible memory orderings. See the [atomic types][atomic] docs for details.
§Unwinding
Rust intrinsics may, in general, unwind. If an intrinsic can never unwind, add the
#[rustc_nounwind] attribute so that the compiler can make use of this fact.
However, even for intrinsics that may unwind, rustc assumes that a Rust intrinsics will never initiate a foreign (non-Rust) unwind, and thus for panic=abort we can always assume that these intrinsics cannot unwind.
Enums§
- Atomic
Ordering Experimental - A type for atomic ordering parameters for intrinsics. This is a separate type from
atomic::Orderingso that we can make itConstParamTyand fix the values used here without a risk of leaking that to stable code.
Functions§
- copyâš
Deprecated - This is an accidentally-stable alias to
ptr::copy; use that instead. - copy_
nonoverlapping âšDeprecated - This is an accidentally-stable alias to
ptr::copy_nonoverlapping; use that instead. - transmuteâš
Deprecated - Reinterprets the bits of a value of one type as another type.
- write_
bytes âšDeprecated - This is an accidentally-stable alias to
ptr::write_bytes; use that instead. - abort
Experimental - Aborts the execution of the process.
- add_
with_ overflow Experimental - Performs checked integer addition.
- aggregate_
raw_ ptr Experimental - Lowers in MIR to
Rvalue::AggregatewithAggregateKind::RawPtr. - align_
of Experimental - The minimum alignment of a type.
- align_
of_ âšval Experimental - The required alignment of the referenced value.
- assert_
inhabited Experimental - A guard for unsafe functions that cannot ever be executed if
Tis uninhabited: This will statically either panic, or do nothing. It does not guarantee to ever panic, and should only be called if an assertion failure will imply language UB in the following code. - assumeâš
Experimental - Informs the optimizer that a condition is always true. If the condition is false, the behavior is undefined.
- atomic_
and âšExperimental - Bitwise and with the current value, returning the previous value.
Tmust be an integer or pointer type.Umust be the same asTif that is an integer type, orusizeifTis a pointer type. - atomic_
cxchg âšExperimental - Stores a value if the current value is the same as the
oldvalue.Tmust be an integer or pointer type. - atomic_
cxchgweak âšExperimental - Stores a value if the current value is the same as the
oldvalue.Tmust be an integer or pointer type. The comparison may spuriously fail. - atomic_
load âšExperimental - Loads the current value of the pointer.
Tmust be an integer or pointer type. - atomic_
nand âšExperimental - Bitwise nand with the current value, returning the previous value.
Tmust be an integer or pointer type.Umust be the same asTif that is an integer type, orusizeifTis a pointer type. - atomic_
or âšExperimental - Bitwise or with the current value, returning the previous value.
Tmust be an integer or pointer type.Umust be the same asTif that is an integer type, orusizeifTis a pointer type. - atomic_
store âšExperimental - Stores the value at the specified memory location.
Tmust be an integer or pointer type. - atomic_
umax âšExperimental - Maximum with the current value using an unsigned comparison.
Tmust be an unsigned integer type. - atomic_
umin âšExperimental - Minimum with the current value using an unsigned comparison.
Tmust be an unsigned integer type. - atomic_
xadd âšExperimental - Adds to the current value, returning the previous value.
Tmust be an integer or pointer type.Umust be the same asTif that is an integer type, orusizeifTis a pointer type. - atomic_
xchg âšExperimental - Stores the value at the specified memory location, returning the old value.
Tmust be an integer or pointer type. - atomic_
xor âšExperimental - Bitwise xor with the current value, returning the previous value.
Tmust be an integer or pointer type.Umust be the same asTif that is an integer type, orusizeifTis a pointer type. - atomic_
xsub âšExperimental - Subtract from the current value, returning the previous value.
Tmust be an integer or pointer type.Umust be the same asTif that is an integer type, orusizeifTis a pointer type. - autodiff
Experimental - Generates the LLVM body for the automatic differentiation of
fusing Enzyme, withdfas the derivative function andargsas its arguments. - cold_
path Experimental - Hints to the compiler that current code path is cold.
- const_
eval_ select Experimental - Selects which function to call depending on the context.
- const_
make_ âšglobal Experimental - ctpop
Experimental - Returns the number of bits set in an integer type
T - cttz_
nonzero âšExperimental - Like
cttz, but extra-unsafe as it returnsundefwhen given anxwith value0. - discriminant_
value Experimental - Returns the value of the discriminant for the variant in ‘v’;
if
Thas no discriminant, returns0. - exact_
div âšExperimental - Performs an exact division, resulting in undefined behavior where
x % y != 0ory == 0orx == T::MIN && y == -1 - mul_
with_ overflow Experimental - Performs checked integer multiplication
- needs_
drop Experimental - Returns
trueif the actual type given asTrequires drop glue; returnsfalseif the actual type provided forTimplementsCopy. - offsetâš
Experimental - Calculates the offset from a pointer.
- ptr_
guaranteed_ cmp Experimental - See documentation of
<*const T>::guaranteed_eqfor details. Returns2if the result is unknown. Returns1if the pointers are guaranteed equal. Returns0if the pointers are guaranteed inequal. - ptr_
metadata Experimental - Lowers in MIR to
Rvalue::UnaryOpwithUnOp::PtrMetadata. - read_
via_ âšcopy Experimental - This is an implementation detail of
crate::ptr::readand should not be used anywhere else. See its comments for why this exists. - size_of
Experimental - The size of a type in bytes.
- size_
of_ âšval Experimental - The size of the referenced value in bytes.
- slice_
get_ âšunchecked Experimental - Projects to the
index-th element ofslice_ptr, as the same kind of pointer as the slice was provided – so&mut [T] → &mut T,&[T] → &T,*mut [T] → *mut T, or*const [T] → *const T– without a bounds check. - sub_
with_ overflow Experimental - Performs checked integer subtraction
- three_
way_ compare Experimental - Does a three-way comparison between the two arguments, which must be of character or integer (signed or unsigned) type.
- transmute_
unchecked âšExperimental - Like
transmute, but even less checked at compile-time: rather than giving an error forsize_of::<Src>() != size_of::<Dst>(), it’s Undefined Behavior at runtime. - type_id
Experimental - Gets an identifier which is globally unique to the specified type. This function will return the same value for a type regardless of whichever crate it is invoked in.
- type_
id_ eq Experimental - Tests (at compile-time) if two
crate::any::TypeIdinstances identify the same type. This is necessary because at const-eval time the actual discriminating data is opaque and cannot be inspected directly. - type_
name Experimental - Gets a static string slice containing the name of a type.
- ub_
checks Experimental - Returns whether we should perform some UB-checking at runtime. This eventually evaluates to
cfg!(ub_checks), but behaves different fromcfg!when mixing crates built with different flags: if the crate has UB checks enabled or carries the#[rustc_preserve_ub_checks]attribute, evaluation is delayed until monomorphization (or until the call gets inlined into a crate that does not delay evaluation further); otherwise it can happen any time. - unchecked_
add âšExperimental - Returns the result of an unchecked addition, resulting in
undefined behavior when
x + y > T::MAXorx + y < T::MIN. - unchecked_
rem âšExperimental - Returns the remainder of an unchecked division, resulting in
undefined behavior when
y == 0orx == T::MIN && y == -1 - unchecked_
shl âšExperimental - Performs an unchecked left shift, resulting in undefined behavior when
y < 0ory >= N, where N is the width of T in bits. - unchecked_
shr âšExperimental - Performs an unchecked right shift, resulting in undefined behavior when
y < 0ory >= N, where N is the width of T in bits. - unchecked_
sub âšExperimental - Returns the result of an unchecked subtraction, resulting in
undefined behavior when
x - y > T::MAXorx - y < T::MIN. - unlikely
Experimental - Hints to the compiler that branch condition is likely to be false. Returns the value passed to it.
- unreachableâš
Experimental - Informs the optimizer that this point in the code is not reachable, enabling further optimizations.
- volatile_
load âšExperimental - Performs a volatile load from the
srcpointer. - volatile_
store âšExperimental - Performs a volatile store to the
dstpointer. - wrapping_
add Experimental - Returns (a + b) mod 2N, where N is the width of T in bits.
- wrapping_
mul Experimental - Returns (a * b) mod 2N, where N is the width of T in bits.
- wrapping_
sub Experimental - Returns (a - b) mod 2N, where N is the width of T in bits.
- write_
via_ âšmove Experimental - This is an implementation detail of
crate::ptr::writeand should not be used anywhere else. See its comments for why this exists.