Skip to content

Commit 676ab86

Browse files
committed
Use ConstantCx for defining anonymous strings
As opposed to directly defining them in the module.
1 parent dafee5c commit 676ab86

File tree

4 files changed

+12
-24
lines changed

4 files changed

+12
-24
lines changed

src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ pub(crate) fn codegen_panic_nounwind<'tcx>(
10841084
msg_str: &str,
10851085
span: Span,
10861086
) {
1087-
let msg_ptr = fx.anonymous_str(msg_str);
1087+
let msg_ptr = crate::constant::pointer_for_anonymous_str(fx, msg_str);
10881088
let msg_len = fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(msg_str.len()).unwrap());
10891089
let args = [msg_ptr, msg_len];
10901090

src/common.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -420,21 +420,6 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
420420
crate::constant::codegen_const_value(self, const_loc, self.tcx.caller_location_ty())
421421
})
422422
}
423-
424-
pub(crate) fn anonymous_str(&mut self, msg: &str) -> Value {
425-
let mut data = DataDescription::new();
426-
data.define(msg.as_bytes().to_vec().into_boxed_slice());
427-
let msg_id = self.module.declare_anonymous_data(false, false).unwrap();
428-
429-
// Ignore DuplicateDefinition error, as the data will be the same
430-
let _ = self.module.define_data(msg_id, &data);
431-
432-
let local_msg_id = self.module.declare_data_in_func(msg_id, self.bcx.func);
433-
if self.clif_comments.enabled() {
434-
self.add_comment(local_msg_id, msg);
435-
}
436-
self.bcx.ins().global_value(self.pointer_type, local_msg_id)
437-
}
438423
}
439424

440425
pub(crate) struct FullyMonomorphizedLayoutCx<'tcx>(pub(crate) TyCtxt<'tcx>);

src/constant.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::cmp::Ordering;
44

55
use cranelift_module::*;
6+
use rustc_const_eval::interpret::CTFE_ALLOC_SALT;
67
use rustc_data_structures::fx::FxHashSet;
78
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
89
use rustc_middle::mir::interpret::{AllocId, GlobalAlloc, Scalar, read_target_uint};
@@ -199,23 +200,20 @@ pub(crate) fn codegen_const_value<'tcx>(
199200
}
200201
},
201202
ConstValue::Indirect { alloc_id, offset } => CValue::by_ref(
202-
pointer_for_allocation(fx, alloc_id)
203+
Pointer::new(pointer_for_allocation(fx, alloc_id))
203204
.offset_i64(fx, i64::try_from(offset.bytes()).unwrap()),
204205
layout,
205206
),
206207
ConstValue::Slice { data, meta } => {
207208
let alloc_id = fx.tcx.reserve_and_set_memory_alloc(data);
208-
let ptr = pointer_for_allocation(fx, alloc_id).get_addr(fx);
209+
let ptr = pointer_for_allocation(fx, alloc_id);
209210
let len = fx.bcx.ins().iconst(fx.pointer_type, meta as i64);
210211
CValue::by_val_pair(ptr, len, layout)
211212
}
212213
}
213214
}
214215

215-
fn pointer_for_allocation<'tcx>(
216-
fx: &mut FunctionCx<'_, '_, 'tcx>,
217-
alloc_id: AllocId,
218-
) -> crate::pointer::Pointer {
216+
fn pointer_for_allocation<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, alloc_id: AllocId) -> Value {
219217
let alloc = fx.tcx.global_alloc(alloc_id).unwrap_memory();
220218
let data_id =
221219
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>(
224222
if fx.clif_comments.enabled() {
225223
fx.add_comment(local_data_id, format!("{:?}", alloc_id));
226224
}
227-
let global_ptr = fx.bcx.ins().global_value(fx.pointer_type, local_data_id);
228-
crate::pointer::Pointer::new(global_ptr)
225+
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
229226
}
230227

231228
fn data_id_for_alloc_id(
@@ -251,6 +248,11 @@ pub(crate) fn data_id_for_vtable<'tcx>(
251248
data_id_for_alloc_id(cx, module, alloc_id, Mutability::Not)
252249
}
253250

251+
pub(crate) fn pointer_for_anonymous_str(fx: &mut FunctionCx<'_, '_, '_>, msg: &str) -> Value {
252+
let alloc_id = fx.tcx.allocate_bytes_dedup(msg.as_bytes(), CTFE_ALLOC_SALT);
253+
pointer_for_allocation(fx, alloc_id)
254+
}
255+
254256
fn data_id_for_static(
255257
tcx: TyCtxt<'_>,
256258
module: &mut dyn Module,

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ extern crate rustc_middle;
2020
extern crate rustc_abi;
2121
extern crate rustc_ast;
2222
extern crate rustc_codegen_ssa;
23+
extern crate rustc_const_eval;
2324
extern crate rustc_data_structures;
2425
extern crate rustc_errors;
2526
extern crate rustc_fs_util;

0 commit comments

Comments
 (0)