Skip to content

Commit 66d6064

Browse files
committed
Auto merge of #134290 - tgross35:windows-i128-callconv, r=bjorn3,wesleywiser
Windows x86: Change i128 to return via the vector ABI Clang and GCC both return `i128` in xmm0 on windows-msvc and windows-gnu. Currently, Rust returns the type on the stack. Add a calling convention adjustment so we also return scalar `i128`s using the vector ABI, which makes our `i128` compatible with C. In the future, Clang may change to return `i128` on the stack for its `-msvc` targets (more at [1]). If this happens, the change here will need to be adjusted to only affect MinGW. Link: #134288 (does not fix) [1] try-job: x86_64-msvc try-job: x86_64-msvc-ext1 try-job: x86_64-mingw-1 try-job: x86_64-mingw-2
2 parents 2f348cb + a44a20e commit 66d6064

File tree

5 files changed

+116
-57
lines changed

5 files changed

+116
-57
lines changed

compiler/rustc_codegen_cranelift/src/abi/mod.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
126126
&mut self,
127127
name: &str,
128128
params: Vec<AbiParam>,
129-
returns: Vec<AbiParam>,
129+
mut returns: Vec<AbiParam>,
130130
args: &[Value],
131131
) -> Cow<'_, [Value]> {
132132
// Pass i128 arguments by-ref on Windows.
@@ -150,15 +150,19 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
150150
(params, args.into())
151151
};
152152

153-
// Return i128 using a return area pointer on Windows and s390x.
154-
let adjust_ret_param =
155-
if self.tcx.sess.target.is_like_windows || self.tcx.sess.target.arch == "s390x" {
156-
returns.len() == 1 && returns[0].value_type == types::I128
157-
} else {
158-
false
159-
};
153+
let ret_single_i128 = returns.len() == 1 && returns[0].value_type == types::I128;
154+
if ret_single_i128 && self.tcx.sess.target.is_like_windows {
155+
// Return i128 using the vector ABI on Windows
156+
returns[0].value_type = types::I64X2;
157+
158+
let ret = self.lib_call_unadjusted(name, params, returns, &args)[0];
160159

161-
if adjust_ret_param {
160+
// FIXME(bytecodealliance/wasmtime#6104) use bitcast instead of store to get from i64x2 to i128
161+
let ret_ptr = self.create_stack_slot(16, 16);
162+
ret_ptr.store(self, ret, MemFlags::trusted());
163+
Cow::Owned(vec![ret_ptr.load(self, types::I128, MemFlags::trusted())])
164+
} else if ret_single_i128 && self.tcx.sess.target.arch == "s390x" {
165+
// Return i128 using a return area pointer on s390x.
162166
let mut params = params;
163167
let mut args = args.to_vec();
164168

compiler/rustc_codegen_cranelift/src/cast.rs

+3-19
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,9 @@ pub(crate) fn clif_int_or_float_cast(
9696
},
9797
);
9898

99-
if fx.tcx.sess.target.is_like_windows {
100-
let ret = fx.lib_call(
101-
&name,
102-
vec![AbiParam::new(from_ty)],
103-
vec![AbiParam::new(types::I64X2)],
104-
&[from],
105-
)[0];
106-
// FIXME(bytecodealliance/wasmtime#6104) use bitcast instead of store to get from i64x2 to i128
107-
let ret_ptr = fx.create_stack_slot(16, 16);
108-
ret_ptr.store(fx, ret, MemFlags::trusted());
109-
ret_ptr.load(fx, types::I128, MemFlags::trusted())
110-
} else {
111-
fx.lib_call(
112-
&name,
113-
vec![AbiParam::new(from_ty)],
114-
vec![AbiParam::new(types::I128)],
115-
&[from],
116-
)[0]
117-
}
99+
fx.lib_call(&name, vec![AbiParam::new(from_ty)], vec![AbiParam::new(types::I128)], &[
100+
from,
101+
])[0]
118102
} else if to_ty == types::I8 || to_ty == types::I16 {
119103
// FIXME implement fcvt_to_*int_sat.i8/i16
120104
let val = if to_signed {

compiler/rustc_codegen_cranelift/src/codegen_i128.rs

+8-22
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,14 @@ pub(crate) fn maybe_codegen<'tcx>(
3333
(BinOp::Rem, true) => "__modti3",
3434
_ => unreachable!(),
3535
};
36-
if fx.tcx.sess.target.is_like_windows {
37-
let args = [lhs.load_scalar(fx), rhs.load_scalar(fx)];
38-
let ret = fx.lib_call(
39-
name,
40-
vec![AbiParam::new(types::I128), AbiParam::new(types::I128)],
41-
vec![AbiParam::new(types::I64X2)],
42-
&args,
43-
)[0];
44-
// FIXME(bytecodealliance/wasmtime#6104) use bitcast instead of store to get from i64x2 to i128
45-
let ret_place = CPlace::new_stack_slot(fx, lhs.layout());
46-
ret_place.to_ptr().store(fx, ret, MemFlags::trusted());
47-
Some(ret_place.to_cvalue(fx))
48-
} else {
49-
let args = [lhs.load_scalar(fx), rhs.load_scalar(fx)];
50-
let ret_val = fx.lib_call(
51-
name,
52-
vec![AbiParam::new(types::I128), AbiParam::new(types::I128)],
53-
vec![AbiParam::new(types::I128)],
54-
&args,
55-
)[0];
56-
Some(CValue::by_val(ret_val, lhs.layout()))
57-
}
36+
let args = [lhs.load_scalar(fx), rhs.load_scalar(fx)];
37+
let ret_val = fx.lib_call(
38+
name,
39+
vec![AbiParam::new(types::I128), AbiParam::new(types::I128)],
40+
vec![AbiParam::new(types::I128)],
41+
&args,
42+
)[0];
43+
Some(CValue::by_val(ret_val, lhs.layout()))
5844
}
5945
BinOp::Lt | BinOp::Le | BinOp::Eq | BinOp::Ge | BinOp::Gt | BinOp::Ne | BinOp::Cmp => None,
6046
BinOp::Shl | BinOp::ShlUnchecked | BinOp::Shr | BinOp::ShrUnchecked => None,

