Skip to content

[SYCL] Support stdlib function abs, labs, llabs in device library #3364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions libdevice/cmath_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
#include "device_math.h"

#ifdef __SPIR__

DEVICE_EXTERN_C
int abs(int x) { return __devicelib_abs(x); }

DEVICE_EXTERN_C
long int labs(long int x) { return __devicelib_labs(x); }

DEVICE_EXTERN_C
long long int llabs(long long int x) { return __devicelib_llabs(x); }

DEVICE_EXTERN_C
div_t div(int x, int y) { return __devicelib_div(x, y); }

Expand Down
9 changes: 9 additions & 0 deletions libdevice/device_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
#ifdef __SPIR__
#include <cstdlib>

DEVICE_EXTERN_C
int __devicelib_abs(int x);

DEVICE_EXTERN_C
long int __devicelib_labs(long int x);

DEVICE_EXTERN_C
long long int __devicelib_llabs(long long int x);

DEVICE_EXTERN_C
div_t __devicelib_div(int x, int y);

Expand Down
9 changes: 9 additions & 0 deletions libdevice/fallback-cmath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
// TODO: generate the DeviceLibFuncMap in sycl-post-link.cpp automatically
// during the build based on libdevice to avoid manually sync.

DEVICE_EXTERN_C
int __devicelib_abs(int x) { return x < 0 ? -x : x; }

DEVICE_EXTERN_C
long int __devicelib_labs(long int x) { return x < 0 ? -x : x; }

DEVICE_EXTERN_C
long long int __devicelib_llabs(long long int x) { return x < 0 ? -x : x; }

DEVICE_EXTERN_C
div_t __devicelib_div(int x, int y) { return {x / y, x % y}; }

Expand Down
3 changes: 3 additions & 0 deletions llvm/tools/sycl-post-link/SYCLDeviceLibReqMask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace {
// Please update SDLMap if any item is added to or removed from
// fallback device libraries in libdevice.
SYCLDeviceLibFuncMap SDLMap = {
{"__devicelib_abs", DeviceLibExt::cl_intel_devicelib_math},
{"__devicelib_acosf", DeviceLibExt::cl_intel_devicelib_math},
{"__devicelib_acoshf", DeviceLibExt::cl_intel_devicelib_math},
{"__devicelib_asinf", DeviceLibExt::cl_intel_devicelib_math},
Expand All @@ -46,9 +47,11 @@ SYCLDeviceLibFuncMap SDLMap = {
{"__devicelib_frexpf", DeviceLibExt::cl_intel_devicelib_math},
{"__devicelib_hypotf", DeviceLibExt::cl_intel_devicelib_math},
{"__devicelib_ilogbf", DeviceLibExt::cl_intel_devicelib_math},
{"__devicelib_labs", DeviceLibExt::cl_intel_devicelib_math},
{"__devicelib_ldiv", DeviceLibExt::cl_intel_devicelib_math},
{"__devicelib_ldexpf", DeviceLibExt::cl_intel_devicelib_math},
{"__devicelib_lgammaf", DeviceLibExt::cl_intel_devicelib_math},
{"__devicelib_llabs", DeviceLibExt::cl_intel_devicelib_math},
{"__devicelib_lldiv", DeviceLibExt::cl_intel_devicelib_math},
{"__devicelib_log10f", DeviceLibExt::cl_intel_devicelib_math},
{"__devicelib_log1pf", DeviceLibExt::cl_intel_devicelib_math},
Expand Down
4 changes: 4 additions & 0 deletions sycl/include/CL/sycl/builtins.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,10 @@ detail::enable_if_t<detail::is_genfloatf<T>::value, T> tan(T x) __NOEXC {

#ifdef __SYCL_DEVICE_ONLY__
extern "C" {
extern SYCL_EXTERNAL int abs(int x);
extern SYCL_EXTERNAL long int labs(long int x);
extern SYCL_EXTERNAL long long int llabs(long long int x);

extern SYCL_EXTERNAL div_t div(int x, int y);
extern SYCL_EXTERNAL ldiv_t ldiv(long int x, long int y);
extern SYCL_EXTERNAL lldiv_t lldiv(long long int x, long long int y);
Expand Down