Skip to content

Commit 4388b6f

Browse files
authored
Rollup merge of rust-lang#96474 - SparrowLii:langcall, r=lcnr
Eliminate duplication code of building panic langcall during codegen From the FIXME in the `codegen_panic_intrinsic` func.
2 parents ddb7fbe + cf00142 commit 4388b6f

File tree

2 files changed

+14
-29
lines changed

2 files changed

+14
-29
lines changed

compiler/rustc_codegen_ssa/src/common.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
use rustc_errors::struct_span_err;
44
use rustc_hir as hir;
5-
use rustc_hir::def_id::DefId;
65
use rustc_hir::LangItem;
76
use rustc_middle::mir::interpret::ConstValue;
87
use rustc_middle::ty::{self, layout::TyAndLayout, Ty, TyCtxt};
98
use rustc_session::Session;
109
use rustc_span::Span;
1110

1211
use crate::base;
13-
use crate::traits::BuilderMethods;
1412
use crate::traits::*;
1513

1614
pub enum IntPredicate {
@@ -118,14 +116,15 @@ mod temp_stable_hash_impls {
118116
}
119117
}
120118

121-
pub fn langcall(tcx: TyCtxt<'_>, span: Option<Span>, msg: &str, li: LangItem) -> DefId {
122-
tcx.lang_items().require(li).unwrap_or_else(|s| {
123-
let msg = format!("{} {}", msg, s);
124-
match span {
125-
Some(span) => tcx.sess.span_fatal(span, &msg),
126-
None => tcx.sess.fatal(&msg),
127-
}
128-
})
119+
pub fn build_langcall<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
120+
bx: &Bx,
121+
span: Option<Span>,
122+
li: LangItem,
123+
) -> (Bx::FnAbiOfResult, Bx::Value) {
124+
let tcx = bx.tcx();
125+
let def_id = tcx.require_lang_item(li, span);
126+
let instance = ty::Instance::mono(tcx, def_id);
127+
(bx.fn_abi_of_instance(instance, ty::List::empty()), bx.get_fn_addr(instance))
129128
}
130129

131130
// To avoid UB from LLVM, these two functions mask RHS with an

compiler/rustc_codegen_ssa/src/mir/block.rs

+5-19
Original file line numberDiff line numberDiff line change
@@ -489,11 +489,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
489489
}
490490
};
491491

492-
// Obtain the panic entry point.
493-
let def_id = common::langcall(bx.tcx(), Some(span), "", lang_item);
494-
let instance = ty::Instance::mono(bx.tcx(), def_id);
495-
let fn_abi = bx.fn_abi_of_instance(instance, ty::List::empty());
496-
let llfn = bx.get_fn_addr(instance);
492+
let (fn_abi, llfn) = common::build_langcall(&bx, Some(span), lang_item);
497493

498494
// Codegen the actual panic invoke/call.
499495
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> {
509505
self.set_debug_loc(&mut bx, terminator.source_info);
510506

511507
// Obtain the panic entry point.
512-
let def_id = common::langcall(bx.tcx(), Some(span), "", LangItem::PanicNoUnwind);
513-
let instance = ty::Instance::mono(bx.tcx(), def_id);
514-
let fn_abi = bx.fn_abi_of_instance(instance, ty::List::empty());
515-
let llfn = bx.get_fn_addr(instance);
508+
let (fn_abi, llfn) = common::build_langcall(&bx, Some(span), LangItem::PanicNoUnwind);
516509

517510
// Codegen the actual panic invoke/call.
518511
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> {
573566
let location = self.get_caller_location(bx, source_info).immediate();
574567

575568
// Obtain the panic entry point.
576-
// FIXME: dedup this with `codegen_assert_terminator` above.
577-
let def_id =
578-
common::langcall(bx.tcx(), Some(source_info.span), "", LangItem::Panic);
579-
let instance = ty::Instance::mono(bx.tcx(), def_id);
580-
let fn_abi = bx.fn_abi_of_instance(instance, ty::List::empty());
581-
let llfn = bx.get_fn_addr(instance);
569+
let (fn_abi, llfn) =
570+
common::build_langcall(bx, Some(source_info.span), LangItem::Panic);
582571

583572
// Codegen the actual panic invoke/call.
584573
helper.do_call(
@@ -1440,10 +1429,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
14401429
let llretty = self.landing_pad_type();
14411430
bx.cleanup_landing_pad(llretty, llpersonality);
14421431

1443-
let def_id = common::langcall(bx.tcx(), None, "", LangItem::PanicNoUnwind);
1444-
let instance = ty::Instance::mono(bx.tcx(), def_id);
1445-
let fn_abi = bx.fn_abi_of_instance(instance, ty::List::empty());
1446-
let fn_ptr = bx.get_fn_addr(instance);
1432+
let (fn_abi, fn_ptr) = common::build_langcall(&bx, None, LangItem::PanicNoUnwind);
14471433
let fn_ty = bx.fn_decl_backend_type(&fn_abi);
14481434

14491435
let llret = bx.call(fn_ty, fn_ptr, &[], None);

0 commit comments

Comments
 (0)