Skip to content

Commit 4c26894

Browse files
committed
Auto merge of #134290 - tgross35:windows-i128-callconv, r=<try>
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) 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 327c7ee + 706db2b commit 4c26894

File tree

3 files changed

+93
-8
lines changed

3 files changed

+93
-8
lines changed

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, no
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() {
4450
// x86_64-pc-windows-gnu doesn't ignore ZSTs.
@@ -50,6 +56,6 @@ pub(crate) fn compute_abi_info<Ty>(cx: &impl HasTargetSpec, fn_abi: &mut FnAbi<'
5056
}
5157
continue;
5258
}
53-
fixup(arg);
59+
fixup(arg, false);
5460
}
5561
}

tests/auxiliary/minicore.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ impl<T: ?Sized> LegacyReceiver for &mut T {}
4040
pub trait Copy: Sized {}
4141

4242
impl_marker_trait!(
43-
Copy => [ bool, char, isize, usize, i8, i16, i32, i64, u8, u16, u32, u64, f32, f64 ]
43+
Copy => [
44+
bool, char, isize, usize, i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, f32, f64
45+
]
4446
);
47+
4548
impl<'a, T: ?Sized> Copy for &'a T {}
4649
impl<T: ?Sized> Copy for *const T {}
4750
impl<T: ?Sized> Copy for *mut T {}

tests/codegen/i128-x86-callconv.rs

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

0 commit comments

Comments
 (0)