Skip to content

Commit 542e73e

Browse files
authored
Merge pull request #1158 from bjorn3/isolate_mono_items_pr
Isolate mono items
2 parents 29a4a55 + 65420b5 commit 542e73e

13 files changed

+80
-85
lines changed

Cargo.lock

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

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ 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"] }
12+
cranelift-codegen = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main", features = ["unwind"] }
1313
cranelift-frontend = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main" }
1414
cranelift-module = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main" }
1515
cranelift-jit = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main", optional = true }
1616
cranelift-object = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main" }
17-
target-lexicon = "0.11.0"
17+
target-lexicon = "0.12.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"] }
2020

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

+9-3
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>) {
@@ -18,9 +19,9 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
1819
let mir = tcx.instance_mir(instance.def);
1920

2021
// Declare function
21-
let name = tcx.symbol_name(instance).name.to_string();
22+
let symbol_name = tcx.symbol_name(instance);
2223
let sig = get_function_sig(tcx, cx.module.isa().triple(), instance);
23-
let func_id = cx.module.declare_function(&name, Linkage::Local, &sig).unwrap();
24+
let func_id = cx.module.declare_function(symbol_name.name, Linkage::Local, &sig).unwrap();
2425

2526
cx.cached_context.clear();
2627

@@ -46,8 +47,11 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
4647
cx,
4748
tcx,
4849
pointer_type,
50+
vtables: FxHashMap::default(),
51+
constants_cx: ConstantCx::new(),
4952

5053
instance,
54+
symbol_name,
5155
mir,
5256
fn_abi: Some(FnAbi::of_instance(&RevealAllLayoutCx(tcx), instance, &[])),
5357

@@ -90,6 +94,8 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
9094
let source_info_set = fx.source_info_set;
9195
let local_map = fx.local_map;
9296

97+
fx.constants_cx.finalize(fx.tcx, &mut *fx.cx.module);
98+
9399
// Store function in context
94100
let context = &mut cx.cached_context;
95101
context.func = func;
@@ -151,7 +157,7 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
151157
debug_context.define_function(
152158
instance,
153159
func_id,
154-
&name,
160+
symbol_name.name,
155161
isa,
156162
context,
157163
&source_info_set,

src/common.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use rustc_index::vec::IndexVec;
2+
use rustc_middle::ty::SymbolName;
23
use rustc_target::abi::call::FnAbi;
34
use rustc_target::abi::{Integer, Primitive};
45
use rustc_target::spec::{HasTargetSpec, Target};
56

7+
use crate::constant::ConstantCx;
68
use crate::prelude::*;
79

810
pub(crate) fn pointer_ty(tcx: TyCtxt<'_>) -> types::Type {
@@ -230,8 +232,11 @@ pub(crate) struct FunctionCx<'m, 'clif, 'tcx> {
230232
pub(crate) cx: &'clif mut crate::CodegenCx<'m, 'tcx>,
231233
pub(crate) tcx: TyCtxt<'tcx>,
232234
pub(crate) pointer_type: Type, // Cached from module
235+
pub(crate) vtables: FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), DataId>,
236+
pub(crate) constants_cx: ConstantCx,
233237

234238
pub(crate) instance: Instance<'tcx>,
239+
pub(crate) symbol_name: SymbolName<'tcx>,
235240
pub(crate) mir: &'tcx Body<'tcx>,
236241
pub(crate) fn_abi: Option<FnAbi<'tcx, Ty<'tcx>>>,
237242

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/inline_asm.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ pub(crate) fn codegen_inline_asm<'tcx>(
9292

9393
let inline_asm_index = fx.inline_asm_index;
9494
fx.inline_asm_index += 1;
95-
let asm_name =
96-
format!("{}__inline_asm_{}", fx.tcx.symbol_name(fx.instance).name, inline_asm_index);
95+
let asm_name = format!("{}__inline_asm_{}", fx.symbol_name, inline_asm_index);
9796

9897
let generated_asm = generate_asm_wrapper(
9998
&asm_name,

0 commit comments

Comments
 (0)