1
Fork 0

Deduplicate function name generation

This commit is contained in:
bjorn3 2018-08-11 13:59:08 +02:00
parent 419cbcbe2f
commit 2e0d6d49bf
2 changed files with 39 additions and 35 deletions

View file

@ -153,16 +153,37 @@ fn ty_fn_sig<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> ty::FnSig<'
tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), &sig) tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), &sig)
} }
fn get_function_name_and_sig<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
inst: Instance<'tcx>,
) -> (String, Signature) {
assert!(!inst.substs.needs_infer() && !inst.substs.has_param_types());
let fn_ty = inst.ty(tcx);
let sig = cton_sig_from_fn_ty(tcx, fn_ty);
let def_path_based_names =
::rustc_mir::monomorphize::item::DefPathBasedNames::new(tcx, false, false);
let mut name = String::new();
def_path_based_names.push_instance_as_string(inst, &mut name);
(name, sig)
}
impl<'a, 'tcx: 'a> CodegenCx<'a, 'tcx, CurrentBackend> {
pub fn predefine_function(&mut self, inst: Instance<'tcx>) -> (FuncId, Function) {
let (name, sig) = crate::abi::get_function_name_and_sig(self.tcx, inst);
let func_id = self
.module
.declare_function(&name, Linkage::Export, &sig)
.unwrap();
let func =
Function::with_name_signature(ExternalName::user(0, func_id.index() as u32), sig);
(func_id, func)
}
}
impl<'a, 'tcx: 'a> FunctionCx<'a, 'tcx> { impl<'a, 'tcx: 'a> FunctionCx<'a, 'tcx> {
/// Instance must be monomorphized /// Instance must be monomorphized
pub fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef { pub fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef {
assert!(!inst.substs.needs_infer() && !inst.substs.has_param_types()); let (name, sig) = get_function_name_and_sig(self.tcx, inst);
let fn_ty = inst.ty(self.tcx);
let sig = cton_sig_from_fn_ty(self.tcx, fn_ty);
let def_path_based_names =
::rustc_mir::monomorphize::item::DefPathBasedNames::new(self.tcx, false, false);
let mut name = String::new();
def_path_based_names.push_instance_as_string(inst, &mut name);
let func_id = self let func_id = self
.module .module
.declare_function(&name, Linkage::Import, &sig) .declare_function(&name, Linkage::Import, &sig)

View file

@ -27,42 +27,25 @@ pub fn trans_mono_item<'a, 'tcx: 'a>(
ty::ParamEnv::reveal_all(), ty::ParamEnv::reveal_all(),
&fn_ty, &fn_ty,
); );
let sig = cton_sig_from_fn_ty(tcx, fn_ty);
let func_id = { let (func_id, mut func) = cx.predefine_function(inst);
// WARNING: keep in sync with FunctionCx::get_function_ref
let def_path_based_names =
::rustc_mir::monomorphize::item::DefPathBasedNames::new(
cx.tcx, false, false,
);
let mut name = String::new();
def_path_based_names.push_instance_as_string(inst, &mut name);
cx.module
.declare_function(&name, Linkage::Export, &sig)
.unwrap()
};
let mut f = Function::with_name_signature( let comments = trans_fn(cx, &mut func, inst);
ExternalName::user(0, func_id.index() as u32),
sig,
);
let comments = trans_fn(cx, &mut f, inst);
let mut writer = crate::pretty_clif::CommentWriter(comments); let mut writer = crate::pretty_clif::CommentWriter(comments);
let mut cton = String::new(); let mut cton = String::new();
::cranelift::codegen::write::decorate_function(&mut writer, &mut cton, &f, None) ::cranelift::codegen::write::decorate_function(&mut writer, &mut cton, &func, None)
.unwrap(); .unwrap();
tcx.sess.warn(&cton); tcx.sess.warn(&cton);
let flags = settings::Flags::new(settings::builder()); let flags = settings::Flags::new(settings::builder());
match ::cranelift::codegen::verify_function(&f, &flags) { match ::cranelift::codegen::verify_function(&func, &flags) {
Ok(_) => {} Ok(_) => {}
Err(err) => { Err(err) => {
tcx.sess.err(&format!("{:?}", err)); tcx.sess.err(&format!("{:?}", err));
let pretty_error = let pretty_error =
::cranelift::codegen::print_errors::pretty_verifier_error( ::cranelift::codegen::print_errors::pretty_verifier_error(
&f, &func,
None, None,
Some(Box::new(writer)), Some(Box::new(writer)),
&err, &err,
@ -72,7 +55,7 @@ pub fn trans_mono_item<'a, 'tcx: 'a>(
} }
} }
context.func = f; context.func = func;
// TODO: cranelift doesn't yet support some of the things needed // TODO: cranelift doesn't yet support some of the things needed
if false { if false {
cx.module.define_function(func_id, context).unwrap(); cx.module.define_function(func_id, context).unwrap();