Skip to content

[X86] Add ABI handling for __float128 to match with GCC #75156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 5, 2024

Conversation

phoebewang
Copy link
Contributor

Fixes #74601

@llvmbot llvmbot added clang Clang issues not falling into any other category backend:X86 clang:codegen IR generation bugs: mangling, exceptions, etc. labels Dec 12, 2023
@llvmbot
Copy link
Member

llvmbot commented Dec 12, 2023

@llvm/pr-subscribers-clang
@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-backend-x86

Author: Phoebe Wang (phoebewang)

Changes

Fixes #74601


Full diff: https://github.com/llvm/llvm-project/pull/75156.diff

2 Files Affected:

  • (modified) clang/lib/CodeGen/Targets/X86.cpp (+2-1)
  • (added) clang/test/CodeGen/X86/fp128-abi.c (+35)
diff --git a/clang/lib/CodeGen/Targets/X86.cpp b/clang/lib/CodeGen/Targets/X86.cpp
index 2af24035043884..c4bf38056a673f 100644
--- a/clang/lib/CodeGen/Targets/X86.cpp
+++ b/clang/lib/CodeGen/Targets/X86.cpp
@@ -1795,7 +1795,8 @@ void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, Class &Lo,
     } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
       Current = Integer;
     } else if (k == BuiltinType::Float || k == BuiltinType::Double ||
-               k == BuiltinType::Float16 || k == BuiltinType::BFloat16) {
+               k == BuiltinType::Float16 || k == BuiltinType::BFloat16 ||
+               k == BuiltinType::Float128) {
       Current = SSE;
     } else if (k == BuiltinType::LongDouble) {
       const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
diff --git a/clang/test/CodeGen/X86/fp128-abi.c b/clang/test/CodeGen/X86/fp128-abi.c
new file mode 100644
index 00000000000000..1c5d7cf1166ee1
--- /dev/null
+++ b/clang/test/CodeGen/X86/fp128-abi.c
@@ -0,0 +1,35 @@
+// RUN: %clang_cc1 -triple x86_64-linux -emit-llvm  -target-feature +sse2 < %s | FileCheck %s --check-prefixes=CHECK
+
+struct st1 {
+  __float128 a;
+};
+
+struct st1 h1(__float128 a) {
+  // CHECK: define{{.*}}fp128 @h1(fp128
+  struct st1 x;
+  x.a = a;
+  return x;
+}
+
+__float128 h2(struct st1 x) {
+  // CHECK: define{{.*}}fp128 @h2(fp128
+  return x.a;
+}
+
+struct st2 {
+  __float128 a;
+  int b;
+};
+
+struct st2 h3(__float128 a, int b) {
+  // CHECK: define{{.*}}void @h3(ptr {{.*}}sret(%struct.st2)
+  struct st2 x;
+  x.a = a;
+  x.b = b;
+  return x;
+}
+
+__float128 h4(struct st2 x) {
+  // CHECK: define{{.*}}fp128 @h4(ptr {{.*}}byval(%struct.st2)
+  return x.a;
+}

@phoebewang phoebewang changed the title [X86] Add ABI handling for fp128 [X86] Add ABI handling for __float128 Dec 12, 2023
@@ -0,0 +1,35 @@
// RUN: %clang_cc1 -triple x86_64-linux -emit-llvm -target-feature +sse2 < %s | FileCheck %s --check-prefixes=CHECK
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth adding a non-SSE RUN?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patch only changes for 64-bit ABI, non-SSE is not a valid case for 64-bit.
OTOH, -sse just generate identical output with +sse2, which doesn't match with GCC https://godbolt.org/z/4nxnhnovd
I think we may need to add a semacheck for it. Do you think we should add it with this patch or a follow up?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're dealing with it soon then handling it in a followup sounds good to me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided not to report error for -sse after investigating the current diagnosis machinism.
We didn't report the SSE error in semacheck but in backend due to backend crash.
__float128 doesn't have crash issue and can be passed on GPR registers without SSE enabled.
IIUC, we prefer to be compatible with early version rather than always to GCC. So I'd like to keep the convention as is.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - so we have test coverage for non-SSE cases already in the backend?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, e.g, llvm/test/CodeGen/X86/{soft-fp,x87}.ll, though I doubt if they can work in reality since they are calling to the same lib functions.

@phoebewang
Copy link
Contributor Author

Ping @RKSimon

@RKSimon
Copy link
Collaborator

RKSimon commented Jan 4, 2024

Add a Release notes entry? I always forget exactly what we need to do for ABI fixes/tweaks

@phoebewang
Copy link
Contributor Author

Add a Release notes entry? I always forget exactly what we need to do for ABI fixes/tweaks

Goot point! Done.

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - please mention in the commit message + release notes that this matches GCC behaviour

@phoebewang phoebewang changed the title [X86] Add ABI handling for __float128 [X86] Add ABI handling for __float128 to match with GCC Jan 5, 2024
@phoebewang phoebewang merged commit f07aba4 into llvm:main Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:X86 clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

struct {__float128} should be passed in registers on X86-64
3 participants