[libc-commits] [PATCH] D152923: [libc] Add support for FMA in the GPU utilities

Joseph Huber via Phabricator via libc-commits libc-commits at lists.llvm.org
Wed Jun 14 07:42:40 PDT 2023


jhuber6 created this revision.
jhuber6 added reviewers: lntue, michaelrj, sivachandra, jdoerfert, JonChesterfield, tra, arsenm.
Herald added projects: libc-project, All.
Herald added a subscriber: libc-commits.
jhuber6 requested review of this revision.
Herald added a subscriber: wdng.

This adds the generic FMA utilities for the GPU. We implement these
through the builtins which map to the FMA instructions in the ISA. These
may not have strict compliance with other assumptions in the the `libc`
such as rounding modes. I've included the relevant information on how
the GPU vendors map the behaviour. This should help make it easier to
implement some future generic versions.

Depends on D152486 <https://reviews.llvm.org/D152486>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152923

Files:
  libc/config/gpu/entrypoints.txt
  libc/src/__support/FPUtil/FMA.h
  libc/src/__support/FPUtil/gpu/FMA.h


Index: libc/src/__support/FPUtil/gpu/FMA.h
===================================================================
--- /dev/null
+++ libc/src/__support/FPUtil/gpu/FMA.h
@@ -0,0 +1,35 @@
+//===-- GPU implementations of the fma function -----------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SUPPORT_FPUTIL_GPU_FMA_H
+#define LLVM_LIBC_SRC_SUPPORT_FPUTIL_GPU_FMA_H
+
+// These intrinsics map to the FMA instrunctions in the target ISA for the GPU.
+// For AMDGPU these will map to instructions with 0.5 ULP accuracy. The NVPTX
+// implementation performs this operation at infinite precision and then rounds
+// to the nearest even value.
+static_assert(__has_builtin(__builtin_fma), "FMA builtins must be defined");
+static_assert(__has_builtin(__builtin_fmaf), "FMA builtins must be defined");
+
+namespace __llvm_libc {
+namespace fputil {
+
+template <typename T>
+LIBC_INLINE cpp::enable_if_t<cpp::is_same_v<T, float>, T> fma(T x, T y, T z) {
+  __builtin_fmaf(x, y, z);
+}
+
+template <typename T>
+LIBC_INLINE cpp::enable_if_t<cpp::is_same_v<T, double>, T> fma(T x, T y, T z) {
+  __builtin_fma(x, y, z);
+}
+
+} // namespace fputil
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_SUPPORT_FPUTIL_GPU_FMA_H
Index: libc/src/__support/FPUtil/FMA.h
===================================================================
--- libc/src/__support/FPUtil/FMA.h
+++ libc/src/__support/FPUtil/FMA.h
@@ -20,6 +20,8 @@
 #include "aarch64/FMA.h"
 #elif defined(LIBC_TARGET_ARCH_IS_RISCV64)
 #include "riscv64/FMA.h"
+#elif defined(LIBC_TARGET_ARCH_IS_GPU)
+#include "gpu/FMA.h"
 #endif
 
 #else
Index: libc/config/gpu/entrypoints.txt
===================================================================
--- libc/config/gpu/entrypoints.txt
+++ libc/config/gpu/entrypoints.txt
@@ -83,6 +83,8 @@
 
 set(TARGET_LIBM_ENTRYPOINTS
     # math.h entrypoints
+    libc.src.math.fma
+    libc.src.math.fmaf
     libc.src.math.sin
     libc.src.math.round
     libc.src.math.roundf


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152923.531329.patch
Type: text/x-patch
Size: 2258 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230614/7d575f17/attachment.bin>


More information about the libc-commits mailing list