Skip to content

Commit d6cc341

Browse files
authored
[libc] Fix missing GPU math implementations (llvm#65616)
These functions were implemented by simply calling their `__builtin_*` equivalents. The builtins were resolving to the libc functions back again. This patch adds explicit vendor versions for these functions to avoid the recursion.
1 parent c10434b commit d6cc341

File tree

12 files changed

+130
-66
lines changed

12 files changed

+130
-66
lines changed

libc/src/math/gpu/CMakeLists.txt

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -183,26 +183,6 @@ add_math_entrypoint_gpu_object(
183183
-O2
184184
)
185185

186-
add_math_entrypoint_gpu_object(
187-
frexp
188-
SRCS
189-
frexp.cpp
190-
HDRS
191-
../frexp.h
192-
COMPILE_OPTIONS
193-
-O2
194-
)
195-
196-
add_math_entrypoint_gpu_object(
197-
frexpf
198-
SRCS
199-
frexpf.cpp
200-
HDRS
201-
../frexpf.h
202-
COMPILE_OPTIONS
203-
-O2
204-
)
205-
206186
add_math_entrypoint_gpu_object(
207187
modf
208188
SRCS
@@ -263,26 +243,6 @@ add_math_entrypoint_gpu_object(
263243
-O2
264244
)
265245

266-
add_math_entrypoint_gpu_object(
267-
remquo
268-
SRCS
269-
remquo.cpp
270-
HDRS
271-
../remquo.h
272-
COMPILE_OPTIONS
273-
-O2
274-
)
275-
276-
add_math_entrypoint_gpu_object(
277-
remquof
278-
SRCS
279-
remquof.cpp
280-
HDRS
281-
../remquof.h
282-
COMPILE_OPTIONS
283-
-O2
284-
)
285-
286246
add_math_entrypoint_gpu_object(
287247
rint
288248
SRCS
@@ -313,26 +273,6 @@ add_math_entrypoint_gpu_object(
313273
-O2
314274
)
315275

316-
add_math_entrypoint_gpu_object(
317-
scalbn
318-
SRCS
319-
scalbn.cpp
320-
HDRS
321-
../scalbn.h
322-
COMPILE_OPTIONS
323-
-O2
324-
)
325-
326-
add_math_entrypoint_gpu_object(
327-
scalbnf
328-
SRCS
329-
scalbnf.cpp
330-
HDRS
331-
../scalbnf.h
332-
COMPILE_OPTIONS
333-
-O2
334-
)
335-
336276
add_math_entrypoint_gpu_object(
337277
sinh
338278
SRCS

libc/src/math/gpu/vendor/CMakeLists.txt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,29 @@ add_entrypoint_object(
293293
-O2
294294
)
295295

296+
add_entrypoint_object(
297+
remquo
298+
SRCS
299+
remquo.cpp
300+
HDRS
301+
../../remquo.h
302+
COMPILE_OPTIONS
303+
${bitcode_link_flags}
304+
-O2
305+
)
306+
307+
add_entrypoint_object(
308+
remquof
309+
SRCS
310+
remquof.cpp
311+
HDRS
312+
../../remquof.h
313+
COMPILE_OPTIONS
314+
${bitcode_link_flags}
315+
-O2
316+
)
317+
318+
296319
add_entrypoint_object(
297320
llround
298321
SRCS
@@ -315,6 +338,29 @@ add_entrypoint_object(
315338
-O2
316339
)
317340

341+
add_entrypoint_object(
342+
scalbn
343+
SRCS
344+
scalbn.cpp
345+
HDRS
346+
../../scalbn.h
347+
COMPILE_OPTIONS
348+
${bitcode_link_flags}
349+
-O2
350+
)
351+
352+
add_entrypoint_object(
353+
scalbnf
354+
SRCS
355+
scalbnf.cpp
356+
HDRS
357+
../../scalbnf.h
358+
COMPILE_OPTIONS
359+
${bitcode_link_flags}
360+
-O2
361+
)
362+
363+
318364
add_entrypoint_object(
319365
nextafter
320366
SRCS
@@ -468,3 +514,25 @@ add_entrypoint_object(
468514
${bitcode_link_flags}
469515
-O2
470516
)
517+
518+
add_entrypoint_object(
519+
frexp
520+
SRCS
521+
frexp.cpp
522+
HDRS
523+
../../frexp.h
524+
COMPILE_OPTIONS
525+
${bitcode_link_flags}
526+
-O2
527+
)
528+
529+
add_entrypoint_object(
530+
frexpf
531+
SRCS
532+
frexpf.cpp
533+
HDRS
534+
../../frexpf.h
535+
COMPILE_OPTIONS
536+
${bitcode_link_flags}
537+
-O2
538+
)

libc/src/math/gpu/vendor/amdgpu/amdgpu.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,30 @@ LIBC_INLINE double tan(double x) { return __ocml_tan_f64(x); }
7272
LIBC_INLINE float tanf(float x) { return __ocml_tan_f32(x); }
7373
LIBC_INLINE double tanh(double x) { return __ocml_tanh_f64(x); }
7474
LIBC_INLINE float tanhf(float x) { return __ocml_tanh_f32(x); }
75+
LIBC_INLINE double scalbn(double x, int i) {
76+
return __builtin_amdgcn_ldexp(x, i);
77+
}
78+
LIBC_INLINE float scalbnf(float x, int i) {
79+
return __builtin_amdgcn_ldexpf(x, i);
80+
}
81+
LIBC_INLINE double frexp(double x, int *nptr) {
82+
return __builtin_frexp(x, nptr);
83+
}
84+
LIBC_INLINE float frexpf(float x, int *nptr) {
85+
return __builtin_frexpf(x, nptr);
86+
}
87+
LIBC_INLINE double remquo(double x, double y, int *q) {
88+
int tmp;
89+
double r = __ocml_remquo_f64(x, y, (gpu::Private<int> *)&tmp);
90+
*q = tmp;
91+
return r;
92+
}
93+
LIBC_INLINE float remquof(float x, float y, int *q) {
94+
int tmp;
95+
float r = __ocml_remquo_f32(x, y, (gpu::Private<int> *)&tmp);
96+
*q = tmp;
97+
return r;
98+
}
7599

76100
} // namespace internal
77101
} // namespace __llvm_libc

libc/src/math/gpu/vendor/amdgpu/declarations.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef LLVM_LIBC_SRC_MATH_GPU_AMDGPU_DECLARATIONS_H
1010
#define LLVM_LIBC_SRC_MATH_GPU_AMDGPU_DECLARATIONS_H
1111

12+
#include "src/__support/GPU/utils.h"
13+
1214
namespace __llvm_libc {
1315

1416
extern "C" {
@@ -52,6 +54,8 @@ float __ocml_tan_f32(float);
5254
double __ocml_tan_f64(double);
5355
float __ocml_tanh_f32(float);
5456
double __ocml_tanh_f64(double);
57+
float __ocml_remquo_f32(float, float, gpu::Private<int> *);
58+
double __ocml_remquo_f64(double, double, gpu::Private<int> *);
5559
}
5660

5761
} // namespace __llvm_libc

libc/src/math/gpu/frexp.cpp renamed to libc/src/math/gpu/vendor/frexp.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
#include "src/math/frexp.h"
1010
#include "src/__support/common.h"
1111

12+
#include "common.h"
13+
1214
namespace __llvm_libc {
1315

1416
LLVM_LIBC_FUNCTION(double, frexp, (double x, int *p)) {
15-
return __builtin_frexp(x, p);
17+
return internal::frexp(x, p);
1618
}
1719

1820
} // namespace __llvm_libc

libc/src/math/gpu/frexpf.cpp renamed to libc/src/math/gpu/vendor/frexpf.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
#include "src/math/frexpf.h"
1010
#include "src/__support/common.h"
1111

12+
#include "common.h"
13+
1214
namespace __llvm_libc {
1315

1416
LLVM_LIBC_FUNCTION(float, frexpf, (float x, int *p)) {
15-
return __builtin_frexpf(x, p);
17+
return internal::frexpf(x, p);
1618
}
1719

1820
} // namespace __llvm_libc

libc/src/math/gpu/vendor/nvptx/declarations.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ double __nv_tan(double);
5252
float __nv_tanf(float);
5353
double __nv_tanh(double);
5454
float __nv_tanhf(float);
55+
double __nv_frexp(double, int *);
56+
float __nv_frexpf(float, int *);
57+
double __nv_scalbn(double, int);
58+
float __nv_scalbnf(float, int);
59+
double __nv_remquo(double, double, int *);
60+
float __nv_remquof(float, float, int *);
5561
}
5662

5763
} // namespace __llvm_libc

libc/src/math/gpu/vendor/nvptx/nvptx.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ LIBC_INLINE double tan(double x) { return __nv_tan(x); }
6161
LIBC_INLINE float tanf(float x) { return __nv_tanf(x); }
6262
LIBC_INLINE double tanh(double x) { return __nv_tanh(x); }
6363
LIBC_INLINE float tanhf(float x) { return __nv_tanhf(x); }
64+
LIBC_INLINE double scalbn(double x, int i) { return __nv_scalbn(x, i); }
65+
LIBC_INLINE float scalbnf(float x, int i) { return __nv_scalbnf(x, i); }
66+
LIBC_INLINE double frexp(double x, int *i) { return __nv_frexp(x, i); }
67+
LIBC_INLINE float frexpf(float x, int *i) { return __nv_frexpf(x, i); }
68+
LIBC_INLINE double remquo(double x, double y, int *i) {
69+
return __nv_remquo(x, y, i);
70+
}
71+
LIBC_INLINE float remquof(float x, float y, int *i) {
72+
return __nv_remquof(x, y, i);
73+
}
6474

6575
} // namespace internal
6676
} // namespace __llvm_libc

