From f3b0f425c5a26f611c1743170cb9caf3ac8b6063 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Wed, 7 Apr 2021 14:54:59 +0200 Subject: [PATCH] Don't deduplicate vtables between functions --- src/base.rs | 8 +++++--- src/common.rs | 3 +++ src/inline_asm.rs | 3 +-- src/lib.rs | 2 -- src/trap.rs | 3 +-- src/vtable.rs | 26 ++++---------------------- 6 files changed, 14 insertions(+), 31 deletions(-) diff --git a/src/base.rs b/src/base.rs index 8b7a7caade5..1bd303b2db7 100644 --- a/src/base.rs +++ b/src/base.rs @@ -18,9 +18,9 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In let mir = tcx.instance_mir(instance.def); // Declare function - let name = tcx.symbol_name(instance).name.to_string(); + let symbol_name = tcx.symbol_name(instance); let sig = get_function_sig(tcx, cx.module.isa().triple(), instance); - let func_id = cx.module.declare_function(&name, Linkage::Local, &sig).unwrap(); + let func_id = cx.module.declare_function(symbol_name.name, Linkage::Local, &sig).unwrap(); cx.cached_context.clear(); @@ -46,8 +46,10 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In cx, tcx, pointer_type, + vtables: FxHashMap::default(), instance, + symbol_name, mir, fn_abi: Some(FnAbi::of_instance(&RevealAllLayoutCx(tcx), instance, &[])), @@ -151,7 +153,7 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In debug_context.define_function( instance, func_id, - &name, + symbol_name.name, isa, context, &source_info_set, diff --git a/src/common.rs b/src/common.rs index fe6cf9a3b01..64618dcf330 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,4 +1,5 @@ use rustc_index::vec::IndexVec; +use rustc_middle::ty::SymbolName; use rustc_target::abi::call::FnAbi; use rustc_target::abi::{Integer, Primitive}; use rustc_target::spec::{HasTargetSpec, Target}; @@ -230,8 +231,10 @@ pub(crate) struct FunctionCx<'m, 'clif, 'tcx> { pub(crate) cx: &'clif mut crate::CodegenCx<'m, 'tcx>, pub(crate) tcx: TyCtxt<'tcx>, pub(crate) pointer_type: Type, // Cached from module + pub(crate) vtables: FxHashMap<(Ty<'tcx>, Option>), DataId>, pub(crate) instance: Instance<'tcx>, + pub(crate) symbol_name: SymbolName<'tcx>, pub(crate) mir: &'tcx Body<'tcx>, pub(crate) fn_abi: Option>>, diff --git a/src/inline_asm.rs b/src/inline_asm.rs index 1fb5e86aed7..7187bc785e4 100644 --- a/src/inline_asm.rs +++ b/src/inline_asm.rs @@ -92,8 +92,7 @@ pub(crate) fn codegen_inline_asm<'tcx>( let inline_asm_index = fx.inline_asm_index; fx.inline_asm_index += 1; - let asm_name = - format!("{}__inline_asm_{}", fx.tcx.symbol_name(fx.instance).name, inline_asm_index); + let asm_name = format!("{}__inline_asm_{}", fx.symbol_name, inline_asm_index); let generated_asm = generate_asm_wrapper( &asm_name, diff --git a/src/lib.rs b/src/lib.rs index d31e05bdbd5..b79c8c7806b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -125,7 +125,6 @@ struct CodegenCx<'m, 'tcx: 'm> { global_asm: String, constants_cx: ConstantCx, cached_context: Context, - vtables: FxHashMap<(Ty<'tcx>, Option>), DataId>, debug_context: Option>, unwind_context: UnwindContext<'tcx>, } @@ -150,7 +149,6 @@ fn new( global_asm: String::new(), constants_cx: ConstantCx::default(), cached_context: Context::new(), - vtables: FxHashMap::default(), debug_context, unwind_context, } diff --git a/src/trap.rs b/src/trap.rs index 1ab0703e981..4e52fb16121 100644 --- a/src/trap.rs +++ b/src/trap.rs @@ -21,8 +21,7 @@ fn codegen_print(fx: &mut FunctionCx<'_, '_, '_>, msg: &str) { fx.add_comment(puts, "puts"); } - let symbol_name = fx.tcx.symbol_name(fx.instance); - let real_msg = format!("trap at {:?} ({}): {}\0", fx.instance, symbol_name, msg); + let real_msg = format!("trap at {:?} ({}): {}\0", fx.instance, fx.symbol_name, msg); let msg_ptr = fx.anonymous_str("trap", &real_msg); fx.bcx.ins().call(puts, &[msg_ptr]); } diff --git a/src/vtable.rs b/src/vtable.rs index 4d2551a061b..60bc53024a7 100644 --- a/src/vtable.rs +++ b/src/vtable.rs @@ -72,11 +72,11 @@ pub(crate) fn get_vtable<'tcx>( layout: TyAndLayout<'tcx>, trait_ref: Option>, ) -> Value { - let data_id = if let Some(data_id) = fx.cx.vtables.get(&(layout.ty, trait_ref)) { + let data_id = if let Some(data_id) = fx.vtables.get(&(layout.ty, trait_ref)) { *data_id } else { let data_id = build_vtable(fx, layout, trait_ref); - fx.cx.vtables.insert((layout.ty, trait_ref), data_id); + fx.vtables.insert((layout.ty, trait_ref), data_id); data_id }; @@ -139,27 +139,9 @@ fn build_vtable<'tcx>( data_ctx.set_align(fx.tcx.data_layout.pointer_align.pref.bytes()); - let data_id = fx - .cx - .module - .declare_data( - &format!( - "__vtable.{}.for.{:?}.{}", - trait_ref - .as_ref() - .map(|trait_ref| format!("{:?}", trait_ref.skip_binder()).into()) - .unwrap_or(std::borrow::Cow::Borrowed("???")), - layout.ty, - fx.cx.vtables.len(), - ), - Linkage::Local, - false, - false, - ) - .unwrap(); + let data_id = fx.cx.module.declare_anonymous_data(false, false).unwrap(); - // FIXME don't duplicate definitions in lazy jit mode - let _ = fx.cx.module.define_data(data_id, &data_ctx); + fx.cx.module.define_data(data_id, &data_ctx).unwrap(); data_id }