Skip to content

Commit 2f20e44

Browse files
committed
Don't deduplicate anonymous allocations
1 parent 38120d0 commit 2f20e44

File tree

10 files changed

+56
-44
lines changed

10 files changed

+56
-44
lines changed

Cargo.lock

+10-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ crate-type = ["dylib"]
99

1010
[dependencies]
1111
# These have to be in sync with each other
12-
cranelift-codegen = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main", features = ["unwind", "x64"] }
13-
cranelift-frontend = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main" }
14-
cranelift-module = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main" }
15-
cranelift-jit = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main", optional = true }
16-
cranelift-object = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main" }
12+
cranelift-codegen = { git = "https://github.com/bjorn3/wasmtime.git", branch = "anon_allocs", features = ["unwind", "x64"] }
13+
cranelift-frontend = { git = "https://github.com/bjorn3/wasmtime.git", branch = "anon_allocs" }
14+
cranelift-module = { git = "https://github.com/bjorn3/wasmtime.git", branch = "anon_allocs" }
15+
cranelift-jit = { git = "https://github.com/bjorn3/wasmtime.git", branch = "anon_allocs", optional = true }
16+
cranelift-object = { git = "https://github.com/bjorn3/wasmtime.git", branch = "anon_allocs" }
1717
target-lexicon = "0.11.0"
1818
gimli = { version = "0.23.0", default-features = false, features = ["write"]}
1919
object = { version = "0.23.0", default-features = false, features = ["std", "read_core", "write", "archive", "coff", "elf", "macho", "pe"] }

example/mini_core_hello_world.rs

+5
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,11 @@ fn main() {
296296
unsafe {
297297
global_asm_test();
298298
}
299+
300+
// Both statics have a reference that points to the same anonymous allocation.
301+
static REF1: &u8 = &42;
302+
static REF2: &u8 = REF1;
303+
assert_eq!(*REF1, *REF2);
299304
}
300305

301306
#[cfg(all(not(jit), target_os = "linux"))]

scripts/test_rustc_tests.sh

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ rm src/test/ui/issues/issue-51947.rs # same
4747
rm src/test/ui/numbers-arithmetic/saturating-float-casts.rs # intrinsic gives different but valid result
4848
rm src/test/ui/mir/mir_misc_casts.rs # depends on deduplication of constants
4949
rm src/test/ui/mir/mir_raw_fat_ptr.rs # same
50+
rm src/test/ui/consts/issue-33537.rs # same
5051
rm src/test/ui/async-await/async-fn-size-moved-locals.rs # -Cpanic=abort shrinks some generator by one byte
5152
rm src/test/ui/async-await/async-fn-size-uninit-locals.rs # same
5253
rm src/test/ui/generator/size-moved-locals.rs # same

src/base.rs

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_middle::ty::adjustment::PointerCast;
66
use rustc_middle::ty::layout::FnAbiExt;
77
use rustc_target::abi::call::FnAbi;
88

9+
use crate::constant::ConstantCx;
910
use crate::prelude::*;
1011

1112
pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: Instance<'tcx>) {
@@ -47,6 +48,7 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
4748
tcx,
4849
pointer_type,
4950
vtables: FxHashMap::default(),
51+
constants_cx: ConstantCx::new(),
5052

5153
instance,
5254
symbol_name,
@@ -92,6 +94,8 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
9294
let source_info_set = fx.source_info_set;
9395
let local_map = fx.local_map;
9496

97+
fx.constants_cx.finalize(fx.tcx, &mut *fx.cx.module);
98+
9599
// Store function in context
96100
let context = &mut cx.cached_context;
97101
context.func = func;

src/common.rs

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_target::abi::call::FnAbi;
44
use rustc_target::abi::{Integer, Primitive};
55
use rustc_target::spec::{HasTargetSpec, Target};
66

7+
use crate::constant::ConstantCx;
78
use crate::prelude::*;
89

910
pub(crate) fn pointer_ty(tcx: TyCtxt<'_>) -> types::Type {
@@ -232,6 +233,7 @@ pub(crate) struct FunctionCx<'m, 'clif, 'tcx> {
232233
pub(crate) tcx: TyCtxt<'tcx>,
233234
pub(crate) pointer_type: Type, // Cached from module
234235
pub(crate) vtables: FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), DataId>,
236+
pub(crate) constants_cx: ConstantCx,
235237

