Skip to content

Commit

Permalink
Don't deduplicate anonymous allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Apr 12, 2021
1 parent ea7057a commit 93831cb
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 41 deletions.
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ crate-type = ["dylib"]

[dependencies]
# These have to be in sync with each other
cranelift-codegen = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main", features = ["unwind", "x64"] }
cranelift-frontend = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main" }
cranelift-module = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main" }
cranelift-jit = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main", optional = true }
cranelift-object = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main" }
cranelift-codegen = { git = "https://github.com/bjorn3/wasmtime.git", branch = "anon_allocs", features = ["unwind", "x64"] }
cranelift-frontend = { git = "https://github.com/bjorn3/wasmtime.git", branch = "anon_allocs" }
cranelift-module = { git = "https://github.com/bjorn3/wasmtime.git", branch = "anon_allocs" }
cranelift-jit = { git = "https://github.com/bjorn3/wasmtime.git", branch = "anon_allocs", optional = true }
cranelift-object = { git = "https://github.com/bjorn3/wasmtime.git", branch = "anon_allocs" }
target-lexicon = "0.11.0"
gimli = { version = "0.23.0", default-features = false, features = ["write"]}
object = { version = "0.23.0", default-features = false, features = ["std", "read_core", "write", "archive", "coff", "elf", "macho", "pe"] }
Expand Down
5 changes: 5 additions & 0 deletions example/mini_core_hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ fn main() {
unsafe {
global_asm_test();
}

// Both statics have a reference that points to the same anonymous allocation.
static REF1: &u8 = &42;
static REF2: &u8 = REF1;
assert_eq!(*REF1, *REF2);
}

#[cfg(all(not(jit), target_os = "linux"))]
Expand Down
1 change: 1 addition & 0 deletions scripts/test_rustc_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ rm src/test/ui/issues/issue-51947.rs # same
rm src/test/ui/numbers-arithmetic/saturating-float-casts.rs # intrinsic gives different but valid result
rm src/test/ui/mir/mir_misc_casts.rs # depends on deduplication of constants
rm src/test/ui/mir/mir_raw_fat_ptr.rs # same
rm src/test/ui/consts/issue-33537.rs # same
rm src/test/ui/async-await/async-fn-size-moved-locals.rs # -Cpanic=abort shrinks some generator by one byte
rm src/test/ui/async-await/async-fn-size-uninit-locals.rs # same
rm src/test/ui/generator/size-moved-locals.rs # same
Expand Down
4 changes: 4 additions & 0 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use rustc_middle::ty::adjustment::PointerCast;
use rustc_middle::ty::layout::FnAbiExt;
use rustc_target::abi::call::FnAbi;

use crate::constant::ConstantCx;
use crate::prelude::*;

pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: Instance<'tcx>) {
Expand Down Expand Up @@ -47,6 +48,7 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
tcx,
pointer_type,
vtables: FxHashMap::default(),
constants_cx: ConstantCx::new(),

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

fx.constants_cx.finalize(fx.tcx, &mut *fx.cx.module);

// Store function in context
let context = &mut cx.cached_context;
context.func = func;
Expand Down
2 changes: 2 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use rustc_target::abi::call::FnAbi;
use rustc_target::abi::{Integer, Primitive};
use rustc_target::spec::{HasTargetSpec, Target};

use crate::constant::ConstantCx;
use crate::prelude::*;

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

pub(crate) instance: Instance<'tcx>,
pub(crate) symbol_name: SymbolName<'tcx>,
Expand Down
52 changes: 32 additions & 20 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use rustc_span::DUMMY_SP;

