diff options
| author | Ben Kimock <kimockb@gmail.com> | 2025-03-17 21:29:07 -0400 |
|---|---|---|
| committer | Ben Kimock <kimockb@gmail.com> | 2025-03-19 23:57:49 -0400 |
| commit | 8e7d8ddffe802180b504e0ecaaa40b10b28b291e (patch) | |
| tree | 0eac6843f8d6a6f9f1c1aac526f6ab961abe5180 /compiler/rustc_middle/src | |
| parent | 43a2e9d2c72db101f5fedac8b3acb78981b06bf2 (diff) | |
| download | rust-8e7d8ddffe802180b504e0ecaaa40b10b28b291e.tar.gz rust-8e7d8ddffe802180b504e0ecaaa40b10b28b291e.zip | |
Lower to a memset(undef) when Rvalue::Repeat repeats uninit
Diffstat (limited to 'compiler/rustc_middle/src')
| -rw-r--r-- | compiler/rustc_middle/src/mir/consts.rs | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/compiler/rustc_middle/src/mir/consts.rs b/compiler/rustc_middle/src/mir/consts.rs index 34b27c2e1cc..2b2ffa71628 100644 --- a/compiler/rustc_middle/src/mir/consts.rs +++ b/compiler/rustc_middle/src/mir/consts.rs @@ -9,7 +9,9 @@ use rustc_span::{DUMMY_SP, Span, Symbol}; use rustc_type_ir::TypeVisitableExt; use super::interpret::ReportedErrorInfo; -use crate::mir::interpret::{AllocId, ConstAllocation, ErrorHandled, Scalar, alloc_range}; +use crate::mir::interpret::{ + AllocId, AllocRange, ConstAllocation, ErrorHandled, GlobalAlloc, Scalar, alloc_range, +}; use crate::mir::{Promoted, pretty_print_const_value}; use crate::ty::print::{pretty_print_const, with_no_trimmed_paths}; use crate::ty::{self, ConstKind, GenericArgsRef, ScalarInt, Ty, TyCtxt}; @@ -192,9 +194,31 @@ impl<'tcx> ConstValue<'tcx> { .unwrap_memory() .inner() .provenance() - .range_empty(super::AllocRange::from(offset..offset + size), &tcx), + .range_empty(AllocRange::from(offset..offset + size), &tcx), } } + + /// Check if a constant only contains uninitialized bytes. + pub fn all_bytes_uninit(&self, tcx: TyCtxt<'tcx>) -> bool { + let ConstValue::Indirect { alloc_id, .. } = self else { + return false; + }; + let alloc = tcx.global_alloc(*alloc_id); + let GlobalAlloc::Memory(alloc) = alloc else { + return false; + }; + let init_mask = alloc.0.init_mask(); + let init_range = init_mask.is_range_initialized(AllocRange { + start: Size::ZERO, + size: Size::from_bytes(alloc.0.len()), + }); + if let Err(range) = init_range { + if range.size == alloc.0.size() { + return true; + } + } + false + } } /////////////////////////////////////////////////////////////////////////// |
