Skip to content

Commit

Permalink
Check for occupied niches
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed Apr 5, 2024
1 parent 5958f5e commit 211d9a2
Show file tree
Hide file tree
Showing 38 changed files with 1,776 additions and 16 deletions.
9 changes: 7 additions & 2 deletions compiler/rustc_codegen_ssa/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use rustc_hir::LangItem;
use rustc_middle::mir;
use rustc_middle::ty::Instance;
use rustc_middle::ty::{self, layout::TyAndLayout, TyCtxt};
use rustc_middle::ty::{self, layout::TyAndLayout, GenericArg, TyCtxt};
use rustc_span::Span;

use crate::traits::*;
Expand Down Expand Up @@ -120,10 +120,15 @@ pub fn build_langcall<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
bx: &Bx,
span: Option<Span>,
li: LangItem,
generic: Option<GenericArg<'tcx>>,
) -> (Bx::FnAbiOfResult, Bx::Value, Instance<'tcx>) {
let tcx = bx.tcx();
let def_id = tcx.require_lang_item(li, span);
let instance = ty::Instance::mono(tcx, def_id);
let instance = if let Some(arg) = generic {
ty::Instance::new(def_id, tcx.mk_args(&[arg]))
} else {
ty::Instance::mono(tcx, def_id)
};
(bx.fn_abi_of_instance(instance, ty::List::empty()), bx.get_fn_addr(instance), instance)
}

Expand Down
30 changes: 23 additions & 7 deletions compiler/rustc_codegen_ssa/src/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
}
};

let (fn_abi, llfn, instance) = common::build_langcall(bx, Some(span), lang_item);
let (fn_abi, llfn, instance) = common::build_langcall(bx, Some(span), lang_item, None);

// Codegen the actual panic invoke/call.
let merging_succ =
Expand All @@ -707,7 +707,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
self.set_debug_loc(bx, terminator.source_info);

// Obtain the panic entry point.
let (fn_abi, llfn, instance) = common::build_langcall(bx, Some(span), reason.lang_item());
let (fn_abi, llfn, instance) =
common::build_langcall(bx, Some(span), reason.lang_item(), None);

// Codegen the actual panic invoke/call.
let merging_succ = helper.do_call(
Expand Down Expand Up @@ -769,8 +770,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
let msg = bx.const_str(&msg_str);

// Obtain the panic entry point.
let (fn_abi, llfn, instance) =
common::build_langcall(bx, Some(source_info.span), LangItem::PanicNounwind);
let (fn_abi, llfn, instance) = common::build_langcall(
bx,
Some(source_info.span),
LangItem::PanicNounwind,
None,
);

// Codegen the actual panic invoke/call.
helper.do_call(
Expand Down Expand Up @@ -1289,6 +1294,17 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
) -> MergingSucc {
debug!("codegen_terminator: {:?}", terminator);

if bx.tcx().may_insert_niche_checks() {
if let mir::TerminatorKind::Return = terminator.kind {
let op = mir::Operand::Copy(mir::Place::return_place());
let ty = op.ty(self.mir, bx.tcx());
let ty = self.monomorphize(ty);
if let Some(niche) = bx.layout_of(ty).largest_niche {
self.codegen_niche_check(bx, op, niche, terminator.source_info);
}
}
}

let helper = TerminatorCodegenHelper { bb, terminator };

let mergeable_succ = || {
Expand Down Expand Up @@ -1583,7 +1599,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
tuple.layout.fields.count()
}

fn get_caller_location(
pub fn get_caller_location(
&mut self,
bx: &mut Bx,
source_info: mir::SourceInfo,
Expand Down Expand Up @@ -1724,12 +1740,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {

self.set_debug_loc(&mut bx, mir::SourceInfo::outermost(self.mir.span));

let (fn_abi, fn_ptr, instance) = common::build_langcall(&bx, None, reason.lang_item());
let (fn_abi, fn_ptr, instance) =
common::build_langcall(&bx, None, reason.lang_item(), None);
if is_call_from_compiler_builtins_to_upstream_monomorphization(bx.tcx(), instance) {
bx.abort();
} else {
let fn_ty = bx.fn_decl_backend_type(fn_abi);

let llret = bx.call(fn_ty, None, Some(fn_abi), fn_ptr, &[], funclet.as_ref(), None);
bx.apply_attrs_to_cleanup_callsite(llret);
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_codegen_ssa/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod coverageinfo;
pub mod debuginfo;
mod intrinsic;
mod locals;
mod niche_check;
pub mod operand;
pub mod place;
mod rvalue;
Expand Down
Loading

0 comments on commit 211d9a2

Please sign in to comment.