Use ConstantCx for defining anonymous strings

As opposed to directly defining them in the module.
This commit is contained in:
bjorn3 2025-06-24 15:47:17 +02:00
parent 4042f40266
commit b2cafc96ec
4 changed files with 12 additions and 24 deletions

View file

@ -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];

View file

@ -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>);

View file

@ -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,

View file

@ -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;