Skip to content

Commit

Permalink
[libc] Add support for powi as an LLVM libc extension on the GPU (#…
Browse files Browse the repository at this point in the history
…98236)

Summary:
This function is used by the CUDA / HIP / OpenMP headers and exists as
an NVIDIA extension basically. This function is implemented in the C23
standard as `pown`, but for now we need to provide `powi` for backwards
compatibility. In the future this entrypoint will just be a redirect to
`pown` once that is implemented.
  • Loading branch information
jhuber6 authored Jul 10, 2024
1 parent c2fb400 commit 60ff9c2
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 0 deletions.
2 changes: 2 additions & 0 deletions libc/config/gpu/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.nexttowardf
libc.src.math.pow
libc.src.math.powf
libc.src.math.powi
libc.src.math.powif
libc.src.math.remainder
libc.src.math.remainderf
libc.src.math.remquo
Expand Down
2 changes: 2 additions & 0 deletions libc/docs/math/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ Higher Math Functions
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| pow | |check| | | | | | 7.12.7.5 | F.10.4.5 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| powi\* | | | | | | | |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| pown | | | | | | 7.12.7.6 | F.10.4.6 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| powr | | | | | | 7.12.7.7 | F.10.4.7 |
Expand Down
3 changes: 3 additions & 0 deletions libc/spec/llvm_libc_ext.td
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def LLVMLibcExt : StandardSpec<"llvm_libc_ext"> {
GuardedFunctionSpec<"f16sqrt", RetValSpec<Float16Type>, [ArgSpec<DoubleType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"f16sqrtf", RetValSpec<Float16Type>, [ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"f16sqrtl", RetValSpec<Float16Type>, [ArgSpec<LongDoubleType>], "LIBC_TYPES_HAS_FLOAT16">,

FunctionSpec<"powi", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntType>]>,
FunctionSpec<"powif", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntType>]>,
]
>;

Expand Down
2 changes: 2 additions & 0 deletions libc/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ add_math_entrypoint_object(nextupf128)

add_math_entrypoint_object(pow)
add_math_entrypoint_object(powf)
add_math_entrypoint_object(powi)
add_math_entrypoint_object(powif)

add_math_entrypoint_object(remainder)
add_math_entrypoint_object(remainderf)
Expand Down
24 changes: 24 additions & 0 deletions libc/src/math/amdgpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,30 @@ add_entrypoint_object(
VENDOR
)

add_entrypoint_object(
powi
SRCS
powi.cpp
HDRS
../powi.h
COMPILE_OPTIONS
${bitcode_link_flags}
-O2
VENDOR
)

add_entrypoint_object(
powif
SRCS
powif.cpp
HDRS
../powif.h
COMPILE_OPTIONS
${bitcode_link_flags}
-O2
VENDOR
)

add_entrypoint_object(
sinh
SRCS
Expand Down
2 changes: 2 additions & 0 deletions libc/src/math/amdgpu/declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ float __ocml_nextafter_f32(float, float);
double __ocml_nextafter_f64(double, double);
float __ocml_pow_f32(float, float);
double __ocml_pow_f64(double, double);
float __ocml_pown_f32(float, int);
double __ocml_pown_f64(double, int);
float __ocml_sin_f32(float);
double __ocml_sin_f64(double);
float __ocml_sincos_f32(float, float *);
Expand Down
20 changes: 20 additions & 0 deletions libc/src/math/amdgpu/powi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of the powi function for GPU -----------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "src/math/powi.h"
#include "src/__support/common.h"

#include "declarations.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(double, powi, (double x, int y)) {
return __ocml_pown_f64(x, y);
}

} // namespace LIBC_NAMESPACE
20 changes: 20 additions & 0 deletions libc/src/math/amdgpu/powif.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of the powi function for GPU -----------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "src/math/powif.h"
#include "src/__support/common.h"

#include "declarations.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float, powif, (float x, int y)) {
return __ocml_pown_f32(x, y);
}

} // namespace LIBC_NAMESPACE
24 changes: 24 additions & 0 deletions libc/src/math/nvptx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,30 @@ add_entrypoint_object(
VENDOR
)

add_entrypoint_object(
powi
SRCS
powi.cpp
HDRS
../powi.h
COMPILE_OPTIONS
${bitcode_link_flags}
-O2
VENDOR
)

add_entrypoint_object(
powif
SRCS
powif.cpp
HDRS
../powif.h
COMPILE_OPTIONS
${bitcode_link_flags}
-O2
VENDOR
)

add_entrypoint_object(
sinh
SRCS
Expand Down
2 changes: 2 additions & 0 deletions libc/src/math/nvptx/declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ double __nv_nextafter(double, double);
float __nv_nextafterf(float, float);
double __nv_pow(double, double);
float __nv_powf(float, float);
double __nv_powi(double, int);
float __nv_powif(float, int);
double __nv_sin(double);
float __nv_sinf(float);
void __nv_sincos(double, double *, double *);
Expand Down
18 changes: 18 additions & 0 deletions libc/src/math/nvptx/powi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation of the powi function for GPU -----------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "src/math/powi.h"
#include "src/__support/common.h"

#include "declarations.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(double, powi, (double x, int y)) { return __nv_powi(x, y); }

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/math/nvptx/powif.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation of the powif function for GPU ----------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "src/math/powif.h"
#include "src/__support/common.h"

#include "declarations.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float, powif, (float x, int y)) { return __nv_powif(x, y); }

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/math/powi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for powi --------------------------*- 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_MATH_POWI_H
#define LLVM_LIBC_SRC_MATH_POWI_H

namespace LIBC_NAMESPACE {

double powi(double x, int y);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_POW_H
18 changes: 18 additions & 0 deletions libc/src/math/powif.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for powif -------------------------*- 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_MATH_POWIF_H
#define LLVM_LIBC_SRC_MATH_POWIF_H

namespace LIBC_NAMESPACE {

float powif(float x, int y);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_POW_H

0 comments on commit 60ff9c2

Please sign in to comment.