compiler/rustc_target/src/callconv/x86_win64.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use rustc_abi::{BackendRepr, Float, Primitive};
1+
use rustc_abi::{BackendRepr, Float, Integer, Primitive, RegKind, Size};
22

33
use crate::abi::call::{ArgAbi, FnAbi, Reg};
44
use crate::spec::HasTargetSpec;
55

66
// Win64 ABI: https://docs.microsoft.com/en-us/cpp/build/parameter-passing
77

88
pub(crate) fn compute_abi_info<Ty>(_cx: &impl HasTargetSpec, fn_abi: &mut FnAbi<'_, Ty>) {
9-
let fixup = |a: &mut ArgAbi<'_, Ty>| {
9+
let fixup = |a: &mut ArgAbi<'_, Ty>, is_ret: bool| {
1010
match a.layout.backend_repr {
1111
BackendRepr::Uninhabited | BackendRepr::Memory { sized: false } => {}
1212
BackendRepr::ScalarPair(..) | BackendRepr::Memory { sized: true } => {
@@ -23,11 +23,16 @@ pub(crate) fn compute_abi_info<Ty>(_cx: &impl HasTargetSpec, fn_abi: &mut FnAbi<
2323
// (probably what clang calls "illegal vectors").
2424
}
2525
BackendRepr::Scalar(scalar) => {
26-
// Match what LLVM does for `f128` so that `compiler-builtins` builtins match up
27-
// with what LLVM expects.
28-
if a.layout.size.bytes() > 8
26+
if is_ret && matches!(scalar.primitive(), Primitive::Int(Integer::I128, _)) {
27+
// `i128` is returned in xmm0 by Clang and GCC
28+
// FIXME(#134288): This may change for the `-msvc` targets in the future.
29+
let reg = Reg { kind: RegKind::Vector, size: Size::from_bits(128) };
30+
a.cast_to(reg);
31+
} else if a.layout.size.bytes() > 8
2932
&& !matches!(scalar.primitive(), Primitive::Float(Float::F128))
3033
{
34+
// Match what LLVM does for `f128` so that `compiler-builtins` builtins match up
35+
// with what LLVM expects.
3136
a.make_indirect();
3237
} else {
3338
a.extend_integer_width_to(32);
@@ -37,8 +42,9 @@ pub(crate) fn compute_abi_info<Ty>(_cx: &impl HasTargetSpec, fn_abi: &mut FnAbi<
3742
};
3843

3944
if !fn_abi.ret.is_ignore() {
40-
fixup(&mut fn_abi.ret);
45+
fixup(&mut fn_abi.ret, true);
4146
}
47+
4248
for arg in fn_abi.args.iter_mut() {
4349
if arg.is_ignore() && arg.layout.is_zst() {
4450
// Windows ABIs do not talk about ZST since such types do not exist in MSVC.
@@ -49,7 +55,7 @@ pub(crate) fn compute_abi_info<Ty>(_cx: &impl HasTargetSpec, fn_abi: &mut FnAbi<
4955
arg.make_indirect_from_ignore();
5056
continue;
5157
}
52-
fixup(arg);
58+
fixup(arg, false);
5359
}
5460
// FIXME: We should likely also do something about ZST return types, similar to above.
5561
// However, that's non-trivial due to `()`.

tests/codegen/i128-x86-callconv.rs

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//! Verify that Rust implements the expected calling convention for `i128`/`u128`.
2+
3+
// Eliminate intermediate instructions during `nop` tests
4+
//@ compile-flags: -Copt-level=1
5+
6+
//@ add-core-stubs
7+
//@ revisions: MSVC MINGW
8+
//@ [MSVC] needs-llvm-components: x86
9+
//@ [MINGW] needs-llvm-components: x86
10+
//@ [MSVC] compile-flags: --target x86_64-pc-windows-msvc
11+
//@ [MINGW] compile-flags: --target x86_64-pc-windows-gnu
12+
//@ [MSVC] filecheck-flags: --check-prefix=WIN
13+
//@ [MINGW] filecheck-flags: --check-prefix=WIN
14+
15+
#![crate_type = "lib"]
16+
#![no_std]
17+
#![no_core]
18+
#![feature(no_core, lang_items)]
19+
20+
extern crate minicore;
21+
22+
extern "C" {
23+
fn extern_call(arg0: i128);
24+
fn extern_ret() -> i128;
25+
}
26+
27+
#[no_mangle]
28+
pub extern "C" fn pass(_arg0: u32, arg1: i128) {
29+
// CHECK-LABEL: @pass(
30+
// i128 is passed indirectly on Windows. It should load the pointer to the stack and pass
31+
// a pointer to that allocation.
32+
// WIN-SAME: %_arg0, ptr{{.*}} %arg1)
33+
// WIN: [[PASS:%[_0-9]+]] = alloca [16 x i8], align 16
34+
// WIN: [[LOADED:%[_0-9]+]] = load i128, ptr %arg1
35+
// WIN: store i128 [[LOADED]], ptr [[PASS]]
36+
// WIN: call void @extern_call
37+
unsafe { extern_call(arg1) };
38+
}
39+
40+
// Check that we produce the correct return ABI
41+
#[no_mangle]
42+
pub extern "C" fn ret(_arg0: u32, arg1: i128) -> i128 {
43+
// CHECK-LABEL: @ret(
44+
// i128 is returned in xmm0 on Windows
45+
// FIXME(#134288): This may change for the `-msvc` targets in the future.
46+
// WIN-SAME: i32{{.*}} %_arg0, ptr{{.*}} %arg1)
47+
// WIN: [[LOADED:%[_0-9]+]] = load <16 x i8>, ptr %arg1
48+
// WIN-NEXT: ret <16 x i8> [[LOADED]]
49+
arg1
50+
}
51+
52+
// Check that we consume the correct return ABI
53+
#[no_mangle]
54+
pub extern "C" fn forward(dst: *mut i128) {
55+
// CHECK-LABEL: @forward
56+
// WIN-SAME: ptr{{.*}} %dst)
57+
// WIN: [[RETURNED:%[_0-9]+]] = tail call <16 x i8> @extern_ret()
58+
// WIN: store <16 x i8> [[RETURNED]], ptr %dst
59+
// WIN: ret void
60+
unsafe { *dst = extern_ret() };
61+
}
62+
63+
#[repr(C)]
64+
struct RetAggregate {
65+
a: i32,
66+
b: i128,
67+
}
68+
69+
#[no_mangle]
70+
pub extern "C" fn ret_aggregate(_arg0: u32, arg1: i128) -> RetAggregate {
71+
// CHECK-LABEL: @ret_aggregate(
72+
// Aggregates should also be returned indirectly
73+
// WIN-SAME: ptr{{.*}}sret([32 x i8]){{.*}}[[RET:%[_0-9]+]], i32{{.*}}%_arg0, ptr{{.*}}%arg1)
74+
// WIN: [[LOADED:%[_0-9]+]] = load i128, ptr %arg1
75+
// WIN: [[GEP:%[_0-9]+]] = getelementptr{{.*}}, ptr [[RET]]
76+
// WIN: store i128 [[LOADED]], ptr [[GEP]]
77+
// WIN: ret void
78+
RetAggregate { a: 1, b: arg1 }
79+
}

0 commit comments

Comments
 (0)