Skip to content

Commit 0b78678

Browse files
committed
x86_win64 ABI: do not use xmm0 with softfloat ABI
1 parent 608e228 commit 0b78678

File tree

3 files changed

+60
-21
lines changed

3 files changed

+60
-21
lines changed

compiler/rustc_target/src/callconv/x86_win64.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use rustc_abi::{BackendRepr, Float, Integer, Primitive, RegKind, Size};
22

33
use crate::callconv::{ArgAbi, FnAbi, Reg};
4-
use crate::spec::HasTargetSpec;
4+
use crate::spec::{HasTargetSpec, RustcAbi};
55

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

8-
pub(crate) fn compute_abi_info<Ty>(_cx: &impl HasTargetSpec, fn_abi: &mut FnAbi<'_, Ty>) {
8+
pub(crate) fn compute_abi_info<Ty>(cx: &impl HasTargetSpec, fn_abi: &mut FnAbi<'_, Ty>) {
99
let fixup = |a: &mut ArgAbi<'_, Ty>, is_ret: bool| {
1010
match a.layout.backend_repr {
1111
BackendRepr::Uninhabited | BackendRepr::Memory { sized: false } => {}
@@ -24,10 +24,14 @@ pub(crate) fn compute_abi_info<Ty>(_cx: &impl HasTargetSpec, fn_abi: &mut FnAbi<
2424
}
2525
BackendRepr::Scalar(scalar) => {
2626
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);
27+
if cx.target_spec().rustc_abi == Some(RustcAbi::X86Softfloat) {
28+
// Use the native `i128` LLVM type for the softfloat ABI -- in other words, adjust nothing.
29+
} else {
30+
// `i128` is returned in xmm0 by Clang and GCC
31+
// FIXME(#134288): This may change for the `-msvc` targets in the future.
32+
let reg = Reg { kind: RegKind::Vector, size: Size::from_bits(128) };
33+
a.cast_to(reg);
34+
}
3135
} else if a.layout.size.bytes() > 8
3236
&& !matches!(scalar.primitive(), Primitive::Float(Float::F128))
3337
{
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//@ assembly-output: emit-asm
2+
//@ add-core-stubs
3+
//@ revisions: msvc softfloat
4+
//@ compile-flags: -Copt-level=3
5+
//@[msvc] compile-flags: --target x86_64-pc-windows-msvc
6+
//@[msvc] needs-llvm-components: x86
7+
//@[softfloat] compile-flags: --target x86_64-unknown-uefi
8+
//@[softfloat] needs-llvm-components: x86
9+
10+
#![feature(f16, f128)]
11+
#![feature(no_core)]
12+
#![no_core]
13+
#![crate_type = "lib"]
14+
15+
extern crate minicore;
16+
use minicore::*;
17+
18+
// CHECK-LABEL: ret_i128
19+
// Hadrdlofat targets return via xmm0, softfloat targets via rax and rdx.
20+
// msvc: movaps {{.*}}, %xmm0
21+
// softfloat: movq (%[[INPUT:.*]]), %rax
22+
// softfloat-NEXT: movq 8(%[[INPUT]]), %rdx
23+
// CHECK-NEXT: retq
24+
#[no_mangle]
25+
pub extern "C" fn ret_i128(x: &i128) -> i128 {
26+
*x
27+
}

tests/codegen/i128-x86-callconv.rs

+23-15
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
//@ compile-flags: -Copt-level=1
55

66
//@ add-core-stubs
7-
//@ revisions: MSVC MINGW
7+
//@ revisions: MSVC MINGW softfloat
88
//@ [MSVC] needs-llvm-components: x86
9-
//@ [MINGW] needs-llvm-components: x86
109
//@ [MSVC] compile-flags: --target x86_64-pc-windows-msvc
11-
//@ [MINGW] compile-flags: --target x86_64-pc-windows-gnu
10+
// Use `WIN` as a common prefix for MSVC and MINGW but *not* the softfloat test.
1211
//@ [MSVC] filecheck-flags: --check-prefix=WIN
12+
//@ [MINGW] needs-llvm-components: x86
13+
//@ [MINGW] compile-flags: --target x86_64-pc-windows-gnu
1314
//@ [MINGW] filecheck-flags: --check-prefix=WIN
15+
// The `x86_64-unknown-uefi` target also uses the Windows calling convention,
16+
// but does not have SSE registers available.
17+
//@ [softfloat] needs-llvm-components: x86
18+
//@ [softfloat] compile-flags: --target x86_64-unknown-uefi
1419

1520
#![crate_type = "lib"]
1621
#![no_std]
@@ -28,24 +33,26 @@ extern "C" {
2833
pub extern "C" fn pass(_arg0: u32, arg1: i128) {
2934
// CHECK-LABEL: @pass(
3035
// 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
36+
// a pointer to that allocation. The softfloat ABI works the same.
37+
// CHECK-SAME: %_arg0, ptr{{.*}} %arg1)
38+
// CHECK: [[PASS:%[_0-9]+]] = alloca [16 x i8], align 16
39+
// CHECK: [[LOADED:%[_0-9]+]] = load i128, ptr %arg1
40+
// CHECK: store i128 [[LOADED]], ptr [[PASS]]
41+
// CHECK: call void @extern_call
3742
unsafe { extern_call(arg1) };
3843
}
3944

4045
// Check that we produce the correct return ABI
4146
#[no_mangle]
4247
pub extern "C" fn ret(_arg0: u32, arg1: i128) -> i128 {
43-
// CHECK-LABEL: @ret(
48+
// WIN-LABEL: @ret(
4449
// i128 is returned in xmm0 on Windows
4550
// FIXME(#134288): This may change for the `-msvc` targets in the future.
4651
// WIN-SAME: i32{{.*}} %_arg0, ptr{{.*}} %arg1)
4752
// WIN: [[LOADED:%[_0-9]+]] = load <16 x i8>, ptr %arg1
4853
// WIN-NEXT: ret <16 x i8> [[LOADED]]
54+
// The softfloat ABI returns this indirectly.
55+
// softfloat-LABEL: i128 @ret(i32{{.*}} %_arg0, ptr{{.*}} %arg1)
4956
arg1
5057
}
5158

@@ -57,6 +64,7 @@ pub extern "C" fn forward(dst: *mut i128) {
5764
// WIN: [[RETURNED:%[_0-9]+]] = tail call <16 x i8> @extern_ret()
5865
// WIN: store <16 x i8> [[RETURNED]], ptr %dst
5966
// WIN: ret void
67+
// softfloat: [[RETURNED:%[_0-9]+]] = tail call {{.*}}i128 @extern_ret()
6068
unsafe { *dst = extern_ret() };
6169
}
6270

@@ -70,10 +78,10 @@ struct RetAggregate {
7078
pub extern "C" fn ret_aggregate(_arg0: u32, arg1: i128) -> RetAggregate {
7179
// CHECK-LABEL: @ret_aggregate(
7280
// 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
81+
// CHECK-SAME: ptr{{.*}}sret([32 x i8]){{.*}}[[RET:%[_0-9]+]], i32{{.*}}%_arg0, ptr{{.*}}%arg1)
82+
// CHECK: [[LOADED:%[_0-9]+]] = load i128, ptr %arg1
83+
// CHECK: [[GEP:%[_0-9]+]] = getelementptr{{.*}}, ptr [[RET]]
84+
// CHECK: store i128 [[LOADED]], ptr [[GEP]]
85+
// CHECK: ret void
7886
RetAggregate { a: 1, b: arg1 }
7987
}

0 commit comments

Comments
 (0)