[OPT] Avoid calling target_triple for every call to codegen

This commit is contained in:
bjorn3 2019-12-17 15:03:32 +01:00
parent ad1f885002
commit d502f8e652
9 changed files with 27 additions and 24 deletions

View file

@ -75,6 +75,7 @@ pub fn fn_sig_for_fn_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> t
fn clif_sig_from_fn_sig<'tcx>(
tcx: TyCtxt<'tcx>,
triple: &target_lexicon::Triple,
sig: FnSig<'tcx>,
is_vtable_fn: bool,
) -> Signature {
@ -89,8 +90,8 @@ fn clif_sig_from_fn_sig<'tcx>(
abi => abi,
};
let (call_conv, inputs, output): (CallConv, Vec<Ty>, Ty) = match abi {
Abi::Rust => (crate::default_call_conv(tcx.sess), sig.inputs().to_vec(), sig.output()),
Abi::C => (crate::default_call_conv(tcx.sess), sig.inputs().to_vec(), sig.output()),
Abi::Rust => (CallConv::triple_default(triple), sig.inputs().to_vec(), sig.output()),
Abi::C => (CallConv::triple_default(triple), sig.inputs().to_vec(), sig.output()),
Abi::RustCall => {
assert_eq!(sig.inputs().len(), 2);
let extra_args = match sig.inputs().last().unwrap().kind {
@ -99,10 +100,10 @@ fn clif_sig_from_fn_sig<'tcx>(
};
let mut inputs: Vec<Ty> = vec![sig.inputs()[0]];
inputs.extend(extra_args.types());
(crate::default_call_conv(tcx.sess), inputs, sig.output())
(CallConv::triple_default(triple), inputs, sig.output())
}
Abi::System => unreachable!(),
Abi::RustIntrinsic => (crate::default_call_conv(tcx.sess), sig.inputs().to_vec(), sig.output()),
Abi::RustIntrinsic => (CallConv::triple_default(triple), sig.inputs().to_vec(), sig.output()),
_ => unimplemented!("unsupported abi {:?}", sig.abi),
};
@ -156,6 +157,7 @@ fn clif_sig_from_fn_sig<'tcx>(
pub fn get_function_name_and_sig<'tcx>(
tcx: TyCtxt<'tcx>,
triple: &target_lexicon::Triple,
inst: Instance<'tcx>,
support_vararg: bool,
) -> (String, Signature) {
@ -165,7 +167,7 @@ pub fn get_function_name_and_sig<'tcx>(
if fn_sig.c_variadic && !support_vararg {
unimpl!("Variadic function definitions are not yet supported");
}
let sig = clif_sig_from_fn_sig(tcx, fn_sig, false);
let sig = clif_sig_from_fn_sig(tcx, triple, fn_sig, false);
(tcx.symbol_name(inst).name.as_str().to_string(), sig)
}
@ -175,7 +177,7 @@ pub fn import_function<'tcx>(
module: &mut Module<impl Backend>,
inst: Instance<'tcx>,
) -> FuncId {
let (name, sig) = get_function_name_and_sig(tcx, inst, true);
let (name, sig) = get_function_name_and_sig(tcx, module.isa().triple(), inst, true);
module
.declare_function(&name, Linkage::Import, &sig)
.unwrap()
@ -205,7 +207,7 @@ impl<'tcx, B: Backend + 'static> FunctionCx<'_, 'tcx, B> {
let sig = Signature {
params: input_tys.iter().cloned().map(AbiParam::new).collect(),
returns: output_tys.iter().cloned().map(AbiParam::new).collect(),
call_conv: crate::default_call_conv(self.tcx.sess),
call_conv: CallConv::triple_default(self.triple()),
};
let func_id = self
.module
@ -579,8 +581,8 @@ fn codegen_call_inner<'tcx>(
let call_inst = if let Some(func_ref) = func_ref {
let sig =
fx.bcx
.import_signature(clif_sig_from_fn_sig(fx.tcx, fn_sig, is_virtual_call));
clif_sig_from_fn_sig(fx.tcx, fx.triple(), fn_sig, is_virtual_call);
let sig = fx.bcx.import_signature(sig);
fx.bcx.ins().call_indirect(sig, func_ref, &call_args)
} else {
let func_ref =
@ -632,9 +634,8 @@ pub fn codegen_drop<'tcx>(fx: &mut FunctionCx<'_, 'tcx, impl Backend>, drop_plac
assert_eq!(fn_sig.output(), fx.tcx.mk_unit());
let sig = fx
.bcx
.import_signature(clif_sig_from_fn_sig(fx.tcx, fn_sig, true));
let sig = clif_sig_from_fn_sig(fx.tcx, fx.triple(), fn_sig, true);
let sig = fx.bcx.import_signature(sig);
fx.bcx.ins().call_indirect(sig, drop_fn, &[ptr]);
}
_ => {

View file

@ -55,7 +55,7 @@ pub fn codegen_inner(sess: &Session, module: &mut Module<impl Backend + 'static>
};
let sig = Signature {
call_conv: crate::default_call_conv(sess),
call_conv: CallConv::triple_default(module.isa().triple()),
params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
returns: output.into_iter().map(AbiParam::new).collect(),
};

View file

@ -12,7 +12,7 @@ pub fn trans_fn<'clif, 'tcx, B: Backend + 'static>(
let mir = *tcx.instance_mir(instance.def);
// Declare function
let (name, sig) = get_function_name_and_sig(tcx, instance, false);
let (name, sig) = get_function_name_and_sig(tcx, cx.module.isa().triple(), instance, false);
let func_id = cx.module.declare_function(&name, linkage, &sig).unwrap();
let mut debug_context = cx
.debug_context

View file

@ -364,4 +364,8 @@ impl<'tcx, B: Backend + 'static> FunctionCx<'_, 'tcx, B> {
));
crate::constant::trans_const_value(self, const_loc)
}
pub fn triple(&self) -> &target_lexicon::Triple {
self.module.isa().triple()
}
}