236238
pub(crate) instance: Instance<'tcx>,
237239
pub(crate) symbol_name: SymbolName<'tcx>,

src/constant.rs

+27-21
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use rustc_span::DUMMY_SP;
44

5-
use rustc_data_structures::fx::FxHashSet;
5+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
66
use rustc_errors::ErrorReported;
77
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
88
use rustc_middle::mir::interpret::{
@@ -13,12 +13,12 @@ use rustc_middle::ty::ConstKind;
1313
use cranelift_codegen::ir::GlobalValueData;
1414
use cranelift_module::*;
1515

16-
use crate::prelude::*;
16+
use crate::{prelude::*, CodegenCx};
1717

18-
#[derive(Default)]
1918
pub(crate) struct ConstantCx {
2019
todo: Vec<TodoItem>,
2120
done: FxHashSet<DataId>,
21+
anon_allocs: FxHashMap<AllocId, DataId>,
2222
}
2323

2424
#[derive(Copy, Clone, Debug)]
@@ -28,6 +28,10 @@ enum TodoItem {
2828
}
2929

3030
impl ConstantCx {
31+
pub(crate) fn new() -> Self {
32+
ConstantCx { todo: vec![], done: FxHashSet::default(), anon_allocs: FxHashMap::default() }
33+
}
34+
3135
pub(crate) fn finalize(mut self, tcx: TyCtxt<'_>, module: &mut dyn Module) {
3236
//println!("todo {:?}", self.todo);
3337
define_all_allocs(tcx, module, &mut self);
@@ -74,8 +78,10 @@ pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool {
7478
all_constants_ok
7579
}
7680

77-
pub(crate) fn codegen_static(constants_cx: &mut ConstantCx, def_id: DefId) {
81+
pub(crate) fn codegen_static(cx: &mut CodegenCx<'_, '_>, def_id: DefId) {
82+
let mut constants_cx = ConstantCx::new();
7883
constants_cx.todo.push(TodoItem::Static(def_id));
84+
constants_cx.finalize(cx.tcx, &mut *cx.module);
7985
}
8086

8187
pub(crate) fn codegen_tls_ref<'tcx>(
@@ -182,9 +188,13 @@ pub(crate) fn codegen_const_value<'tcx>(
182188
let alloc_kind = fx.tcx.get_global_alloc(ptr.alloc_id);
183189
let base_addr = match alloc_kind {
184190
Some(GlobalAlloc::Memory(alloc)) => {
185-
fx.cx.constants_cx.todo.push(TodoItem::Alloc(ptr.alloc_id));
186-
let data_id =
187-
data_id_for_alloc_id(fx.cx.module, ptr.alloc_id, alloc.mutability);
191+
fx.constants_cx.todo.push(TodoItem::Alloc(ptr.alloc_id));
192+
let data_id = data_id_for_alloc_id(
193+
&mut fx.constants_cx,
194+
fx.cx.module,
195+
ptr.alloc_id,
196+
alloc.mutability,
197+
);
188198
let local_data_id =
189199
fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
190200
if fx.clif_comments.enabled() {
@@ -243,8 +253,9 @@ fn pointer_for_allocation<'tcx>(
243253
alloc: &'tcx Allocation,
244254
) -> crate::pointer::Pointer {
245255
let alloc_id = fx.tcx.create_memory_alloc(alloc);
246-
fx.cx.constants_cx.todo.push(TodoItem::Alloc(alloc_id));
247-
let data_id = data_id_for_alloc_id(fx.cx.module, alloc_id, alloc.mutability);
256+
fx.constants_cx.todo.push(TodoItem::Alloc(alloc_id));
257+
let data_id =
258+
data_id_for_alloc_id(&mut fx.constants_cx, &mut *fx.cx.module, alloc_id, alloc.mutability);
248259

249260
let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
250261
if fx.clif_comments.enabled() {
@@ -255,18 +266,14 @@ fn pointer_for_allocation<'tcx>(
255266
}
256267

257268
fn data_id_for_alloc_id(
269+
cx: &mut ConstantCx,
258270
module: &mut dyn Module,
259271
alloc_id: AllocId,
260272
mutability: rustc_hir::Mutability,
261273
) -> DataId {
262-
module
263-
.declare_data(
264-
&format!(".L__alloc_{:x}", alloc_id.0),
265-
Linkage::Local,
266-
mutability == rustc_hir::Mutability::Mut,
267-
false,
268-
)
269-
.unwrap()
274+
*cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
275+
module.declare_anonymous_data(mutability == rustc_hir::Mutability::Mut, false).unwrap()
276+
})
270277
}
271278

272279
fn data_id_for_static(
@@ -344,7 +351,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
344351
GlobalAlloc::Memory(alloc) => alloc,
345352
GlobalAlloc::Function(_) | GlobalAlloc::Static(_) => unreachable!(),
346353
};
347-
let data_id = data_id_for_alloc_id(module, alloc_id, alloc.mutability);
354+
let data_id = data_id_for_alloc_id(cx, module, alloc_id, alloc.mutability);
348355
(data_id, alloc, None)
349356
}
350357
TodoItem::Static(def_id) => {
@@ -397,7 +404,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
397404
}
398405
GlobalAlloc::Memory(target_alloc) => {
399406
cx.todo.push(TodoItem::Alloc(reloc));
400-
data_id_for_alloc_id(module, reloc, target_alloc.mutability)
407+
data_id_for_alloc_id(cx, module, reloc, target_alloc.mutability)
401408
}
402409
GlobalAlloc::Static(def_id) => {
403410
if tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL)
@@ -419,8 +426,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
419426
data_ctx.write_data_addr(offset.bytes() as u32, global_value, addend as i64);
420427
}
421428

422-
// FIXME don't duplicate definitions in lazy jit mode
423-
let _ = module.define_data(data_id, &data_ctx);
429+
module.define_data(data_id, &data_ctx).unwrap();
424430
cx.done.insert(data_id);
425431
}
426432

src/driver/aot.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ fn module_codegen(
121121
MonoItem::Fn(inst) => {
122122
cx.tcx.sess.time("codegen fn", || crate::base::codegen_fn(&mut cx, inst));
123123
}
124-
MonoItem::Static(def_id) => {
125-
crate::constant::codegen_static(&mut cx.constants_cx, def_id)
126-
}
124+
MonoItem::Static(def_id) => crate::constant::codegen_static(&mut cx, def_id),
127125
MonoItem::GlobalAsm(item_id) => {
128126
let item = cx.tcx.hir().item(item_id);
129127
if let rustc_hir::ItemKind::GlobalAsm(rustc_hir::GlobalAsm { asm }) = item.kind {

src/driver/jit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
5858
CodegenMode::JitLazy => codegen_shim(&mut cx, inst),
5959
},
6060
MonoItem::Static(def_id) => {
61-
crate::constant::codegen_static(&mut cx.constants_cx, def_id);
61+
crate::constant::codegen_static(&mut cx, def_id);
6262
}
6363
MonoItem::GlobalAsm(item_id) => {
6464
let item = cx.tcx.hir().item(item_id);

src/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ use rustc_session::Session;
3636
use cranelift_codegen::settings::{self, Configurable};
3737

3838
pub use crate::config::*;
39-
use crate::constant::ConstantCx;
4039
use crate::prelude::*;
4140

4241
mod abi;
@@ -123,7 +122,6 @@ struct CodegenCx<'m, 'tcx: 'm> {
123122
tcx: TyCtxt<'tcx>,
124123
module: &'m mut dyn Module,
125124
global_asm: String,
126-
constants_cx: ConstantCx,
127125
cached_context: Context,
128126
debug_context: Option<DebugContext<'tcx>>,
129127
unwind_context: UnwindContext<'tcx>,
@@ -147,15 +145,13 @@ impl<'m, 'tcx> CodegenCx<'m, 'tcx> {
147145
tcx,
148146
module,
149147
global_asm: String::new(),
150-
constants_cx: ConstantCx::default(),
151148
cached_context: Context::new(),
152149
debug_context,
153150
unwind_context,
154151
}
155152
}
156153

157154
fn finalize(self) -> (String, Option<DebugContext<'tcx>>, UnwindContext<'tcx>) {
158-
self.constants_cx.finalize(self.tcx, self.module);
159155
(self.global_asm, self.debug_context, self.unwind_context)
160156
}
161157
}

0 commit comments

Comments
 (0)