From a6056bce92a8983f2bd3a8c9d03914bd4fb93c5a Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 2 Jul 2024 21:05:22 +0200 Subject: [PATCH 1/4] Miri function identity hack: account for possible inlining --- src/constant.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/constant.rs b/src/constant.rs index 87c5da3b7c3..9f7b95261d5 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -155,7 +155,7 @@ pub(crate) fn codegen_const_value<'tcx>( fx.bcx.ins().global_value(fx.pointer_type, local_data_id) } } - GlobalAlloc::Function(instance) => { + GlobalAlloc::Function { instance, .. } => { let func_id = crate::abi::import_function(fx.tcx, fx.module, instance); let local_func_id = fx.module.declare_func_in_func(func_id, &mut fx.bcx.func); @@ -351,7 +351,9 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant TodoItem::Alloc(alloc_id) => { let alloc = match tcx.global_alloc(alloc_id) { GlobalAlloc::Memory(alloc) => alloc, - GlobalAlloc::Function(_) | GlobalAlloc::Static(_) | GlobalAlloc::VTable(..) => { + GlobalAlloc::Function { .. } + | GlobalAlloc::Static(_) + | GlobalAlloc::VTable(..) => { unreachable!() } }; @@ -415,7 +417,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant let reloc_target_alloc = tcx.global_alloc(alloc_id); let data_id = match reloc_target_alloc { - GlobalAlloc::Function(instance) => { + GlobalAlloc::Function { instance, .. } => { assert_eq!(addend, 0); let func_id = crate::abi::import_function(tcx, module, instance.polymorphize(tcx)); From e4bde05ed6e4028e936ac223e0322ff66020ad9f Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 12 Jun 2024 13:06:23 -0400 Subject: [PATCH 2/4] Give Instance::expect_resolve a span --- src/abi/mod.rs | 11 ++++++++--- src/main_shim.rs | 2 ++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/abi/mod.rs b/src/abi/mod.rs index 0d7eee7afb4..81dfde81e93 100644 --- a/src/abi/mod.rs +++ b/src/abi/mod.rs @@ -371,9 +371,14 @@ pub(crate) fn codegen_terminator_call<'tcx>( // Handle special calls like intrinsics and empty drop glue. let instance = if let ty::FnDef(def_id, fn_args) = *func.layout().ty.kind() { - let instance = - ty::Instance::expect_resolve(fx.tcx, ty::ParamEnv::reveal_all(), def_id, fn_args) - .polymorphize(fx.tcx); + let instance = ty::Instance::expect_resolve( + fx.tcx, + ty::ParamEnv::reveal_all(), + def_id, + fn_args, + Some(source_info.span), + ) + .polymorphize(fx.tcx); if is_call_from_compiler_builtins_to_upstream_monomorphization(fx.tcx, instance) { if target.is_some() { diff --git a/src/main_shim.rs b/src/main_shim.rs index 33d3f9b8a90..d1dc147dba8 100644 --- a/src/main_shim.rs +++ b/src/main_shim.rs @@ -119,6 +119,7 @@ fn create_entry_fn( ParamEnv::reveal_all(), report.def_id, tcx.mk_args(&[GenericArg::from(main_ret_ty)]), + None, ) .polymorphize(tcx); @@ -144,6 +145,7 @@ fn create_entry_fn( ParamEnv::reveal_all(), start_def_id, tcx.mk_args(&[main_ret_ty.into()]), + None, ) .polymorphize(tcx); let start_func_id = import_function(tcx, m, start_instance); From c6b883bb2eefd5377f2d75be95d8f20e271924ca Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 1 Jul 2024 16:32:32 -0400 Subject: [PATCH 3/4] Fix spans --- src/abi/mod.rs | 2 +- src/main_shim.rs | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/abi/mod.rs b/src/abi/mod.rs index 81dfde81e93..9dc94ab33ea 100644 --- a/src/abi/mod.rs +++ b/src/abi/mod.rs @@ -376,7 +376,7 @@ pub(crate) fn codegen_terminator_call<'tcx>( ty::ParamEnv::reveal_all(), def_id, fn_args, - Some(source_info.span), + source_info.span, ) .polymorphize(fx.tcx); diff --git a/src/main_shim.rs b/src/main_shim.rs index d1dc147dba8..fe0a1551419 100644 --- a/src/main_shim.rs +++ b/src/main_shim.rs @@ -4,6 +4,7 @@ use rustc_middle::ty::GenericArg; use rustc_session::config::{sigpipe, EntryFnType}; use rustc_span::symbol::Ident; +use rustc_span::DUMMY_SP; use crate::prelude::*; @@ -119,7 +120,7 @@ fn create_entry_fn( ParamEnv::reveal_all(), report.def_id, tcx.mk_args(&[GenericArg::from(main_ret_ty)]), - None, + DUMMY_SP, ) .polymorphize(tcx); @@ -145,7 +146,7 @@ fn create_entry_fn( ParamEnv::reveal_all(), start_def_id, tcx.mk_args(&[main_ret_ty.into()]), - None, + DUMMY_SP, ) .polymorphize(tcx); let start_func_id = import_function(tcx, m, start_instance); From 9978261a6e38ca3583cdf3eb2956cb4d14cd5a6f Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Thu, 15 Feb 2024 19:54:37 +0000 Subject: [PATCH 4/4] Support tail calls in mir via `TerminatorKind::TailCall` --- src/base.rs | 5 +++++ src/constant.rs | 1 + 2 files changed, 6 insertions(+) diff --git a/src/base.rs b/src/base.rs index c5b4277015a..5adbbb09ac8 100644 --- a/src/base.rs +++ b/src/base.rs @@ -491,6 +491,11 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) { ) }); } + // FIXME(explicit_tail_calls): add support for tail calls to the cranelift backend, once cranelift supports tail calls + TerminatorKind::TailCall { fn_span, .. } => span_bug!( + *fn_span, + "tail calls are not yet supported in `rustc_codegen_cranelift` backend" + ), TerminatorKind::InlineAsm { template, operands, diff --git a/src/constant.rs b/src/constant.rs index 87c5da3b7c3..fc12f0ff738 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -565,6 +565,7 @@ pub(crate) fn mir_operand_get_const_val<'tcx>( { return None; } + TerminatorKind::TailCall { .. } => return None, TerminatorKind::Call { .. } => {} } }