-
-
Notifications
You must be signed in to change notification settings - Fork 16.8k
"Handle" calls to upstream monomorphizations in compiler_builtins #122580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ use super::{CachedLlbb, FunctionCx, LocalRef}; | |
|
|
||
| use crate::base; | ||
| use crate::common::{self, IntPredicate}; | ||
| use crate::errors::CompilerBuiltinsCannotCall; | ||
| use crate::meth; | ||
| use crate::traits::*; | ||
| use crate::MemFlags; | ||
|
|
@@ -16,6 +17,7 @@ use rustc_middle::mir::{self, AssertKind, BasicBlock, SwitchTargets, UnwindTermi | |
| use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf, ValidityRequirement}; | ||
| use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths}; | ||
| use rustc_middle::ty::{self, Instance, Ty}; | ||
| use rustc_monomorphize::is_call_from_compiler_builtins_to_upstream_monomorphization; | ||
| use rustc_session::config::OptLevel; | ||
| use rustc_span::{source_map::Spanned, sym, Span}; | ||
| use rustc_target::abi::call::{ArgAbi, FnAbi, PassMode, Reg}; | ||
|
|
@@ -157,8 +159,28 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> { | |
| destination: Option<(ReturnDest<'tcx, Bx::Value>, mir::BasicBlock)>, | ||
| mut unwind: mir::UnwindAction, | ||
| copied_constant_arguments: &[PlaceRef<'tcx, <Bx as BackendTypes>::Value>], | ||
| instance: Option<Instance<'tcx>>, | ||
| mergeable_succ: bool, | ||
| ) -> MergingSucc { | ||
| let tcx = bx.tcx(); | ||
| if let Some(instance) = instance { | ||
| if is_call_from_compiler_builtins_to_upstream_monomorphization(tcx, instance) { | ||
| if destination.is_some() { | ||
| let caller = with_no_trimmed_paths!(tcx.def_path_str(fx.instance.def_id())); | ||
| let callee = with_no_trimmed_paths!(tcx.def_path_str(instance.def_id())); | ||
| tcx.dcx().emit_err(CompilerBuiltinsCannotCall { caller, callee }); | ||
| } else { | ||
| info!( | ||
| "compiler_builtins call to diverging function {:?} replaced with abort", | ||
| instance.def_id() | ||
| ); | ||
| bx.abort(); | ||
| bx.unreachable(); | ||
| return MergingSucc::False; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // If there is a cleanup block and the function we're calling can unwind, then | ||
| // do an invoke, otherwise do a call. | ||
| let fn_ty = bx.fn_decl_backend_type(fn_abi); | ||
|
|
@@ -480,6 +502,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | |
| let ty = location.ty(self.mir, bx.tcx()).ty; | ||
| let ty = self.monomorphize(ty); | ||
| let drop_fn = Instance::resolve_drop_in_place(bx.tcx(), ty); | ||
| let instance = drop_fn.clone(); | ||
|
|
||
| if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def { | ||
| // we don't actually need to drop anything. | ||
|
|
@@ -582,6 +605,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | |
| Some((ReturnDest::Nothing, target)), | ||
| unwind, | ||
| &[], | ||
| Some(instance), | ||
| mergeable_succ, | ||
| ) | ||
| } | ||
|
|
@@ -658,10 +682,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | |
| } | ||
| }; | ||
|
|
||
| let (fn_abi, llfn) = common::build_langcall(bx, Some(span), lang_item); | ||
| let (fn_abi, llfn, instance) = common::build_langcall(bx, Some(span), lang_item); | ||
|
|
||
| // Codegen the actual panic invoke/call. | ||
| let merging_succ = helper.do_call(self, bx, fn_abi, llfn, &args, None, unwind, &[], false); | ||
| let merging_succ = | ||
| helper.do_call(self, bx, fn_abi, llfn, &args, None, unwind, &[], Some(instance), false); | ||
| assert_eq!(merging_succ, MergingSucc::False); | ||
| MergingSucc::False | ||
| } | ||
|
|
@@ -677,7 +702,7 @@ 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) = common::build_langcall(bx, Some(span), reason.lang_item()); | ||
| let (fn_abi, llfn, instance) = common::build_langcall(bx, Some(span), reason.lang_item()); | ||
|
|
||
| // Codegen the actual panic invoke/call. | ||
| let merging_succ = helper.do_call( | ||
|
|
@@ -689,6 +714,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | |
| None, | ||
| mir::UnwindAction::Unreachable, | ||
| &[], | ||
| Some(instance), | ||
| false, | ||
| ); | ||
| assert_eq!(merging_succ, MergingSucc::False); | ||
|
|
@@ -738,7 +764,7 @@ 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) = | ||
| let (fn_abi, llfn, instance) = | ||
| common::build_langcall(bx, Some(source_info.span), LangItem::PanicNounwind); | ||
|
|
||
| // Codegen the actual panic invoke/call. | ||
|
|
@@ -751,6 +777,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | |
| target.as_ref().map(|bb| (ReturnDest::Nothing, *bb)), | ||
| unwind, | ||
| &[], | ||
| Some(instance), | ||
| mergeable_succ, | ||
| ) | ||
| } else { | ||
|
|
@@ -798,6 +825,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | |
| ty::FnPtr(_) => (None, Some(callee.immediate())), | ||
| _ => bug!("{} is not callable", callee.layout.ty), | ||
| }; | ||
|
|
||
| let def = instance.map(|i| i.def); | ||
|
|
||
| if let Some(ty::InstanceDef::DropGlue(_, None)) = def { | ||
|
|
@@ -1106,6 +1134,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | |
| destination, | ||
| unwind, | ||
| &copied_constant_arguments, | ||
| instance, | ||
| mergeable_succ, | ||
| ) | ||
| } | ||
|
|
@@ -1664,11 +1693,15 @@ 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) = common::build_langcall(&bx, None, reason.lang_item()); | ||
| let fn_ty = bx.fn_decl_backend_type(fn_abi); | ||
| let (fn_abi, fn_ptr, instance) = common::build_langcall(&bx, None, reason.lang_item()); | ||
| if is_call_from_compiler_builtins_to_upstream_monomorphization(bx.tcx(), instance) { | ||
| bx.abort(); | ||
| } else { | ||
|
Comment on lines
+1697
to
+1699
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And then there's another "replace call by abort" thing here that doesn't even have a comment explaining why we are silently patching the code while compiling it. |
||
| 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()); | ||
| bx.apply_attrs_to_cleanup_callsite(llret); | ||
| let llret = bx.call(fn_ty, None, Some(fn_abi), fn_ptr, &[], funclet.as_ref()); | ||
| bx.apply_attrs_to_cleanup_callsite(llret); | ||
| } | ||
|
|
||
| bx.unreachable(); | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cg_clif will need this change too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I apply this change to cranelift, how would I verify that it works?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/rust-lang/rustc_codegen_cranelift/#building-and-testing-with-changes-in-rustc-code has instructions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And the new code would likely be somewhere around https://github.com/rust-lang/rustc_codegen_cranelift/blob/4cf4ffc6ba514f171b3f52d1c731063e4fc45be3/src/abi/mod.rs#L374