Skip to content

Commit 0aa5a23

Browse files
committed
[WIP] Remove dependency on enable_multi_ret_implicit_sret
1 parent 7e0d1b6 commit 0aa5a23

File tree

4 files changed

+64
-24
lines changed

4 files changed

+64
-24
lines changed

src/abi/mod.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc_session::Session;
2121
use rustc_span::source_map::Spanned;
2222
use rustc_target::callconv::{Conv, FnAbi, PassMode};
2323

24+
pub(crate) use self::pass_mode::adjust_fn_abi_for_rust_abi_mistakes;
2425
use self::pass_mode::*;
2526
pub(crate) use self::returning::codegen_return;
2627
use crate::prelude::*;
@@ -80,7 +81,10 @@ pub(crate) fn get_function_sig<'tcx>(
8081
clif_sig_from_fn_abi(
8182
tcx,
8283
default_call_conv,
83-
&FullyMonomorphizedLayoutCx(tcx).fn_abi_of_instance(inst, ty::List::empty()),
84+
&adjust_fn_abi_for_rust_abi_mistakes(
85+
tcx,
86+
FullyMonomorphizedLayoutCx(tcx).fn_abi_of_instance(inst, ty::List::empty()),
87+
),
8488
)
8589
}
8690

@@ -451,11 +455,14 @@ pub(crate) fn codegen_terminator_call<'tcx>(
451455
let extra_args = fx.tcx.mk_type_list_from_iter(
452456
extra_args.iter().map(|op_arg| fx.monomorphize(op_arg.node.ty(fx.mir, fx.tcx))),
453457
);
454-
let fn_abi = if let Some(instance) = instance {
455-
FullyMonomorphizedLayoutCx(fx.tcx).fn_abi_of_instance(instance, extra_args)
456-
} else {
457-
FullyMonomorphizedLayoutCx(fx.tcx).fn_abi_of_fn_ptr(fn_sig, extra_args)
458-
};
458+
let fn_abi = adjust_fn_abi_for_rust_abi_mistakes(
459+
fx.tcx,
460+
if let Some(instance) = instance {
461+
FullyMonomorphizedLayoutCx(fx.tcx).fn_abi_of_instance(instance, extra_args)
462+
} else {
463+
FullyMonomorphizedLayoutCx(fx.tcx).fn_abi_of_fn_ptr(fn_sig, extra_args)
464+
},
465+
);
459466

460467
let is_cold = if fn_sig.abi() == ExternAbi::RustCold {
461468
true
@@ -735,8 +742,11 @@ pub(crate) fn codegen_drop<'tcx>(
735742
def: ty::InstanceKind::Virtual(drop_instance.def_id(), 0),
736743
args: drop_instance.args,
737744
};
738-
let fn_abi = FullyMonomorphizedLayoutCx(fx.tcx)
739-
.fn_abi_of_instance(virtual_drop, ty::List::empty());
745+
let fn_abi = adjust_fn_abi_for_rust_abi_mistakes(
746+
fx.tcx,
747+
FullyMonomorphizedLayoutCx(fx.tcx)
748+
.fn_abi_of_instance(virtual_drop, ty::List::empty()),
749+
);
740750

741751
let sig = clif_sig_from_fn_abi(fx.tcx, fx.target_config.default_call_conv, &fn_abi);
742752
let sig = fx.bcx.import_signature(sig);
@@ -778,8 +788,11 @@ pub(crate) fn codegen_drop<'tcx>(
778788
def: ty::InstanceKind::Virtual(drop_instance.def_id(), 0),
779789
args: drop_instance.args,
780790
};
781-
let fn_abi = FullyMonomorphizedLayoutCx(fx.tcx)
782-
.fn_abi_of_instance(virtual_drop, ty::List::empty());
791+
let fn_abi = adjust_fn_abi_for_rust_abi_mistakes(
792+
fx.tcx,
793+
FullyMonomorphizedLayoutCx(fx.tcx)
794+
.fn_abi_of_instance(virtual_drop, ty::List::empty()),
795+
);
783796