libc/src/math/gpu/remquo.cpp renamed to libc/src/math/gpu/vendor/remquo.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
#include "src/math/remquo.h"
1010
#include "src/__support/common.h"
1111

12+
#include "common.h"
13+
1214
namespace __llvm_libc {
1315

1416
LLVM_LIBC_FUNCTION(double, remquo, (double x, double y, int *quo)) {
15-
return __builtin_remquo(x, y, quo);
17+
return internal::remquo(x, y, quo);
1618
}
1719

1820
} // namespace __llvm_libc

libc/src/math/gpu/remquof.cpp renamed to libc/src/math/gpu/vendor/remquof.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
#include "src/math/remquof.h"
1010
#include "src/__support/common.h"
1111

12+
#include "common.h"
13+
1214
namespace __llvm_libc {
1315

1416
LLVM_LIBC_FUNCTION(float, remquof, (float x, float y, int *quo)) {
15-
return __builtin_remquof(x, y, quo);
17+
return internal::remquof(x, y, quo);
1618
}
1719

1820
} // namespace __llvm_libc

libc/src/math/gpu/scalbn.cpp renamed to libc/src/math/gpu/vendor/scalbn.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
#include "src/math/scalbn.h"
1010
#include "src/__support/common.h"
1111

12+
#include "common.h"
13+
1214
namespace __llvm_libc {
1315

1416
LLVM_LIBC_FUNCTION(double, scalbn, (double x, int y)) {
15-
return __builtin_scalbn(x, y);
17+
return internal::scalbn(x, y);
1618
}
1719

1820
} // namespace __llvm_libc

libc/src/math/gpu/scalbnf.cpp renamed to libc/src/math/gpu/vendor/scalbnf.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
#include "src/math/scalbnf.h"
1010
#include "src/__support/common.h"
1111

12+
#include "common.h"
13+
1214
namespace __llvm_libc {
1315

1416
LLVM_LIBC_FUNCTION(float, scalbnf, (float x, int y)) {
15-
return __builtin_scalbnf(x, y);
17+
return internal::scalbnf(x, y);
1618
}
1719

1820
} // namespace __llvm_libc

0 commit comments

Comments
 (0)