Skip to content

HIP: Use builtin_nan instead of manual expansion #128023

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

Open
wants to merge 1 commit into
base: users/arsenm/clang-hip-remove-unused-ocml-declarations
Choose a base branch
from

Conversation

arsenm
Copy link
Contributor

@arsenm arsenm commented Feb 20, 2025

I'm guessing the only reason the __make_mantissa* functions
exist were to support this, so maybe these can be deleted now.

This is broken in the non-constant string case, since it ends
up emitting a call to the libm function

I'm guessing the only reason the __make_mantissa* functions
exist were to support this, so maybe these can be deleted now.

This is broken in the non-constant string case, since it ends
up emitting a call to the libm function
@arsenm arsenm added backend:X86 clang Clang issues not falling into any other category clang:headers Headers provided by Clang, e.g. for intrinsics labels Feb 20, 2025 — with Graphite App
@arsenm arsenm requested review from AlexVlx and yxsamliu February 20, 2025 16:09
@arsenm arsenm marked this pull request as ready for review February 20, 2025 16:09
Copy link
Contributor Author

arsenm commented Feb 20, 2025

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@llvmbot
Copy link
Member

llvmbot commented Feb 20, 2025

@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-clang

Author: Matt Arsenault (arsenm)

Changes

I'm guessing the only reason the __make_mantissa* functions
exist were to support this, so maybe these can be deleted now.

This is broken in the non-constant string case, since it ends
up emitting a call to the libm function


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

1 Files Affected:

  • (modified) clang/lib/Headers/__clang_hip_math.h (+3-42)
diff --git a/clang/lib/Headers/__clang_hip_math.h b/clang/lib/Headers/__clang_hip_math.h
index 79cb7906852c4..8c21f5d882181 100644
--- a/clang/lib/Headers/__clang_hip_math.h
+++ b/clang/lib/Headers/__clang_hip_math.h
@@ -519,24 +519,8 @@ float modff(float __x, float *__iptr) {
 }
 
 __DEVICE__
-float nanf(const char *__tagp __attribute__((nonnull))) {
-  union {
-    float val;
-    struct ieee_float {
-      unsigned int mantissa : 22;
-      unsigned int quiet : 1;
-      unsigned int exponent : 8;
-      unsigned int sign : 1;
-    } bits;
-  } __tmp;
-  __static_assert_type_size_equal(sizeof(__tmp.val), sizeof(__tmp.bits));
-
-  __tmp.bits.sign = 0u;
-  __tmp.bits.exponent = ~0u;
-  __tmp.bits.quiet = 1u;
-  __tmp.bits.mantissa = __make_mantissa(__tagp);
-
-  return __tmp.val;
+float nanf(const char *__tagp) {
+  return __builtin_nanf(__tagp);
 }
 
 __DEVICE__
@@ -1072,30 +1056,7 @@ double modf(double __x, double *__iptr) {
 
 __DEVICE__
 double nan(const char *__tagp) {
-#if !_WIN32
-  union {
-    double val;
-    struct ieee_double {
-      uint64_t mantissa : 51;
-      uint32_t quiet : 1;
-      uint32_t exponent : 11;
-      uint32_t sign : 1;
-    } bits;
-  } __tmp;
-  __static_assert_type_size_equal(sizeof(__tmp.val), sizeof(__tmp.bits));
-
-  __tmp.bits.sign = 0u;
-  __tmp.bits.exponent = ~0u;
-  __tmp.bits.quiet = 1u;
-  __tmp.bits.mantissa = __make_mantissa(__tagp);
-
-  return __tmp.val;
-#else
-  __static_assert_type_size_equal(sizeof(uint64_t), sizeof(double));
-  uint64_t __val = __make_mantissa(__tagp);
-  __val |= 0xFFF << 51;
-  return *reinterpret_cast<double *>(&__val);
-#endif
+  return __builtin_nan(__tagp);
 }
 
 __DEVICE__

Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff c6c75b5d7a9a1869bfcdc413a98b00fe4b9f07d1 88b441975bc452c5b19d30b4d534fbce0b16dc0b --extensions h -- clang/lib/Headers/__clang_hip_math.h
View the diff from clang-format here.
diff --git a/clang/lib/Headers/__clang_hip_math.h b/clang/lib/Headers/__clang_hip_math.h
index 8c21f5d882..1f62a06b50 100644
--- a/clang/lib/Headers/__clang_hip_math.h
+++ b/clang/lib/Headers/__clang_hip_math.h
@@ -519,9 +519,7 @@ float modff(float __x, float *__iptr) {
 }
 
 __DEVICE__
-float nanf(const char *__tagp) {
-  return __builtin_nanf(__tagp);
-}
+float nanf(const char *__tagp) { return __builtin_nanf(__tagp); }
 
 __DEVICE__
 float nearbyintf(float __x) { return __builtin_nearbyintf(__x); }
@@ -1055,9 +1053,7 @@ double modf(double __x, double *__iptr) {
 }
 
 __DEVICE__
-double nan(const char *__tagp) {
-  return __builtin_nan(__tagp);
-}
+double nan(const char *__tagp) { return __builtin_nan(__tagp); }
 
 __DEVICE__
 double nearbyint(double __x) { return __builtin_nearbyint(__x); }

@yxsamliu
Copy link
Collaborator

why do we want to do this if it is broken for non-literal string?

@yxsamliu yxsamliu requested a review from b-sumner February 20, 2025 18:44
@b-sumner
Copy link

why do we want to do this if it is broken for non-literal string?

Right. It doesn't seem likely that it won't be literal, but no documentation I've found requires it to be.

@arsenm
Copy link
Contributor Author

arsenm commented Feb 21, 2025

why do we want to do this if it is broken for non-literal string?

It's WIP from 3 years ago, I'm putting this up for reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:X86 clang:headers Headers provided by Clang, e.g. for intrinsics clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants