2019-09-06 03:57:44 +01:00
|
|
|
//! An interpreter for MIR used in CTFE and by miri.
|
2017-07-21 17:25:30 +02:00
|
|
|
|
2017-08-02 16:59:01 +02:00
|
|
|
#[macro_export]
|
2019-08-01 09:49:01 +05:30
|
|
|
macro_rules! err_unsup {
|
2019-07-29 13:06:42 +05:30
|
|
|
($($tt:tt)*) => {
|
2019-08-01 09:49:01 +05:30
|
|
|
$crate::mir::interpret::InterpError::Unsupported(
|
|
|
|
$crate::mir::interpret::UnsupportedOpInfo::$($tt)*
|
|
|
|
)
|
2019-07-29 13:06:42 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-08-03 20:35:52 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! err_unsup_format {
|
|
|
|
($($tt:tt)*) => { err_unsup!(Unsupported(format!($($tt)*))) };
|
|
|
|
}
|
|
|
|
|
2019-07-30 15:25:12 +05:30
|
|
|
#[macro_export]
|
|
|
|
macro_rules! err_inval {
|
|
|
|
($($tt:tt)*) => {
|
|
|
|
$crate::mir::interpret::InterpError::InvalidProgram(
|
|
|
|
$crate::mir::interpret::InvalidProgramInfo::$($tt)*
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
2019-08-01 09:49:01 +05:30
|
|
|
macro_rules! err_ub {
|
2019-07-30 15:25:12 +05:30
|
|
|
($($tt:tt)*) => {
|
2019-08-02 23:24:27 +02:00
|
|
|
$crate::mir::interpret::InterpError::UndefinedBehavior(
|
|
|
|
$crate::mir::interpret::UndefinedBehaviorInfo::$($tt)*
|
2019-08-01 09:49:01 +05:30
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-08-03 20:35:52 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! err_ub_format {
|
|
|
|
($($tt:tt)*) => { err_ub!(Ub(format!($($tt)*))) };
|
|
|
|
}
|
|
|
|
|
2019-07-31 12:48:54 +05:30
|
|
|
#[macro_export]
|
|
|
|
macro_rules! err_exhaust {
|
|
|
|
($($tt:tt)*) => {
|
|
|
|
$crate::mir::interpret::InterpError::ResourceExhaustion(
|
|
|
|
$crate::mir::interpret::ResourceExhaustionInfo::$($tt)*
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-23 08:48:03 +01:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! err_machine_stop {
|
|
|
|
($($tt:tt)*) => {
|
|
|
|
$crate::mir::interpret::InterpError::MachineStop(Box::new($($tt)*))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-19 09:07:43 +01:00
|
|
|
// In the `throw_*` macros, avoid `return` to make them work with `try {}`.
|
2019-07-31 12:48:54 +05:30
|
|
|
#[macro_export]
|
2019-08-01 09:49:01 +05:30
|
|
|
macro_rules! throw_unsup {
|
2022-05-29 01:19:52 -07:00
|
|
|
($($tt:tt)*) => { do yeet err_unsup!($($tt)*) };
|
2019-08-01 09:49:01 +05:30
|
|
|
}
|
|
|
|
|
2019-08-02 23:41:24 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! throw_unsup_format {
|
|
|
|
($($tt:tt)*) => { throw_unsup!(Unsupported(format!($($tt)*))) };
|
|
|
|
}
|
|
|
|
|
2019-08-01 09:49:01 +05:30
|
|
|
#[macro_export]
|
|
|
|
macro_rules! throw_inval {
|
2022-05-29 01:19:52 -07:00
|
|
|
($($tt:tt)*) => { do yeet err_inval!($($tt)*) };
|
2019-08-01 09:49:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! throw_ub {
|
2022-05-29 01:19:52 -07:00
|
|
|
($($tt:tt)*) => { do yeet err_ub!($($tt)*) };
|
2019-07-31 12:48:54 +05:30
|
|
|
}
|
|
|
|
|
2019-08-02 23:41:24 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! throw_ub_format {
|
|
|
|
($($tt:tt)*) => { throw_ub!(Ub(format!($($tt)*))) };
|
|
|
|
}
|
|
|
|
|
2019-08-01 09:49:01 +05:30
|
|
|
#[macro_export]
|
|
|
|
macro_rules! throw_exhaust {
|
2022-05-29 01:19:52 -07:00
|
|
|
($($tt:tt)*) => { do yeet err_exhaust!($($tt)*) };
|
2019-08-01 09:49:01 +05:30
|
|
|
}
|
|
|
|
|
2019-12-02 10:59:06 +01:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! throw_machine_stop {
|
2022-05-29 01:19:52 -07:00
|
|
|
($($tt:tt)*) => { do yeet err_machine_stop!($($tt)*) };
|
2019-12-02 10:59:06 +01:00
|
|
|
}
|
|
|
|
|
2023-05-17 10:30:14 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! err_ub_custom {
|
|
|
|
($msg:expr $(, $($name:ident = $value:expr),* $(,)?)?) => {{
|
|
|
|
$(
|
|
|
|
let ($($name,)*) = ($($value,)*);
|
|
|
|
)?
|
|
|
|
err_ub!(Custom(
|
|
|
|
rustc_middle::error::CustomSubdiagnostic {
|
|
|
|
msg: || $msg,
|
|
|
|
add_args: Box::new(move |mut set_arg| {
|
|
|
|
$($(
|
|
|
|
set_arg(stringify!($name).into(), rustc_errors::IntoDiagnosticArg::into_diagnostic_arg($name));
|
|
|
|
)*)?
|
|
|
|
})
|
|
|
|
}
|
|
|
|
))
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! throw_ub_custom {
|
|
|
|
($($tt:tt)*) => { do yeet err_ub_custom!($($tt)*) };
|
|
|
|
}
|
|
|
|
|
2018-10-23 18:05:32 +02:00
|
|
|
mod allocation;
|
2016-03-14 21:48:00 -06:00
|
|
|
mod error;
|
2018-10-25 18:23:09 +02:00
|
|
|
mod pointer;
|
2019-11-30 08:42:56 +13:00
|
|
|
mod queries;
|
2016-12-07 20:30:37 -08:00
|
|
|
mod value;
|
|
|
|
|
2020-03-21 13:49:02 +01:00
|
|
|
use std::fmt;
|
|
|
|
use std::io;
|
2020-08-20 02:37:00 -07:00
|
|
|
use std::io::{Read, Write};
|
2021-07-13 08:58:59 +02:00
|
|
|
use std::num::{NonZeroU32, NonZeroU64};
|
2020-03-21 13:49:02 +01:00
|
|
|
use std::sync::atomic::{AtomicU32, Ordering};
|
|
|
|
|
2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast::LitKind;
|
2020-03-21 13:49:02 +01:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
|
|
|
use rustc_data_structures::sync::{HashMapExt, Lock};
|
|
|
|
use rustc_data_structures::tiny_list::TinyList;
|
2022-11-10 05:39:06 +00:00
|
|
|
use rustc_errors::ErrorGuaranteed;
|
2020-03-21 13:49:02 +01:00
|
|
|
use rustc_hir::def_id::DefId;
|
|
|
|
use rustc_macros::HashStable;
|
2020-09-02 10:40:56 +03:00
|
|
|
use rustc_middle::ty::print::with_no_trimmed_paths;
|
2020-06-11 15:49:57 +01:00
|
|
|
use rustc_serialize::{Decodable, Encodable};
|
2023-01-25 01:46:19 -05:00
|
|
|
use rustc_target::abi::{AddressSpace, Endian, HasDataLayout};
|
2020-03-21 13:49:02 +01:00
|
|
|
|
|
|
|
use crate::mir;
|
2020-06-11 15:49:57 +01:00
|
|
|
use crate::ty::codec::{TyDecoder, TyEncoder};
|
2023-07-11 22:35:29 +01:00
|
|
|
use crate::ty::GenericArgKind;
|
2022-05-27 20:03:57 -07:00
|
|
|
use crate::ty::{self, Instance, Ty, TyCtxt};
|
2020-03-21 13:49:02 +01:00
|
|
|
|
2018-06-25 18:46:02 +02:00
|
|
|
pub use self::error::{
|
2020-08-20 18:55:07 +02:00
|
|
|
struct_error, CheckInAllocMsg, ErrorHandled, EvalToAllocationRawResult, EvalToConstValueResult,
|
2023-05-17 10:30:14 +00:00
|
|
|
EvalToValTreeResult, ExpectedKind, InterpError, InterpErrorInfo, InterpResult, InvalidMetaKind,
|
|
|
|
InvalidProgramInfo, MachineStopType, PointerKind, ReportedErrorInfo, ResourceExhaustionInfo,
|
|
|
|
ScalarSizeMismatch, UndefinedBehaviorInfo, UninitBytesAccess, UnsupportedOpInfo,
|
|
|
|
ValidationErrorInfo, ValidationErrorKind,
|
2018-06-25 18:46:02 +02:00
|
|
|
};
|
2016-06-10 13:01:51 +02:00
|
|
|
|
2022-08-01 21:40:29 -04:00
|
|
|
pub use self::value::{get_slice_bytes, ConstAlloc, ConstValue, Scalar};
|
2016-06-10 13:01:51 +02:00
|
|
|
|
2021-03-31 00:06:01 -04:00
|
|
|
pub use self::allocation::{
|
2023-02-14 15:26:47 -05:00
|
|
|
alloc_range, AllocBytes, AllocError, AllocRange, AllocResult, Allocation, ConstAllocation,
|
|
|
|
InitChunk, InitChunkIter,
|
2021-03-31 00:06:01 -04:00
|
|
|
};
|
2018-10-23 18:05:32 +02:00
|
|
|
|
2021-07-12 18:22:15 +02:00
|
|
|
pub use self::pointer::{Pointer, PointerArithmetic, Provenance};
|
2018-10-25 18:23:09 +02:00
|
|
|
|
2019-11-11 14:32:36 +00:00
|
|
|
/// Uniquely identifies one of the following:
|
|
|
|
/// - A constant
|
|
|
|
/// - A static
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, TyEncodable, TyDecodable)]
|
2022-09-15 15:05:03 +00:00
|
|
|
#[derive(HashStable, Lift, TypeFoldable, TypeVisitable)]
|
2017-12-12 17:14:49 +01:00
|
|
|
pub struct GlobalId<'tcx> {
|
|
|
|
/// For a constant or static, the `Instance` of the item itself.
|
|
|
|
/// For a promoted global, the `Instance` of the function they belong to.
|
|
|
|
pub instance: ty::Instance<'tcx>,
|
|
|
|
|
2019-05-29 00:26:56 +03:00
|
|
|
/// The index for promoted globals within their function's `mir::Body`.
|
2017-12-12 17:14:49 +01:00
|
|
|
pub promoted: Option<mir::Promoted>,
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
impl<'tcx> GlobalId<'tcx> {
|
2020-08-19 12:05:32 +02:00
|
|
|
pub fn display(self, tcx: TyCtxt<'tcx>) -> String {
|
2022-02-16 13:04:48 -05:00
|
|
|
let instance_name = with_no_trimmed_paths!(tcx.def_path_str(self.instance.def.def_id()));
|
2020-08-19 12:05:32 +02:00
|
|
|
if let Some(promoted) = self.promoted {
|
|
|
|
format!("{}::{:?}", instance_name, promoted)
|
|
|
|
} else {
|
|
|
|
instance_name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-11 20:57:38 +13:00
|
|
|
/// Input argument for `tcx.lit_to_const`.
|
2020-01-11 15:22:36 +13:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, HashStable)]
|
|
|
|
pub struct LitToConstInput<'tcx> {
|
2020-01-11 20:57:38 +13:00
|
|
|
/// The absolute value of the resultant constant.
|
2020-01-11 15:22:36 +13:00
|
|
|
pub lit: &'tcx LitKind,
|
2020-01-11 20:57:38 +13:00
|
|
|
/// The type of the constant.
|
2020-01-11 15:22:36 +13:00
|
|
|
pub ty: Ty<'tcx>,
|
2020-01-11 20:57:38 +13:00
|
|
|
/// If the constant is negative.
|
2020-01-11 15:22:36 +13:00
|
|
|
pub neg: bool,
|
|
|
|
}
|
|
|
|
|
2020-01-11 20:57:38 +13:00
|
|
|
/// Error type for `tcx.lit_to_const`.
|
2020-01-11 15:22:36 +13:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, HashStable)]
|
|
|
|
pub enum LitToConstError {
|
2020-02-20 23:23:22 +01:00
|
|
|
/// The literal's inferred type did not match the expected `ty` in the input.
|
|
|
|
/// This is used for graceful error handling (`delay_span_bug`) in
|
2020-03-23 22:24:31 +01:00
|
|
|
/// type checking (`Const::from_anon_const`).
|
2020-02-20 23:23:22 +01:00
|
|
|
TypeError,
|
2022-11-10 05:39:06 +00:00
|
|
|
Reported(ErrorGuaranteed),
|
2020-01-11 15:22:36 +13:00
|
|
|
}
|
|
|
|
|
2019-12-23 17:41:06 +01:00
|
|
|
#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
2021-07-13 08:58:59 +02:00
|
|
|
pub struct AllocId(pub NonZeroU64);
|
2017-12-12 17:14:49 +01:00
|
|
|
|
2020-04-26 18:59:20 +02:00
|
|
|
// We want the `Debug` output to be readable as it is used by `derive(Debug)` for
|
|
|
|
// all the Miri types.
|
|
|
|
impl fmt::Debug for AllocId {
|
2020-04-26 18:14:16 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2020-04-26 19:33:16 +02:00
|
|
|
if f.alternate() { write!(f, "a{}", self.0) } else { write!(f, "alloc{}", self.0) }
|
2020-03-08 18:52:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-02 10:53:34 -04:00
|
|
|
// No "Display" since AllocIds are not usually user-visible.
|
2019-12-23 17:41:06 +01:00
|
|
|
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(TyDecodable, TyEncodable)]
|
2018-12-03 16:30:43 +01:00
|
|
|
enum AllocDiscriminant {
|
2018-04-10 09:58:46 +02:00
|
|
|
Alloc,
|
|
|
|
Fn,
|
2022-07-19 19:57:44 -04:00
|
|
|
VTable,
|
2018-04-10 16:25:10 +02:00
|
|
|
Static,
|
2018-04-10 09:58:46 +02:00
|
|
|
}
|
2018-03-16 09:59:42 +01:00
|
|
|
|
2022-05-27 20:03:57 -07:00
|
|
|
pub fn specialized_encode_alloc_id<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>>(
|
2018-03-16 09:59:42 +01:00
|
|
|
encoder: &mut E,
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-03-16 09:59:42 +01:00
|
|
|
alloc_id: AllocId,
|
Use delayed error handling for `Encodable` and `Encoder` infallible.
There are two impls of the `Encoder` trait: `opaque::Encoder` and
`opaque::FileEncoder`. The former encodes into memory and is infallible, the
latter writes to file and is fallible.
Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a
bit verbose and has non-trivial cost, which is annoying given how rare failures
are (especially in the infallible `opaque::Encoder` case).
This commit changes how `Encoder` fallibility is handled. All the `emit_*`
methods are now infallible. `opaque::Encoder` requires no great changes for
this. `opaque::FileEncoder` now implements a delayed error handling strategy.
If a failure occurs, it records this via the `res` field, and all subsequent
encoding operations are skipped if `res` indicates an error has occurred. Once
encoding is complete, the new `finish` method is called, which returns a
`Result`. In other words, there is now a single `Result`-producing method
instead of many of them.
This has very little effect on how any file errors are reported if
`opaque::FileEncoder` has any failures.
Much of this commit is boring mechanical changes, removing `Result` return
values and `?` or `unwrap` from expressions. The more interesting parts are as
follows.
- serialize.rs: The `Encoder` trait gains an `Ok` associated type. The
`into_inner` method is changed into `finish`, which returns
`Result<Vec<u8>, !>`.
- opaque.rs: The `FileEncoder` adopts the delayed error handling
strategy. Its `Ok` type is a `usize`, returning the number of bytes
written, replacing previous uses of `FileEncoder::position`.
- Various methods that take an encoder now consume it, rather than being
passed a mutable reference, e.g. `serialize_query_result_cache`.
2022-06-07 13:30:45 +10:00
|
|
|
) {
|
2020-05-08 10:58:53 +02:00
|
|
|
match tcx.global_alloc(alloc_id) {
|
2019-05-30 13:05:05 +02:00
|
|
|
GlobalAlloc::Memory(alloc) => {
|
2018-05-02 06:03:06 +02:00
|
|
|
trace!("encoding {:?} with {:#?}", alloc_id, alloc);
|
Use delayed error handling for `Encodable` and `Encoder` infallible.
There are two impls of the `Encoder` trait: `opaque::Encoder` and
`opaque::FileEncoder`. The former encodes into memory and is infallible, the
latter writes to file and is fallible.
Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a
bit verbose and has non-trivial cost, which is annoying given how rare failures
are (especially in the infallible `opaque::Encoder` case).
This commit changes how `Encoder` fallibility is handled. All the `emit_*`
methods are now infallible. `opaque::Encoder` requires no great changes for
this. `opaque::FileEncoder` now implements a delayed error handling strategy.
If a failure occurs, it records this via the `res` field, and all subsequent
encoding operations are skipped if `res` indicates an error has occurred. Once
encoding is complete, the new `finish` method is called, which returns a
`Result`. In other words, there is now a single `Result`-producing method
instead of many of them.
This has very little effect on how any file errors are reported if
`opaque::FileEncoder` has any failures.
Much of this commit is boring mechanical changes, removing `Result` return
values and `?` or `unwrap` from expressions. The more interesting parts are as
follows.
- serialize.rs: The `Encoder` trait gains an `Ok` associated type. The
`into_inner` method is changed into `finish`, which returns
`Result<Vec<u8>, !>`.
- opaque.rs: The `FileEncoder` adopts the delayed error handling
strategy. Its `Ok` type is a `usize`, returning the number of bytes
written, replacing previous uses of `FileEncoder::position`.
- Various methods that take an encoder now consume it, rather than being
passed a mutable reference, e.g. `serialize_query_result_cache`.
2022-06-07 13:30:45 +10:00
|
|
|
AllocDiscriminant::Alloc.encode(encoder);
|
|
|
|
alloc.encode(encoder);
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
2019-05-30 13:05:05 +02:00
|
|
|
GlobalAlloc::Function(fn_instance) => {
|
2018-05-02 06:03:06 +02:00
|
|
|
trace!("encoding {:?} with {:#?}", alloc_id, fn_instance);
|
Use delayed error handling for `Encodable` and `Encoder` infallible.
There are two impls of the `Encoder` trait: `opaque::Encoder` and
`opaque::FileEncoder`. The former encodes into memory and is infallible, the
latter writes to file and is fallible.
Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a
bit verbose and has non-trivial cost, which is annoying given how rare failures
are (especially in the infallible `opaque::Encoder` case).
This commit changes how `Encoder` fallibility is handled. All the `emit_*`
methods are now infallible. `opaque::Encoder` requires no great changes for
this. `opaque::FileEncoder` now implements a delayed error handling strategy.
If a failure occurs, it records this via the `res` field, and all subsequent
encoding operations are skipped if `res` indicates an error has occurred. Once
encoding is complete, the new `finish` method is called, which returns a
`Result`. In other words, there is now a single `Result`-producing method
instead of many of them.
This has very little effect on how any file errors are reported if
`opaque::FileEncoder` has any failures.
Much of this commit is boring mechanical changes, removing `Result` return
values and `?` or `unwrap` from expressions. The more interesting parts are as
follows.
- serialize.rs: The `Encoder` trait gains an `Ok` associated type. The
`into_inner` method is changed into `finish`, which returns
`Result<Vec<u8>, !>`.
- opaque.rs: The `FileEncoder` adopts the delayed error handling
strategy. Its `Ok` type is a `usize`, returning the number of bytes
written, replacing previous uses of `FileEncoder::position`.
- Various methods that take an encoder now consume it, rather than being
passed a mutable reference, e.g. `serialize_query_result_cache`.
2022-06-07 13:30:45 +10:00
|
|
|
AllocDiscriminant::Fn.encode(encoder);
|
|
|
|
fn_instance.encode(encoder);
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
2022-07-19 19:57:44 -04:00
|
|
|
GlobalAlloc::VTable(ty, poly_trait_ref) => {
|
2022-07-17 11:36:37 -04:00
|
|
|
trace!("encoding {:?} with {ty:#?}, {poly_trait_ref:#?}", alloc_id);
|
2022-07-19 19:57:44 -04:00
|
|
|
AllocDiscriminant::VTable.encode(encoder);
|
2022-07-17 11:36:37 -04:00
|
|
|
ty.encode(encoder);
|
|
|
|
poly_trait_ref.encode(encoder);
|
|
|
|
}
|
2019-05-30 13:05:05 +02:00
|
|
|
GlobalAlloc::Static(did) => {
|
2020-05-02 21:44:25 +02:00
|
|
|
assert!(!tcx.is_thread_local_static(did));
|
2019-09-06 03:57:44 +01:00
|
|
|
// References to statics doesn't need to know about their allocations,
|
|
|
|
// just about its `DefId`.
|
Use delayed error handling for `Encodable` and `Encoder` infallible.
There are two impls of the `Encoder` trait: `opaque::Encoder` and
`opaque::FileEncoder`. The former encodes into memory and is infallible, the
latter writes to file and is fallible.
Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a
bit verbose and has non-trivial cost, which is annoying given how rare failures
are (especially in the infallible `opaque::Encoder` case).
This commit changes how `Encoder` fallibility is handled. All the `emit_*`
methods are now infallible. `opaque::Encoder` requires no great changes for
this. `opaque::FileEncoder` now implements a delayed error handling strategy.
If a failure occurs, it records this via the `res` field, and all subsequent
encoding operations are skipped if `res` indicates an error has occurred. Once
encoding is complete, the new `finish` method is called, which returns a
`Result`. In other words, there is now a single `Result`-producing method
instead of many of them.
This has very little effect on how any file errors are reported if
`opaque::FileEncoder` has any failures.
Much of this commit is boring mechanical changes, removing `Result` return
values and `?` or `unwrap` from expressions. The more interesting parts are as
follows.
- serialize.rs: The `Encoder` trait gains an `Ok` associated type. The
`into_inner` method is changed into `finish`, which returns
`Result<Vec<u8>, !>`.
- opaque.rs: The `FileEncoder` adopts the delayed error handling
strategy. Its `Ok` type is a `usize`, returning the number of bytes
written, replacing previous uses of `FileEncoder::position`.
- Various methods that take an encoder now consume it, rather than being
passed a mutable reference, e.g. `serialize_query_result_cache`.
2022-06-07 13:30:45 +10:00
|
|
|
AllocDiscriminant::Static.encode(encoder);
|
2023-03-25 09:46:19 +01:00
|
|
|
// Cannot use `did.encode(encoder)` because of a bug around
|
|
|
|
// specializations and method calls.
|
|
|
|
Encodable::<E>::encode(&did, encoder);
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
2018-03-16 09:59:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 17:19:31 +02:00
|
|
|
// Used to avoid infinite recursion when decoding cyclic allocations.
|
|
|
|
type DecodingSessionId = NonZeroU32;
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
enum State {
|
|
|
|
Empty,
|
|
|
|
InProgressNonAlloc(TinyList<DecodingSessionId>),
|
|
|
|
InProgress(TinyList<DecodingSessionId>, AllocId),
|
|
|
|
Done(AllocId),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AllocDecodingState {
|
2019-09-06 03:57:44 +01:00
|
|
|
// For each `AllocId`, we keep track of which decoding state it's currently in.
|
|
|
|
decoding_state: Vec<Lock<State>>,
|
2018-05-25 17:19:31 +02:00
|
|
|
// The offsets of each allocation in the data stream.
|
2023-07-10 21:39:28 -04:00
|
|
|
data_offsets: Vec<u64>,
|
2018-05-25 17:19:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AllocDecodingState {
|
2021-06-01 00:00:00 +00:00
|
|
|
#[inline]
|
2018-08-29 22:02:42 -07:00
|
|
|
pub fn new_decoding_session(&self) -> AllocDecodingSession<'_> {
|
2018-05-25 17:19:31 +02:00
|
|
|
static DECODER_SESSION_ID: AtomicU32 = AtomicU32::new(0);
|
|
|
|
let counter = DECODER_SESSION_ID.fetch_add(1, Ordering::SeqCst);
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
// Make sure this is never zero.
|
2018-05-25 17:19:31 +02:00
|
|
|
let session_id = DecodingSessionId::new((counter & 0x7FFFFFFF) + 1).unwrap();
|
|
|
|
|
|
|
|
AllocDecodingSession { state: self, session_id }
|
|
|
|
}
|
|
|
|
|
2023-07-10 21:39:28 -04:00
|
|
|
pub fn new(data_offsets: Vec<u64>) -> Self {
|
2023-03-14 11:30:23 +00:00
|
|
|
let decoding_state =
|
|
|
|
std::iter::repeat_with(|| Lock::new(State::Empty)).take(data_offsets.len()).collect();
|
2018-05-25 17:19:31 +02:00
|
|
|
|
2018-10-30 14:37:26 +01:00
|
|
|
Self { decoding_state, data_offsets }
|
2018-05-25 17:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct AllocDecodingSession<'s> {
|
|
|
|
state: &'s AllocDecodingState,
|
|
|
|
session_id: DecodingSessionId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'s> AllocDecodingSession<'s> {
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Decodes an `AllocId` in a thread-safe way.
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
pub fn decode_alloc_id<'tcx, D>(&self, decoder: &mut D) -> AllocId
|
2019-06-12 00:11:55 +03:00
|
|
|
where
|
2022-05-27 20:03:57 -07:00
|
|
|
D: TyDecoder<I = TyCtxt<'tcx>>,
|
2018-05-25 17:19:31 +02:00
|
|
|
{
|
2019-09-06 03:57:44 +01:00
|
|
|
// Read the index of the allocation.
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
let idx = usize::try_from(decoder.read_u32()).unwrap();
|
2020-03-21 13:49:02 +01:00
|
|
|
let pos = usize::try_from(self.state.data_offsets[idx]).unwrap();
|
2018-05-25 17:19:31 +02:00
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
// Decode the `AllocDiscriminant` now so that we know if we have to reserve an
|
|
|
|
// `AllocId`.
|
2018-05-25 17:19:31 +02:00
|
|
|
let (alloc_kind, pos) = decoder.with_position(pos, |decoder| {
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
let alloc_kind = AllocDiscriminant::decode(decoder);
|
|
|
|
(alloc_kind, decoder.position())
|
|
|
|
});
|
2018-05-25 17:19:31 +02:00
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
// Check the decoding state to see if it's already decoded or if we should
|
2018-05-25 17:19:31 +02:00
|
|
|
// decode it here.
|
|
|
|
let alloc_id = {
|
|
|
|
let mut entry = self.state.decoding_state[idx].lock();
|
|
|
|
|
|
|
|
match *entry {
|
|
|
|
State::Done(alloc_id) => {
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
return alloc_id;
|
2018-05-25 17:19:31 +02:00
|
|
|
}
|
|
|
|
ref mut entry @ State::Empty => {
|
2019-09-06 03:57:44 +01:00
|
|
|
// We are allowed to decode.
|
2018-05-25 17:19:31 +02:00
|
|
|
match alloc_kind {
|
2018-12-03 16:30:43 +01:00
|
|
|
AllocDiscriminant::Alloc => {
|
2018-05-25 17:19:31 +02:00
|
|
|
// If this is an allocation, we need to reserve an
|
2019-09-06 03:57:44 +01:00
|
|
|
// `AllocId` so we can decode cyclic graphs.
|
2022-05-27 20:03:57 -07:00
|
|
|
let alloc_id = decoder.interner().reserve_alloc_id();
|
2018-05-25 17:19:31 +02:00
|
|
|
*entry =
|
|
|
|
State::InProgress(TinyList::new_single(self.session_id), alloc_id);
|
|
|
|
Some(alloc_id)
|
|
|
|
}
|
2022-07-17 11:40:34 -04:00
|
|
|
AllocDiscriminant::Fn
|
|
|
|
| AllocDiscriminant::Static
|
2022-07-19 19:57:44 -04:00
|
|
|
| AllocDiscriminant::VTable => {
|
2019-09-06 03:57:44 +01:00
|
|
|
// Fns and statics cannot be cyclic, and their `AllocId`
|
|
|
|
// is determined later by interning.
|
2018-05-25 17:19:31 +02:00
|
|
|
*entry =
|
|
|
|
State::InProgressNonAlloc(TinyList::new_single(self.session_id));
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
State::InProgressNonAlloc(ref mut sessions) => {
|
|
|
|
if sessions.contains(&self.session_id) {
|
2019-09-06 03:57:44 +01:00
|
|
|
bug!("this should be unreachable");
|
2018-05-25 17:19:31 +02:00
|
|
|
} else {
|
2019-09-06 03:57:44 +01:00
|
|
|
// Start decoding concurrently.
|
2018-05-25 17:19:31 +02:00
|
|
|
sessions.insert(self.session_id);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
State::InProgress(ref mut sessions, alloc_id) => {
|
|
|
|
if sessions.contains(&self.session_id) {
|
|
|
|
// Don't recurse.
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
return alloc_id;
|
2018-05-25 17:19:31 +02:00
|
|
|
} else {
|
2019-09-06 03:57:44 +01:00
|
|
|
// Start decoding concurrently.
|
2018-05-25 17:19:31 +02:00
|
|
|
sessions.insert(self.session_id);
|
|
|
|
Some(alloc_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
// Now decode the actual data.
|
2018-05-25 17:19:31 +02:00
|
|
|
let alloc_id = decoder.with_position(pos, |decoder| {
|
|
|
|
match alloc_kind {
|
2018-12-03 16:30:43 +01:00
|
|
|
AllocDiscriminant::Alloc => {
|
Introduce `ConstAllocation`.
Currently some `Allocation`s are interned, some are not, and it's very
hard to tell at a use point which is which.
This commit introduces `ConstAllocation` for the known-interned ones,
which makes the division much clearer. `ConstAllocation::inner()` is
used to get the underlying `Allocation`.
In some places it's natural to use an `Allocation`, in some it's natural
to use a `ConstAllocation`, and in some places there's no clear choice.
I've tried to make things look as nice as possible, while generally
favouring `ConstAllocation`, which is the type that embodies more
information. This does require quite a few calls to `inner()`.
The commit also tweaks how `PartialOrd` works for `Interned`. The
previous code was too clever by half, building on `T: Ord` to make the
code shorter. That caused problems with deriving `PartialOrd` and `Ord`
for `ConstAllocation`, so I changed it to build on `T: PartialOrd`,
which is slightly more verbose but much more standard and avoided the
problems.
2022-03-02 07:15:04 +11:00
|
|
|
let alloc = <ConstAllocation<'tcx> as Decodable<_>>::decode(decoder);
|
2019-09-06 03:57:44 +01:00
|
|
|
// We already have a reserved `AllocId`.
|
2018-05-25 17:19:31 +02:00
|
|
|
let alloc_id = alloc_id.unwrap();
|
2019-09-06 03:57:44 +01:00
|
|
|
trace!("decoded alloc {:?}: {:#?}", alloc_id, alloc);
|
2022-05-27 20:03:57 -07:00
|
|
|
decoder.interner().set_alloc_id_same_memory(alloc_id, alloc);
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
alloc_id
|
2018-05-25 17:19:31 +02:00
|
|
|
}
|
2018-12-03 16:30:43 +01:00
|
|
|
AllocDiscriminant::Fn => {
|
2018-05-25 17:19:31 +02:00
|
|
|
assert!(alloc_id.is_none());
|
2019-09-06 03:57:44 +01:00
|
|
|
trace!("creating fn alloc ID");
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
let instance = ty::Instance::decode(decoder);
|
2018-05-25 17:19:31 +02:00
|
|
|
trace!("decoded fn alloc instance: {:?}", instance);
|
2022-05-27 20:03:57 -07:00
|
|
|
let alloc_id = decoder.interner().create_fn_alloc(instance);
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
alloc_id
|
2018-05-25 17:19:31 +02:00
|
|
|
}
|
2022-07-19 19:57:44 -04:00
|
|
|
AllocDiscriminant::VTable => {
|
2022-07-17 11:36:37 -04:00
|
|
|
assert!(alloc_id.is_none());
|
2022-07-20 10:47:49 -04:00
|
|
|
trace!("creating vtable alloc ID");
|
2022-07-17 11:36:37 -04:00
|
|
|
let ty = <Ty<'_> as Decodable<D>>::decode(decoder);
|
2022-07-17 11:40:34 -04:00
|
|
|
let poly_trait_ref =
|
|
|
|
<Option<ty::PolyExistentialTraitRef<'_>> as Decodable<D>>::decode(decoder);
|
2022-07-17 11:36:37 -04:00
|
|
|
trace!("decoded vtable alloc instance: {ty:?}, {poly_trait_ref:?}");
|
|
|
|
let alloc_id = decoder.interner().create_vtable_alloc(ty, poly_trait_ref);
|
|
|
|
alloc_id
|
|
|
|
}
|
2018-12-03 16:30:43 +01:00
|
|
|
AllocDiscriminant::Static => {
|
2018-05-25 17:19:31 +02:00
|
|
|
assert!(alloc_id.is_none());
|
2019-09-06 03:57:44 +01:00
|
|
|
trace!("creating extern static alloc ID");
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
let did = <DefId as Decodable<D>>::decode(decoder);
|
2019-09-06 03:57:44 +01:00
|
|
|
trace!("decoded static def-ID: {:?}", did);
|
2022-05-27 20:03:57 -07:00
|
|
|
let alloc_id = decoder.interner().create_static_alloc(did);
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
alloc_id
|
2018-05-25 17:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
});
|
2018-05-25 17:19:31 +02:00
|
|
|
|
|
|
|
self.state.decoding_state[idx].with_lock(|entry| {
|
|
|
|
*entry = State::Done(alloc_id);
|
|
|
|
});
|
|
|
|
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
alloc_id
|
2018-05-25 17:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 13:05:05 +02:00
|
|
|
/// An allocation in the global (tcx-managed) memory can be either a function pointer,
|
|
|
|
/// a static, or a "real" allocation with some data in it.
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, Hash, TyDecodable, TyEncodable, HashStable)]
|
2019-05-30 13:05:05 +02:00
|
|
|
pub enum GlobalAlloc<'tcx> {
|
2019-09-06 03:57:44 +01:00
|
|
|
/// The alloc ID is used as a function pointer.
|
2018-05-02 06:03:06 +02:00
|
|
|
Function(Instance<'tcx>),
|
2022-07-17 11:36:37 -04:00
|
|
|
/// This alloc ID points to a symbolic (not-reified) vtable.
|
2022-07-19 19:57:44 -04:00
|
|
|
VTable(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>),
|
2019-02-08 14:53:55 +01:00
|
|
|
/// The alloc ID points to a "lazy" static variable that did not get computed (yet).
|
2018-08-23 19:04:33 +02:00
|
|
|
/// This is also used to break the cycle in recursive statics.
|
2018-05-02 06:03:06 +02:00
|
|
|
Static(DefId),
|
2019-02-08 14:53:55 +01:00
|
|
|
/// The alloc ID points to memory.
|
Introduce `ConstAllocation`.
Currently some `Allocation`s are interned, some are not, and it's very
hard to tell at a use point which is which.
This commit introduces `ConstAllocation` for the known-interned ones,
which makes the division much clearer. `ConstAllocation::inner()` is
used to get the underlying `Allocation`.
In some places it's natural to use an `Allocation`, in some it's natural
to use a `ConstAllocation`, and in some places there's no clear choice.
I've tried to make things look as nice as possible, while generally
favouring `ConstAllocation`, which is the type that embodies more
information. This does require quite a few calls to `inner()`.
The commit also tweaks how `PartialOrd` works for `Interned`. The
previous code was too clever by half, building on `T: Ord` to make the
code shorter. That caused problems with deriving `PartialOrd` and `Ord`
for `ConstAllocation`, so I changed it to build on `T: PartialOrd`,
which is slightly more verbose but much more standard and avoided the
problems.
2022-03-02 07:15:04 +11:00
|
|
|
Memory(ConstAllocation<'tcx>),
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
impl<'tcx> GlobalAlloc<'tcx> {
|
2020-04-24 17:33:25 +02:00
|
|
|
/// Panics if the `GlobalAlloc` does not refer to an `GlobalAlloc::Memory`
|
|
|
|
#[track_caller]
|
|
|
|
#[inline]
|
Introduce `ConstAllocation`.
Currently some `Allocation`s are interned, some are not, and it's very
hard to tell at a use point which is which.
This commit introduces `ConstAllocation` for the known-interned ones,
which makes the division much clearer. `ConstAllocation::inner()` is
used to get the underlying `Allocation`.
In some places it's natural to use an `Allocation`, in some it's natural
to use a `ConstAllocation`, and in some places there's no clear choice.
I've tried to make things look as nice as possible, while generally
favouring `ConstAllocation`, which is the type that embodies more
information. This does require quite a few calls to `inner()`.
The commit also tweaks how `PartialOrd` works for `Interned`. The
previous code was too clever by half, building on `T: Ord` to make the
code shorter. That caused problems with deriving `PartialOrd` and `Ord`
for `ConstAllocation`, so I changed it to build on `T: PartialOrd`,
which is slightly more verbose but much more standard and avoided the
problems.
2022-03-02 07:15:04 +11:00
|
|
|
pub fn unwrap_memory(&self) -> ConstAllocation<'tcx> {
|
2020-04-24 17:33:25 +02:00
|
|
|
match *self {
|
|
|
|
GlobalAlloc::Memory(mem) => mem,
|
|
|
|
_ => bug!("expected memory, got {:?}", self),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Panics if the `GlobalAlloc` is not `GlobalAlloc::Function`
|
|
|
|
#[track_caller]
|
|
|
|
#[inline]
|
|
|
|
pub fn unwrap_fn(&self) -> Instance<'tcx> {
|
|
|
|
match *self {
|
|
|
|
GlobalAlloc::Function(instance) => instance,
|
|
|
|
_ => bug!("expected function, got {:?}", self),
|
|
|
|
}
|
|
|
|
}
|
2022-07-17 11:36:37 -04:00
|
|
|
|
2022-07-19 19:57:44 -04:00
|
|
|
/// Panics if the `GlobalAlloc` is not `GlobalAlloc::VTable`
|
2022-07-17 11:36:37 -04:00
|
|
|
#[track_caller]
|
|
|
|
#[inline]
|
|
|
|
pub fn unwrap_vtable(&self) -> (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>) {
|
|
|
|
match *self {
|
2022-07-19 19:57:44 -04:00
|
|
|
GlobalAlloc::VTable(ty, poly_trait_ref) => (ty, poly_trait_ref),
|
2022-07-17 11:36:37 -04:00
|
|
|
_ => bug!("expected vtable, got {:?}", self),
|
|
|
|
}
|
|
|
|
}
|
2023-01-25 01:46:19 -05:00
|
|
|
|
|
|
|
/// The address space that this `GlobalAlloc` should be placed in.
|
|
|
|
#[inline]
|
|
|
|
pub fn address_space(&self, cx: &impl HasDataLayout) -> AddressSpace {
|
|
|
|
match self {
|
|
|
|
GlobalAlloc::Function(..) => cx.data_layout().instruction_address_space,
|
|
|
|
GlobalAlloc::Static(..) | GlobalAlloc::Memory(..) | GlobalAlloc::VTable(..) => {
|
|
|
|
AddressSpace::DATA
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 17:33:25 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) struct AllocMap<'tcx> {
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Maps `AllocId`s to their corresponding allocations.
|
2019-05-30 13:05:05 +02:00
|
|
|
alloc_map: FxHashMap<AllocId, GlobalAlloc<'tcx>>,
|
2018-05-02 06:03:06 +02:00
|
|
|
|
2019-05-30 13:05:05 +02:00
|
|
|
/// Used to ensure that statics and functions only get one associated `AllocId`.
|
|
|
|
/// Should never contain a `GlobalAlloc::Memory`!
|
2019-09-06 03:57:44 +01:00
|
|
|
//
|
|
|
|
// FIXME: Should we just have two separate dedup maps for statics and functions each?
|
2019-05-30 13:05:05 +02:00
|
|
|
dedup: FxHashMap<GlobalAlloc<'tcx>, AllocId>,
|
2018-05-02 06:03:06 +02:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// The `AllocId` to assign to the next requested ID.
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Always incremented; never gets smaller.
|
2018-05-02 06:03:06 +02:00
|
|
|
next_id: AllocId,
|
|
|
|
}
|
|
|
|
|
2018-12-03 14:54:58 +01:00
|
|
|
impl<'tcx> AllocMap<'tcx> {
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn new() -> Self {
|
2021-07-14 09:56:54 +02:00
|
|
|
AllocMap {
|
|
|
|
alloc_map: Default::default(),
|
|
|
|
dedup: Default::default(),
|
|
|
|
next_id: AllocId(NonZeroU64::new(1).unwrap()),
|
|
|
|
}
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
2020-04-24 17:34:56 +02:00
|
|
|
fn reserve(&mut self) -> AllocId {
|
2018-05-02 06:03:06 +02:00
|
|
|
let next = self.next_id;
|
|
|
|
self.next_id.0 = self.next_id.0.checked_add(1).expect(
|
|
|
|
"You overflowed a u64 by incrementing by 1... \
|
2020-01-03 13:31:56 +01:00
|
|
|
You've just earned yourself a free drink if we ever meet. \
|
|
|
|
Seriously, how did you do that?!",
|
2018-05-02 06:03:06 +02:00
|
|
|
);
|
|
|
|
next
|
|
|
|
}
|
2020-04-24 12:53:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> TyCtxt<'tcx> {
|
|
|
|
/// Obtains a new allocation ID that can be referenced but does not
|
|
|
|
/// yet have an allocation backing it.
|
|
|
|
///
|
|
|
|
/// Make sure to call `set_alloc_id_memory` or `set_alloc_id_same_memory` before returning such
|
|
|
|
/// an `AllocId` from a query.
|
2020-09-18 20:49:25 +02:00
|
|
|
pub fn reserve_alloc_id(self) -> AllocId {
|
2020-04-24 12:53:18 +02:00
|
|
|
self.alloc_map.lock().reserve()
|
|
|
|
}
|
2018-05-02 06:03:06 +02:00
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Reserves a new ID *if* this allocation has not been dedup-reserved before.
|
2022-07-17 11:36:37 -04:00
|
|
|
/// Should only be used for "symbolic" allocations (function pointers, vtables, statics), we
|
|
|
|
/// don't want to dedup IDs for "real" memory!
|
2020-09-18 20:49:25 +02:00
|
|
|
fn reserve_and_set_dedup(self, alloc: GlobalAlloc<'tcx>) -> AllocId {
|
2020-04-24 12:53:18 +02:00
|
|
|
let mut alloc_map = self.alloc_map.lock();
|
2019-05-30 13:05:05 +02:00
|
|
|
match alloc {
|
2022-07-19 19:57:44 -04:00
|
|
|
GlobalAlloc::Function(..) | GlobalAlloc::Static(..) | GlobalAlloc::VTable(..) => {}
|
2019-05-30 13:05:05 +02:00
|
|
|
GlobalAlloc::Memory(..) => bug!("Trying to dedup-reserve memory with real data!"),
|
|
|
|
}
|
2020-04-24 12:53:18 +02:00
|
|
|
if let Some(&alloc_id) = alloc_map.dedup.get(&alloc) {
|
2018-05-02 06:03:06 +02:00
|
|
|
return alloc_id;
|
|
|
|
}
|
2020-04-24 12:53:18 +02:00
|
|
|
let id = alloc_map.reserve();
|
2022-07-02 13:37:24 -04:00
|
|
|
debug!("creating alloc {alloc:?} with id {id:?}");
|
2020-04-24 12:53:18 +02:00
|
|
|
alloc_map.alloc_map.insert(id, alloc.clone());
|
|
|
|
alloc_map.dedup.insert(alloc, id);
|
2018-05-02 06:03:06 +02:00
|
|
|
id
|
|
|
|
}
|
|
|
|
|
2019-05-30 13:05:05 +02:00
|
|
|
/// Generates an `AllocId` for a static or return a cached one in case this function has been
|
|
|
|
/// called on the same static before.
|
2020-09-18 20:49:25 +02:00
|
|
|
pub fn create_static_alloc(self, static_id: DefId) -> AllocId {
|
2019-05-30 13:05:05 +02:00
|
|
|
self.reserve_and_set_dedup(GlobalAlloc::Static(static_id))
|
|
|
|
}
|
|
|
|
|
2022-11-16 20:34:16 +00:00
|
|
|
/// Generates an `AllocId` for a function. Depending on the function type,
|
2019-05-30 13:05:05 +02:00
|
|
|
/// this might get deduplicated or assigned a new ID each time.
|
2020-09-18 20:49:25 +02:00
|
|
|
pub fn create_fn_alloc(self, instance: Instance<'tcx>) -> AllocId {
|
2019-02-09 15:44:54 +01:00
|
|
|
// Functions cannot be identified by pointers, as asm-equal functions can get deduplicated
|
|
|
|
// by the linker (we set the "unnamed_addr" attribute for LLVM) and functions can be
|
|
|
|
// duplicated across crates.
|
|
|
|
// We thus generate a new `AllocId` for every mention of a function. This means that
|
|
|
|
// `main as fn() == main as fn()` is false, while `let x = main as fn(); x == x` is true.
|
|
|
|
// However, formatting code relies on function identity (see #58320), so we only do
|
2022-11-16 20:34:16 +00:00
|
|
|
// this for generic functions. Lifetime parameters are ignored.
|
2020-09-21 04:53:44 +02:00
|
|
|
let is_generic = instance
|
2023-07-11 22:35:29 +01:00
|
|
|
.args
|
2020-09-21 04:53:44 +02:00
|
|
|
.into_iter()
|
|
|
|
.any(|kind| !matches!(kind.unpack(), GenericArgKind::Lifetime(_)));
|
2019-02-09 15:44:54 +01:00
|
|
|
if is_generic {
|
2019-09-06 03:57:44 +01:00
|
|
|
// Get a fresh ID.
|
2020-04-24 12:53:18 +02:00
|
|
|
let mut alloc_map = self.alloc_map.lock();
|
|
|
|
let id = alloc_map.reserve();
|
|
|
|
alloc_map.alloc_map.insert(id, GlobalAlloc::Function(instance));
|
2019-02-09 15:44:54 +01:00
|
|
|
id
|
|
|
|
} else {
|
2019-09-06 03:57:44 +01:00
|
|
|
// Deduplicate.
|
2019-05-30 13:05:05 +02:00
|
|
|
self.reserve_and_set_dedup(GlobalAlloc::Function(instance))
|
2019-02-09 15:44:54 +01:00
|
|
|
}
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:34:16 +00:00
|
|
|
/// Generates an `AllocId` for a (symbolic, not-reified) vtable. Will get deduplicated.
|
2022-07-17 11:40:34 -04:00
|
|
|
pub fn create_vtable_alloc(
|
|
|
|
self,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
poly_trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
|
|
|
|
) -> AllocId {
|
2022-07-19 19:57:44 -04:00
|
|
|
self.reserve_and_set_dedup(GlobalAlloc::VTable(ty, poly_trait_ref))
|
2022-07-17 11:36:37 -04:00
|
|
|
}
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Interns the `Allocation` and return a new `AllocId`, even if there's already an identical
|
2019-05-30 13:05:05 +02:00
|
|
|
/// `Allocation` with a different `AllocId`.
|
|
|
|
/// Statics with identical content will still point to the same `Allocation`, i.e.,
|
|
|
|
/// their data will be deduplicated through `Allocation` interning -- but they
|
|
|
|
/// are different places in memory and as such need different IDs.
|
Introduce `ConstAllocation`.
Currently some `Allocation`s are interned, some are not, and it's very
hard to tell at a use point which is which.
This commit introduces `ConstAllocation` for the known-interned ones,
which makes the division much clearer. `ConstAllocation::inner()` is
used to get the underlying `Allocation`.
In some places it's natural to use an `Allocation`, in some it's natural
to use a `ConstAllocation`, and in some places there's no clear choice.
I've tried to make things look as nice as possible, while generally
favouring `ConstAllocation`, which is the type that embodies more
information. This does require quite a few calls to `inner()`.
The commit also tweaks how `PartialOrd` works for `Interned`. The
previous code was too clever by half, building on `T: Ord` to make the
code shorter. That caused problems with deriving `PartialOrd` and `Ord`
for `ConstAllocation`, so I changed it to build on `T: PartialOrd`,
which is slightly more verbose but much more standard and avoided the
problems.
2022-03-02 07:15:04 +11:00
|
|
|
pub fn create_memory_alloc(self, mem: ConstAllocation<'tcx>) -> AllocId {
|
2020-04-24 12:53:18 +02:00
|
|
|
let id = self.reserve_alloc_id();
|
2019-05-30 13:05:05 +02:00
|
|
|
self.set_alloc_id_memory(id, mem);
|
|
|
|
id
|
|
|
|
}
|
|
|
|
|
2019-03-27 10:57:03 +09:00
|
|
|
/// Returns `None` in case the `AllocId` is dangling. An `InterpretCx` can still have a
|
2018-12-04 09:58:36 +01:00
|
|
|
/// local `Allocation` for that `AllocId`, but having such an `AllocId` in a constant is
|
|
|
|
/// illegal and will likely ICE.
|
2018-12-03 16:18:31 +01:00
|
|
|
/// This function exists to allow const eval to detect the difference between evaluation-
|
|
|
|
/// local dangling pointers and allocations in constants/statics.
|
2019-05-27 09:40:13 +02:00
|
|
|
#[inline]
|
2022-07-17 11:40:34 -04:00
|
|
|
pub fn try_get_global_alloc(self, id: AllocId) -> Option<GlobalAlloc<'tcx>> {
|
2020-04-24 12:53:18 +02:00
|
|
|
self.alloc_map.lock().alloc_map.get(&id).cloned()
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 10:58:53 +02:00
|
|
|
#[inline]
|
|
|
|
#[track_caller]
|
2020-05-08 21:59:17 +02:00
|
|
|
/// Panics in case the `AllocId` is dangling. Since that is impossible for `AllocId`s in
|
|
|
|
/// constants (as all constants must pass interning and validation that check for dangling
|
|
|
|
/// ids), this function is frequently used throughout rustc, but should not be used within
|
|
|
|
/// the miri engine.
|
2020-09-18 20:49:25 +02:00
|
|
|
pub fn global_alloc(self, id: AllocId) -> GlobalAlloc<'tcx> {
|
2022-07-17 11:40:34 -04:00
|
|
|
match self.try_get_global_alloc(id) {
|
2020-05-08 10:58:53 +02:00
|
|
|
Some(alloc) => alloc,
|
2022-07-02 10:53:34 -04:00
|
|
|
None => bug!("could not find allocation for {id:?}"),
|
2019-10-02 20:29:16 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. Trying to
|
2018-12-03 16:18:31 +01:00
|
|
|
/// call this function twice, even with the same `Allocation` will ICE the compiler.
|
Introduce `ConstAllocation`.
Currently some `Allocation`s are interned, some are not, and it's very
hard to tell at a use point which is which.
This commit introduces `ConstAllocation` for the known-interned ones,
which makes the division much clearer. `ConstAllocation::inner()` is
used to get the underlying `Allocation`.
In some places it's natural to use an `Allocation`, in some it's natural
to use a `ConstAllocation`, and in some places there's no clear choice.
I've tried to make things look as nice as possible, while generally
favouring `ConstAllocation`, which is the type that embodies more
information. This does require quite a few calls to `inner()`.
The commit also tweaks how `PartialOrd` works for `Interned`. The
previous code was too clever by half, building on `T: Ord` to make the
code shorter. That caused problems with deriving `PartialOrd` and `Ord`
for `ConstAllocation`, so I changed it to build on `T: PartialOrd`,
which is slightly more verbose but much more standard and avoided the
problems.
2022-03-02 07:15:04 +11:00
|
|
|
pub fn set_alloc_id_memory(self, id: AllocId, mem: ConstAllocation<'tcx>) {
|
2020-04-24 12:53:18 +02:00
|
|
|
if let Some(old) = self.alloc_map.lock().alloc_map.insert(id, GlobalAlloc::Memory(mem)) {
|
2022-07-02 10:53:34 -04:00
|
|
|
bug!("tried to set allocation ID {id:?}, but it was already existing as {old:#?}");
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-25 17:19:31 +02:00
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. May be called
|
2018-12-03 16:18:31 +01:00
|
|
|
/// twice for the same `(AllocId, Allocation)` pair.
|
Introduce `ConstAllocation`.
Currently some `Allocation`s are interned, some are not, and it's very
hard to tell at a use point which is which.
This commit introduces `ConstAllocation` for the known-interned ones,
which makes the division much clearer. `ConstAllocation::inner()` is
used to get the underlying `Allocation`.
In some places it's natural to use an `Allocation`, in some it's natural
to use a `ConstAllocation`, and in some places there's no clear choice.
I've tried to make things look as nice as possible, while generally
favouring `ConstAllocation`, which is the type that embodies more
information. This does require quite a few calls to `inner()`.
The commit also tweaks how `PartialOrd` works for `Interned`. The
previous code was too clever by half, building on `T: Ord` to make the
code shorter. That caused problems with deriving `PartialOrd` and `Ord`
for `ConstAllocation`, so I changed it to build on `T: PartialOrd`,
which is slightly more verbose but much more standard and avoided the
problems.
2022-03-02 07:15:04 +11:00
|
|
|
fn set_alloc_id_same_memory(self, id: AllocId, mem: ConstAllocation<'tcx>) {
|
2020-04-24 12:53:18 +02:00
|
|
|
self.alloc_map.lock().alloc_map.insert_same(id, GlobalAlloc::Memory(mem));
|
2018-05-25 17:19:31 +02:00
|
|
|
}
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
|
|
|
|
2018-04-26 09:18:19 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Methods to access integers in the target endianness
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-05-27 09:40:13 +02:00
|
|
|
#[inline]
|
2018-04-26 09:18:19 +02:00
|
|
|
pub fn write_target_uint(
|
2020-03-31 18:16:47 +02:00
|
|
|
endianness: Endian,
|
2018-04-26 09:18:19 +02:00
|
|
|
mut target: &mut [u8],
|
|
|
|
data: u128,
|
|
|
|
) -> Result<(), io::Error> {
|
2020-08-21 00:55:33 -07:00
|
|
|
// This u128 holds an "any-size uint" (since smaller uints can fits in it)
|
|
|
|
// So we do not write all bytes of the u128, just the "payload".
|
2018-04-26 09:18:19 +02:00
|
|
|
match endianness {
|
2020-08-20 02:37:00 -07:00
|
|
|
Endian::Little => target.write(&data.to_le_bytes())?,
|
2020-08-22 03:54:15 -07:00
|
|
|
Endian::Big => target.write(&data.to_be_bytes()[16 - target.len()..])?,
|
2020-08-20 02:37:00 -07:00
|
|
|
};
|
2020-08-21 00:55:33 -07:00
|
|
|
debug_assert!(target.len() == 0); // We should have filled the target buffer.
|
2020-08-20 02:37:00 -07:00
|
|
|
Ok(())
|
2018-04-26 09:18:19 +02:00
|
|
|
}
|
|
|
|
|
2019-05-27 09:40:13 +02:00
|
|
|
#[inline]
|
2020-03-31 18:16:47 +02:00
|
|
|
pub fn read_target_uint(endianness: Endian, mut source: &[u8]) -> Result<u128, io::Error> {
|
2020-08-21 00:55:33 -07:00
|
|
|
// This u128 holds an "any-size uint" (since smaller uints can fits in it)
|
2020-08-20 15:55:02 -07:00
|
|
|
let mut buf = [0u8; std::mem::size_of::<u128>()];
|
2020-08-21 00:55:33 -07:00
|
|
|
// So we do not read exactly 16 bytes into the u128, just the "payload".
|
2020-08-22 03:54:15 -07:00
|
|
|
let uint = match endianness {
|
|
|
|
Endian::Little => {
|
|
|
|
source.read(&mut buf)?;
|
|
|
|
Ok(u128::from_le_bytes(buf))
|
|
|
|
}
|
|
|
|
Endian::Big => {
|
|
|
|
source.read(&mut buf[16 - source.len()..])?;
|
|
|
|
Ok(u128::from_be_bytes(buf))
|
|
|
|
}
|
|
|
|
};
|
2020-08-21 00:55:33 -07:00
|
|
|
debug_assert!(source.len() == 0); // We should have consumed the source buffer.
|
2020-08-22 03:54:15 -07:00
|
|
|
uint
|
2017-12-12 17:14:49 +01:00
|
|
|
}
|