Add documentation for the semantics of MIR rvalues

This commit is contained in:
Jakob Degen 2022-03-24 22:29:33 -04:00
parent 2f4a7a0742
commit 634369170a
2 changed files with 101 additions and 23 deletions

View file

@ -59,6 +59,7 @@
#![feature(unwrap_infallible)]
#![feature(decl_macro)]
#![feature(drain_filter)]
#![feature(intra_doc_pointers)]
#![recursion_limit = "512"]
#![allow(rustc::potential_query_instability)]

View file

@ -2388,57 +2388,134 @@ impl<'tcx> Operand<'tcx> {
#[derive(Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq)]
/// The various kinds of rvalues that can appear in MIR.
///
/// Not all of these are allowed at every [`MirPhase`]. Check the documentation there to see which
/// ones you do not have to worry about. The MIR validator will generally enforce such restrictions,
/// causing an ICE if they are violated.
/// Not all of these are allowed at every [`MirPhase`] - when this is the case, it's stated below.
///
/// Computing any rvalue begins by evaluating the places and operands in the rvalue in the order in
/// which they appear. These are then used to produce a "value" - the same kind of value that an
/// [`Operand`] is.
pub enum Rvalue<'tcx> {
/// x (either a move or copy, depending on type of x)
/// Yields the operand unchanged
Use(Operand<'tcx>),
/// [x; 32]
/// Creates an array where each element is the value of the operand. This currently does not
/// drop the value even if the number of repetitions is zero, see [#74836].
///
/// Corresponds to source code like `[x; 32]`.
///
/// [#74836]: https://github.com/rust-lang/rust/issues/74836
Repeat(Operand<'tcx>, ty::Const<'tcx>),
/// &x or &mut x
/// Creates a reference of the indicated kind to the place.
///
/// There is not much to document here, because besides the obvious parts the semantics of this
/// are essentially entirely a part of the aliasing model. There are many UCG issues discussing
/// exactly what the behavior of this operation should be.
///
/// `Shallow` borrows are disallowed after drop lowering.
Ref(Region<'tcx>, BorrowKind, Place<'tcx>),
/// Accessing a thread local static. This is inherently a runtime operation, even if llvm
/// treats it as an access to a static. This `Rvalue` yields a reference to the thread local
/// static.
/// Returns a pointer/reference to the given thread local.
///
/// The yielded type is a `*mut T` if the static is mutable, otherwise if the static is extern a
/// `*const T`, and if neither of those apply a `&T`.
///
/// **Note:** This is a runtime operation that actually executes code and is in this sense more
/// like a function call. Also, DSEing these causes `fn main() {}` to SIGILL for some reason
/// that I never got a chance to look into.
///
/// **Needs clarification**: Are there weird additional semantics here related to the runtime
/// nature of this operation?
ThreadLocalRef(DefId),
/// Create a raw pointer to the given place
/// Can be generated by raw address of expressions (`&raw const x`),
/// or when casting a reference to a raw pointer.
/// Creates a pointer with the indicated mutability to the place.
///
/// This is generated by pointer casts like `&v as *const _` or raw address of expressions like
/// `&raw v` or `addr_of!(v)`.
///
/// Like with references, the semantics of this operation are heavily dependent on the aliasing
/// model.
AddressOf(Mutability, Place<'tcx>),
/// length of a `[X]` or `[X;n]` value
/// Yields the length of the place, as a `usize`.
///
/// If the type of the place is an array, this is the array length. This also works for slices
/// (`[T]`, not `&[T]`) through some mechanism that depends on how exactly places work (see
/// there for more details).
Len(Place<'tcx>),
/// Performs essentially all of the casts that can be performed via `as`.
///
/// This allows for casts from/to a variety of types.
///
/// **FIXME**: Document exactly which `CastKind`s allow which types of casts. Figure out why
/// `ArrayToPointer` and `MutToConstPointer` are special.
Cast(CastKind, Operand<'tcx>, Ty<'tcx>),
/// * `Offset` has the same semantics as [`offset`](pointer::offset), except that the second
/// paramter may be a `usize` as well.
/// * The comparison operations accept `bool`s, `char`s, signed or unsigned integers, floats,
/// raw pointers, or function pointers and return a `bool`.
/// * Left and right shift operations accept signed or unsigned integers not necessarily of the
/// same type and return a value of the same type as their LHS. For all other operations, the
/// types of the operands must match.
/// * The `Bit*` operations accept signed integers, unsigned integers, or bools and return a
/// value of that type.
/// * The remaining operations accept signed integers, unsigned integers, or floats of any
/// matching type and return a value of that type.
BinaryOp(BinOp, Box<(Operand<'tcx>, Operand<'tcx>)>),
/// Same as `BinaryOp`, but yields `(T, bool)` instead of `T`. In addition to performing the
/// same computation as the matching `BinaryOp`, checks if the infinite precison result would be
/// unequal to the actual result and sets the `bool` if this is the case. `BinOp::Offset` is not
/// allowed here.
///
/// **FIXME**: What about division/modulo? Are they allowed here at all? Are zero divisors still
/// UB? Also, which other combinations of types are disallowed?
CheckedBinaryOp(BinOp, Box<(Operand<'tcx>, Operand<'tcx>)>),
/// Yields the size or alignment of the type as a `usize`.
NullaryOp(NullOp, Ty<'tcx>),
/// Exactly like `BinaryOp`, but less operands.
///
/// Also does two's-complement arithmetic. Negation requires a signed integer or a float; binary
/// not requires a signed integer, unsigned integer, or bool. Both operation kinds return a
/// value with the same type as their operand.
UnaryOp(UnOp, Operand<'tcx>),
/// Read the discriminant of an ADT.
/// Computes the discriminant of the place, returning it as an integer of type
/// [`discriminant_ty`].
///
/// Undefined (i.e., no effort is made to make it defined, but theres no reason why it cannot
/// be defined to return, say, a 0) if ADT is not an enum.
/// The validity requirements for the underlying value are undecided for this rvalue, see
/// [#91095]. Note too that the value of the discriminant is not the same thing as the
/// variant index; use [`discriminant_for_variant`] to convert.
///
/// For types defined in the source code as enums, this is well behaved. This is also well
/// formed for other types, but yields no particular value - there is no reason it couldn't be
/// defined to yield eg zero though.
///
/// [`discriminant_ty`]: crate::ty::Ty::discriminant_ty
/// [#91095]: https://github.com/rust-lang/rust/issues/91095
/// [`discriminant_for_variant`]: crate::ty::Ty::discriminant_for_variant
Discriminant(Place<'tcx>),
/// Creates an aggregate value, like a tuple or struct. This is
/// only needed because we want to distinguish `dest = Foo { x:
/// ..., y: ... }` from `dest.x = ...; dest.y = ...;` in the case
/// that `Foo` has a destructor. These rvalues can be optimized
/// away after type-checking and before lowering.
/// Creates an aggregate value, like a tuple or struct.
///
/// This is needed because dataflow analysis needs to distinguish
/// `dest = Foo { x: ..., y: ... }` from `dest.x = ...; dest.y = ...;` in the case that `Foo`
/// has a destructor.
///
/// Disallowed after deaggregation for all aggregate kinds except `Array` and `Generator`. After
/// generator lowering, `Generator` aggregate kinds are disallowed too.
Aggregate(Box<AggregateKind<'tcx>>, Vec<Operand<'tcx>>),
/// Transmutes a `*mut u8` into shallow-initialized `Box<T>`.
///
/// This is different a normal transmute because dataflow analysis will treat the box
/// as initialized but its content as uninitialized.
/// This is different a normal transmute because dataflow analysis will treat the box as
/// initialized but its content as uninitialized. Like other pointer casts, this in general
/// affects alias analysis.
///
/// Disallowed after drop elaboration.
ShallowInitBox(Operand<'tcx>, Ty<'tcx>),
}