Skip to content

Commit 627d38d

Browse files
committed
[WIP] Remove dependency on enable_multi_ret_implicit_sret
1 parent 34eb0ce commit 627d38d

File tree

4 files changed

+59
-22
lines changed

4 files changed

+59
-22
lines changed

src/abi/mod.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc_span::source_map::Spanned;
2424
use rustc_target::callconv::{FnAbi, PassMode};
2525
use smallvec::{SmallVec, smallvec};
2626

27+
pub(crate) use self::pass_mode::adjust_fn_abi_for_rust_abi_mistakes;
2728
use self::pass_mode::*;
2829
pub(crate) use self::returning::codegen_return;
2930
use crate::base::codegen_unwind_terminate;
@@ -85,7 +86,10 @@ pub(crate) fn get_function_sig<'tcx>(
8586
clif_sig_from_fn_abi(
8687
tcx,
8788
default_call_conv,
88-
&FullyMonomorphizedLayoutCx(tcx).fn_abi_of_instance(inst, ty::List::empty()),
89+
&adjust_fn_abi_for_rust_abi_mistakes(
90+
tcx,
91+
FullyMonomorphizedLayoutCx(tcx).fn_abi_of_instance(inst, ty::List::empty()),
92+
),
8993
)
9094
}
9195

@@ -461,11 +465,14 @@ pub(crate) fn codegen_terminator_call<'tcx>(
461465
let extra_args = fx.tcx.mk_type_list_from_iter(
462466
extra_args.iter().map(|op_arg| fx.monomorphize(op_arg.node.ty(fx.mir, fx.tcx))),
463467
);
464-
let fn_abi = if let Some(instance) = instance {
465-
FullyMonomorphizedLayoutCx(fx.tcx).fn_abi_of_instance(instance, extra_args)
466-
} else {
467-
FullyMonomorphizedLayoutCx(fx.tcx).fn_abi_of_fn_ptr(fn_sig, extra_args)
468-
};
468+
let fn_abi = adjust_fn_abi_for_rust_abi_mistakes(
469+
fx.tcx,
470+
if let Some(instance) = instance {
471+
FullyMonomorphizedLayoutCx(fx.tcx).fn_abi_of_instance(instance, extra_args)
472+
} else {
473+
FullyMonomorphizedLayoutCx(fx.tcx).fn_abi_of_fn_ptr(fn_sig, extra_args)
474+
},
475+
);
469476

470477
let is_cold = if fn_sig.abi() == ExternAbi::RustCold {
471478
true
@@ -736,8 +743,11 @@ pub(crate) fn codegen_drop<'tcx>(
736743
def: ty::InstanceKind::Virtual(drop_instance.def_id(), 0),
737744
args: drop_instance.args,
738745
};
739-
let fn_abi = FullyMonomorphizedLayoutCx(fx.tcx)
740-
.fn_abi_of_instance(virtual_drop, ty::List::empty());
746+
let fn_abi = adjust_fn_abi_for_rust_abi_mistakes(
747+
fx.tcx,
748+
FullyMonomorphizedLayoutCx(fx.tcx)
749+
.fn_abi_of_instance(virtual_drop, ty::List::empty()),
750+
);
741751

742752
let sig = clif_sig_from_fn_abi(fx.tcx, fx.target_config.default_call_conv, &fn_abi);
743753
let sig = fx.bcx.import_signature(sig);
@@ -753,8 +763,11 @@ pub(crate) fn codegen_drop<'tcx>(
753763
_ => {
754764
assert!(!matches!(drop_instance.def, InstanceKind::Virtual(_, _)));
755765

756-
let fn_abi = FullyMonomorphizedLayoutCx(fx.tcx)
757-
.fn_abi_of_instance(drop_instance, ty::List::empty());
766+
let fn_abi = adjust_fn_abi_for_rust_abi_mistakes(
767+
fx.tcx,
768+
FullyMonomorphizedLayoutCx(fx.tcx)
769+
.fn_abi_of_instance(drop_instance, ty::List::empty()),
770+
);
758771

759772
let arg_value = drop_place.place_ref(
760773
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::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

@@ -337,3 +337,33 @@ pub(super) fn cvalue_for_param<'tcx>(
337337
}
338338
}
339339
}
340+
341+
pub(crate) fn adjust_fn_abi_for_rust_abi_mistakes<'tcx>(
342+
tcx: TyCtxt<'tcx>,
343+
fn_abi: &'tcx FnAbi<'tcx, Ty<'tcx>>,
344+
) -> &'tcx FnAbi<'tcx, Ty<'tcx>> {
345+
if fn_abi.conv != Conv::Rust {
346+
// Non-Rust ABI's should be correctly implemented.
347+
return fn_abi;
348+
}
349+
350+
if !tcx.sess.target.is_like_windows && tcx.sess.target.arch != "s390x" {
351+
// Out of the targets cg_clif supports, only Windows and s390x don't have two return
352+
// registers.
353+
return fn_abi;
354+
}
355+
356+
match fn_abi.ret.mode {
357+
PassMode::Ignore | PassMode::Cast { .. } | PassMode::Indirect { .. } => return fn_abi,
358+
PassMode::Direct(_) => {
359+
if fn_abi.ret.layout.size <= tcx.data_layout.pointer_size {
360+
return fn_abi;
361+
}
362+
}
363+
PassMode::Pair(..) => {}
364+
}
365+
366+
let mut fn_abi = fn_abi.clone();
367+
fn_abi.ret.make_indirect();
368+
tcx.arena.alloc(fn_abi)
369+
}

src/base.rs

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

78-
let fn_abi = FullyMonomorphizedLayoutCx(tcx).fn_abi_of_instance(instance, ty::List::empty());
78+
let fn_abi = adjust_fn_abi_for_rust_abi_mistakes(
79+
tcx,
80+
FullyMonomorphizedLayoutCx(tcx).fn_abi_of_instance(instance, ty::List::empty()),
81+
);
7982

8083
// Make FunctionCx
8184
let target_config = module.target_config();
@@ -167,6 +170,7 @@ pub(crate) fn compile_fn(
167170
context.clear();
168171
context.func = codegened_func.func;
169172

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

src/lib.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,6 @@ fn build_isa(sess: &Session, jit: bool) -> Arc<dyn TargetIsa + 'static> {
297297
}
298298
}
299299

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

0 commit comments

Comments
 (0)