diff options
| author | bors <bors@rust-lang.org> | 2025-07-07 14:20:33 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2025-07-07 14:20:33 +0000 |
| commit | 1b0bc594a75dfc1cdedc6c17052cf44de101e632 (patch) | |
| tree | 567a101d089fd1ad07fde9091f7a66612743334b /compiler/stable_mir/src/alloc.rs | |
| parent | 25cf7d13c960a3ac47d1424ca354077efb6946ff (diff) | |
| parent | 364dbd61f2b0928bb66d8c82626046cff11eae36 (diff) | |
| download | rust-1b0bc594a75dfc1cdedc6c17052cf44de101e632.tar.gz rust-1b0bc594a75dfc1cdedc6c17052cf44de101e632.zip | |
Auto merge of #143582 - jieyouxu:rollup-8t9mhfj, r=jieyouxu
Rollup of 11 pull requests Successful merges: - rust-lang/rust#143130 (doc(std): clarify `NonZero<T>` usage limitation in doc comment) - rust-lang/rust#143415 (Get rid of build-powerpc64le-toolchain.sh) - rust-lang/rust#143464 (Make tests/ui/abi/debug.rs cross-compile) - rust-lang/rust#143482 (Fix short linker error output) - rust-lang/rust#143524 (Move `stable_mir` back to its own crate) - rust-lang/rust#143528 (interpret: rename StackPopCleanup) - rust-lang/rust#143551 (Dont resolve instance of root in `mir_callgraph_cyclic`) - rust-lang/rust#143558 (mbe: Refactors and function extractions in `compile_declarative_macro`) - rust-lang/rust#143563 (std: fix typo in `std::path`) - rust-lang/rust#143564 (compiler: Deduplicate `must_emit_unwind_tables()` comments) - rust-lang/rust#143577 (Disable download-rustc for library profile) r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/stable_mir/src/alloc.rs')
| -rw-r--r-- | compiler/stable_mir/src/alloc.rs | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/compiler/stable_mir/src/alloc.rs b/compiler/stable_mir/src/alloc.rs new file mode 100644 index 00000000000..349b83231e3 --- /dev/null +++ b/compiler/stable_mir/src/alloc.rs @@ -0,0 +1,76 @@ +//! Memory allocation implementation for StableMIR. +//! +//! This module is responsible for constructing stable components. +//! All operations requiring rustc queries must be delegated +//! to `rustc_smir::alloc` to maintain stability guarantees. + +use rustc_abi::Align; +use rustc_middle::mir::ConstValue; +use rustc_middle::mir::interpret::AllocRange; +use rustc_smir::bridge::SmirError; +use rustc_smir::context::SmirCtxt; +use rustc_smir::{Tables, alloc}; + +use super::Error; +use super::compiler_interface::BridgeTys; +use super::mir::Mutability; +use super::ty::{Allocation, ProvenanceMap}; +use super::unstable::Stable; + +/// Creates new empty `Allocation` from given `Align`. +fn new_empty_allocation(align: Align) -> Allocation { + Allocation { + bytes: Vec::new(), + provenance: ProvenanceMap { ptrs: Vec::new() }, + align: align.bytes(), + mutability: Mutability::Not, + } +} + +// We need this method instead of a Stable implementation +// because we need to get `Ty` of the const we are trying to create, to do that +// we need to have access to `ConstantKind` but we can't access that inside Stable impl. +#[allow(rustc::usage_of_qualified_ty)] +pub(crate) fn new_allocation<'tcx>( + ty: rustc_middle::ty::Ty<'tcx>, + const_value: ConstValue<'tcx>, + tables: &mut Tables<'tcx, BridgeTys>, + cx: &SmirCtxt<'tcx, BridgeTys>, +) -> Allocation { + try_new_allocation(ty, const_value, tables, cx) + .unwrap_or_else(|_| panic!("Failed to convert: {const_value:?} to {ty:?}")) +} + +#[allow(rustc::usage_of_qualified_ty)] +pub(crate) fn try_new_allocation<'tcx>( + ty: rustc_middle::ty::Ty<'tcx>, + const_value: ConstValue<'tcx>, + tables: &mut Tables<'tcx, BridgeTys>, + cx: &SmirCtxt<'tcx, BridgeTys>, +) -> Result<Allocation, Error> { + let layout = alloc::create_ty_and_layout(cx, ty).map_err(|e| Error::from_internal(e))?; + match const_value { + ConstValue::Scalar(scalar) => { + alloc::try_new_scalar(layout, scalar, cx).map(|alloc| alloc.stable(tables, cx)) + } + ConstValue::ZeroSized => Ok(new_empty_allocation(layout.align.abi)), + ConstValue::Slice { data, meta } => { + alloc::try_new_slice(layout, data, meta, cx).map(|alloc| alloc.stable(tables, cx)) + } + ConstValue::Indirect { alloc_id, offset } => { + let alloc = alloc::try_new_indirect(alloc_id, cx); + use rustc_smir::context::SmirAllocRange; + Ok(allocation_filter(&alloc.0, cx.alloc_range(offset, layout.size), tables, cx)) + } + } +} + +/// Creates an `Allocation` only from information within the `AllocRange`. +pub(super) fn allocation_filter<'tcx>( + alloc: &rustc_middle::mir::interpret::Allocation, + alloc_range: AllocRange, + tables: &mut Tables<'tcx, BridgeTys>, + cx: &SmirCtxt<'tcx, BridgeTys>, +) -> Allocation { + alloc::allocation_filter(alloc, alloc_range, tables, cx) +} |
