From b2cafc96ec61a2bfa43caf1ef68be0cc348e9140 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Tue, 24 Jun 2025 15:47:17 +0200 Subject: [PATCH] Use ConstantCx for defining anonymous strings As opposed to directly defining them in the module. --- src/base.rs | 2 +- src/common.rs | 15 --------------- src/constant.rs | 18 ++++++++++-------- src/lib.rs | 1 + 4 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/base.rs b/src/base.rs index 7f799b5730b4..d79cf5aa5842 100644 --- a/src/base.rs +++ b/src/base.rs @@ -1084,7 +1084,7 @@ pub(crate) fn codegen_panic_nounwind<'tcx>( msg_str: &str, span: Span, ) { - let msg_ptr = fx.anonymous_str(msg_str); + let msg_ptr = crate::constant::pointer_for_anonymous_str(fx, msg_str); let msg_len = fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(msg_str.len()).unwrap()); let args = [msg_ptr, msg_len]; diff --git a/src/common.rs b/src/common.rs index b47b2a4ee4a5..80b1f0881347 100644 --- a/src/common.rs +++ b/src/common.rs @@ -420,21 +420,6 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> { crate::constant::codegen_const_value(self, const_loc, self.tcx.caller_location_ty()) }) } - - pub(crate) fn anonymous_str(&mut self, msg: &str) -> Value { - let mut data = DataDescription::new(); - data.define(msg.as_bytes().to_vec().into_boxed_slice()); - let msg_id = self.module.declare_anonymous_data(false, false).unwrap(); - - // Ignore DuplicateDefinition error, as the data will be the same - let _ = self.module.define_data(msg_id, &data); - - let local_msg_id = self.module.declare_data_in_func(msg_id, self.bcx.func); - if self.clif_comments.enabled() { - self.add_comment(local_msg_id, msg); - } - self.bcx.ins().global_value(self.pointer_type, local_msg_id) - } } pub(crate) struct FullyMonomorphizedLayoutCx<'tcx>(pub(crate) TyCtxt<'tcx>); diff --git a/src/constant.rs b/src/constant.rs index 3a62cd52a9d9..ff2c0d577bb4 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -3,6 +3,7 @@ use std::cmp::Ordering; use cranelift_module::*; +use rustc_const_eval::interpret::CTFE_ALLOC_SALT; use rustc_data_structures::fx::FxHashSet; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; use rustc_middle::mir::interpret::{AllocId, GlobalAlloc, Scalar, read_target_uint}; @@ -199,23 +200,20 @@ pub(crate) fn codegen_const_value<'tcx>( } }, ConstValue::Indirect { alloc_id, offset } => CValue::by_ref( - pointer_for_allocation(fx, alloc_id) + Pointer::new(pointer_for_allocation(fx, alloc_id)) .offset_i64(fx, i64::try_from(offset.bytes()).unwrap()), layout, ), ConstValue::Slice { data, meta } => { let alloc_id = fx.tcx.reserve_and_set_memory_alloc(data); - let ptr = pointer_for_allocation(fx, alloc_id).get_addr(fx); + let ptr = pointer_for_allocation(fx, alloc_id); let len = fx.bcx.ins().iconst(fx.pointer_type, meta as i64); CValue::by_val_pair(ptr, len, layout) } } } -fn pointer_for_allocation<'tcx>( - fx: &mut FunctionCx<'_, '_, 'tcx>, - alloc_id: AllocId, -) -> crate::pointer::Pointer { +fn pointer_for_allocation<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, alloc_id: AllocId) -> Value { let alloc = fx.tcx.global_alloc(alloc_id).unwrap_memory(); let data_id = data_id_for_alloc_id(&mut fx.constants_cx, fx.module, alloc_id, alloc.inner().mutability); @@ -224,8 +222,7 @@ fn pointer_for_allocation<'tcx>( if fx.clif_comments.enabled() { fx.add_comment(local_data_id, format!("{:?}", alloc_id)); } - let global_ptr = fx.bcx.ins().global_value(fx.pointer_type, local_data_id); - crate::pointer::Pointer::new(global_ptr) + fx.bcx.ins().global_value(fx.pointer_type, local_data_id) } fn data_id_for_alloc_id( @@ -251,6 +248,11 @@ pub(crate) fn data_id_for_vtable<'tcx>( data_id_for_alloc_id(cx, module, alloc_id, Mutability::Not) } +pub(crate) fn pointer_for_anonymous_str(fx: &mut FunctionCx<'_, '_, '_>, msg: &str) -> Value { + let alloc_id = fx.tcx.allocate_bytes_dedup(msg.as_bytes(), CTFE_ALLOC_SALT); + pointer_for_allocation(fx, alloc_id) +} + fn data_id_for_static( tcx: TyCtxt<'_>, module: &mut dyn Module, diff --git a/src/lib.rs b/src/lib.rs index a5ec796c1c2d..678580a23723 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,7 @@ extern crate rustc_middle; extern crate rustc_abi; extern crate rustc_ast; extern crate rustc_codegen_ssa; +extern crate rustc_const_eval; extern crate rustc_data_structures; extern crate rustc_errors; extern crate rustc_fs_util;