use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::ErrorReported;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::mir::interpret::{
Expand All @@ -13,12 +13,12 @@ use rustc_middle::ty::ConstKind;
use cranelift_codegen::ir::GlobalValueData;
use cranelift_module::*;

use crate::prelude::*;
use crate::{CodegenCx, prelude::*};

#[derive(Default)]
pub(crate) struct ConstantCx {
todo: Vec<TodoItem>,
done: FxHashSet<DataId>,
anon_allocs: FxHashMap<AllocId, DataId>,
}

#[derive(Copy, Clone, Debug)]
Expand All @@ -28,6 +28,14 @@ enum TodoItem {
}

impl ConstantCx {
pub(crate) fn new() -> Self {
ConstantCx {
todo: vec![],
done: FxHashSet::default(),
anon_allocs: FxHashMap::default(),
}
}

pub(crate) fn finalize(mut self, tcx: TyCtxt<'_>, module: &mut dyn Module) {
//println!("todo {:?}", self.todo);
define_all_allocs(tcx, module, &mut self);
Expand Down Expand Up @@ -74,8 +82,10 @@ pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool {
all_constants_ok
}

pub(crate) fn codegen_static(constants_cx: &mut ConstantCx, def_id: DefId) {
pub(crate) fn codegen_static(cx: &mut CodegenCx<'_, '_>, def_id: DefId) {
let mut constants_cx = ConstantCx::new();
constants_cx.todo.push(TodoItem::Static(def_id));
constants_cx.finalize(cx.tcx, &mut *cx.module);
}

pub(crate) fn codegen_tls_ref<'tcx>(
Expand Down Expand Up @@ -182,9 +192,13 @@ pub(crate) fn codegen_const_value<'tcx>(
let alloc_kind = fx.tcx.get_global_alloc(ptr.alloc_id);
let base_addr = match alloc_kind {
Some(GlobalAlloc::Memory(alloc)) => {
fx.cx.constants_cx.todo.push(TodoItem::Alloc(ptr.alloc_id));
let data_id =
data_id_for_alloc_id(fx.cx.module, ptr.alloc_id, alloc.mutability);
fx.constants_cx.todo.push(TodoItem::Alloc(ptr.alloc_id));
let data_id = data_id_for_alloc_id(
&mut fx.constants_cx,
fx.cx.module,
ptr.alloc_id,
alloc.mutability,
);
let local_data_id =
fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
if fx.clif_comments.enabled() {
Expand Down Expand Up @@ -243,8 +257,9 @@ fn pointer_for_allocation<'tcx>(
alloc: &'tcx Allocation,
) -> crate::pointer::Pointer {
let alloc_id = fx.tcx.create_memory_alloc(alloc);
fx.cx.constants_cx.todo.push(TodoItem::Alloc(alloc_id));
let data_id = data_id_for_alloc_id(fx.cx.module, alloc_id, alloc.mutability);
fx.constants_cx.todo.push(TodoItem::Alloc(alloc_id));
let data_id =
data_id_for_alloc_id(&mut fx.constants_cx, &mut *fx.cx.module, alloc_id, alloc.mutability);

let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
if fx.clif_comments.enabled() {
Expand All @@ -255,18 +270,16 @@ fn pointer_for_allocation<'tcx>(
}

fn data_id_for_alloc_id(
cx: &mut ConstantCx,
module: &mut dyn Module,
alloc_id: AllocId,
mutability: rustc_hir::Mutability,
) -> DataId {
module
.declare_data(
&format!(".L__alloc_{:x}", alloc_id.0),
Linkage::Local,
mutability == rustc_hir::Mutability::Mut,
false,
)
*cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
module
.declare_anonymous_data(mutability == rustc_hir::Mutability::Mut, false)
.unwrap()
})
}

fn data_id_for_static(
Expand Down Expand Up @@ -344,7 +357,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
GlobalAlloc::Memory(alloc) => alloc,
GlobalAlloc::Function(_) | GlobalAlloc::Static(_) => unreachable!(),
};
let data_id = data_id_for_alloc_id(module, alloc_id, alloc.mutability);
let data_id = data_id_for_alloc_id(cx, module, alloc_id, alloc.mutability);
(data_id, alloc, None)
}
TodoItem::Static(def_id) => {
Expand Down Expand Up @@ -397,7 +410,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
}
GlobalAlloc::Memory(target_alloc) => {
cx.todo.push(TodoItem::Alloc(reloc));
data_id_for_alloc_id(module, reloc, target_alloc.mutability)
data_id_for_alloc_id(cx, module, reloc, target_alloc.mutability)
}
GlobalAlloc::Static(def_id) => {
if tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL)
Expand All @@ -419,8 +432,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
data_ctx.write_data_addr(offset.bytes() as u32, global_value, addend as i64);
}

// FIXME don't duplicate definitions in lazy jit mode
let _ = module.define_data(data_id, &data_ctx);
module.define_data(data_id, &data_ctx).unwrap();
cx.done.insert(data_id);
}

Expand Down
2 changes: 1 addition & 1 deletion src/driver/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn module_codegen(
cx.tcx.sess.time("codegen fn", || crate::base::codegen_fn(&mut cx, inst));
}
MonoItem::Static(def_id) => {
crate::constant::codegen_static(&mut cx.constants_cx, def_id)
crate::constant::codegen_static(&mut cx, def_id)
}
MonoItem::GlobalAsm(item_id) => {
let item = cx.tcx.hir().item(item_id);
Expand Down
2 changes: 1 addition & 1 deletion src/driver/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
CodegenMode::JitLazy => codegen_shim(&mut cx, inst),
},
MonoItem::Static(def_id) => {
crate::constant::codegen_static(&mut cx.constants_cx, def_id);
crate::constant::codegen_static(&mut cx, def_id);
}
MonoItem::GlobalAsm(item_id) => {
let item = cx.tcx.hir().item(item_id);
Expand Down
4 changes: 0 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ use rustc_session::Session;
use cranelift_codegen::settings::{self, Configurable};

pub use crate::config::*;
use crate::constant::ConstantCx;
use crate::prelude::*;

mod abi;
Expand Down Expand Up @@ -123,7 +122,6 @@ struct CodegenCx<'m, 'tcx: 'm> {
tcx: TyCtxt<'tcx>,
module: &'m mut dyn Module,
global_asm: String,
constants_cx: ConstantCx,
cached_context: Context,
debug_context: Option<DebugContext<'tcx>>,
unwind_context: UnwindContext<'tcx>,
Expand All @@ -147,15 +145,13 @@ impl<'m, 'tcx> CodegenCx<'m, 'tcx> {
tcx,
module,
global_asm: String::new(),
constants_cx: ConstantCx::default(),
cached_context: Context::new(),
debug_context,
unwind_context,
}
}

fn finalize(self) -> (String, Option<DebugContext<'tcx>>, UnwindContext<'tcx>) {
self.constants_cx.finalize(self.tcx, self.module);
(self.global_asm, self.debug_context, self.unwind_context)
}
}
Expand Down

0 comments on commit 93831cb

Please sign in to comment.