Skip to content

Commit 88d2884

Browse files
authored
[libc] Add lgamma and lgamma_r stubs for the GPU (#102019)
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 c4ec19b commit 88d2884

15 files changed

+238
-0
lines changed

libc/config/gpu/entrypoints.txt

+2
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

+39
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+
- gnu
2177+
return_type: double
2178+
arguments:
2179+
- type: double
2180+
- type: int *
2181+
- name: lgammaf_r
2182+
standards:
2183+
- gnu
2184+
return_type: float
2185+
arguments:
2186+
- type: float
2187+
- type: int *
2188+
- name: lgammal_r
2189+
standards:
2190+
- gnu
2191+
return_type: long double
2192+
arguments:
2193+
- type: long double
2194+
- type: int *

libc/spec/gnu_ext.td

+15
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ def GnuExtensions : StandardSpec<"GNUExtensions"> {
3333
RetValSpec<VoidType>,
3434
[ArgSpec<FloatType>, ArgSpec<FloatPtr>, ArgSpec<FloatPtr>]
3535
>,
36+
FunctionSpec<
37+
"lgamma_r",
38+
RetValSpec<DoubleType>,
39+
[ArgSpec<DoubleType, IntPtr>]
40+
>,
41+
FunctionSpec<
42+
"lgammaf_r",
43+
RetValSpec<FloatType>,
44+
[ArgSpec<FloatType, IntPtr>]
45+
>,
46+
FunctionSpec<
47+
"lgammal_r",
48+
RetValSpec<LongDoubleType>,
49+
[ArgSpec<LongDoubleType, IntPtr>]
50+
>,
3651
]
3752
>;
3853

libc/spec/stdc.td

+4
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,10 @@ 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>]>,
770774
]
771775
>;
772776

libc/src/math/CMakeLists.txt

+2
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

+24
Original file line numberDiff line numberDiff line change
@@ -515,3 +515,27 @@ add_entrypoint_object(
515515
-O2
516516
VENDOR
517517
)
518+
519+
add_entrypoint_object(
520+
lgamma
521+
SRCS
522+
lgamma.cpp
523+
HDRS
524+
../lgamma.h
525+
COMPILE_OPTIONS
526+
${bitcode_link_flags}
527+
-O2
528+
VENDOR
529+
)
530+
531+
add_entrypoint_object(
532+
lgamma_r
533+
SRCS
534+
lgamma_r.cpp
535+
HDRS
536+
../lgamma_r.h
537+
COMPILE_OPTIONS
538+
${bitcode_link_flags}
539+
-O2
540+
VENDOR
541+
)

libc/src/math/amdgpu/declarations.h

+2
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

+19
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

+24
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

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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

libc/src/math/lgamma_r.h

+20
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

+24
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,27 @@ add_entrypoint_object(
468468
-O2
469469
VENDOR
470470
)
471+
472+
add_entrypoint_object(
473+
lgamma
474+
SRCS
475+
lgamma.cpp
476+
HDRS
477+
../lgamma.h
478+
COMPILE_OPTIONS
479+
${bitcode_link_flags}
480+
-O2
481+
VENDOR
482+
)
483+
484+
add_entrypoint_object(
485+
lgamma_r
486+
SRCS
487+
lgamma_r.cpp
488+
HDRS
489+
../lgamma_r.h
490+
COMPILE_OPTIONS
491+
${bitcode_link_flags}
492+
-O2
493+
VENDOR
494+
)

libc/src/math/nvptx/declarations.h

+1
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

+19
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 __nv_lgamma(x); }
18+
19+
} // namespace LIBC_NAMESPACE_DECL

libc/src/math/nvptx/lgamma_r.cpp

+23
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+
double result = __nv_lgamma(x);
19+
*signp = (result < 0.0) ? -1 : 1;
20+
return result;
21+
}
22+
23+
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)