[libc-commits] [libc] [libc] Fix incorrectly enabled GPU math functions (PR #83594)

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Fri Mar 1 09:04:29 PST 2024


https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/83594

>From c3cd89f40fdb6dc0169da32c135db0fd49c2a946 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Fri, 1 Mar 2024 10:45:55 -0600
Subject: [PATCH] [libc] Fix incorrectly enabled GPU math functions

Summary:
These functions were implemented via builtins that aren't acually
supported. See https://godbolt.org/z/Wq6q6T1za. This caused the build to
crash if they were included. Remove these and replace with correct
implementations.
---
 libc/src/math/amdgpu/exp.cpp      | 2 +-
 libc/src/math/amdgpu/expf.cpp     | 2 +-
 libc/src/math/amdgpu/llround.cpp  | 2 +-
 libc/src/math/amdgpu/llroundf.cpp | 2 +-
 libc/src/math/amdgpu/lround.cpp   | 4 +++-
 libc/src/math/amdgpu/lroundf.cpp  | 4 +++-
 libc/src/math/nvptx/llround.cpp   | 2 +-
 libc/src/math/nvptx/llroundf.cpp  | 2 +-
 libc/src/math/nvptx/lround.cpp    | 4 +++-
 libc/src/math/nvptx/lroundf.cpp   | 4 +++-
 10 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/libc/src/math/amdgpu/exp.cpp b/libc/src/math/amdgpu/exp.cpp
index 8590ac75901930..d19c73dd024266 100644
--- a/libc/src/math/amdgpu/exp.cpp
+++ b/libc/src/math/amdgpu/exp.cpp
@@ -13,6 +13,6 @@
 
 namespace LIBC_NAMESPACE {
 
-LLVM_LIBC_FUNCTION(double, exp, (double x)) { return __builtin_exp(x); }
+LLVM_LIBC_FUNCTION(double, exp, (double x)) { return __ocml_exp_f64(x); }
 
 } // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/amdgpu/expf.cpp b/libc/src/math/amdgpu/expf.cpp
index d682f6293a6cb7..33393078cfa3a4 100644
--- a/libc/src/math/amdgpu/expf.cpp
+++ b/libc/src/math/amdgpu/expf.cpp
@@ -13,6 +13,6 @@
 
 namespace LIBC_NAMESPACE {
 
-LLVM_LIBC_FUNCTION(float, expf, (float x)) { return __builtin_expf(x); }
+LLVM_LIBC_FUNCTION(float, expf, (float x)) { return __ocml_exp_f32(x); }
 
 } // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/amdgpu/llround.cpp b/libc/src/math/amdgpu/llround.cpp
index afd98308730a62..a8bd07f45997d4 100644
--- a/libc/src/math/amdgpu/llround.cpp
+++ b/libc/src/math/amdgpu/llround.cpp
@@ -12,7 +12,7 @@
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(long long, llround, (double x)) {
-  return __builtin_llround(x);
+  return static_cast<long long>(__builtin_round(x));
 }
 
 } // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/amdgpu/llroundf.cpp b/libc/src/math/amdgpu/llroundf.cpp
index 897ed15b692848..6d7d0459ae9e51 100644
--- a/libc/src/math/amdgpu/llroundf.cpp
+++ b/libc/src/math/amdgpu/llroundf.cpp
@@ -12,7 +12,7 @@
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(long long, llroundf, (float x)) {
-  return __builtin_lroundf(x);
+  return static_cast<long long>(__builtin_roundf(x));
 }
 
 } // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/amdgpu/lround.cpp b/libc/src/math/amdgpu/lround.cpp
index 51e8f2245af8ea..8f56fbc5ba8e2d 100644
--- a/libc/src/math/amdgpu/lround.cpp
+++ b/libc/src/math/amdgpu/lround.cpp
@@ -11,6 +11,8 @@
 
 namespace LIBC_NAMESPACE {
 
-LLVM_LIBC_FUNCTION(long, lround, (double x)) { return __builtin_lround(x); }
+LLVM_LIBC_FUNCTION(long, lround, (double x)) {
+  return static_cast<long>(__builtin_round(x));
+}
 
 } // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/amdgpu/lroundf.cpp b/libc/src/math/amdgpu/lroundf.cpp
index 2a6fe7200d8cba..d175a8891e5997 100644
--- a/libc/src/math/amdgpu/lroundf.cpp
+++ b/libc/src/math/amdgpu/lroundf.cpp
@@ -11,6 +11,8 @@
 
 namespace LIBC_NAMESPACE {
 
-LLVM_LIBC_FUNCTION(long, lroundf, (float x)) { return __builtin_lroundf(x); }
+LLVM_LIBC_FUNCTION(long, lroundf, (float x)) {
+  return static_cast<long>(__builtin_roundf(x));
+}
 
 } // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/nvptx/llround.cpp b/libc/src/math/nvptx/llround.cpp
index afd98308730a62..a8bd07f45997d4 100644
--- a/libc/src/math/nvptx/llround.cpp
+++ b/libc/src/math/nvptx/llround.cpp
@@ -12,7 +12,7 @@
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(long long, llround, (double x)) {
-  return __builtin_llround(x);
+  return static_cast<long long>(__builtin_round(x));
 }
 
 } // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/nvptx/llroundf.cpp b/libc/src/math/nvptx/llroundf.cpp
index 897ed15b692848..6d7d0459ae9e51 100644
--- a/libc/src/math/nvptx/llroundf.cpp
+++ b/libc/src/math/nvptx/llroundf.cpp
@@ -12,7 +12,7 @@
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(long long, llroundf, (float x)) {
-  return __builtin_lroundf(x);
+  return static_cast<long long>(__builtin_roundf(x));
 }
 
 } // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/nvptx/lround.cpp b/libc/src/math/nvptx/lround.cpp
index 51e8f2245af8ea..8f56fbc5ba8e2d 100644
--- a/libc/src/math/nvptx/lround.cpp
+++ b/libc/src/math/nvptx/lround.cpp
@@ -11,6 +11,8 @@
 
 namespace LIBC_NAMESPACE {
 
-LLVM_LIBC_FUNCTION(long, lround, (double x)) { return __builtin_lround(x); }
+LLVM_LIBC_FUNCTION(long, lround, (double x)) {
+  return static_cast<long>(__builtin_round(x));
+}
 
 } // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/nvptx/lroundf.cpp b/libc/src/math/nvptx/lroundf.cpp
index 2a6fe7200d8cba..d175a8891e5997 100644
--- a/libc/src/math/nvptx/lroundf.cpp
+++ b/libc/src/math/nvptx/lroundf.cpp
@@ -11,6 +11,8 @@
 
 namespace LIBC_NAMESPACE {
 
-LLVM_LIBC_FUNCTION(long, lroundf, (float x)) { return __builtin_lroundf(x); }
+LLVM_LIBC_FUNCTION(long, lroundf, (float x)) {
+  return static_cast<long>(__builtin_roundf(x));
+}
 
 } // namespace LIBC_NAMESPACE



More information about the libc-commits mailing list