Eliminate duplication of building panic langcall in codegen

This commit is contained in:
SparrowLii 2022-04-27 18:58:59 +08:00
parent 1f631e8e93
commit d735aa6810
2 changed files with 16 additions and 24 deletions

View File

@ -2,7 +2,6 @@
use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::LangItem;
use rustc_middle::mir::interpret::ConstValue;
use rustc_middle::ty::{self, layout::TyAndLayout, Ty, TyCtxt};
@ -10,7 +9,6 @@ use rustc_session::Session;
use rustc_span::Span;
use crate::base;
use crate::traits::BuilderMethods;
use crate::traits::*;
pub enum IntPredicate {
@ -118,14 +116,22 @@ mod temp_stable_hash_impls {
}
}
pub fn langcall(tcx: TyCtxt<'_>, span: Option<Span>, msg: &str, li: LangItem) -> DefId {
tcx.lang_items().require(li).unwrap_or_else(|s| {
pub fn build_langcall<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
bx: &Bx,
span: Option<Span>,
msg: &str,
li: LangItem,
) -> (Bx::FnAbiOfResult, Bx::Value) {
let tcx = bx.tcx();
let def_id = tcx.lang_items().require(li).unwrap_or_else(|s| {
let msg = format!("{} {}", msg, s);
match span {
Some(span) => tcx.sess.span_fatal(span, &msg),
None => tcx.sess.fatal(&msg),
}
})
});
let instance = ty::Instance::mono(tcx, def_id);
(bx.fn_abi_of_instance(instance, ty::List::empty()), bx.get_fn_addr(instance))
}
// To avoid UB from LLVM, these two functions mask RHS with an

View File

@ -489,11 +489,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
}
};
// Obtain the panic entry point.
let def_id = common::langcall(bx.tcx(), Some(span), "", lang_item);
let instance = ty::Instance::mono(bx.tcx(), def_id);
let fn_abi = bx.fn_abi_of_instance(instance, ty::List::empty());
let llfn = bx.get_fn_addr(instance);
let (fn_abi, llfn) = common::build_langcall(&bx, Some(span), "", lang_item);
// Codegen the actual panic invoke/call.
helper.do_call(self, &mut bx, fn_abi, llfn, &args, None, cleanup);
@ -509,10 +505,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
self.set_debug_loc(&mut bx, terminator.source_info);
// Obtain the panic entry point.
let def_id = common::langcall(bx.tcx(), Some(span), "", LangItem::PanicNoUnwind);
let instance = ty::Instance::mono(bx.tcx(), def_id);
let fn_abi = bx.fn_abi_of_instance(instance, ty::List::empty());
let llfn = bx.get_fn_addr(instance);
let (fn_abi, llfn) = common::build_langcall(&bx, Some(span), "", LangItem::PanicNoUnwind);
// Codegen the actual panic invoke/call.
helper.do_call(self, &mut bx, fn_abi, llfn, &[], None, None);
@ -573,12 +566,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
let location = self.get_caller_location(bx, source_info).immediate();
// Obtain the panic entry point.
// FIXME: dedup this with `codegen_assert_terminator` above.
let def_id =
common::langcall(bx.tcx(), Some(source_info.span), "", LangItem::Panic);
let instance = ty::Instance::mono(bx.tcx(), def_id);
let fn_abi = bx.fn_abi_of_instance(instance, ty::List::empty());
let llfn = bx.get_fn_addr(instance);
let (fn_abi, llfn) =
common::build_langcall(bx, Some(source_info.span), "", LangItem::Panic);
// Codegen the actual panic invoke/call.
helper.do_call(
@ -1440,10 +1429,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
let llretty = self.landing_pad_type();
bx.cleanup_landing_pad(llretty, llpersonality);
let def_id = common::langcall(bx.tcx(), None, "", LangItem::PanicNoUnwind);
let instance = ty::Instance::mono(bx.tcx(), def_id);
let fn_abi = bx.fn_abi_of_instance(instance, ty::List::empty());
let fn_ptr = bx.get_fn_addr(instance);
let (fn_abi, fn_ptr) = common::build_langcall(&bx, None, "", LangItem::PanicNoUnwind);
let fn_ty = bx.fn_decl_backend_type(&fn_abi);
let llret = bx.call(fn_ty, fn_ptr, &[], None);