Skip to content

[X86][CodeGen] Emit float128 libcalls for math functions #79611

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 3 commits into from
Feb 9, 2024

Conversation

pranavk
Copy link
Contributor

@pranavk pranavk commented Jan 26, 2024

Make LLVM emit libcalls to proper float128 variants for float128 types.

@pranavk pranavk self-assigned this Jan 26, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 26, 2024

@llvm/pr-subscribers-backend-x86
@llvm/pr-subscribers-backend-aarch64
@llvm/pr-subscribers-llvm-globalisel

@llvm/pr-subscribers-llvm-ir

Author: Pranav Kant (pranavk)

Changes

Make LLVM emit libcalls to proper float128 variants for float128 types.

There are many other variants that may require this change. I am just putting this out for now to enable it for simple functions that are blocking me.


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

1 Files Affected:

  • (modified) llvm/include/llvm/IR/RuntimeLibcalls.def (+3-3)
diff --git a/llvm/include/llvm/IR/RuntimeLibcalls.def b/llvm/include/llvm/IR/RuntimeLibcalls.def
index 19dea60bebf9be5..01cab92bf8414d8 100644
--- a/llvm/include/llvm/IR/RuntimeLibcalls.def
+++ b/llvm/include/llvm/IR/RuntimeLibcalls.def
@@ -252,17 +252,17 @@ HANDLE_LIBCALL(FLOOR_PPCF128, "floorl")
 HANDLE_LIBCALL(COPYSIGN_F32, "copysignf")
 HANDLE_LIBCALL(COPYSIGN_F64, "copysign")
 HANDLE_LIBCALL(COPYSIGN_F80, "copysignl")
-HANDLE_LIBCALL(COPYSIGN_F128, "copysignl")
+HANDLE_LIBCALL(COPYSIGN_F128, "copysignf128")
 HANDLE_LIBCALL(COPYSIGN_PPCF128, "copysignl")
 HANDLE_LIBCALL(FMIN_F32, "fminf")
 HANDLE_LIBCALL(FMIN_F64, "fmin")
 HANDLE_LIBCALL(FMIN_F80, "fminl")
-HANDLE_LIBCALL(FMIN_F128, "fminl")
+HANDLE_LIBCALL(FMIN_F128, "fminf128")
 HANDLE_LIBCALL(FMIN_PPCF128, "fminl")
 HANDLE_LIBCALL(FMAX_F32, "fmaxf")
 HANDLE_LIBCALL(FMAX_F64, "fmax")
 HANDLE_LIBCALL(FMAX_F80, "fmaxl")
-HANDLE_LIBCALL(FMAX_F128, "fmaxl")
+HANDLE_LIBCALL(FMAX_F128, "fmaxf128")
 HANDLE_LIBCALL(FMAX_PPCF128, "fmaxl")
 HANDLE_LIBCALL(LROUND_F32, "lroundf")
 HANDLE_LIBCALL(LROUND_F64, "lround")

@MaskRay
Copy link
Member

MaskRay commented Jan 26, 2024

This LGTM, but we'd need __builtin_*f128 clang/test/CodeGen tests.

My digging into the log says that 4bf47bc (2013) (@TNorthover) introduced these l libcall names. aarch64 long double is quad precision and its l suffix (for long double) does indicate quad precision.

glibc only introduced *f128 functions (aliases to *l on ldbl128 systems?) in 2017.

Copy link

github-actions bot commented Feb 8, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@pranavk
Copy link
Contributor Author

pranavk commented Feb 8, 2024

@MaskRay clang codegens correct LLVM IR. So I don't think we need any more tests in clang/test/CodeGen/. All the fp128 __builtin tests we have there already check for generation of appropriate fp128 LLVM IR calls (https://github.com/llvm/llvm-project/blob/3d71e4166de81bc3b86d127d9ac6607bda2b2755/clang/test/CodeGen/X86/math-builtins.c)

The problem is in lowering of LLVM IR into machine code which is what this patch is trying to fix for X86_64. I have changed the tests to check for that lowering.

@MaskRay MaskRay requested a review from andykaylor February 8, 2024 19:20
@pranavk pranavk changed the title Emit float128 libcalls for some F128 variants [X86][CodeGen] Emit float128 libcalls for math functions Feb 8, 2024
@pranavk
Copy link
Contributor Author

pranavk commented Feb 8, 2024

Also worth noting that x86 codegen for fp128 in clang right now seems completely broken because LLVM lowers to fFOOl variants without proper casting.

https://godbolt.org/z/drP5Wrr47

Copy link
Contributor

@andykaylor andykaylor left a comment

Choose a reason for hiding this comment

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

This looks good to me.

@pranavk pranavk merged commit 2e4d276 into llvm:main Feb 9, 2024
aeubanks pushed a commit that referenced this pull request Feb 14, 2024
Otherwise it breaks some environment like X64 Android that doesn't have
f128 functions available in its libc.

Followup to #79611.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants