Skip to content

Commit 20f6ee0

Browse files
committed
[libc] Add lgamma and lgamma_r stubs for the GPU
Summary: These functions are used by the <random> implementation in libc++ and cause a lot of tests to fail. For now we provide these through the vendor abstraction until we have a real version. The NVPTX version doesn't even update the output correctly so these are just temporary.
1 parent 9fea731 commit 20f6ee0

File tree

14 files changed

+229
-0
lines changed

14 files changed

+229
-0
lines changed

libc/config/gpu/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ set(TARGET_LIBM_ENTRYPOINTS
349349
libc.src.math.tanhf
350350
libc.src.math.tgamma
351351
libc.src.math.tgammaf
352+
libc.src.math.lgamma
353+
libc.src.math.lgamma_r
352354
libc.src.math.trunc
353355
libc.src.math.truncf
354356
)

libc/newhdrgen/yaml/math.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,3 +2153,42 @@ functions:
21532153
- type: int
21542154
- type: unsigned int
21552155
guard: LIBC_TYPES_HAS_FLOAT128
2156+
- name: lgamma
2157+
standards:
2158+
- stdc
2159+
return_type: double
2160+
arguments:
2161+
- type: double
2162+
- name: lgammaf
2163+
standards:
2164+
- stdc
2165+
return_type: float
2166+
arguments:
2167+
- type: float
2168+
- name: lgammal
2169+
standards:
2170+
- stdc
2171+
return_type: long double
2172+
arguments:
2173+
- type: long double
2174+
- name: lgamma_r
2175+
standards:
2176+
- stdc
2177+
return_type: double
2178+
arguments:
2179+
- type: double
2180+
- type: int *
2181+
- name: lgammaf_r
2182+
standards:
2183+
- stdc
2184+
return_type: float
2185+
arguments:
2186+
- type: float
2187+
- type: int *
2188+
- name: lgammal_r
2189+
standards:
2190+
- stdc
2191+
return_type: long double
2192+
arguments:
2193+
- type: long double
2194+
- type: int *

libc/spec/stdc.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,14 @@ def StdC : StandardSpec<"stdc"> {
767767
GuardedFunctionSpec<"f16divf128", RetValSpec<Float16Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128">,
768768

769769
GuardedFunctionSpec<"f16sqrtf128", RetValSpec<Float16Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128">,
770+
771+
FunctionSpec<"lgamma", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
772+
FunctionSpec<"lgammaf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
773+
FunctionSpec<"lgammal", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
774+
775+
FunctionSpec<"lgamma_r", RetValSpec<DoubleType>, [ArgSpec<DoubleType, IntPtr>]>,
776+
FunctionSpec<"lgammaf_r", RetValSpec<FloatType>, [ArgSpec<FloatType, IntPtr>]>,
777+
FunctionSpec<"lgammal_r", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType, IntPtr>]>,
770778
]
771779
>;
772780

libc/src/math/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,8 @@ add_math_entrypoint_object(tanhf)
448448

449449
add_math_entrypoint_object(tgamma)
450450
add_math_entrypoint_object(tgammaf)
451+
add_math_entrypoint_object(lgamma)
452+
add_math_entrypoint_object(lgamma_r)
451453

452454
add_math_entrypoint_object(totalorder)
453455
add_math_entrypoint_object(totalorderf)

libc/src/math/amdgpu/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,3 +527,27 @@ add_entrypoint_object(
527527
-O2
528528
VENDOR
529529
)
530+
531+
add_entrypoint_object(
532+
lgamma
533+
SRCS
534+
lgamma.cpp
535+
HDRS
536+
../lgamma.h
537+
COMPILE_OPTIONS
538+
${bitcode_link_flags}
539+
-O2
540+
VENDOR
541+
)
542+
543+
add_entrypoint_object(
544+
lgamma_r
545+
SRCS
546+
lgamma_r.cpp
547+
HDRS
548+
../lgamma_r.h
549+
COMPILE_OPTIONS
550+
${bitcode_link_flags}
551+
-O2
552+
VENDOR
553+
)

libc/src/math/amdgpu/declarations.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ float __ocml_remquo_f32(float, float, gpu::Private<int> *);
8282
double __ocml_remquo_f64(double, double, gpu::Private<int> *);
8383
double __ocml_tgamma_f64(double);
8484
float __ocml_tgamma_f32(float);
85+
double __ocml_lgamma_f64(double);
86+
double __ocml_lgamma_r_f64(double, gpu::Private<int> *);
8587
}
8688

8789
} // namespace LIBC_NAMESPACE_DECL

libc/src/math/amdgpu/lgamma.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of the lgamma function for GPU ---------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/math/lgamma.h"
10+
#include "src/__support/common.h"
11+
12+
#include "declarations.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(double, lgamma, (double x)) { return __ocml_lgamma_f64(x); }
18+
19+
} // namespace LIBC_NAMESPACE_DECL

libc/src/math/amdgpu/lgamma_r.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- Implementation of the lgamma_r function for GPU -------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/math/lgamma_r.h"
10+
#include "src/__support/common.h"
11+
12+
#include "declarations.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(double, lgamma_r, (double x, int *signp)) {
18+
int tmp = *signp;
19+
double r = __ocml_lgamma_r_f64(x, (gpu::Private<int> *)&tmp);
20+
*signp = tmp;
21+
return r;
22+
}
23+
24+
} // namespace LIBC_NAMESPACE_DECL

libc/src/math/lgamma.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for lgamma ------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_LGAMMA_H
10+
#define LLVM_LIBC_SRC_MATH_LGAMMA_H
11+
12+
#include "src/__support/macros/config.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
double lgamma(double x);
17+
18+
} // namespace LIBC_NAMESPACE_DECL
19+
20+
#endif // LLVM_LIBC_SRC_MATH_LGAMMA_H
21+

libc/src/math/lgamma_r.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for lgamma_r-----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_LGAMMA_R_H
10+
#define LLVM_LIBC_SRC_MATH_LGAMMA_R_H
11+
12+
#include "src/__support/macros/config.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
double lgamma_r(double x, int *signp);
17+
18+
} // namespace LIBC_NAMESPACE_DECL
19+
20+
#endif // LLVM_LIBC_SRC_MATH_LGAMMA_R_H

libc/src/math/nvptx/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,27 @@ add_entrypoint_object(
480480
-O2
481481
VENDOR
482482
)
483+
484+
add_entrypoint_object(
485+
lgamma
486+
SRCS
487+
lgamma.cpp
488+
HDRS
489+
../lgamma.h
490+
COMPILE_OPTIONS
491+
${bitcode_link_flags}
492+
-O2
493+
VENDOR
494+
)
495+
496+
add_entrypoint_object(
497+
lgamma_r
498+
SRCS
499+
lgamma_r.cpp
500+
HDRS
501+
../lgamma_r.h
502+
COMPILE_OPTIONS
503+
${bitcode_link_flags}
504+
-O2
505+
VENDOR
506+
)

libc/src/math/nvptx/declarations.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ double __nv_remquo(double, double, int *);
8686
float __nv_remquof(float, float, int *);
8787
double __nv_tgamma(double);
8888
float __nv_tgammaf(float);
89+
float __nv_lgamma(double);
8990
}
9091

9192
} // namespace LIBC_NAMESPACE_DECL

libc/src/math/nvptx/lgamma.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of the lgamma function for GPU ---------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/math/lgamma.h"
10+
#include "src/__support/common.h"
11+
12+
#include "declarations.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(double, lgamma, (double x)) { return __nv_lgamma(x); }
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+

libc/src/math/nvptx/lgamma_r.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Implementation of the lgamma_r function for GPU -------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/math/lgamma_r.h"
10+
#include "src/__support/common.h"
11+
12+
#include "declarations.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(double, lgamma_r, (double x, int *signp)) {
18+
// The NVPTX device library does not export this, just pretend for now.
19+
*signp = -1;
20+
return __nv_lgamma(x);
21+
}
22+
23+
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)