View file

@ -54,7 +54,7 @@ fn run_jit(tcx: TyCtxt<'_>) -> ! {
returns: vec![AbiParam::new(
jit_module.target_config().pointer_type(), /*isize*/
)],
call_conv: crate::default_call_conv(tcx.sess),
call_conv: CallConv::triple_default(&crate::target_triple(tcx.sess)),
};
let main_func_id = jit_module
.declare_function("main", Linkage::Import, &sig)
@ -298,7 +298,8 @@ fn codegen_mono_items<'tcx>(
for (&mono_item, &(linkage, visibility)) in &mono_items {
match mono_item {
MonoItem::Fn(instance) => {
let (name, sig) = get_function_name_and_sig(tcx, instance, false);
let (name, sig) =
get_function_name_and_sig(tcx, cx.module.isa().triple(), instance, false);
let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility);
cx.module.declare_function(&name, linkage, &sig).unwrap();
}

View file

@ -1085,7 +1085,7 @@ pub fn codegen_intrinsic_call<'tcx>(
try, (v f, v data, v _local_ptr) {
// FIXME once unwinding is supported, change this to actually catch panics
let f_sig = fx.bcx.func.import_signature(Signature {
call_conv: crate::default_call_conv(fx.tcx.sess),
call_conv: CallConv::triple_default(fx.triple()),
params: vec![AbiParam::new(fx.bcx.func.dfg.value_type(data))],
returns: vec![],
});

View file

@ -245,10 +245,6 @@ fn target_triple(sess: &Session) -> target_lexicon::Triple {
sess.target.target.llvm_target.parse().unwrap()
}
fn default_call_conv(sess: &Session) -> CallConv {
CallConv::triple_default(&target_triple(sess))
}
fn build_isa(sess: &Session, enable_pic: bool) -> Box<dyn isa::TargetIsa + 'static> {
let mut flags_builder = settings::builder();
if enable_pic {

View file

@ -41,7 +41,7 @@ pub fn maybe_create_entry_wrapper(tcx: TyCtxt<'_>, module: &mut Module<impl Back
returns: vec![AbiParam::new(
m.target_config().pointer_type(), /*isize*/
)],
call_conv: crate::default_call_conv(tcx.sess),
call_conv: CallConv::triple_default(m.isa().triple()),
};
let cmain_func_id = m
@ -50,7 +50,8 @@ pub fn maybe_create_entry_wrapper(tcx: TyCtxt<'_>, module: &mut Module<impl Back
let instance = Instance::mono(tcx, rust_main_def_id);
let (main_name, main_sig) = get_function_name_and_sig(tcx, instance, false);
let (main_name, main_sig) =
get_function_name_and_sig(tcx, m.isa().triple(), instance, false);
let main_func_id = m
.declare_function(&main_name, Linkage::Import, &main_sig)
.unwrap();

View file

@ -7,7 +7,7 @@ fn codegen_print(fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>, ms
"puts",
Linkage::Import,
&Signature {
call_conv: crate::default_call_conv(fx.tcx.sess),
call_conv: CallConv::triple_default(fx.triple()),
params: vec![AbiParam::new(pointer_ty(fx.tcx))],
returns: vec![],
},