Skip to content

Commit 61e48a2

Browse files
committed
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: rust-lang/rust#134288
1 parent b52c07f commit 61e48a2

File tree

3 files changed

+24
-50
lines changed

3 files changed

+24
-50
lines changed

src/abi/mod.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
122122
&mut self,
123123
name: &str,
124124
params: Vec<AbiParam>,
125-
returns: Vec<AbiParam>,
125+
mut returns: Vec<AbiParam>,
126126
args: &[Value],
127127
) -> Cow<'_, [Value]> {
128128
// Pass i128 arguments by-ref on Windows.
@@ -146,15 +146,19 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
146146
(params, args.into())
147147
};
148148

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

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

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 {

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,

0 commit comments

Comments
 (0)