Skip to content

Commit

Permalink
x86_win64 ABI: do not use xmm0 with softfloat ABI
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Feb 18, 2025
1 parent 608e228 commit 778ec18
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
12 changes: 4 additions & 8 deletions compiler/rustc_target/src/callconv/x86_win64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_abi::{BackendRepr, Float, Integer, Primitive, RegKind, Size};
use rustc_abi::{BackendRepr, Float, Integer, Primitive};

use crate::callconv::{ArgAbi, FnAbi, Reg};
use crate::spec::HasTargetSpec;
Expand All @@ -23,15 +23,11 @@ pub(crate) fn compute_abi_info<Ty>(_cx: &impl HasTargetSpec, fn_abi: &mut FnAbi<
// (probably what clang calls "illegal vectors").
}
BackendRepr::Scalar(scalar) => {
if is_ret && matches!(scalar.primitive(), Primitive::Int(Integer::I128, _)) {
// `i128` is returned in xmm0 by Clang and GCC
// FIXME(#134288): This may change for the `-msvc` targets in the future.
let reg = Reg { kind: RegKind::Vector, size: Size::from_bits(128) };
a.cast_to(reg);
} else if a.layout.size.bytes() > 8
if a.layout.size.bytes() > 8
&& !matches!(scalar.primitive(), Primitive::Float(Float::F128))
&& !(is_ret && matches!(scalar.primitive(), Primitive::Int(Integer::I128, _)))
{
// Match what LLVM does for `f128` so that `compiler-builtins` builtins match up
// Match what LLVM does for `f128` and `i128` (the latter only for return values) so that `compiler-builtins` builtins match up
// with what LLVM expects.
a.make_indirect();
} else {
Expand Down
52 changes: 28 additions & 24 deletions tests/codegen/i128-x86-callconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
//@ compile-flags: -Copt-level=1

//@ add-core-stubs
//@ revisions: MSVC MINGW
//@ revisions: MSVC MINGW softfloat
//@ [MSVC] needs-llvm-components: x86
//@ [MINGW] needs-llvm-components: x86
//@ [MSVC] compile-flags: --target x86_64-pc-windows-msvc
//@ [MINGW] compile-flags: --target x86_64-pc-windows-gnu
// Use `WIN` as a common prefix for MSVC and MINGW but *not* the softfloat test.
//@ [MSVC] filecheck-flags: --check-prefix=WIN
//@ [MINGW] needs-llvm-components: x86
//@ [MINGW] compile-flags: --target x86_64-pc-windows-gnu
//@ [MINGW] filecheck-flags: --check-prefix=WIN
// The `x86_64-unknown-uefi` target also uses the Windows calling convention,
// but does not have SSE registers available.
//@ [softfloat] needs-llvm-components: x86
//@ [softfloat] compile-flags: --target x86_64-unknown-uefi

#![crate_type = "lib"]
#![no_std]
Expand All @@ -28,35 +33,34 @@ extern "C" {
pub extern "C" fn pass(_arg0: u32, arg1: i128) {
// CHECK-LABEL: @pass(
// i128 is passed indirectly on Windows. It should load the pointer to the stack and pass
// a pointer to that allocation.
// WIN-SAME: %_arg0, ptr{{.*}} %arg1)
// WIN: [[PASS:%[_0-9]+]] = alloca [16 x i8], align 16
// WIN: [[LOADED:%[_0-9]+]] = load i128, ptr %arg1
// WIN: store i128 [[LOADED]], ptr [[PASS]]
// WIN: call void @extern_call
// a pointer to that allocation. The softfloat ABI works the same.
// CHECK-SAME: %_arg0, ptr{{.*}} %arg1)
// CHECK: [[PASS:%[_0-9]+]] = alloca [16 x i8], align 16
// CHECK: [[LOADED:%[_0-9]+]] = load i128, ptr %arg1
// CHECK: store i128 [[LOADED]], ptr [[PASS]]
// CHECK: call void @extern_call
unsafe { extern_call(arg1) };
}

// Check that we produce the correct return ABI
#[no_mangle]
pub extern "C" fn ret(_arg0: u32, arg1: i128) -> i128 {
// CHECK-LABEL: @ret(
// i128 is returned in xmm0 on Windows
// FIXME(#134288): This may change for the `-msvc` targets in the future.
// WIN-SAME: i32{{.*}} %_arg0, ptr{{.*}} %arg1)
// WIN: [[LOADED:%[_0-9]+]] = load <16 x i8>, ptr %arg1
// WIN-NEXT: ret <16 x i8> [[LOADED]]
// we use the LLVM native ABI for the return value
// CHECK-LABEL: i128 @ret(
// CHECK-SAME: i32{{.*}} %_arg0, ptr{{.*}} %arg1)
// CHECK: [[LOADED:%[_0-9]+]] = load i128, ptr %arg1
// CHECK-NEXT: ret i128 [[LOADED]]
arg1
}

// Check that we consume the correct return ABI
#[no_mangle]
pub extern "C" fn forward(dst: *mut i128) {
// CHECK-LABEL: @forward
// WIN-SAME: ptr{{.*}} %dst)
// WIN: [[RETURNED:%[_0-9]+]] = tail call <16 x i8> @extern_ret()
// WIN: store <16 x i8> [[RETURNED]], ptr %dst
// WIN: ret void
// CHECK-SAME: ptr{{.*}} %dst)
// CHECK: [[RETURNED:%[_0-9]+]] = tail call {{.*}}i128 @extern_ret()
// CHECK: store i128 [[RETURNED]], ptr %dst
// CHECK: ret void
unsafe { *dst = extern_ret() };
}

Expand All @@ -70,10 +74,10 @@ struct RetAggregate {
pub extern "C" fn ret_aggregate(_arg0: u32, arg1: i128) -> RetAggregate {
// CHECK-LABEL: @ret_aggregate(
// Aggregates should also be returned indirectly
// WIN-SAME: ptr{{.*}}sret([32 x i8]){{.*}}[[RET:%[_0-9]+]], i32{{.*}}%_arg0, ptr{{.*}}%arg1)
// WIN: [[LOADED:%[_0-9]+]] = load i128, ptr %arg1
// WIN: [[GEP:%[_0-9]+]] = getelementptr{{.*}}, ptr [[RET]]
// WIN: store i128 [[LOADED]], ptr [[GEP]]
// WIN: ret void
// CHECK-SAME: ptr{{.*}}sret([32 x i8]){{.*}}[[RET:%[_0-9]+]], i32{{.*}}%_arg0, ptr{{.*}}%arg1)
// CHECK: [[LOADED:%[_0-9]+]] = load i128, ptr %arg1
// CHECK: [[GEP:%[_0-9]+]] = getelementptr{{.*}}, ptr [[RET]]
// CHECK: store i128 [[LOADED]], ptr [[GEP]]
// CHECK: ret void
RetAggregate { a: 1, b: arg1 }
}

0 comments on commit 778ec18

Please sign in to comment.