784797
let sig = clif_sig_from_fn_abi(fx.tcx, fx.target_config.default_call_conv, &fn_abi);
785798
let sig = fx.bcx.import_signature(sig);
@@ -788,8 +801,11 @@ pub(crate) fn codegen_drop<'tcx>(
788801
_ => {
789802
assert!(!matches!(drop_instance.def, InstanceKind::Virtual(_, _)));
790803

791-
let fn_abi = FullyMonomorphizedLayoutCx(fx.tcx)
792-
.fn_abi_of_instance(drop_instance, ty::List::empty());
804+
let fn_abi = adjust_fn_abi_for_rust_abi_mistakes(
805+
fx.tcx,
806+
FullyMonomorphizedLayoutCx(fx.tcx)
807+
.fn_abi_of_instance(drop_instance, ty::List::empty()),
808+
);
793809

794810
let arg_value = drop_place.place_ref(
795811
fx,

src/abi/pass_mode.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use cranelift_codegen::ir::{ArgumentExtension, ArgumentPurpose};
44
use rustc_abi::{Reg, RegKind};
55
use rustc_target::callconv::{
6-
ArgAbi, ArgAttributes, ArgExtension as RustcArgExtension, CastTarget, PassMode,
6+
ArgAbi, ArgAttributes, ArgExtension as RustcArgExtension, CastTarget, Conv, FnAbi, PassMode,
77
};
88
use smallvec::{SmallVec, smallvec};
99

@@ -302,3 +302,33 @@ pub(super) fn cvalue_for_param<'tcx>(
302302
}
303303
}
304304
}
305+
306+
pub(crate) fn adjust_fn_abi_for_rust_abi_mistakes<'tcx>(
307+
tcx: TyCtxt<'tcx>,
308+
fn_abi: &'tcx FnAbi<'tcx, Ty<'tcx>>,
309+
) -> &'tcx FnAbi<'tcx, Ty<'tcx>> {
310+
if fn_abi.conv != Conv::Rust {
311+
// Non-Rust ABI's should be correctly implemented.
312+
return fn_abi;
313+
}
314+
315+
if !tcx.sess.target.is_like_windows && tcx.sess.target.arch != "s390x" {
316+
// Out of the targets cg_clif supports, only Windows and s390x don't have two return
317+
// registers.
318+
return fn_abi;
319+
}
320+
321+
match fn_abi.ret.mode {
322+
PassMode::Ignore | PassMode::Cast { .. } | PassMode::Indirect { .. } => return fn_abi,
323+
PassMode::Direct(_) => {
324+
if fn_abi.ret.layout.size <= tcx.data_layout.pointer_size {
325+
return fn_abi;
326+
}
327+
}
328+
PassMode::Pair(..) => {}
329+
}
330+
331+
let mut fn_abi = fn_abi.clone();
332+
fn_abi.ret.make_indirect();
333+
tcx.arena.alloc(fn_abi)
334+
}

src/base.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ pub(crate) fn codegen_fn<'tcx>(
105105
let block_map: IndexVec<BasicBlock, Block> =
106106
(0..mir.basic_blocks.len()).map(|_| bcx.create_block()).collect();
107107

108-
let fn_abi = FullyMonomorphizedLayoutCx(tcx).fn_abi_of_instance(instance, ty::List::empty());
108+
let fn_abi = adjust_fn_abi_for_rust_abi_mistakes(
109+
tcx,
110+
FullyMonomorphizedLayoutCx(tcx).fn_abi_of_instance(instance, ty::List::empty()),
111+
);
109112

110113
// Make FunctionCx
111114
let target_config = module.target_config();
@@ -186,6 +189,7 @@ pub(crate) fn compile_fn(
186189
context.clear();
187190
context.func = codegened_func.func;
188191

192+
// FIXME run this code when define_function returns an error
189193
#[cfg(any())] // This is never true
190194
let _clif_guard = {
191195
use std::fmt::Write;

src/lib.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -294,16 +294,6 @@ fn build_isa(sess: &Session) -> Arc<dyn TargetIsa + 'static> {
294294
}
295295
}
296296

297-
if let target_lexicon::OperatingSystem::Windows = target_triple.operating_system {
298-
// FIXME remove dependency on this from the Rust ABI. cc bytecodealliance/wasmtime#9510
299-
flags_builder.enable("enable_multi_ret_implicit_sret").unwrap();
300-
}
301-
302-
if let target_lexicon::Architecture::S390x = target_triple.architecture {
303-
// FIXME remove dependency on this from the Rust ABI. cc bytecodealliance/wasmtime#9510
304-
flags_builder.enable("enable_multi_ret_implicit_sret").unwrap();
305-
}
306-
307297
if let target_lexicon::Architecture::Aarch64(_)
308298
| target_lexicon::Architecture::Riscv64(_)
309299
| target_lexicon::Architecture::X86_64 = target_triple.architecture

0 commit comments

Comments
 (0)